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--------------------------------------- 36Node* 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------------------------------------------ 46const 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------------------------------------------ 68const 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. 79Node* ConvD2FNode::Identity(PhaseGVN* phase) { 80 return (in(1)->Opcode() == Op_ConvF2D) ? in(1)->in(1) : this; 81} 82 83//============================================================================= 84//------------------------------Value------------------------------------------ 85const 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 95Node *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. 104Node* ConvD2INode::Identity(PhaseGVN* phase) { 105 return (in(1)->Opcode() == Op_ConvI2D) ? in(1)->in(1) : this; 106} 107 108//============================================================================= 109//------------------------------Value------------------------------------------ 110const 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--------------------------------------- 119Node* 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 129Node *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------------------------------------------ 137const 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------------------------------------------ 147const 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--------------------------------------- 156Node* 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 166Node *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------------------------------------------ 174const 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--------------------------------------- 183Node* 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 193Node *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------------------------------------------ 201const 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------------------------------------------ 211const 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--------------------------------------- 220Node* 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------------------------------------------ 230const 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 241static 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------------------------------------------ 249Node *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)) 298 // but only if x and y have subranges that cannot cause 32-bit overflow, 299 // under the assumption that x+y is in my own subrange this->type(). 300 301 // This assumption is based on a constraint (i.e., type assertion) 302 // established in Parse::array_addressing or perhaps elsewhere. 303 // This constraint has been adjoined to the "natural" type of 304 // the incoming argument in(0). We know (because of runtime 305 // checks) - that the result value I2L(x+y) is in the joined range. 306 // Hence we can restrict the incoming terms (x, y) to values such 307 // that their sum also lands in that range. 308 309 // This optimization is useful only on 64-bit systems, where we hope 310 // the addition will end up subsumed in an addressing mode. 311 // It is necessary to do this when optimizing an unrolled array 312 // copy loop such as x[i++] = y[i++]. 313 314 // On 32-bit systems, it's better to perform as much 32-bit math as 315 // possible before the I2L conversion, because 32-bit math is cheaper. 316 // There's no common reason to "leak" a constant offset through the I2L. 317 // Addressing arithmetic will not absorb it as part of a 64-bit AddL. 318 319 Node* z = in(1); 320 int op = z->Opcode(); 321 if (op == Op_AddI || op == Op_SubI) { 322 Node* x = z->in(1); 323 Node* y = z->in(2); 324 assert (x != z && y != z, "dead loop in ConvI2LNode::Ideal"); 325 if (phase->type(x) == Type::TOP) return this_changed; 326 if (phase->type(y) == Type::TOP) return this_changed; 327 const TypeInt* tx = phase->type(x)->is_int(); 328 const TypeInt* ty = phase->type(y)->is_int(); 329 const TypeLong* tz = this_type; 330 jlong xlo = tx->_lo; 331 jlong xhi = tx->_hi; 332 jlong ylo = ty->_lo; 333 jlong yhi = ty->_hi; 334 jlong zlo = tz->_lo; 335 jlong zhi = tz->_hi; 336 jlong vbit = CONST64(1) << BitsPerInt; 337 int widen = MAX2(tx->_widen, ty->_widen); 338 if (op == Op_SubI) { 339 jlong ylo0 = ylo; 340 ylo = -yhi; 341 yhi = -ylo0; 342 } 343 // See if x+y can cause positive overflow into z+2**32 344 if (long_ranges_overlap(xlo+ylo, xhi+yhi, zlo+vbit, zhi+vbit)) { 345 return this_changed; 346 } 347 // See if x+y can cause negative overflow into z-2**32 348 if (long_ranges_overlap(xlo+ylo, xhi+yhi, zlo-vbit, zhi-vbit)) { 349 return this_changed; 350 } 351 // Now it's always safe to assume x+y does not overflow. 352 // This is true even if some pairs x,y might cause overflow, as long 353 // as that overflow value cannot fall into [zlo,zhi]. 354 355 // Confident that the arithmetic is "as if infinite precision", 356 // we can now use z's range to put constraints on those of x and y. 357 // The "natural" range of x [xlo,xhi] can perhaps be narrowed to a 358 // more "restricted" range by intersecting [xlo,xhi] with the 359 // range obtained by subtracting y's range from the asserted range 360 // of the I2L conversion. Here's the interval arithmetic algebra: 361 // x == z-y == [zlo,zhi]-[ylo,yhi] == [zlo,zhi]+[-yhi,-ylo] 362 // => x in [zlo-yhi, zhi-ylo] 363 // => x in [zlo-yhi, zhi-ylo] INTERSECT [xlo,xhi] 364 // => x in [xlo MAX zlo-yhi, xhi MIN zhi-ylo] 365 jlong rxlo = MAX2(xlo, zlo - yhi); 366 jlong rxhi = MIN2(xhi, zhi - ylo); 367 // And similarly, x changing place with y: 368 jlong rylo = MAX2(ylo, zlo - xhi); 369 jlong ryhi = MIN2(yhi, zhi - xlo); 370 if (rxlo > rxhi || rylo > ryhi) { 371 return this_changed; // x or y is dying; don't mess w/ it 372 } 373 if (op == Op_SubI) { 374 jlong rylo0 = rylo; 375 rylo = -ryhi; 376 ryhi = -rylo0; 377 } 378 assert(rxlo == (int)rxlo && rxhi == (int)rxhi, "x should not overflow"); 379 assert(rylo == (int)rylo && ryhi == (int)ryhi, "y should not overflow"); 380 Node* cx = phase->C->constrained_convI2L(phase, x, TypeInt::make(rxlo, rxhi, widen), NULL); 381 Node* cy = phase->C->constrained_convI2L(phase, y, TypeInt::make(rylo, ryhi, widen), NULL); 382 switch (op) { 383 case Op_AddI: return new AddLNode(cx, cy); 384 case Op_SubI: return new SubLNode(cx, cy); 385 default: ShouldNotReachHere(); 386 } 387 } 388#endif //_LP64 389 390 return this_changed; 391} 392 393//============================================================================= 394//------------------------------Value------------------------------------------ 395const Type* ConvL2DNode::Value(PhaseGVN* phase) const { 396 const Type *t = phase->type( in(1) ); 397 if( t == Type::TOP ) return Type::TOP; 398 const TypeLong *tl = t->is_long(); 399 if( tl->is_con() ) return TypeD::make( (double)tl->get_con() ); 400 return bottom_type(); 401} 402 403//============================================================================= 404//------------------------------Value------------------------------------------ 405const Type* ConvL2FNode::Value(PhaseGVN* phase) const { 406 const Type *t = phase->type( in(1) ); 407 if( t == Type::TOP ) return Type::TOP; 408 const TypeLong *tl = t->is_long(); 409 if( tl->is_con() ) return TypeF::make( (float)tl->get_con() ); 410 return bottom_type(); 411} 412 413//============================================================================= 414//----------------------------Identity----------------------------------------- 415Node* ConvL2INode::Identity(PhaseGVN* phase) { 416 // Convert L2I(I2L(x)) => x 417 if (in(1)->Opcode() == Op_ConvI2L) return in(1)->in(1); 418 return this; 419} 420 421//------------------------------Value------------------------------------------ 422const Type* ConvL2INode::Value(PhaseGVN* phase) const { 423 const Type *t = phase->type( in(1) ); 424 if( t == Type::TOP ) return Type::TOP; 425 const TypeLong *tl = t->is_long(); 426 if (tl->is_con()) 427 // Easy case. 428 return TypeInt::make((jint)tl->get_con()); 429 return bottom_type(); 430} 431 432//------------------------------Ideal------------------------------------------ 433// Return a node which is more "ideal" than the current node. 434// Blow off prior masking to int 435Node *ConvL2INode::Ideal(PhaseGVN *phase, bool can_reshape) { 436 Node *andl = in(1); 437 uint andl_op = andl->Opcode(); 438 if( andl_op == Op_AndL ) { 439 // Blow off prior masking to int 440 if( phase->type(andl->in(2)) == TypeLong::make( 0xFFFFFFFF ) ) { 441 set_req(1,andl->in(1)); 442 return this; 443 } 444 } 445 446 // Swap with a prior add: convL2I(addL(x,y)) ==> addI(convL2I(x),convL2I(y)) 447 // This replaces an 'AddL' with an 'AddI'. 448 if( andl_op == Op_AddL ) { 449 // Don't do this for nodes which have more than one user since 450 // we'll end up computing the long add anyway. 451 if (andl->outcnt() > 1) return NULL; 452 453 Node* x = andl->in(1); 454 Node* y = andl->in(2); 455 assert( x != andl && y != andl, "dead loop in ConvL2INode::Ideal" ); 456 if (phase->type(x) == Type::TOP) return NULL; 457 if (phase->type(y) == Type::TOP) return NULL; 458 Node *add1 = phase->transform(new ConvL2INode(x)); 459 Node *add2 = phase->transform(new ConvL2INode(y)); 460 return new AddINode(add1,add2); 461 } 462 463 // Disable optimization: LoadL->ConvL2I ==> LoadI. 464 // It causes problems (sizes of Load and Store nodes do not match) 465 // in objects initialization code and Escape Analysis. 466 return NULL; 467} 468 469 470 471//============================================================================= 472//------------------------------Identity--------------------------------------- 473// Remove redundant roundings 474Node* RoundFloatNode::Identity(PhaseGVN* phase) { 475 assert(Matcher::strict_fp_requires_explicit_rounding, "should only generate for Intel"); 476 // Do not round constants 477 if (phase->type(in(1))->base() == Type::FloatCon) return in(1); 478 int op = in(1)->Opcode(); 479 // Redundant rounding 480 if( op == Op_RoundFloat ) return in(1); 481 // Already rounded 482 if( op == Op_Parm ) return in(1); 483 if( op == Op_LoadF ) return in(1); 484 return this; 485} 486 487//------------------------------Value------------------------------------------ 488const Type* RoundFloatNode::Value(PhaseGVN* phase) const { 489 return phase->type( in(1) ); 490} 491 492//============================================================================= 493//------------------------------Identity--------------------------------------- 494// Remove redundant roundings. Incoming arguments are already rounded. 495Node* RoundDoubleNode::Identity(PhaseGVN* phase) { 496 assert(Matcher::strict_fp_requires_explicit_rounding, "should only generate for Intel"); 497 // Do not round constants 498 if (phase->type(in(1))->base() == Type::DoubleCon) return in(1); 499 int op = in(1)->Opcode(); 500 // Redundant rounding 501 if( op == Op_RoundDouble ) return in(1); 502 // Already rounded 503 if( op == Op_Parm ) return in(1); 504 if( op == Op_LoadD ) return in(1); 505 if( op == Op_ConvF2D ) return in(1); 506 if( op == Op_ConvI2D ) return in(1); 507 return this; 508} 509 510//------------------------------Value------------------------------------------ 511const Type* RoundDoubleNode::Value(PhaseGVN* phase) const { 512 return phase->type( in(1) ); 513} 514 515 516