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