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