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/callnode.hpp"
  28 #include "opto/castnode.hpp"
  29 #include "opto/connode.hpp"
  30 #include "opto/matcher.hpp"
  31 #include "opto/phaseX.hpp"
  32 #include "opto/subnode.hpp"
  33 #include "opto/type.hpp"
  34 
  35 //=============================================================================
  36 // If input is already higher or equal to cast type, then this is an identity.
  37 Node* ConstraintCastNode::Identity(PhaseGVN* phase) {
  38   Node* dom = dominating_cast(phase, phase);
  39   if (dom != NULL) {
  40     return dom;
  41   }
  42   if (_carry_dependency) {
  43     return this;
  44   }
  45   return phase->type(in(1))->higher_equal_speculative(_type) ? in(1) : this;
  46 }
  47 
  48 //------------------------------Value------------------------------------------
  49 // Take 'join' of input and cast-up type
  50 const Type* ConstraintCastNode::Value(PhaseGVN* phase) const {
  51   if (in(0) && phase->type(in(0)) == Type::TOP) return Type::TOP;
  52   const Type* ft = phase->type(in(1))->filter_speculative(_type);
  53 
  54 #ifdef ASSERT
  55   // Previous versions of this function had some special case logic,
  56   // which is no longer necessary.  Make sure of the required effects.
  57   switch (Opcode()) {
  58     case Op_CastII:
  59     {
  60       const Type* t1 = phase->type(in(1));
  61       if( t1 == Type::TOP )  assert(ft == Type::TOP, "special case #1");
  62       const Type* rt = t1->join_speculative(_type);
  63       if (rt->empty())       assert(ft == Type::TOP, "special case #2");
  64       break;
  65     }
  66     case Op_CastLL:
  67     {
  68       const Type* t1 = phase->type(in(1));
  69       if (t1 == Type::TOP)   assert(ft == Type::TOP, "special case #1");
  70       const Type* rt = t1->join_speculative(_type);
  71       if (rt->empty())       assert(ft == Type::TOP, "special case #2");
  72       break;
  73     }
  74     case Op_CastPP:
  75     if (phase->type(in(1)) == TypePtr::NULL_PTR &&
  76         _type->isa_ptr() && _type->is_ptr()->_ptr == TypePtr::NotNull)
  77     assert(ft == Type::TOP, "special case #3");
  78     break;
  79   }
  80 #endif //ASSERT
  81 
  82   return ft;
  83 }
  84 
  85 //------------------------------Ideal------------------------------------------
  86 // Return a node which is more "ideal" than the current node.  Strip out
  87 // control copies
  88 Node *ConstraintCastNode::Ideal(PhaseGVN *phase, bool can_reshape) {
  89   return (in(0) && remove_dead_region(phase, can_reshape)) ? this : NULL;
  90 }
  91 
  92 bool ConstraintCastNode::cmp(const Node &n) const {
  93   return TypeNode::cmp(n) && ((ConstraintCastNode&)n)._carry_dependency == _carry_dependency;
  94 }
  95 
  96 uint ConstraintCastNode::size_of() const {
  97   return sizeof(*this);
  98 }
  99 
 100 Node* ConstraintCastNode::make_cast(int opcode, Node* c, Node *n, const Type *t, bool carry_dependency) {
 101   switch(opcode) {
 102   case Op_CastII: {
 103     Node* cast = new CastIINode(n, t, carry_dependency);
 104     cast->set_req(0, c);
 105     return cast;
 106   }
 107   case Op_CastLL: {
 108     Node* cast = new CastLLNode(n, t, carry_dependency);
 109     cast->set_req(0, c);
 110     return cast;
 111   }
 112   case Op_CastPP: {
 113     Node* cast = new CastPPNode(n, t, carry_dependency);
 114     cast->set_req(0, c);
 115     return cast;
 116   }
 117   case Op_CheckCastPP: return new CheckCastPPNode(c, n, t, carry_dependency);
 118   default:
 119     fatal("Bad opcode %d", opcode);
 120   }
 121   return NULL;
 122 }
 123 
 124 TypeNode* ConstraintCastNode::dominating_cast(PhaseGVN* gvn, PhaseTransform* pt) const {
 125   Node* val = in(1);
 126   Node* ctl = in(0);
 127   int opc = Opcode();
 128   if (ctl == NULL) {
 129     return NULL;
 130   }
 131   // Range check CastIIs may all end up under a single range check and
 132   // in that case only the narrower CastII would be kept by the code
 133   // below which would be incorrect.
 134   if (is_CastII() && as_CastII()->has_range_check()) {
 135     return NULL;
 136   }
 137   if (type()->isa_rawptr() && (gvn->type_or_null(val) == NULL || gvn->type(val)->isa_oopptr())) {
 138     return NULL;
 139   }
 140   for (DUIterator_Fast imax, i = val->fast_outs(imax); i < imax; i++) {
 141     Node* u = val->fast_out(i);
 142     if (u != this &&
 143         u->outcnt() > 0 &&
 144         u->Opcode() == opc &&
 145         u->in(0) != NULL &&
 146         u->bottom_type()->higher_equal(type())) {
 147       if (pt->is_dominator(u->in(0), ctl)) {
 148         return u->as_Type();
 149       }
 150       if (is_CheckCastPP() && u->in(1)->is_Proj() && u->in(1)->in(0)->is_Allocate() &&
 151           u->in(0)->is_Proj() && u->in(0)->in(0)->is_Initialize() &&
 152           u->in(1)->in(0)->as_Allocate()->initialization() == u->in(0)->in(0)) {
 153         // CheckCastPP following an allocation always dominates all
 154         // use of the allocation result
 155         return u->as_Type();
 156       }
 157     }
 158   }
 159   return NULL;
 160 }
 161 
 162 #ifndef PRODUCT
 163 void ConstraintCastNode::dump_spec(outputStream *st) const {
 164   TypeNode::dump_spec(st);
 165   if (_carry_dependency) {
 166     st->print(" carry dependency");
 167   }
 168 }
 169 #endif
 170 
 171 const Type* CastIINode::Value(PhaseGVN* phase) const {
 172   const Type *res = ConstraintCastNode::Value(phase);
 173 
 174   // Try to improve the type of the CastII if we recognize a CmpI/If
 175   // pattern.
 176   if (_carry_dependency) {
 177     if (in(0) != NULL && in(0)->in(0) != NULL && in(0)->in(0)->is_If()) {
 178       assert(in(0)->is_IfFalse() || in(0)->is_IfTrue(), "should be If proj");
 179       Node* proj = in(0);
 180       if (proj->in(0)->in(1)->is_Bool()) {
 181         Node* b = proj->in(0)->in(1);
 182         if (b->in(1)->Opcode() == Op_CmpI) {
 183           Node* cmp = b->in(1);
 184           if (cmp->in(1) == in(1) && phase->type(cmp->in(2))->isa_int()) {
 185             const TypeInt* in2_t = phase->type(cmp->in(2))->is_int();
 186             const Type* t = TypeInt::INT;
 187             BoolTest test = b->as_Bool()->_test;
 188             if (proj->is_IfFalse()) {
 189               test = test.negate();
 190             }
 191             BoolTest::mask m = test._test;
 192             jlong lo_long = min_jint;
 193             jlong hi_long = max_jint;
 194             if (m == BoolTest::le || m == BoolTest::lt) {
 195               hi_long = in2_t->_hi;
 196               if (m == BoolTest::lt) {
 197                 hi_long -= 1;
 198               }
 199             } else if (m == BoolTest::ge || m == BoolTest::gt) {
 200               lo_long = in2_t->_lo;
 201               if (m == BoolTest::gt) {
 202                 lo_long += 1;
 203               }
 204             } else if (m == BoolTest::eq) {
 205               lo_long = in2_t->_lo;
 206               hi_long = in2_t->_hi;
 207             } else if (m == BoolTest::ne) {
 208               // can't do any better
 209             } else {
 210               stringStream ss;
 211               test.dump_on(&ss);
 212               fatal("unexpected comparison %s", ss.as_string());
 213             }
 214             int lo_int = (int)lo_long;
 215             int hi_int = (int)hi_long;
 216 
 217             if (lo_long != (jlong)lo_int) {
 218               lo_int = min_jint;
 219             }
 220             if (hi_long != (jlong)hi_int) {
 221               hi_int = max_jint;
 222             }
 223 
 224             t = TypeInt::make(lo_int, hi_int, Type::WidenMax);
 225 
 226             res = res->filter_speculative(t);
 227 
 228             return res;
 229           }
 230         }
 231       }
 232     }
 233   }
 234   return res;
 235 }
 236 
 237 Node *CastIINode::Ideal(PhaseGVN *phase, bool can_reshape) {
 238   Node* progress = ConstraintCastNode::Ideal(phase, can_reshape);
 239   if (progress != NULL) {
 240     return progress;
 241   }
 242 
 243   // Similar to ConvI2LNode::Ideal() for the same reasons
 244   // Do not narrow the type of range check dependent CastIINodes to
 245   // avoid corruption of the graph if a CastII is replaced by TOP but
 246   // the corresponding range check is not removed.
 247   if (can_reshape && !_range_check_dependency && !phase->C->major_progress()) {
 248     const TypeInt* this_type = this->type()->is_int();
 249     const TypeInt* in_type = phase->type(in(1))->isa_int();
 250     if (in_type != NULL && this_type != NULL &&
 251         (in_type->_lo != this_type->_lo ||
 252          in_type->_hi != this_type->_hi)) {
 253       jint lo1 = this_type->_lo;
 254       jint hi1 = this_type->_hi;
 255       int w1  = this_type->_widen;
 256 
 257       if (lo1 >= 0) {
 258         // Keep a range assertion of >=0.
 259         lo1 = 0;        hi1 = max_jint;
 260       } else if (hi1 < 0) {
 261         // Keep a range assertion of <0.
 262         lo1 = min_jint; hi1 = -1;
 263       } else {
 264         lo1 = min_jint; hi1 = max_jint;
 265       }
 266       const TypeInt* wtype = TypeInt::make(MAX2(in_type->_lo, lo1),
 267                                            MIN2(in_type->_hi, hi1),
 268                                            MAX2((int)in_type->_widen, w1));
 269       if (wtype != type()) {
 270         set_type(wtype);
 271         return this;
 272       }
 273     }
 274   }
 275   return NULL;
 276 }
 277 
 278 bool CastIINode::cmp(const Node &n) const {
 279   return ConstraintCastNode::cmp(n) && ((CastIINode&)n)._range_check_dependency == _range_check_dependency;
 280 }
 281 
 282 uint CastIINode::size_of() const {
 283   return sizeof(*this);
 284 }
 285 
 286 #ifndef PRODUCT
 287 void CastIINode::dump_spec(outputStream* st) const {
 288   ConstraintCastNode::dump_spec(st);
 289   if (_range_check_dependency) {
 290     st->print(" range check dependency");
 291   }
 292 }
 293 #endif
 294 
 295 Node* CastLLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
 296   Node* progress = ConstraintCastNode::Ideal(phase, can_reshape);
 297   if (progress != NULL) {
 298     return progress;
 299   }
 300 
 301   // Same as in CastIINode::Ideal but for TypeLong instead of TypeInt
 302   if (can_reshape && !phase->C->major_progress()) {
 303     const TypeLong* this_type = this->type()->is_long();
 304     const TypeLong* in_type = phase->type(in(1))->isa_long();
 305     if (in_type != NULL && this_type != NULL &&
 306         (in_type->_lo != this_type->_lo ||
 307          in_type->_hi != this_type->_hi)) {
 308       jlong lo1 = this_type->_lo;
 309       jlong hi1 = this_type->_hi;
 310       int w1  = this_type->_widen;
 311 
 312       if (lo1 >= 0) {
 313         // Keep a range assertion of >=0.
 314         lo1 = 0;         hi1 = max_jlong;
 315       } else if (hi1 < 0) {
 316         // Keep a range assertion of <0.
 317         lo1 = min_jlong; hi1 = -1;
 318       } else {
 319         lo1 = min_jlong; hi1 = max_jlong;
 320       }
 321       const TypeLong* wtype = TypeLong::make(MAX2(in_type->_lo, lo1),
 322                                              MIN2(in_type->_hi, hi1),
 323                                              MAX2((int)in_type->_widen, w1));
 324       if (wtype != type()) {
 325         set_type(wtype);
 326         return this;
 327       }
 328     }
 329   }
 330   return NULL;
 331 }
 332 
 333 
 334 //=============================================================================
 335 //------------------------------Identity---------------------------------------
 336 // If input is already higher or equal to cast type, then this is an identity.
 337 Node* CheckCastPPNode::Identity(PhaseGVN* phase) {
 338   Node* dom = dominating_cast(phase, phase);
 339   if (dom != NULL) {
 340     return dom;
 341   }
 342   if (_carry_dependency) {
 343     return this;
 344   }
 345   // Toned down to rescue meeting at a Phi 3 different oops all implementing
 346   // the same interface.
 347   return (phase->type(in(1)) == phase->type(this)) ? in(1) : this;
 348 }
 349 
 350 //------------------------------Value------------------------------------------
 351 // Take 'join' of input and cast-up type, unless working with an Interface
 352 const Type* CheckCastPPNode::Value(PhaseGVN* phase) const {
 353   if( in(0) && phase->type(in(0)) == Type::TOP ) return Type::TOP;
 354 
 355   const Type *inn = phase->type(in(1));
 356   if( inn == Type::TOP ) return Type::TOP;  // No information yet
 357 
 358   const TypePtr *in_type   = inn->isa_ptr();
 359   const TypePtr *my_type   = _type->isa_ptr();
 360   const Type *result = _type;
 361   if( in_type != NULL && my_type != NULL ) {
 362     TypePtr::PTR   in_ptr    = in_type->ptr();
 363     if (in_ptr == TypePtr::Null) {
 364       result = in_type;
 365     } else if (in_ptr == TypePtr::Constant) {
 366       if (my_type->isa_rawptr()) {
 367         result = my_type;
 368       } else {
 369         const TypeOopPtr *jptr = my_type->isa_oopptr();
 370         assert(jptr, "");
 371         result = !in_type->higher_equal(_type)
 372           ? my_type->cast_to_ptr_type(TypePtr::NotNull)
 373           : in_type;
 374       }
 375     } else {
 376       result =  my_type->cast_to_ptr_type( my_type->join_ptr(in_ptr) );
 377     }
 378   }
 379 
 380   // This is the code from TypePtr::xmeet() that prevents us from
 381   // having 2 ways to represent the same type. We have to replicate it
 382   // here because we don't go through meet/join.
 383   if (result->remove_speculative() == result->speculative()) {
 384     result = result->remove_speculative();
 385   }
 386 
 387   // Same as above: because we don't go through meet/join, remove the
 388   // speculative type if we know we won't use it.
 389   return result->cleanup_speculative();
 390 
 391   // JOIN NOT DONE HERE BECAUSE OF INTERFACE ISSUES.
 392   // FIX THIS (DO THE JOIN) WHEN UNION TYPES APPEAR!
 393 
 394   //
 395   // Remove this code after overnight run indicates no performance
 396   // loss from not performing JOIN at CheckCastPPNode
 397   //
 398   // const TypeInstPtr *in_oop = in->isa_instptr();
 399   // const TypeInstPtr *my_oop = _type->isa_instptr();
 400   // // If either input is an 'interface', return destination type
 401   // assert (in_oop == NULL || in_oop->klass() != NULL, "");
 402   // assert (my_oop == NULL || my_oop->klass() != NULL, "");
 403   // if( (in_oop && in_oop->klass()->is_interface())
 404   //   ||(my_oop && my_oop->klass()->is_interface()) ) {
 405   //   TypePtr::PTR  in_ptr = in->isa_ptr() ? in->is_ptr()->_ptr : TypePtr::BotPTR;
 406   //   // Preserve cast away nullness for interfaces
 407   //   if( in_ptr == TypePtr::NotNull && my_oop && my_oop->_ptr == TypePtr::BotPTR ) {
 408   //     return my_oop->cast_to_ptr_type(TypePtr::NotNull);
 409   //   }
 410   //   return _type;
 411   // }
 412   //
 413   // // Neither the input nor the destination type is an interface,
 414   //
 415   // // history: JOIN used to cause weird corner case bugs
 416   // //          return (in == TypeOopPtr::NULL_PTR) ? in : _type;
 417   // // JOIN picks up NotNull in common instance-of/check-cast idioms, both oops.
 418   // // JOIN does not preserve NotNull in other cases, e.g. RawPtr vs InstPtr
 419   // const Type *join = in->join(_type);
 420   // // Check if join preserved NotNull'ness for pointers
 421   // if( join->isa_ptr() && _type->isa_ptr() ) {
 422   //   TypePtr::PTR join_ptr = join->is_ptr()->_ptr;
 423   //   TypePtr::PTR type_ptr = _type->is_ptr()->_ptr;
 424   //   // If there isn't any NotNull'ness to preserve
 425   //   // OR if join preserved NotNull'ness then return it
 426   //   if( type_ptr == TypePtr::BotPTR  || type_ptr == TypePtr::Null ||
 427   //       join_ptr == TypePtr::NotNull || join_ptr == TypePtr::Constant ) {
 428   //     return join;
 429   //   }
 430   //   // ELSE return same old type as before
 431   //   return _type;
 432   // }
 433   // // Not joining two pointers
 434   // return join;
 435 }
 436 
 437 //=============================================================================
 438 //------------------------------Value------------------------------------------
 439 const Type* CastX2PNode::Value(PhaseGVN* phase) const {
 440   const Type* t = phase->type(in(1));
 441   if (t == Type::TOP) return Type::TOP;
 442   if (t->base() == Type_X && t->singleton()) {
 443     uintptr_t bits = (uintptr_t) t->is_intptr_t()->get_con();
 444     if (bits == 0)   return TypePtr::NULL_PTR;
 445     return TypeRawPtr::make((address) bits);
 446   }
 447   return CastX2PNode::bottom_type();
 448 }
 449 
 450 //------------------------------Idealize---------------------------------------
 451 static inline bool fits_in_int(const Type* t, bool but_not_min_int = false) {
 452   if (t == Type::TOP)  return false;
 453   const TypeX* tl = t->is_intptr_t();
 454   jint lo = min_jint;
 455   jint hi = max_jint;
 456   if (but_not_min_int)  ++lo;  // caller wants to negate the value w/o overflow
 457   return (tl->_lo >= lo) && (tl->_hi <= hi);
 458 }
 459 
 460 static inline Node* addP_of_X2P(PhaseGVN *phase,
 461                                 Node* base,
 462                                 Node* dispX,
 463                                 bool negate = false) {
 464   if (negate) {
 465     dispX = phase->transform(new SubXNode(phase->MakeConX(0), dispX));
 466   }
 467   return new AddPNode(phase->C->top(),
 468                       phase->transform(new CastX2PNode(base)),
 469                       dispX);
 470 }
 471 
 472 Node *CastX2PNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 473   // convert CastX2P(AddX(x, y)) to AddP(CastX2P(x), y) if y fits in an int
 474   int op = in(1)->Opcode();
 475   Node* x;
 476   Node* y;
 477   switch (op) {
 478     case Op_SubX:
 479     x = in(1)->in(1);
 480     // Avoid ideal transformations ping-pong between this and AddP for raw pointers.
 481     if (phase->find_intptr_t_con(x, -1) == 0)
 482     break;
 483     y = in(1)->in(2);
 484     if (fits_in_int(phase->type(y), true)) {
 485       return addP_of_X2P(phase, x, y, true);
 486     }
 487     break;
 488     case Op_AddX:
 489     x = in(1)->in(1);
 490     y = in(1)->in(2);
 491     if (fits_in_int(phase->type(y))) {
 492       return addP_of_X2P(phase, x, y);
 493     }
 494     if (fits_in_int(phase->type(x))) {
 495       return addP_of_X2P(phase, y, x);
 496     }
 497     break;
 498   }
 499   return NULL;
 500 }
 501 
 502 //------------------------------Identity---------------------------------------
 503 Node* CastX2PNode::Identity(PhaseGVN* phase) {
 504   if (in(1)->Opcode() == Op_CastP2X)  return in(1)->in(1);
 505   return this;
 506 }
 507 
 508 //=============================================================================
 509 //------------------------------Value------------------------------------------
 510 const Type* CastP2XNode::Value(PhaseGVN* phase) const {
 511   const Type* t = phase->type(in(1));
 512   if (t == Type::TOP) return Type::TOP;
 513   if (t->base() == Type::RawPtr && t->singleton()) {
 514     uintptr_t bits = (uintptr_t) t->is_rawptr()->get_con();
 515     return TypeX::make(bits);
 516   }
 517   return CastP2XNode::bottom_type();
 518 }
 519 
 520 Node *CastP2XNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 521   return (in(0) && remove_dead_region(phase, can_reshape)) ? this : NULL;
 522 }
 523 
 524 //------------------------------Identity---------------------------------------
 525 Node* CastP2XNode::Identity(PhaseGVN* phase) {
 526   if (in(1)->Opcode() == Op_CastX2P)  return in(1)->in(1);
 527   return this;
 528 }