1 /* 2 * Copyright (c) 2014, 2015, 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 "opto/addnode.hpp" 27 #include "opto/castnode.hpp" 28 #include "opto/convertnode.hpp" 29 #include "opto/matcher.hpp" 30 #include "opto/phaseX.hpp" 31 #include "opto/subnode.hpp" 32 #include "runtime/sharedRuntime.hpp" 33 34 //============================================================================= 35 //------------------------------Identity--------------------------------------- 36 Node* Conv2BNode::Identity(PhaseGVN* phase) { 37 const Type *t = phase->type( in(1) ); 38 if( t == Type::TOP ) return in(1); 39 if( t == TypeInt::ZERO ) return in(1); 40 if( t == TypeInt::ONE ) return in(1); 41 if( t == TypeInt::BOOL ) return in(1); 42 return this; 43 } 44 45 //------------------------------Value------------------------------------------ 46 const Type* Conv2BNode::Value(PhaseGVN* phase) const { 47 const Type *t = phase->type( in(1) ); 48 if( t == Type::TOP ) return Type::TOP; 49 if( t == TypeInt::ZERO ) return TypeInt::ZERO; 50 if( t == TypePtr::NULL_PTR ) return TypeInt::ZERO; 51 const TypePtr *tp = t->isa_ptr(); 52 if( tp != NULL ) { 53 if( tp->ptr() == TypePtr::AnyNull ) return Type::TOP; 54 if( tp->ptr() == TypePtr::Constant) return TypeInt::ONE; 55 if (tp->ptr() == TypePtr::NotNull) return TypeInt::ONE; 56 return TypeInt::BOOL; 57 } 58 if (t->base() != Type::Int) return TypeInt::BOOL; 59 const TypeInt *ti = t->is_int(); 60 if( ti->_hi < 0 || ti->_lo > 0 ) return TypeInt::ONE; 61 return TypeInt::BOOL; 62 } 63 64 65 // The conversions operations are all Alpha sorted. Please keep it that way! 66 //============================================================================= 67 //------------------------------Value------------------------------------------ 68 const Type* ConvD2FNode::Value(PhaseGVN* phase) const { 69 const Type *t = phase->type( in(1) ); 70 if( t == Type::TOP ) return Type::TOP; 71 if( t == Type::DOUBLE ) return Type::FLOAT; 72 const TypeD *td = t->is_double_constant(); 73 return TypeF::make( (float)td->getd() ); 74 } 75 76 //------------------------------Identity--------------------------------------- 77 // Float's can be converted to doubles with no loss of bits. Hence 78 // converting a float to a double and back to a float is a NOP. 79 Node* ConvD2FNode::Identity(PhaseGVN* phase) { 80 return (in(1)->Opcode() == Op_ConvF2D) ? in(1)->in(1) : this; 81 } 82 83 //============================================================================= 84 //------------------------------Value------------------------------------------ 85 const Type* ConvD2INode::Value(PhaseGVN* phase) const { 86 const Type *t = phase->type( in(1) ); 87 if( t == Type::TOP ) return Type::TOP; 88 if( t == Type::DOUBLE ) return TypeInt::INT; 89 const TypeD *td = t->is_double_constant(); 90 return TypeInt::make( SharedRuntime::d2i( td->getd() ) ); 91 } 92 93 //------------------------------Ideal------------------------------------------ 94 // If converting to an int type, skip any rounding nodes 95 Node *ConvD2INode::Ideal(PhaseGVN *phase, bool can_reshape) { 96 if( in(1)->Opcode() == Op_RoundDouble ) 97 set_req(1,in(1)->in(1)); 98 return NULL; 99 } 100 101 //------------------------------Identity--------------------------------------- 102 // Int's can be converted to doubles with no loss of bits. Hence 103 // converting an integer to a double and back to an integer is a NOP. 104 Node* ConvD2INode::Identity(PhaseGVN* phase) { 105 return (in(1)->Opcode() == Op_ConvI2D) ? in(1)->in(1) : this; 106 } 107 108 //============================================================================= 109 //------------------------------Value------------------------------------------ 110 const Type* ConvD2LNode::Value(PhaseGVN* phase) const { 111 const Type *t = phase->type( in(1) ); 112 if( t == Type::TOP ) return Type::TOP; 113 if( t == Type::DOUBLE ) return TypeLong::LONG; 114 const TypeD *td = t->is_double_constant(); 115 return TypeLong::make( SharedRuntime::d2l( td->getd() ) ); 116 } 117 118 //------------------------------Identity--------------------------------------- 119 Node* ConvD2LNode::Identity(PhaseGVN* phase) { 120 // Remove ConvD2L->ConvL2D->ConvD2L sequences. 121 if( in(1) ->Opcode() == Op_ConvL2D && 122 in(1)->in(1)->Opcode() == Op_ConvD2L ) 123 return in(1)->in(1); 124 return this; 125 } 126 127 //------------------------------Ideal------------------------------------------ 128 // If converting to an int type, skip any rounding nodes 129 Node *ConvD2LNode::Ideal(PhaseGVN *phase, bool can_reshape) { 130 if( in(1)->Opcode() == Op_RoundDouble ) 131 set_req(1,in(1)->in(1)); 132 return NULL; 133 } 134 135 //============================================================================= 136 //------------------------------Value------------------------------------------ 137 const Type* ConvF2DNode::Value(PhaseGVN* phase) const { 138 const Type *t = phase->type( in(1) ); 139 if( t == Type::TOP ) return Type::TOP; 140 if( t == Type::FLOAT ) return Type::DOUBLE; 141 const TypeF *tf = t->is_float_constant(); 142 return TypeD::make( (double)tf->getf() ); 143 } 144 145 //============================================================================= 146 //------------------------------Value------------------------------------------ 147 const Type* ConvF2INode::Value(PhaseGVN* phase) const { 148 const Type *t = phase->type( in(1) ); 149 if( t == Type::TOP ) return Type::TOP; 150 if( t == Type::FLOAT ) return TypeInt::INT; 151 const TypeF *tf = t->is_float_constant(); 152 return TypeInt::make( SharedRuntime::f2i( tf->getf() ) ); 153 } 154 155 //------------------------------Identity--------------------------------------- 156 Node* ConvF2INode::Identity(PhaseGVN* phase) { 157 // Remove ConvF2I->ConvI2F->ConvF2I sequences. 158 if( in(1) ->Opcode() == Op_ConvI2F && 159 in(1)->in(1)->Opcode() == Op_ConvF2I ) 160 return in(1)->in(1); 161 return this; 162 } 163 164 //------------------------------Ideal------------------------------------------ 165 // If converting to an int type, skip any rounding nodes 166 Node *ConvF2INode::Ideal(PhaseGVN *phase, bool can_reshape) { 167 if( in(1)->Opcode() == Op_RoundFloat ) 168 set_req(1,in(1)->in(1)); 169 return NULL; 170 } 171 172 //============================================================================= 173 //------------------------------Value------------------------------------------ 174 const Type* ConvF2LNode::Value(PhaseGVN* phase) const { 175 const Type *t = phase->type( in(1) ); 176 if( t == Type::TOP ) return Type::TOP; 177 if( t == Type::FLOAT ) return TypeLong::LONG; 178 const TypeF *tf = t->is_float_constant(); 179 return TypeLong::make( SharedRuntime::f2l( tf->getf() ) ); 180 } 181 182 //------------------------------Identity--------------------------------------- 183 Node* ConvF2LNode::Identity(PhaseGVN* phase) { 184 // Remove ConvF2L->ConvL2F->ConvF2L sequences. 185 if( in(1) ->Opcode() == Op_ConvL2F && 186 in(1)->in(1)->Opcode() == Op_ConvF2L ) 187 return in(1)->in(1); 188 return this; 189 } 190 191 //------------------------------Ideal------------------------------------------ 192 // If converting to an int type, skip any rounding nodes 193 Node *ConvF2LNode::Ideal(PhaseGVN *phase, bool can_reshape) { 194 if( in(1)->Opcode() == Op_RoundFloat ) 195 set_req(1,in(1)->in(1)); 196 return NULL; 197 } 198 199 //============================================================================= 200 //------------------------------Value------------------------------------------ 201 const Type* ConvI2DNode::Value(PhaseGVN* phase) const { 202 const Type *t = phase->type( in(1) ); 203 if( t == Type::TOP ) return Type::TOP; 204 const TypeInt *ti = t->is_int(); 205 if( ti->is_con() ) return TypeD::make( (double)ti->get_con() ); 206 return bottom_type(); 207 } 208 209 //============================================================================= 210 //------------------------------Value------------------------------------------ 211 const Type* ConvI2FNode::Value(PhaseGVN* phase) const { 212 const Type *t = phase->type( in(1) ); 213 if( t == Type::TOP ) return Type::TOP; 214 const TypeInt *ti = t->is_int(); 215 if( ti->is_con() ) return TypeF::make( (float)ti->get_con() ); 216 return bottom_type(); 217 } 218 219 //------------------------------Identity--------------------------------------- 220 Node* ConvI2FNode::Identity(PhaseGVN* phase) { 221 // Remove ConvI2F->ConvF2I->ConvI2F sequences. 222 if( in(1) ->Opcode() == Op_ConvF2I && 223 in(1)->in(1)->Opcode() == Op_ConvI2F ) 224 return in(1)->in(1); 225 return this; 226 } 227 228 //============================================================================= 229 //------------------------------Value------------------------------------------ 230 const Type* ConvI2LNode::Value(PhaseGVN* phase) const { 231 const Type *t = phase->type( in(1) ); 232 if( t == Type::TOP ) return Type::TOP; 233 const TypeInt *ti = t->is_int(); 234 const Type* tl = TypeLong::make(ti->_lo, ti->_hi, ti->_widen); 235 // Join my declared type against my incoming type. 236 tl = tl->filter(_type); 237 return tl; 238 } 239 240 #ifdef _LP64 241 static inline bool long_ranges_overlap(jlong lo1, jlong hi1, 242 jlong lo2, jlong hi2) { 243 // Two ranges overlap iff one range's low point falls in the other range. 244 return (lo2 <= lo1 && lo1 <= hi2) || (lo1 <= lo2 && lo2 <= hi1); 245 } 246 #endif 247 248 //------------------------------Ideal------------------------------------------ 249 Node *ConvI2LNode::Ideal(PhaseGVN *phase, bool can_reshape) { 250 const TypeLong* this_type = this->type()->is_long(); 251 Node* this_changed = NULL; 252 253 // If _major_progress, then more loop optimizations follow. Do NOT 254 // remove this node's type assertion until no more loop ops can happen. 255 // The progress bit is set in the major loop optimizations THEN comes the 256 // call to IterGVN and any chance of hitting this code. Cf. Opaque1Node. 257 if (can_reshape && !phase->C->major_progress()) { 258 const TypeInt* in_type = phase->type(in(1))->isa_int(); 259 if (in_type != NULL && this_type != NULL && 260 (in_type->_lo != this_type->_lo || 261 in_type->_hi != this_type->_hi)) { 262 // Although this WORSENS the type, it increases GVN opportunities, 263 // because I2L nodes with the same input will common up, regardless 264 // of slightly differing type assertions. Such slight differences 265 // arise routinely as a result of loop unrolling, so this is a 266 // post-unrolling graph cleanup. Choose a type which depends only 267 // on my input. (Exception: Keep a range assertion of >=0 or <0.) 268 jlong lo1 = this_type->_lo; 269 jlong hi1 = this_type->_hi; 270 int w1 = this_type->_widen; 271 if (lo1 != (jint)lo1 || 272 hi1 != (jint)hi1 || 273 lo1 > hi1) { 274 // Overflow leads to wraparound, wraparound leads to range saturation. 275 lo1 = min_jint; hi1 = max_jint; 276 } else if (lo1 >= 0) { 277 // Keep a range assertion of >=0. 278 lo1 = 0; hi1 = max_jint; 279 } else if (hi1 < 0) { 280 // Keep a range assertion of <0. 281 lo1 = min_jint; hi1 = -1; 282 } else { 283 lo1 = min_jint; hi1 = max_jint; 284 } 285 const TypeLong* wtype = TypeLong::make(MAX2((jlong)in_type->_lo, lo1), 286 MIN2((jlong)in_type->_hi, hi1), 287 MAX2((int)in_type->_widen, w1)); 288 if (wtype != type()) { 289 set_type(wtype); 290 // Note: this_type still has old type value, for the logic below. 291 this_changed = this; 292 } 293 } 294 } 295 296 #ifdef _LP64 297 // Convert ConvI2L(AddI(x, y)) to AddL(ConvI2L(x), ConvI2L(y)) or 298 // ConvI2L(CastII(AddI(x, y))) to AddL(ConvI2L(CastII(x)), ConvI2L(CastII(y))), 299 // but only if x and y have subranges that cannot cause 32-bit overflow, 300 // under the assumption that x+y is in my own subrange this->type(). 301 302 // This assumption is based on a constraint (i.e., type assertion) 303 // established in Parse::array_addressing or perhaps elsewhere. 304 // This constraint has been adjoined to the "natural" type of 305 // the incoming argument in(0). We know (because of runtime 306 // checks) - that the result value I2L(x+y) is in the joined range. 307 // Hence we can restrict the incoming terms (x, y) to values such 308 // that their sum also lands in that range. 309 310 // This optimization is useful only on 64-bit systems, where we hope 311 // the addition will end up subsumed in an addressing mode. 312 // It is necessary to do this when optimizing an unrolled array 313 // copy loop such as x[i++] = y[i++]. 314 315 // On 32-bit systems, it's better to perform as much 32-bit math as 316 // possible before the I2L conversion, because 32-bit math is cheaper. 317 // There's no common reason to "leak" a constant offset through the I2L. 318 // Addressing arithmetic will not absorb it as part of a 64-bit AddL. 319 320 Node* z = in(1); 321 int op = z->Opcode(); 322 Node* ctrl = NULL; 323 if (op == Op_CastII && z->as_CastII()->has_range_check()) { 324 // Skip CastII node but save control dependency 325 ctrl = z->in(0); 326 z = z->in(1); 327 op = z->Opcode(); 328 } 329 if (op == Op_AddI || op == Op_SubI) { 330 Node* x = z->in(1); 331 Node* y = z->in(2); 332 assert (x != z && y != z, "dead loop in ConvI2LNode::Ideal"); 333 if (phase->type(x) == Type::TOP) return this_changed; 334 if (phase->type(y) == Type::TOP) return this_changed; 335 const TypeInt* tx = phase->type(x)->is_int(); 336 const TypeInt* ty = phase->type(y)->is_int(); 337 const TypeLong* tz = this_type; 338 jlong xlo = tx->_lo; 339 jlong xhi = tx->_hi; 340 jlong ylo = ty->_lo; 341 jlong yhi = ty->_hi; 342 jlong zlo = tz->_lo; 343 jlong zhi = tz->_hi; 344 jlong vbit = CONST64(1) << BitsPerInt; 345 int widen = MAX2(tx->_widen, ty->_widen); 346 if (op == Op_SubI) { 347 jlong ylo0 = ylo; 348 ylo = -yhi; 349 yhi = -ylo0; 350 } 351 // See if x+y can cause positive overflow into z+2**32 352 if (long_ranges_overlap(xlo+ylo, xhi+yhi, zlo+vbit, zhi+vbit)) { 353 return this_changed; 354 } 355 // See if x+y can cause negative overflow into z-2**32 356 if (long_ranges_overlap(xlo+ylo, xhi+yhi, zlo-vbit, zhi-vbit)) { 357 return this_changed; 358 } 359 // Now it's always safe to assume x+y does not overflow. 360 // This is true even if some pairs x,y might cause overflow, as long 361 // as that overflow value cannot fall into [zlo,zhi]. 362 363 // Confident that the arithmetic is "as if infinite precision", 364 // we can now use z's range to put constraints on those of x and y. 365 // The "natural" range of x [xlo,xhi] can perhaps be narrowed to a 366 // more "restricted" range by intersecting [xlo,xhi] with the 367 // range obtained by subtracting y's range from the asserted range 368 // of the I2L conversion. Here's the interval arithmetic algebra: 369 // x == z-y == [zlo,zhi]-[ylo,yhi] == [zlo,zhi]+[-yhi,-ylo] 370 // => x in [zlo-yhi, zhi-ylo] 371 // => x in [zlo-yhi, zhi-ylo] INTERSECT [xlo,xhi] 372 // => x in [xlo MAX zlo-yhi, xhi MIN zhi-ylo] 373 jlong rxlo = MAX2(xlo, zlo - yhi); 374 jlong rxhi = MIN2(xhi, zhi - ylo); 375 // And similarly, x changing place with y: 376 jlong rylo = MAX2(ylo, zlo - xhi); 377 jlong ryhi = MIN2(yhi, zhi - xlo); 378 if (rxlo > rxhi || rylo > ryhi) { 379 return this_changed; // x or y is dying; don't mess w/ it 380 } 381 if (op == Op_SubI) { 382 jlong rylo0 = rylo; 383 rylo = -ryhi; 384 ryhi = -rylo0; 385 } 386 assert(rxlo == (int)rxlo && rxhi == (int)rxhi, "x should not overflow"); 387 assert(rylo == (int)rylo && ryhi == (int)ryhi, "y should not overflow"); 388 Node* cx = phase->C->constrained_convI2L(phase, x, TypeInt::make(rxlo, rxhi, widen), ctrl); 389 Node* cy = phase->C->constrained_convI2L(phase, y, TypeInt::make(rylo, ryhi, widen), ctrl); 390 switch (op) { 391 case Op_AddI: return new AddLNode(cx, cy); 392 case Op_SubI: return new SubLNode(cx, cy); 393 default: ShouldNotReachHere(); 394 } 395 } 396 #endif //_LP64 397 398 return this_changed; 399 } 400 401 //============================================================================= 402 //------------------------------Value------------------------------------------ 403 const Type* ConvL2DNode::Value(PhaseGVN* phase) const { 404 const Type *t = phase->type( in(1) ); 405 if( t == Type::TOP ) return Type::TOP; 406 const TypeLong *tl = t->is_long(); 407 if( tl->is_con() ) return TypeD::make( (double)tl->get_con() ); 408 return bottom_type(); 409 } 410 411 //============================================================================= 412 //------------------------------Value------------------------------------------ 413 const Type* ConvL2FNode::Value(PhaseGVN* phase) const { 414 const Type *t = phase->type( in(1) ); 415 if( t == Type::TOP ) return Type::TOP; 416 const TypeLong *tl = t->is_long(); 417 if( tl->is_con() ) return TypeF::make( (float)tl->get_con() ); 418 return bottom_type(); 419 } 420 421 //============================================================================= 422 //----------------------------Identity----------------------------------------- 423 Node* ConvL2INode::Identity(PhaseGVN* phase) { 424 // Convert L2I(I2L(x)) => x 425 if (in(1)->Opcode() == Op_ConvI2L) return in(1)->in(1); 426 return this; 427 } 428 429 //------------------------------Value------------------------------------------ 430 const Type* ConvL2INode::Value(PhaseGVN* phase) const { 431 const Type *t = phase->type( in(1) ); 432 if( t == Type::TOP ) return Type::TOP; 433 const TypeLong *tl = t->is_long(); 434 if (tl->is_con()) 435 // Easy case. 436 return TypeInt::make((jint)tl->get_con()); 437 return bottom_type(); 438 } 439 440 //------------------------------Ideal------------------------------------------ 441 // Return a node which is more "ideal" than the current node. 442 // Blow off prior masking to int 443 Node *ConvL2INode::Ideal(PhaseGVN *phase, bool can_reshape) { 444 Node *andl = in(1); 445 uint andl_op = andl->Opcode(); 446 if( andl_op == Op_AndL ) { 447 // Blow off prior masking to int 448 if( phase->type(andl->in(2)) == TypeLong::make( 0xFFFFFFFF ) ) { 449 set_req(1,andl->in(1)); 450 return this; 451 } 452 } 453 454 // Swap with a prior add: convL2I(addL(x,y)) ==> addI(convL2I(x),convL2I(y)) 455 // This replaces an 'AddL' with an 'AddI'. 456 if( andl_op == Op_AddL ) { 457 // Don't do this for nodes which have more than one user since 458 // we'll end up computing the long add anyway. 459 if (andl->outcnt() > 1) return NULL; 460 461 Node* x = andl->in(1); 462 Node* y = andl->in(2); 463 assert( x != andl && y != andl, "dead loop in ConvL2INode::Ideal" ); 464 if (phase->type(x) == Type::TOP) return NULL; 465 if (phase->type(y) == Type::TOP) return NULL; 466 Node *add1 = phase->transform(new ConvL2INode(x)); 467 Node *add2 = phase->transform(new ConvL2INode(y)); 468 return new AddINode(add1,add2); 469 } 470 471 // Disable optimization: LoadL->ConvL2I ==> LoadI. 472 // It causes problems (sizes of Load and Store nodes do not match) 473 // in objects initialization code and Escape Analysis. 474 return NULL; 475 } 476 477 478 479 //============================================================================= 480 //------------------------------Identity--------------------------------------- 481 // Remove redundant roundings 482 Node* RoundFloatNode::Identity(PhaseGVN* phase) { 483 assert(Matcher::strict_fp_requires_explicit_rounding, "should only generate for Intel"); 484 // Do not round constants 485 if (phase->type(in(1))->base() == Type::FloatCon) return in(1); 486 int op = in(1)->Opcode(); 487 // Redundant rounding 488 if( op == Op_RoundFloat ) return in(1); 489 // Already rounded 490 if( op == Op_Parm ) return in(1); 491 if( op == Op_LoadF ) return in(1); 492 return this; 493 } 494 495 //------------------------------Value------------------------------------------ 496 const Type* RoundFloatNode::Value(PhaseGVN* phase) const { 497 return phase->type( in(1) ); 498 } 499 500 //============================================================================= 501 //------------------------------Identity--------------------------------------- 502 // Remove redundant roundings. Incoming arguments are already rounded. 503 Node* RoundDoubleNode::Identity(PhaseGVN* phase) { 504 assert(Matcher::strict_fp_requires_explicit_rounding, "should only generate for Intel"); 505 // Do not round constants 506 if (phase->type(in(1))->base() == Type::DoubleCon) return in(1); 507 int op = in(1)->Opcode(); 508 // Redundant rounding 509 if( op == Op_RoundDouble ) return in(1); 510 // Already rounded 511 if( op == Op_Parm ) return in(1); 512 if( op == Op_LoadD ) return in(1); 513 if( op == Op_ConvF2D ) return in(1); 514 if( op == Op_ConvI2D ) return in(1); 515 return this; 516 } 517 518 //------------------------------Value------------------------------------------ 519 const Type* RoundDoubleNode::Value(PhaseGVN* phase) const { 520 return phase->type( in(1) ); 521 } 522 523