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