1 /*
   2  * Copyright (c) 1997, 2017, 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 "ci/ciField.hpp"
  27 #include "ci/ciMethodData.hpp"
  28 #include "ci/ciTypeFlow.hpp"
  29 #include "ci/ciValueKlass.hpp"
  30 #include "classfile/symbolTable.hpp"
  31 #include "classfile/systemDictionary.hpp"
  32 #include "compiler/compileLog.hpp"
  33 #include "gc/shared/gcLocker.hpp"
  34 #include "libadt/dict.hpp"
  35 #include "memory/oopFactory.hpp"
  36 #include "memory/resourceArea.hpp"
  37 #include "oops/instanceKlass.hpp"
  38 #include "oops/instanceMirrorKlass.hpp"
  39 #include "oops/objArrayKlass.hpp"
  40 #include "oops/typeArrayKlass.hpp"
  41 #include "opto/matcher.hpp"
  42 #include "opto/node.hpp"
  43 #include "opto/opcodes.hpp"
  44 #include "opto/type.hpp"
  45 
  46 // Portions of code courtesy of Clifford Click
  47 
  48 // Optimization - Graph Style
  49 
  50 // Dictionary of types shared among compilations.
  51 Dict* Type::_shared_type_dict = NULL;
  52 const Type::Offset Type::Offset::top(Type::OffsetTop);
  53 const Type::Offset Type::Offset::bottom(Type::OffsetBot);
  54 
  55 const Type::Offset Type::Offset::meet(const Type::Offset other) const {
  56   // Either is 'TOP' offset?  Return the other offset!
  57   int offset = other._offset;
  58   if (_offset == OffsetTop) return Offset(offset);
  59   if (offset == OffsetTop) return Offset(_offset);
  60   // If either is different, return 'BOTTOM' offset
  61   if (_offset != offset) return bottom;
  62   return Offset(_offset);
  63 }
  64 
  65 const Type::Offset Type::Offset::dual() const {
  66   if (_offset == OffsetTop) return bottom;// Map 'TOP' into 'BOTTOM'
  67   if (_offset == OffsetBot) return top;// Map 'BOTTOM' into 'TOP'
  68   return Offset(_offset);               // Map everything else into self
  69 }
  70 
  71 const Type::Offset Type::Offset::add(intptr_t offset) const {
  72   // Adding to 'TOP' offset?  Return 'TOP'!
  73   if (_offset == OffsetTop || offset == OffsetTop) return top;
  74   // Adding to 'BOTTOM' offset?  Return 'BOTTOM'!
  75   if (_offset == OffsetBot || offset == OffsetBot) return bottom;
  76   // Addition overflows or "accidentally" equals to OffsetTop? Return 'BOTTOM'!
  77   offset += (intptr_t)_offset;
  78   if (offset != (int)offset || offset == OffsetTop) return bottom;
  79 
  80   // assert( _offset >= 0 && _offset+offset >= 0, "" );
  81   // It is possible to construct a negative offset during PhaseCCP
  82 
  83   return Offset((int)offset);        // Sum valid offsets
  84 }
  85 
  86 void Type::Offset::dump2(outputStream *st) const {
  87   if (_offset == 0) {
  88     return;
  89   } else if (_offset == OffsetTop) {
  90     st->print("+top");
  91   }
  92   else if (_offset == OffsetBot) {
  93     st->print("+bot");
  94   } else if (_offset) {
  95     st->print("+%d", _offset);
  96   }
  97 }
  98 
  99 // Array which maps compiler types to Basic Types
 100 const Type::TypeInfo Type::_type_info[Type::lastype] = {
 101   { Bad,             T_ILLEGAL,    "bad",           false, Node::NotAMachineReg, relocInfo::none          },  // Bad
 102   { Control,         T_ILLEGAL,    "control",       false, 0,                    relocInfo::none          },  // Control
 103   { Bottom,          T_VOID,       "top",           false, 0,                    relocInfo::none          },  // Top
 104   { Bad,             T_INT,        "int:",          false, Op_RegI,              relocInfo::none          },  // Int
 105   { Bad,             T_LONG,       "long:",         false, Op_RegL,              relocInfo::none          },  // Long
 106   { Half,            T_VOID,       "half",          false, 0,                    relocInfo::none          },  // Half
 107   { Bad,             T_NARROWOOP,  "narrowoop:",    false, Op_RegN,              relocInfo::none          },  // NarrowOop
 108   { Bad,             T_NARROWKLASS,"narrowklass:",  false, Op_RegN,              relocInfo::none          },  // NarrowKlass
 109   { Bad,             T_ILLEGAL,    "tuple:",        false, Node::NotAMachineReg, relocInfo::none          },  // Tuple
 110   { Bad,             T_ARRAY,      "array:",        false, Node::NotAMachineReg, relocInfo::none          },  // Array
 111 
 112 #ifdef SPARC
 113   { Bad,             T_ILLEGAL,    "vectors:",      false, 0,                    relocInfo::none          },  // VectorS
 114   { Bad,             T_ILLEGAL,    "vectord:",      false, Op_RegD,              relocInfo::none          },  // VectorD
 115   { Bad,             T_ILLEGAL,    "vectorx:",      false, 0,                    relocInfo::none          },  // VectorX
 116   { Bad,             T_ILLEGAL,    "vectory:",      false, 0,                    relocInfo::none          },  // VectorY
 117   { Bad,             T_ILLEGAL,    "vectorz:",      false, 0,                    relocInfo::none          },  // VectorZ
 118 #elif defined(PPC64) || defined(S390)
 119   { Bad,             T_ILLEGAL,    "vectors:",      false, 0,                    relocInfo::none          },  // VectorS
 120   { Bad,             T_ILLEGAL,    "vectord:",      false, Op_RegL,              relocInfo::none          },  // VectorD
 121   { Bad,             T_ILLEGAL,    "vectorx:",      false, 0,                    relocInfo::none          },  // VectorX
 122   { Bad,             T_ILLEGAL,    "vectory:",      false, 0,                    relocInfo::none          },  // VectorY
 123   { Bad,             T_ILLEGAL,    "vectorz:",      false, 0,                    relocInfo::none          },  // VectorZ
 124 #else // all other
 125   { Bad,             T_ILLEGAL,    "vectors:",      false, Op_VecS,              relocInfo::none          },  // VectorS
 126   { Bad,             T_ILLEGAL,    "vectord:",      false, Op_VecD,              relocInfo::none          },  // VectorD
 127   { Bad,             T_ILLEGAL,    "vectorx:",      false, Op_VecX,              relocInfo::none          },  // VectorX
 128   { Bad,             T_ILLEGAL,    "vectory:",      false, Op_VecY,              relocInfo::none          },  // VectorY
 129   { Bad,             T_ILLEGAL,    "vectorz:",      false, Op_VecZ,              relocInfo::none          },  // VectorZ
 130 #endif
 131   { Bad,             T_VALUETYPE,  "value:",        false, Node::NotAMachineReg, relocInfo::none          },  // ValueType
 132   { Bad,             T_ADDRESS,    "anyptr:",       false, Op_RegP,              relocInfo::none          },  // AnyPtr
 133   { Bad,             T_ADDRESS,    "rawptr:",       false, Op_RegP,              relocInfo::none          },  // RawPtr
 134   { Bad,             T_OBJECT,     "oop:",          true,  Op_RegP,              relocInfo::oop_type      },  // OopPtr
 135   { Bad,             T_OBJECT,     "inst:",         true,  Op_RegP,              relocInfo::oop_type      },  // InstPtr
 136   { Bad,             T_OBJECT,     "valueptr:",     true,  Op_RegP,              relocInfo::oop_type      },  // ValueTypePtr
 137   { Bad,             T_OBJECT,     "ary:",          true,  Op_RegP,              relocInfo::oop_type      },  // AryPtr
 138   { Bad,             T_METADATA,   "metadata:",     false, Op_RegP,              relocInfo::metadata_type },  // MetadataPtr
 139   { Bad,             T_METADATA,   "klass:",        false, Op_RegP,              relocInfo::metadata_type },  // KlassPtr
 140   { Bad,             T_OBJECT,     "func",          false, 0,                    relocInfo::none          },  // Function
 141   { Abio,            T_ILLEGAL,    "abIO",          false, 0,                    relocInfo::none          },  // Abio
 142   { Return_Address,  T_ADDRESS,    "return_address",false, Op_RegP,              relocInfo::none          },  // Return_Address
 143   { Memory,          T_ILLEGAL,    "memory",        false, 0,                    relocInfo::none          },  // Memory
 144   { FloatBot,        T_FLOAT,      "float_top",     false, Op_RegF,              relocInfo::none          },  // FloatTop
 145   { FloatCon,        T_FLOAT,      "ftcon:",        false, Op_RegF,              relocInfo::none          },  // FloatCon
 146   { FloatTop,        T_FLOAT,      "float",         false, Op_RegF,              relocInfo::none          },  // FloatBot
 147   { DoubleBot,       T_DOUBLE,     "double_top",    false, Op_RegD,              relocInfo::none          },  // DoubleTop
 148   { DoubleCon,       T_DOUBLE,     "dblcon:",       false, Op_RegD,              relocInfo::none          },  // DoubleCon
 149   { DoubleTop,       T_DOUBLE,     "double",        false, Op_RegD,              relocInfo::none          },  // DoubleBot
 150   { Top,             T_ILLEGAL,    "bottom",        false, 0,                    relocInfo::none          }   // Bottom
 151 };
 152 
 153 // Map ideal registers (machine types) to ideal types
 154 const Type *Type::mreg2type[_last_machine_leaf];
 155 
 156 // Map basic types to canonical Type* pointers.
 157 const Type* Type::     _const_basic_type[T_CONFLICT+1];
 158 
 159 // Map basic types to constant-zero Types.
 160 const Type* Type::            _zero_type[T_CONFLICT+1];
 161 
 162 // Map basic types to array-body alias types.
 163 const TypeAryPtr* TypeAryPtr::_array_body_type[T_CONFLICT+1];
 164 
 165 //=============================================================================
 166 // Convenience common pre-built types.
 167 const Type *Type::ABIO;         // State-of-machine only
 168 const Type *Type::BOTTOM;       // All values
 169 const Type *Type::CONTROL;      // Control only
 170 const Type *Type::DOUBLE;       // All doubles
 171 const Type *Type::FLOAT;        // All floats
 172 const Type *Type::HALF;         // Placeholder half of doublewide type
 173 const Type *Type::MEMORY;       // Abstract store only
 174 const Type *Type::RETURN_ADDRESS;
 175 const Type *Type::TOP;          // No values in set
 176 
 177 //------------------------------get_const_type---------------------------
 178 const Type* Type::get_const_type(ciType* type) {
 179   if (type == NULL) {
 180     return NULL;
 181   } else if (type->is_primitive_type()) {
 182     return get_const_basic_type(type->basic_type());
 183   } else {
 184     return TypeOopPtr::make_from_klass(type->as_klass());
 185   }
 186 }
 187 
 188 //---------------------------array_element_basic_type---------------------------------
 189 // Mapping to the array element's basic type.
 190 BasicType Type::array_element_basic_type() const {
 191   BasicType bt = basic_type();
 192   if (bt == T_INT) {
 193     if (this == TypeInt::INT)   return T_INT;
 194     if (this == TypeInt::CHAR)  return T_CHAR;
 195     if (this == TypeInt::BYTE)  return T_BYTE;
 196     if (this == TypeInt::BOOL)  return T_BOOLEAN;
 197     if (this == TypeInt::SHORT) return T_SHORT;
 198     return T_VOID;
 199   }
 200   return bt;
 201 }
 202 
 203 // For two instance arrays of same dimension, return the base element types.
 204 // Otherwise or if the arrays have different dimensions, return NULL.
 205 void Type::get_arrays_base_elements(const Type *a1, const Type *a2,
 206                                     const TypeInstPtr **e1, const TypeInstPtr **e2) {
 207 
 208   if (e1) *e1 = NULL;
 209   if (e2) *e2 = NULL;
 210   const TypeAryPtr* a1tap = (a1 == NULL) ? NULL : a1->isa_aryptr();
 211   const TypeAryPtr* a2tap = (a2 == NULL) ? NULL : a2->isa_aryptr();
 212 
 213   if (a1tap != NULL && a2tap != NULL) {
 214     // Handle multidimensional arrays
 215     const TypePtr* a1tp = a1tap->elem()->make_ptr();
 216     const TypePtr* a2tp = a2tap->elem()->make_ptr();
 217     while (a1tp && a1tp->isa_aryptr() && a2tp && a2tp->isa_aryptr()) {
 218       a1tap = a1tp->is_aryptr();
 219       a2tap = a2tp->is_aryptr();
 220       a1tp = a1tap->elem()->make_ptr();
 221       a2tp = a2tap->elem()->make_ptr();
 222     }
 223     if (a1tp && a1tp->isa_instptr() && a2tp && a2tp->isa_instptr()) {
 224       if (e1) *e1 = a1tp->is_instptr();
 225       if (e2) *e2 = a2tp->is_instptr();
 226     }
 227   }
 228 }
 229 
 230 //---------------------------get_typeflow_type---------------------------------
 231 // Import a type produced by ciTypeFlow.
 232 const Type* Type::get_typeflow_type(ciType* type) {
 233   switch (type->basic_type()) {
 234 
 235   case ciTypeFlow::StateVector::T_BOTTOM:
 236     assert(type == ciTypeFlow::StateVector::bottom_type(), "");
 237     return Type::BOTTOM;
 238 
 239   case ciTypeFlow::StateVector::T_TOP:
 240     assert(type == ciTypeFlow::StateVector::top_type(), "");
 241     return Type::TOP;
 242 
 243   case ciTypeFlow::StateVector::T_NULL:
 244     assert(type == ciTypeFlow::StateVector::null_type(), "");
 245     return TypePtr::NULL_PTR;
 246 
 247   case ciTypeFlow::StateVector::T_LONG2:
 248     // The ciTypeFlow pass pushes a long, then the half.
 249     // We do the same.
 250     assert(type == ciTypeFlow::StateVector::long2_type(), "");
 251     return TypeInt::TOP;
 252 
 253   case ciTypeFlow::StateVector::T_DOUBLE2:
 254     // The ciTypeFlow pass pushes double, then the half.
 255     // Our convention is the same.
 256     assert(type == ciTypeFlow::StateVector::double2_type(), "");
 257     return Type::TOP;
 258 
 259   case T_ADDRESS:
 260     assert(type->is_return_address(), "");
 261     return TypeRawPtr::make((address)(intptr_t)type->as_return_address()->bci());
 262 
 263   case T_VALUETYPE:
 264     if (type == ciEnv::current()->___Value_klass()) {
 265       return TypeValueTypePtr::NOTNULL;
 266     } else {
 267       return TypeValueType::make(type->as_value_klass());
 268     }
 269 
 270   default:
 271     // make sure we did not mix up the cases:
 272     assert(type != ciTypeFlow::StateVector::bottom_type(), "");
 273     assert(type != ciTypeFlow::StateVector::top_type(), "");
 274     assert(type != ciTypeFlow::StateVector::null_type(), "");
 275     assert(type != ciTypeFlow::StateVector::long2_type(), "");
 276     assert(type != ciTypeFlow::StateVector::double2_type(), "");
 277     assert(!type->is_return_address(), "");
 278 
 279     return Type::get_const_type(type);
 280   }
 281 }
 282 
 283 
 284 //-----------------------make_from_constant------------------------------------
 285 const Type* Type::make_from_constant(ciConstant constant, bool require_constant,
 286                                      int stable_dimension, bool is_narrow_oop,
 287                                      bool is_autobox_cache) {
 288   switch (constant.basic_type()) {
 289     case T_BOOLEAN:  return TypeInt::make(constant.as_boolean());
 290     case T_CHAR:     return TypeInt::make(constant.as_char());
 291     case T_BYTE:     return TypeInt::make(constant.as_byte());
 292     case T_SHORT:    return TypeInt::make(constant.as_short());
 293     case T_INT:      return TypeInt::make(constant.as_int());
 294     case T_LONG:     return TypeLong::make(constant.as_long());
 295     case T_FLOAT:    return TypeF::make(constant.as_float());
 296     case T_DOUBLE:   return TypeD::make(constant.as_double());
 297     case T_ARRAY:
 298     case T_VALUETYPE:
 299     case T_OBJECT: {
 300         // cases:
 301         //   can_be_constant    = (oop not scavengable || ScavengeRootsInCode != 0)
 302         //   should_be_constant = (oop not scavengable || ScavengeRootsInCode >= 2)
 303         // An oop is not scavengable if it is in the perm gen.
 304         const Type* con_type = NULL;
 305         ciObject* oop_constant = constant.as_object();
 306         if (oop_constant->is_null_object()) {
 307           con_type = Type::get_zero_type(T_OBJECT);
 308         } else if (require_constant || oop_constant->should_be_constant()) {
 309           con_type = TypeOopPtr::make_from_constant(oop_constant, require_constant);
 310           if (con_type != NULL) {
 311             if (Compile::current()->eliminate_boxing() && is_autobox_cache) {
 312               con_type = con_type->is_aryptr()->cast_to_autobox_cache(true);
 313             }
 314             if (stable_dimension > 0) {
 315               assert(FoldStableValues, "sanity");
 316               assert(!con_type->is_zero_type(), "default value for stable field");
 317               con_type = con_type->is_aryptr()->cast_to_stable(true, stable_dimension);
 318             }
 319           }
 320         }
 321         if (is_narrow_oop) {
 322           con_type = con_type->make_narrowoop();
 323         }
 324         return con_type;
 325       }
 326     case T_ILLEGAL:
 327       // Invalid ciConstant returned due to OutOfMemoryError in the CI
 328       assert(Compile::current()->env()->failing(), "otherwise should not see this");
 329       return NULL;
 330   }
 331   // Fall through to failure
 332   return NULL;
 333 }
 334 
 335 static ciConstant check_mismatched_access(ciConstant con, BasicType loadbt, bool is_unsigned) {
 336   BasicType conbt = con.basic_type();
 337   switch (conbt) {
 338     case T_BOOLEAN: conbt = T_BYTE;   break;
 339     case T_ARRAY:   conbt = T_OBJECT; break;
 340     case T_VALUETYPE: conbt = T_OBJECT; break;
 341   }
 342   switch (loadbt) {
 343     case T_BOOLEAN:   loadbt = T_BYTE;   break;
 344     case T_NARROWOOP: loadbt = T_OBJECT; break;
 345     case T_ARRAY:     loadbt = T_OBJECT; break;
 346     case T_VALUETYPE: loadbt = T_OBJECT; break;
 347     case T_ADDRESS:   loadbt = T_OBJECT; break;
 348   }
 349   if (conbt == loadbt) {
 350     if (is_unsigned && conbt == T_BYTE) {
 351       // LoadB (T_BYTE) with a small mask (<=8-bit) is converted to LoadUB (T_BYTE).
 352       return ciConstant(T_INT, con.as_int() & 0xFF);
 353     } else {
 354       return con;
 355     }
 356   }
 357   if (conbt == T_SHORT && loadbt == T_CHAR) {
 358     // LoadS (T_SHORT) with a small mask (<=16-bit) is converted to LoadUS (T_CHAR).
 359     return ciConstant(T_INT, con.as_int() & 0xFFFF);
 360   }
 361   return ciConstant(); // T_ILLEGAL
 362 }
 363 
 364 // Try to constant-fold a stable array element.
 365 const Type* Type::make_constant_from_array_element(ciArray* array, int off, int stable_dimension,
 366                                                    BasicType loadbt, bool is_unsigned_load) {
 367   // Decode the results of GraphKit::array_element_address.
 368   ciConstant element_value = array->element_value_by_offset(off);
 369   if (element_value.basic_type() == T_ILLEGAL) {
 370     return NULL; // wrong offset
 371   }
 372   ciConstant con = check_mismatched_access(element_value, loadbt, is_unsigned_load);
 373 
 374   assert(con.basic_type() != T_ILLEGAL, "elembt=%s; loadbt=%s; unsigned=%d",
 375          type2name(element_value.basic_type()), type2name(loadbt), is_unsigned_load);
 376 
 377   if (con.is_valid() &&          // not a mismatched access
 378       !con.is_null_or_zero()) {  // not a default value
 379     bool is_narrow_oop = (loadbt == T_NARROWOOP);
 380     return Type::make_from_constant(con, /*require_constant=*/true, stable_dimension, is_narrow_oop, /*is_autobox_cache=*/false);
 381   }
 382   return NULL;
 383 }
 384 
 385 const Type* Type::make_constant_from_field(ciInstance* holder, int off, bool is_unsigned_load, BasicType loadbt) {
 386   ciField* field;
 387   ciType* type = holder->java_mirror_type();
 388   if (type != NULL && type->is_instance_klass() && off >= InstanceMirrorKlass::offset_of_static_fields()) {
 389     // Static field
 390     field = type->as_instance_klass()->get_field_by_offset(off, /*is_static=*/true);
 391   } else {
 392     // Instance field
 393     field = holder->klass()->as_instance_klass()->get_field_by_offset(off, /*is_static=*/false);
 394   }
 395   if (field == NULL) {
 396     return NULL; // Wrong offset
 397   }
 398   return Type::make_constant_from_field(field, holder, loadbt, is_unsigned_load);
 399 }
 400 
 401 const Type* Type::make_constant_from_field(ciField* field, ciInstance* holder,
 402                                            BasicType loadbt, bool is_unsigned_load) {
 403   if (!field->is_constant()) {
 404     return NULL; // Non-constant field
 405   }
 406   ciConstant field_value;
 407   if (field->is_static()) {
 408     // final static field
 409     field_value = field->constant_value();
 410   } else if (holder != NULL) {
 411     // final or stable non-static field
 412     // Treat final non-static fields of trusted classes (classes in
 413     // java.lang.invoke and sun.invoke packages and subpackages) as
 414     // compile time constants.
 415     field_value = field->constant_value_of(holder);
 416   }
 417   if (!field_value.is_valid()) {
 418     return NULL; // Not a constant
 419   }
 420 
 421   ciConstant con = check_mismatched_access(field_value, loadbt, is_unsigned_load);
 422 
 423   assert(con.is_valid(), "elembt=%s; loadbt=%s; unsigned=%d",
 424          type2name(field_value.basic_type()), type2name(loadbt), is_unsigned_load);
 425 
 426   bool is_stable_array = FoldStableValues && field->is_stable() && field->type()->is_array_klass();
 427   int stable_dimension = (is_stable_array ? field->type()->as_array_klass()->dimension() : 0);
 428   bool is_narrow_oop = (loadbt == T_NARROWOOP);
 429 
 430   const Type* con_type = make_from_constant(con, /*require_constant=*/ true,
 431                                             stable_dimension, is_narrow_oop,
 432                                             field->is_autobox_cache());
 433   if (con_type != NULL && field->is_call_site_target()) {
 434     ciCallSite* call_site = holder->as_call_site();
 435     if (!call_site->is_constant_call_site()) {
 436       ciMethodHandle* target = con.as_object()->as_method_handle();
 437       Compile::current()->dependencies()->assert_call_site_target_value(call_site, target);
 438     }
 439   }
 440   return con_type;
 441 }
 442 
 443 //------------------------------make-------------------------------------------
 444 // Create a simple Type, with default empty symbol sets.  Then hashcons it
 445 // and look for an existing copy in the type dictionary.
 446 const Type *Type::make( enum TYPES t ) {
 447   return (new Type(t))->hashcons();
 448 }
 449 
 450 //------------------------------cmp--------------------------------------------
 451 int Type::cmp( const Type *const t1, const Type *const t2 ) {
 452   if( t1->_base != t2->_base )
 453     return 1;                   // Missed badly
 454   assert(t1 != t2 || t1->eq(t2), "eq must be reflexive");
 455   return !t1->eq(t2);           // Return ZERO if equal
 456 }
 457 
 458 const Type* Type::maybe_remove_speculative(bool include_speculative) const {
 459   if (!include_speculative) {
 460     return remove_speculative();
 461   }
 462   return this;
 463 }
 464 
 465 //------------------------------hash-------------------------------------------
 466 int Type::uhash( const Type *const t ) {
 467   return t->hash();
 468 }
 469 
 470 #define SMALLINT ((juint)3)  // a value too insignificant to consider widening
 471 
 472 //--------------------------Initialize_shared----------------------------------
 473 void Type::Initialize_shared(Compile* current) {
 474   // This method does not need to be locked because the first system
 475   // compilations (stub compilations) occur serially.  If they are
 476   // changed to proceed in parallel, then this section will need
 477   // locking.
 478 
 479   Arena* save = current->type_arena();
 480   Arena* shared_type_arena = new (mtCompiler)Arena(mtCompiler);
 481 
 482   current->set_type_arena(shared_type_arena);
 483   _shared_type_dict =
 484     new (shared_type_arena) Dict( (CmpKey)Type::cmp, (Hash)Type::uhash,
 485                                   shared_type_arena, 128 );
 486   current->set_type_dict(_shared_type_dict);
 487 
 488   // Make shared pre-built types.
 489   CONTROL = make(Control);      // Control only
 490   TOP     = make(Top);          // No values in set
 491   MEMORY  = make(Memory);       // Abstract store only
 492   ABIO    = make(Abio);         // State-of-machine only
 493   RETURN_ADDRESS=make(Return_Address);
 494   FLOAT   = make(FloatBot);     // All floats
 495   DOUBLE  = make(DoubleBot);    // All doubles
 496   BOTTOM  = make(Bottom);       // Everything
 497   HALF    = make(Half);         // Placeholder half of doublewide type
 498 
 499   TypeF::ZERO = TypeF::make(0.0); // Float 0 (positive zero)
 500   TypeF::ONE  = TypeF::make(1.0); // Float 1
 501 
 502   TypeD::ZERO = TypeD::make(0.0); // Double 0 (positive zero)
 503   TypeD::ONE  = TypeD::make(1.0); // Double 1
 504 
 505   TypeInt::MINUS_1 = TypeInt::make(-1);  // -1
 506   TypeInt::ZERO    = TypeInt::make( 0);  //  0
 507   TypeInt::ONE     = TypeInt::make( 1);  //  1
 508   TypeInt::BOOL    = TypeInt::make(0,1,   WidenMin);  // 0 or 1, FALSE or TRUE.
 509   TypeInt::CC      = TypeInt::make(-1, 1, WidenMin);  // -1, 0 or 1, condition codes
 510   TypeInt::CC_LT   = TypeInt::make(-1,-1, WidenMin);  // == TypeInt::MINUS_1
 511   TypeInt::CC_GT   = TypeInt::make( 1, 1, WidenMin);  // == TypeInt::ONE
 512   TypeInt::CC_EQ   = TypeInt::make( 0, 0, WidenMin);  // == TypeInt::ZERO
 513   TypeInt::CC_LE   = TypeInt::make(-1, 0, WidenMin);
 514   TypeInt::CC_GE   = TypeInt::make( 0, 1, WidenMin);  // == TypeInt::BOOL
 515   TypeInt::BYTE    = TypeInt::make(-128,127,     WidenMin); // Bytes
 516   TypeInt::UBYTE   = TypeInt::make(0, 255,       WidenMin); // Unsigned Bytes
 517   TypeInt::CHAR    = TypeInt::make(0,65535,      WidenMin); // Java chars
 518   TypeInt::SHORT   = TypeInt::make(-32768,32767, WidenMin); // Java shorts
 519   TypeInt::POS     = TypeInt::make(0,max_jint,   WidenMin); // Non-neg values
 520   TypeInt::POS1    = TypeInt::make(1,max_jint,   WidenMin); // Positive values
 521   TypeInt::INT     = TypeInt::make(min_jint,max_jint, WidenMax); // 32-bit integers
 522   TypeInt::SYMINT  = TypeInt::make(-max_jint,max_jint,WidenMin); // symmetric range
 523   TypeInt::TYPE_DOMAIN  = TypeInt::INT;
 524   // CmpL is overloaded both as the bytecode computation returning
 525   // a trinary (-1,0,+1) integer result AND as an efficient long
 526   // compare returning optimizer ideal-type flags.
 527   assert( TypeInt::CC_LT == TypeInt::MINUS_1, "types must match for CmpL to work" );
 528   assert( TypeInt::CC_GT == TypeInt::ONE,     "types must match for CmpL to work" );
 529   assert( TypeInt::CC_EQ == TypeInt::ZERO,    "types must match for CmpL to work" );
 530   assert( TypeInt::CC_GE == TypeInt::BOOL,    "types must match for CmpL to work" );
 531   assert( (juint)(TypeInt::CC->_hi - TypeInt::CC->_lo) <= SMALLINT, "CC is truly small");
 532 
 533   TypeLong::MINUS_1 = TypeLong::make(-1);        // -1
 534   TypeLong::ZERO    = TypeLong::make( 0);        //  0
 535   TypeLong::ONE     = TypeLong::make( 1);        //  1
 536   TypeLong::POS     = TypeLong::make(0,max_jlong, WidenMin); // Non-neg values
 537   TypeLong::LONG    = TypeLong::make(min_jlong,max_jlong,WidenMax); // 64-bit integers
 538   TypeLong::INT     = TypeLong::make((jlong)min_jint,(jlong)max_jint,WidenMin);
 539   TypeLong::UINT    = TypeLong::make(0,(jlong)max_juint,WidenMin);
 540   TypeLong::TYPE_DOMAIN  = TypeLong::LONG;
 541 
 542   const Type **fboth =(const Type**)shared_type_arena->Amalloc_4(2*sizeof(Type*));
 543   fboth[0] = Type::CONTROL;
 544   fboth[1] = Type::CONTROL;
 545   TypeTuple::IFBOTH = TypeTuple::make( 2, fboth );
 546 
 547   const Type **ffalse =(const Type**)shared_type_arena->Amalloc_4(2*sizeof(Type*));
 548   ffalse[0] = Type::CONTROL;
 549   ffalse[1] = Type::TOP;
 550   TypeTuple::IFFALSE = TypeTuple::make( 2, ffalse );
 551 
 552   const Type **fneither =(const Type**)shared_type_arena->Amalloc_4(2*sizeof(Type*));
 553   fneither[0] = Type::TOP;
 554   fneither[1] = Type::TOP;
 555   TypeTuple::IFNEITHER = TypeTuple::make( 2, fneither );
 556 
 557   const Type **ftrue =(const Type**)shared_type_arena->Amalloc_4(2*sizeof(Type*));
 558   ftrue[0] = Type::TOP;
 559   ftrue[1] = Type::CONTROL;
 560   TypeTuple::IFTRUE = TypeTuple::make( 2, ftrue );
 561 
 562   const Type **floop =(const Type**)shared_type_arena->Amalloc_4(2*sizeof(Type*));
 563   floop[0] = Type::CONTROL;
 564   floop[1] = TypeInt::INT;
 565   TypeTuple::LOOPBODY = TypeTuple::make( 2, floop );
 566 
 567   TypePtr::NULL_PTR= TypePtr::make(AnyPtr, TypePtr::Null, Offset(0));
 568   TypePtr::NOTNULL = TypePtr::make(AnyPtr, TypePtr::NotNull, Offset::bottom);
 569   TypePtr::BOTTOM  = TypePtr::make(AnyPtr, TypePtr::BotPTR, Offset::bottom);
 570 
 571   TypeRawPtr::BOTTOM = TypeRawPtr::make( TypePtr::BotPTR );
 572   TypeRawPtr::NOTNULL= TypeRawPtr::make( TypePtr::NotNull );
 573 
 574   const Type **fmembar = TypeTuple::fields(0);
 575   TypeTuple::MEMBAR = TypeTuple::make(TypeFunc::Parms+0, fmembar);
 576 
 577   const Type **fsc = (const Type**)shared_type_arena->Amalloc_4(2*sizeof(Type*));
 578   fsc[0] = TypeInt::CC;
 579   fsc[1] = Type::MEMORY;
 580   TypeTuple::STORECONDITIONAL = TypeTuple::make(2, fsc);
 581 
 582   TypeInstPtr::NOTNULL = TypeInstPtr::make(TypePtr::NotNull, current->env()->Object_klass());
 583   TypeInstPtr::BOTTOM  = TypeInstPtr::make(TypePtr::BotPTR,  current->env()->Object_klass());
 584   TypeInstPtr::MIRROR  = TypeInstPtr::make(TypePtr::NotNull, current->env()->Class_klass());
 585   TypeInstPtr::MARK    = TypeInstPtr::make(TypePtr::BotPTR,  current->env()->Object_klass(),
 586                                            false, 0, Offset(oopDesc::mark_offset_in_bytes()));
 587   TypeInstPtr::KLASS   = TypeInstPtr::make(TypePtr::BotPTR,  current->env()->Object_klass(),
 588                                            false, 0, Offset(oopDesc::klass_offset_in_bytes()));
 589   TypeOopPtr::BOTTOM  = TypeOopPtr::make(TypePtr::BotPTR, Offset::bottom, TypeOopPtr::InstanceBot);
 590 
 591   TypeMetadataPtr::BOTTOM = TypeMetadataPtr::make(TypePtr::BotPTR, NULL, Offset::bottom);
 592 
 593   TypeNarrowOop::NULL_PTR = TypeNarrowOop::make( TypePtr::NULL_PTR );
 594   TypeNarrowOop::BOTTOM   = TypeNarrowOop::make( TypeInstPtr::BOTTOM );
 595 
 596   TypeNarrowKlass::NULL_PTR = TypeNarrowKlass::make( TypePtr::NULL_PTR );
 597 
 598   TypeValueTypePtr::NOTNULL = (EnableValhalla || EnableMVT) ? TypeValueTypePtr::make(TypePtr::NotNull, current->env()->___Value_klass()->as_value_klass()) : NULL;
 599 
 600   mreg2type[Op_Node] = Type::BOTTOM;
 601   mreg2type[Op_Set ] = 0;
 602   mreg2type[Op_RegN] = TypeNarrowOop::BOTTOM;
 603   mreg2type[Op_RegI] = TypeInt::INT;
 604   mreg2type[Op_RegP] = TypePtr::BOTTOM;
 605   mreg2type[Op_RegF] = Type::FLOAT;
 606   mreg2type[Op_RegD] = Type::DOUBLE;
 607   mreg2type[Op_RegL] = TypeLong::LONG;
 608   mreg2type[Op_RegFlags] = TypeInt::CC;
 609 
 610   TypeAryPtr::RANGE   = TypeAryPtr::make(TypePtr::BotPTR, TypeAry::make(Type::BOTTOM,TypeInt::POS), NULL /* current->env()->Object_klass() */, false, Offset(arrayOopDesc::length_offset_in_bytes()));
 611 
 612   TypeAryPtr::NARROWOOPS = TypeAryPtr::make(TypePtr::BotPTR, TypeAry::make(TypeNarrowOop::BOTTOM, TypeInt::POS), NULL /*ciArrayKlass::make(o)*/,  false,  Offset::bottom);
 613 
 614 #ifdef _LP64
 615   if (UseCompressedOops) {
 616     assert(TypeAryPtr::NARROWOOPS->is_ptr_to_narrowoop(), "array of narrow oops must be ptr to narrow oop");
 617     TypeAryPtr::OOPS  = TypeAryPtr::NARROWOOPS;
 618   } else
 619 #endif
 620   {
 621     // There is no shared klass for Object[].  See note in TypeAryPtr::klass().
 622     TypeAryPtr::OOPS  = TypeAryPtr::make(TypePtr::BotPTR, TypeAry::make(TypeInstPtr::BOTTOM,TypeInt::POS), NULL /*ciArrayKlass::make(o)*/,  false,  Offset::bottom);
 623   }
 624   TypeAryPtr::BYTES   = TypeAryPtr::make(TypePtr::BotPTR, TypeAry::make(TypeInt::BYTE      ,TypeInt::POS), ciTypeArrayKlass::make(T_BYTE),   true,  Offset::bottom);
 625   TypeAryPtr::SHORTS  = TypeAryPtr::make(TypePtr::BotPTR, TypeAry::make(TypeInt::SHORT     ,TypeInt::POS), ciTypeArrayKlass::make(T_SHORT),  true,  Offset::bottom);
 626   TypeAryPtr::CHARS   = TypeAryPtr::make(TypePtr::BotPTR, TypeAry::make(TypeInt::CHAR      ,TypeInt::POS), ciTypeArrayKlass::make(T_CHAR),   true,  Offset::bottom);
 627   TypeAryPtr::INTS    = TypeAryPtr::make(TypePtr::BotPTR, TypeAry::make(TypeInt::INT       ,TypeInt::POS), ciTypeArrayKlass::make(T_INT),    true,  Offset::bottom);
 628   TypeAryPtr::LONGS   = TypeAryPtr::make(TypePtr::BotPTR, TypeAry::make(TypeLong::LONG     ,TypeInt::POS), ciTypeArrayKlass::make(T_LONG),   true,  Offset::bottom);
 629   TypeAryPtr::FLOATS  = TypeAryPtr::make(TypePtr::BotPTR, TypeAry::make(Type::FLOAT        ,TypeInt::POS), ciTypeArrayKlass::make(T_FLOAT),  true,  Offset::bottom);
 630   TypeAryPtr::DOUBLES = TypeAryPtr::make(TypePtr::BotPTR, TypeAry::make(Type::DOUBLE       ,TypeInt::POS), ciTypeArrayKlass::make(T_DOUBLE), true,  Offset::bottom);
 631 
 632   // Nobody should ask _array_body_type[T_NARROWOOP]. Use NULL as assert.
 633   TypeAryPtr::_array_body_type[T_NARROWOOP] = NULL;
 634   TypeAryPtr::_array_body_type[T_OBJECT]  = TypeAryPtr::OOPS;
 635   TypeAryPtr::_array_body_type[T_ARRAY]   = TypeAryPtr::OOPS; // arrays are stored in oop arrays
 636   TypeAryPtr::_array_body_type[T_VALUETYPE] = TypeAryPtr::OOPS;
 637   TypeAryPtr::_array_body_type[T_BYTE]    = TypeAryPtr::BYTES;
 638   TypeAryPtr::_array_body_type[T_BOOLEAN] = TypeAryPtr::BYTES;  // boolean[] is a byte array
 639   TypeAryPtr::_array_body_type[T_SHORT]   = TypeAryPtr::SHORTS;
 640   TypeAryPtr::_array_body_type[T_CHAR]    = TypeAryPtr::CHARS;
 641   TypeAryPtr::_array_body_type[T_INT]     = TypeAryPtr::INTS;
 642   TypeAryPtr::_array_body_type[T_LONG]    = TypeAryPtr::LONGS;
 643   TypeAryPtr::_array_body_type[T_FLOAT]   = TypeAryPtr::FLOATS;
 644   TypeAryPtr::_array_body_type[T_DOUBLE]  = TypeAryPtr::DOUBLES;
 645 
 646   TypeKlassPtr::OBJECT = TypeKlassPtr::make(TypePtr::NotNull, current->env()->Object_klass(), Offset(0) );
 647   TypeKlassPtr::OBJECT_OR_NULL = TypeKlassPtr::make(TypePtr::BotPTR, current->env()->Object_klass(), Offset(0) );
 648   TypeKlassPtr::BOTTOM = (EnableValhalla | EnableMVT) ? TypeKlassPtr::make(TypePtr::BotPTR, NULL, Offset(0)) : TypeKlassPtr::OBJECT_OR_NULL;
 649 
 650   const Type **fi2c = TypeTuple::fields(2);
 651   fi2c[TypeFunc::Parms+0] = TypeInstPtr::BOTTOM; // Method*
 652   fi2c[TypeFunc::Parms+1] = TypeRawPtr::BOTTOM; // argument pointer
 653   TypeTuple::START_I2C = TypeTuple::make(TypeFunc::Parms+2, fi2c);
 654 
 655   const Type **intpair = TypeTuple::fields(2);
 656   intpair[0] = TypeInt::INT;
 657   intpair[1] = TypeInt::INT;
 658   TypeTuple::INT_PAIR = TypeTuple::make(2, intpair);
 659 
 660   const Type **longpair = TypeTuple::fields(2);
 661   longpair[0] = TypeLong::LONG;
 662   longpair[1] = TypeLong::LONG;
 663   TypeTuple::LONG_PAIR = TypeTuple::make(2, longpair);
 664 
 665   const Type **intccpair = TypeTuple::fields(2);
 666   intccpair[0] = TypeInt::INT;
 667   intccpair[1] = TypeInt::CC;
 668   TypeTuple::INT_CC_PAIR = TypeTuple::make(2, intccpair);
 669 
 670   const Type **longccpair = TypeTuple::fields(2);
 671   longccpair[0] = TypeLong::LONG;
 672   longccpair[1] = TypeInt::CC;
 673   TypeTuple::LONG_CC_PAIR = TypeTuple::make(2, longccpair);
 674 
 675   _const_basic_type[T_NARROWOOP]   = TypeNarrowOop::BOTTOM;
 676   _const_basic_type[T_NARROWKLASS] = Type::BOTTOM;
 677   _const_basic_type[T_BOOLEAN]     = TypeInt::BOOL;
 678   _const_basic_type[T_CHAR]        = TypeInt::CHAR;
 679   _const_basic_type[T_BYTE]        = TypeInt::BYTE;
 680   _const_basic_type[T_SHORT]       = TypeInt::SHORT;
 681   _const_basic_type[T_INT]         = TypeInt::INT;
 682   _const_basic_type[T_LONG]        = TypeLong::LONG;
 683   _const_basic_type[T_FLOAT]       = Type::FLOAT;
 684   _const_basic_type[T_DOUBLE]      = Type::DOUBLE;
 685   _const_basic_type[T_OBJECT]      = TypeInstPtr::BOTTOM;
 686   _const_basic_type[T_ARRAY]       = TypeInstPtr::BOTTOM; // there is no separate bottom for arrays
 687   _const_basic_type[T_VALUETYPE]   = TypeInstPtr::BOTTOM;
 688   _const_basic_type[T_VOID]        = TypePtr::NULL_PTR;   // reflection represents void this way
 689   _const_basic_type[T_ADDRESS]     = TypeRawPtr::BOTTOM;  // both interpreter return addresses & random raw ptrs
 690   _const_basic_type[T_CONFLICT]    = Type::BOTTOM;        // why not?
 691 
 692   _zero_type[T_NARROWOOP]   = TypeNarrowOop::NULL_PTR;
 693   _zero_type[T_NARROWKLASS] = TypeNarrowKlass::NULL_PTR;
 694   _zero_type[T_BOOLEAN]     = TypeInt::ZERO;     // false == 0
 695   _zero_type[T_CHAR]        = TypeInt::ZERO;     // '\0' == 0
 696   _zero_type[T_BYTE]        = TypeInt::ZERO;     // 0x00 == 0
 697   _zero_type[T_SHORT]       = TypeInt::ZERO;     // 0x0000 == 0
 698   _zero_type[T_INT]         = TypeInt::ZERO;
 699   _zero_type[T_LONG]        = TypeLong::ZERO;
 700   _zero_type[T_FLOAT]       = TypeF::ZERO;
 701   _zero_type[T_DOUBLE]      = TypeD::ZERO;
 702   _zero_type[T_OBJECT]      = TypePtr::NULL_PTR;
 703   _zero_type[T_ARRAY]       = TypePtr::NULL_PTR; // null array is null oop
 704   _zero_type[T_VALUETYPE]   = TypePtr::NULL_PTR;
 705   _zero_type[T_ADDRESS]     = TypePtr::NULL_PTR; // raw pointers use the same null
 706   _zero_type[T_VOID]        = Type::TOP;         // the only void value is no value at all
 707 
 708   // get_zero_type() should not happen for T_CONFLICT
 709   _zero_type[T_CONFLICT]= NULL;
 710 
 711   // Vector predefined types, it needs initialized _const_basic_type[].
 712   if (Matcher::vector_size_supported(T_BYTE,4)) {
 713     TypeVect::VECTS = TypeVect::make(T_BYTE,4);
 714   }
 715   if (Matcher::vector_size_supported(T_FLOAT,2)) {
 716     TypeVect::VECTD = TypeVect::make(T_FLOAT,2);
 717   }
 718   if (Matcher::vector_size_supported(T_FLOAT,4)) {
 719     TypeVect::VECTX = TypeVect::make(T_FLOAT,4);
 720   }
 721   if (Matcher::vector_size_supported(T_FLOAT,8)) {
 722     TypeVect::VECTY = TypeVect::make(T_FLOAT,8);
 723   }
 724   if (Matcher::vector_size_supported(T_FLOAT,16)) {
 725     TypeVect::VECTZ = TypeVect::make(T_FLOAT,16);
 726   }
 727   mreg2type[Op_VecS] = TypeVect::VECTS;
 728   mreg2type[Op_VecD] = TypeVect::VECTD;
 729   mreg2type[Op_VecX] = TypeVect::VECTX;
 730   mreg2type[Op_VecY] = TypeVect::VECTY;
 731   mreg2type[Op_VecZ] = TypeVect::VECTZ;
 732 
 733   // Restore working type arena.
 734   current->set_type_arena(save);
 735   current->set_type_dict(NULL);
 736 }
 737 
 738 //------------------------------Initialize-------------------------------------
 739 void Type::Initialize(Compile* current) {
 740   assert(current->type_arena() != NULL, "must have created type arena");
 741 
 742   if (_shared_type_dict == NULL) {
 743     Initialize_shared(current);
 744   }
 745 
 746   Arena* type_arena = current->type_arena();
 747 
 748   // Create the hash-cons'ing dictionary with top-level storage allocation
 749   Dict *tdic = new (type_arena) Dict( (CmpKey)Type::cmp,(Hash)Type::uhash, type_arena, 128 );
 750   current->set_type_dict(tdic);
 751 
 752   // Transfer the shared types.
 753   DictI i(_shared_type_dict);
 754   for( ; i.test(); ++i ) {
 755     Type* t = (Type*)i._value;
 756     tdic->Insert(t,t);  // New Type, insert into Type table
 757   }
 758 }
 759 
 760 //------------------------------hashcons---------------------------------------
 761 // Do the hash-cons trick.  If the Type already exists in the type table,
 762 // delete the current Type and return the existing Type.  Otherwise stick the
 763 // current Type in the Type table.
 764 const Type *Type::hashcons(void) {
 765   debug_only(base());           // Check the assertion in Type::base().
 766   // Look up the Type in the Type dictionary
 767   Dict *tdic = type_dict();
 768   Type* old = (Type*)(tdic->Insert(this, this, false));
 769   if( old ) {                   // Pre-existing Type?
 770     if( old != this )           // Yes, this guy is not the pre-existing?
 771       delete this;              // Yes, Nuke this guy
 772     assert( old->_dual, "" );
 773     return old;                 // Return pre-existing
 774   }
 775 
 776   // Every type has a dual (to make my lattice symmetric).
 777   // Since we just discovered a new Type, compute its dual right now.
 778   assert( !_dual, "" );         // No dual yet
 779   _dual = xdual();              // Compute the dual
 780   if( cmp(this,_dual)==0 ) {    // Handle self-symmetric
 781     _dual = this;
 782     return this;
 783   }
 784   assert( !_dual->_dual, "" );  // No reverse dual yet
 785   assert( !(*tdic)[_dual], "" ); // Dual not in type system either
 786   // New Type, insert into Type table
 787   tdic->Insert((void*)_dual,(void*)_dual);
 788   ((Type*)_dual)->_dual = this; // Finish up being symmetric
 789 #ifdef ASSERT
 790   Type *dual_dual = (Type*)_dual->xdual();
 791   assert( eq(dual_dual), "xdual(xdual()) should be identity" );
 792   delete dual_dual;
 793 #endif
 794   return this;                  // Return new Type
 795 }
 796 
 797 //------------------------------eq---------------------------------------------
 798 // Structural equality check for Type representations
 799 bool Type::eq( const Type * ) const {
 800   return true;                  // Nothing else can go wrong
 801 }
 802 
 803 //------------------------------hash-------------------------------------------
 804 // Type-specific hashing function.
 805 int Type::hash(void) const {
 806   return _base;
 807 }
 808 
 809 //------------------------------is_finite--------------------------------------
 810 // Has a finite value
 811 bool Type::is_finite() const {
 812   return false;
 813 }
 814 
 815 //------------------------------is_nan-----------------------------------------
 816 // Is not a number (NaN)
 817 bool Type::is_nan()    const {
 818   return false;
 819 }
 820 
 821 //----------------------interface_vs_oop---------------------------------------
 822 #ifdef ASSERT
 823 bool Type::interface_vs_oop_helper(const Type *t) const {
 824   bool result = false;
 825 
 826   const TypePtr* this_ptr = this->make_ptr(); // In case it is narrow_oop
 827   const TypePtr*    t_ptr =    t->make_ptr();
 828   if( this_ptr == NULL || t_ptr == NULL )
 829     return result;
 830 
 831   const TypeInstPtr* this_inst = this_ptr->isa_instptr();
 832   const TypeInstPtr*    t_inst =    t_ptr->isa_instptr();
 833   if( this_inst && this_inst->is_loaded() && t_inst && t_inst->is_loaded() ) {
 834     bool this_interface = this_inst->klass()->is_interface();
 835     bool    t_interface =    t_inst->klass()->is_interface();
 836     result = this_interface ^ t_interface;
 837   }
 838 
 839   return result;
 840 }
 841 
 842 bool Type::interface_vs_oop(const Type *t) const {
 843   if (interface_vs_oop_helper(t)) {
 844     return true;
 845   }
 846   // Now check the speculative parts as well
 847   const TypePtr* this_spec = isa_ptr() != NULL ? is_ptr()->speculative() : NULL;
 848   const TypePtr* t_spec = t->isa_ptr() != NULL ? t->is_ptr()->speculative() : NULL;
 849   if (this_spec != NULL && t_spec != NULL) {
 850     if (this_spec->interface_vs_oop_helper(t_spec)) {
 851       return true;
 852     }
 853     return false;
 854   }
 855   if (this_spec != NULL && this_spec->interface_vs_oop_helper(t)) {
 856     return true;
 857   }
 858   if (t_spec != NULL && interface_vs_oop_helper(t_spec)) {
 859     return true;
 860   }
 861   return false;
 862 }
 863 
 864 #endif
 865 
 866 //------------------------------meet-------------------------------------------
 867 // Compute the MEET of two types.  NOT virtual.  It enforces that meet is
 868 // commutative and the lattice is symmetric.
 869 const Type *Type::meet_helper(const Type *t, bool include_speculative) const {
 870   if (isa_narrowoop() && t->isa_narrowoop()) {
 871     const Type* result = make_ptr()->meet_helper(t->make_ptr(), include_speculative);
 872     return result->make_narrowoop();
 873   }
 874   if (isa_narrowklass() && t->isa_narrowklass()) {
 875     const Type* result = make_ptr()->meet_helper(t->make_ptr(), include_speculative);
 876     return result->make_narrowklass();
 877   }
 878 
 879   const Type *this_t = maybe_remove_speculative(include_speculative);
 880   t = t->maybe_remove_speculative(include_speculative);
 881 
 882   const Type *mt = this_t->xmeet(t);
 883   if (isa_narrowoop() || t->isa_narrowoop()) return mt;
 884   if (isa_narrowklass() || t->isa_narrowklass()) return mt;
 885 #ifdef ASSERT
 886   assert(mt == t->xmeet(this_t), "meet not commutative");
 887   const Type* dual_join = mt->_dual;
 888   const Type *t2t    = dual_join->xmeet(t->_dual);
 889   const Type *t2this = dual_join->xmeet(this_t->_dual);
 890 
 891   // Interface meet Oop is Not Symmetric:
 892   // Interface:AnyNull meet Oop:AnyNull == Interface:AnyNull
 893   // Interface:NotNull meet Oop:NotNull == java/lang/Object:NotNull
 894 
 895   if( !interface_vs_oop(t) && (t2t != t->_dual || t2this != this_t->_dual) ) {
 896     tty->print_cr("=== Meet Not Symmetric ===");
 897     tty->print("t   =                   ");              t->dump(); tty->cr();
 898     tty->print("this=                   ");         this_t->dump(); tty->cr();
 899     tty->print("mt=(t meet this)=       ");             mt->dump(); tty->cr();
 900 
 901     tty->print("t_dual=                 ");       t->_dual->dump(); tty->cr();
 902     tty->print("this_dual=              ");  this_t->_dual->dump(); tty->cr();
 903     tty->print("mt_dual=                ");      mt->_dual->dump(); tty->cr();
 904 
 905     tty->print("mt_dual meet t_dual=    "); t2t           ->dump(); tty->cr();
 906     tty->print("mt_dual meet this_dual= "); t2this        ->dump(); tty->cr();
 907 
 908     fatal("meet not symmetric" );
 909   }
 910 #endif
 911   return mt;
 912 }
 913 
 914 //------------------------------xmeet------------------------------------------
 915 // Compute the MEET of two types.  It returns a new Type object.
 916 const Type *Type::xmeet( const Type *t ) const {
 917   // Perform a fast test for common case; meeting the same types together.
 918   if( this == t ) return this;  // Meeting same type-rep?
 919 
 920   // Meeting TOP with anything?
 921   if( _base == Top ) return t;
 922 
 923   // Meeting BOTTOM with anything?
 924   if( _base == Bottom ) return BOTTOM;
 925 
 926   // Current "this->_base" is one of: Bad, Multi, Control, Top,
 927   // Abio, Abstore, Floatxxx, Doublexxx, Bottom, lastype.
 928   switch (t->base()) {  // Switch on original type
 929 
 930   // Cut in half the number of cases I must handle.  Only need cases for when
 931   // the given enum "t->type" is less than or equal to the local enum "type".
 932   case FloatCon:
 933   case DoubleCon:
 934   case Int:
 935   case Long:
 936     return t->xmeet(this);
 937 
 938   case OopPtr:
 939     return t->xmeet(this);
 940 
 941   case InstPtr:
 942   case ValueTypePtr:
 943     return t->xmeet(this);
 944 
 945   case MetadataPtr:
 946   case KlassPtr:
 947     return t->xmeet(this);
 948 
 949   case AryPtr:
 950     return t->xmeet(this);
 951 
 952   case NarrowOop:
 953     return t->xmeet(this);
 954 
 955   case NarrowKlass:
 956     return t->xmeet(this);
 957 
 958   case ValueType:
 959     return t->xmeet(this);
 960 
 961   case Bad:                     // Type check
 962   default:                      // Bogus type not in lattice
 963     typerr(t);
 964     return Type::BOTTOM;
 965 
 966   case Bottom:                  // Ye Olde Default
 967     return t;
 968 
 969   case FloatTop:
 970     if( _base == FloatTop ) return this;
 971   case FloatBot:                // Float
 972     if( _base == FloatBot || _base == FloatTop ) return FLOAT;
 973     if( _base == DoubleTop || _base == DoubleBot ) return Type::BOTTOM;
 974     typerr(t);
 975     return Type::BOTTOM;
 976 
 977   case DoubleTop:
 978     if( _base == DoubleTop ) return this;
 979   case DoubleBot:               // Double
 980     if( _base == DoubleBot || _base == DoubleTop ) return DOUBLE;
 981     if( _base == FloatTop || _base == FloatBot ) return Type::BOTTOM;
 982     typerr(t);
 983     return Type::BOTTOM;
 984 
 985   // These next few cases must match exactly or it is a compile-time error.
 986   case Control:                 // Control of code
 987   case Abio:                    // State of world outside of program
 988   case Memory:
 989     if( _base == t->_base )  return this;
 990     typerr(t);
 991     return Type::BOTTOM;
 992 
 993   case Top:                     // Top of the lattice
 994     return this;
 995   }
 996 
 997   // The type is unchanged
 998   return this;
 999 }
1000 
1001 //-----------------------------filter------------------------------------------
1002 const Type *Type::filter_helper(const Type *kills, bool include_speculative) const {
1003   const Type* ft = join_helper(kills, include_speculative);
1004   if (ft->empty())
1005     return Type::TOP;           // Canonical empty value
1006   return ft;
1007 }
1008 
1009 //------------------------------xdual------------------------------------------
1010 // Compute dual right now.
1011 const Type::TYPES Type::dual_type[Type::lastype] = {
1012   Bad,          // Bad
1013   Control,      // Control
1014   Bottom,       // Top
1015   Bad,          // Int - handled in v-call
1016   Bad,          // Long - handled in v-call
1017   Half,         // Half
1018   Bad,          // NarrowOop - handled in v-call
1019   Bad,          // NarrowKlass - handled in v-call
1020 
1021   Bad,          // Tuple - handled in v-call
1022   Bad,          // Array - handled in v-call
1023   Bad,          // VectorS - handled in v-call
1024   Bad,          // VectorD - handled in v-call
1025   Bad,          // VectorX - handled in v-call
1026   Bad,          // VectorY - handled in v-call
1027   Bad,          // VectorZ - handled in v-call
1028   Bad,          // ValueType - handled in v-call
1029 
1030   Bad,          // AnyPtr - handled in v-call
1031   Bad,          // RawPtr - handled in v-call
1032   Bad,          // OopPtr - handled in v-call
1033   Bad,          // InstPtr - handled in v-call
1034   Bad,          // ValueTypePtr - handled in v-call
1035   Bad,          // AryPtr - handled in v-call
1036 
1037   Bad,          //  MetadataPtr - handled in v-call
1038   Bad,          // KlassPtr - handled in v-call
1039 
1040   Bad,          // Function - handled in v-call
1041   Abio,         // Abio
1042   Return_Address,// Return_Address
1043   Memory,       // Memory
1044   FloatBot,     // FloatTop
1045   FloatCon,     // FloatCon
1046   FloatTop,     // FloatBot
1047   DoubleBot,    // DoubleTop
1048   DoubleCon,    // DoubleCon
1049   DoubleTop,    // DoubleBot
1050   Top           // Bottom
1051 };
1052 
1053 const Type *Type::xdual() const {
1054   // Note: the base() accessor asserts the sanity of _base.
1055   assert(_type_info[base()].dual_type != Bad, "implement with v-call");
1056   return new Type(_type_info[_base].dual_type);
1057 }
1058 
1059 //------------------------------has_memory-------------------------------------
1060 bool Type::has_memory() const {
1061   Type::TYPES tx = base();
1062   if (tx == Memory) return true;
1063   if (tx == Tuple) {
1064     const TypeTuple *t = is_tuple();
1065     for (uint i=0; i < t->cnt(); i++) {
1066       tx = t->field_at(i)->base();
1067       if (tx == Memory)  return true;
1068     }
1069   }
1070   return false;
1071 }
1072 
1073 #ifndef PRODUCT
1074 //------------------------------dump2------------------------------------------
1075 void Type::dump2( Dict &d, uint depth, outputStream *st ) const {
1076   st->print("%s", _type_info[_base].msg);
1077 }
1078 
1079 //------------------------------dump-------------------------------------------
1080 void Type::dump_on(outputStream *st) const {
1081   ResourceMark rm;
1082   Dict d(cmpkey,hashkey);       // Stop recursive type dumping
1083   dump2(d,1, st);
1084   if (is_ptr_to_narrowoop()) {
1085     st->print(" [narrow]");
1086   } else if (is_ptr_to_narrowklass()) {
1087     st->print(" [narrowklass]");
1088   }
1089 }
1090 
1091 //-----------------------------------------------------------------------------
1092 const char* Type::str(const Type* t) {
1093   stringStream ss;
1094   t->dump_on(&ss);
1095   return ss.as_string();
1096 }
1097 #endif
1098 
1099 //------------------------------singleton--------------------------------------
1100 // TRUE if Type is a singleton type, FALSE otherwise.   Singletons are simple
1101 // constants (Ldi nodes).  Singletons are integer, float or double constants.
1102 bool Type::singleton(void) const {
1103   return _base == Top || _base == Half;
1104 }
1105 
1106 //------------------------------empty------------------------------------------
1107 // TRUE if Type is a type with no values, FALSE otherwise.
1108 bool Type::empty(void) const {
1109   switch (_base) {
1110   case DoubleTop:
1111   case FloatTop:
1112   case Top:
1113     return true;
1114 
1115   case Half:
1116   case Abio:
1117   case Return_Address:
1118   case Memory:
1119   case Bottom:
1120   case FloatBot:
1121   case DoubleBot:
1122     return false;  // never a singleton, therefore never empty
1123   }
1124 
1125   ShouldNotReachHere();
1126   return false;
1127 }
1128 
1129 //------------------------------dump_stats-------------------------------------
1130 // Dump collected statistics to stderr
1131 #ifndef PRODUCT
1132 void Type::dump_stats() {
1133   tty->print("Types made: %d\n", type_dict()->Size());
1134 }
1135 #endif
1136 
1137 //------------------------------typerr-----------------------------------------
1138 void Type::typerr( const Type *t ) const {
1139 #ifndef PRODUCT
1140   tty->print("\nError mixing types: ");
1141   dump();
1142   tty->print(" and ");
1143   t->dump();
1144   tty->print("\n");
1145 #endif
1146   ShouldNotReachHere();
1147 }
1148 
1149 
1150 //=============================================================================
1151 // Convenience common pre-built types.
1152 const TypeF *TypeF::ZERO;       // Floating point zero
1153 const TypeF *TypeF::ONE;        // Floating point one
1154 
1155 //------------------------------make-------------------------------------------
1156 // Create a float constant
1157 const TypeF *TypeF::make(float f) {
1158   return (TypeF*)(new TypeF(f))->hashcons();
1159 }
1160 
1161 //------------------------------meet-------------------------------------------
1162 // Compute the MEET of two types.  It returns a new Type object.
1163 const Type *TypeF::xmeet( const Type *t ) const {
1164   // Perform a fast test for common case; meeting the same types together.
1165   if( this == t ) return this;  // Meeting same type-rep?
1166 
1167   // Current "this->_base" is FloatCon
1168   switch (t->base()) {          // Switch on original type
1169   case AnyPtr:                  // Mixing with oops happens when javac
1170   case RawPtr:                  // reuses local variables
1171   case OopPtr:
1172   case InstPtr:
1173   case ValueTypePtr:
1174   case AryPtr:
1175   case MetadataPtr:
1176   case KlassPtr:
1177   case NarrowOop:
1178   case NarrowKlass:
1179   case Int:
1180   case Long:
1181   case DoubleTop:
1182   case DoubleCon:
1183   case DoubleBot:
1184   case Bottom:                  // Ye Olde Default
1185     return Type::BOTTOM;
1186 
1187   case FloatBot:
1188     return t;
1189 
1190   default:                      // All else is a mistake
1191     typerr(t);
1192 
1193   case FloatCon:                // Float-constant vs Float-constant?
1194     if( jint_cast(_f) != jint_cast(t->getf()) )         // unequal constants?
1195                                 // must compare bitwise as positive zero, negative zero and NaN have
1196                                 // all the same representation in C++
1197       return FLOAT;             // Return generic float
1198                                 // Equal constants
1199   case Top:
1200   case FloatTop:
1201     break;                      // Return the float constant
1202   }
1203   return this;                  // Return the float constant
1204 }
1205 
1206 //------------------------------xdual------------------------------------------
1207 // Dual: symmetric
1208 const Type *TypeF::xdual() const {
1209   return this;
1210 }
1211 
1212 //------------------------------eq---------------------------------------------
1213 // Structural equality check for Type representations
1214 bool TypeF::eq(const Type *t) const {
1215   // Bitwise comparison to distinguish between +/-0. These values must be treated
1216   // as different to be consistent with C1 and the interpreter.
1217   return (jint_cast(_f) == jint_cast(t->getf()));
1218 }
1219 
1220 //------------------------------hash-------------------------------------------
1221 // Type-specific hashing function.
1222 int TypeF::hash(void) const {
1223   return *(int*)(&_f);
1224 }
1225 
1226 //------------------------------is_finite--------------------------------------
1227 // Has a finite value
1228 bool TypeF::is_finite() const {
1229   return g_isfinite(getf()) != 0;
1230 }
1231 
1232 //------------------------------is_nan-----------------------------------------
1233 // Is not a number (NaN)
1234 bool TypeF::is_nan()    const {
1235   return g_isnan(getf()) != 0;
1236 }
1237 
1238 //------------------------------dump2------------------------------------------
1239 // Dump float constant Type
1240 #ifndef PRODUCT
1241 void TypeF::dump2( Dict &d, uint depth, outputStream *st ) const {
1242   Type::dump2(d,depth, st);
1243   st->print("%f", _f);
1244 }
1245 #endif
1246 
1247 //------------------------------singleton--------------------------------------
1248 // TRUE if Type is a singleton type, FALSE otherwise.   Singletons are simple
1249 // constants (Ldi nodes).  Singletons are integer, float or double constants
1250 // or a single symbol.
1251 bool TypeF::singleton(void) const {
1252   return true;                  // Always a singleton
1253 }
1254 
1255 bool TypeF::empty(void) const {
1256   return false;                 // always exactly a singleton
1257 }
1258 
1259 //=============================================================================
1260 // Convenience common pre-built types.
1261 const TypeD *TypeD::ZERO;       // Floating point zero
1262 const TypeD *TypeD::ONE;        // Floating point one
1263 
1264 //------------------------------make-------------------------------------------
1265 const TypeD *TypeD::make(double d) {
1266   return (TypeD*)(new TypeD(d))->hashcons();
1267 }
1268 
1269 //------------------------------meet-------------------------------------------
1270 // Compute the MEET of two types.  It returns a new Type object.
1271 const Type *TypeD::xmeet( const Type *t ) const {
1272   // Perform a fast test for common case; meeting the same types together.
1273   if( this == t ) return this;  // Meeting same type-rep?
1274 
1275   // Current "this->_base" is DoubleCon
1276   switch (t->base()) {          // Switch on original type
1277   case AnyPtr:                  // Mixing with oops happens when javac
1278   case RawPtr:                  // reuses local variables
1279   case OopPtr:
1280   case InstPtr:
1281   case ValueTypePtr:
1282   case AryPtr:
1283   case MetadataPtr:
1284   case KlassPtr:
1285   case NarrowOop:
1286   case NarrowKlass:
1287   case Int:
1288   case Long:
1289   case FloatTop:
1290   case FloatCon:
1291   case FloatBot:
1292   case Bottom:                  // Ye Olde Default
1293     return Type::BOTTOM;
1294 
1295   case DoubleBot:
1296     return t;
1297 
1298   default:                      // All else is a mistake
1299     typerr(t);
1300 
1301   case DoubleCon:               // Double-constant vs Double-constant?
1302     if( jlong_cast(_d) != jlong_cast(t->getd()) )       // unequal constants? (see comment in TypeF::xmeet)
1303       return DOUBLE;            // Return generic double
1304   case Top:
1305   case DoubleTop:
1306     break;
1307   }
1308   return this;                  // Return the double constant
1309 }
1310 
1311 //------------------------------xdual------------------------------------------
1312 // Dual: symmetric
1313 const Type *TypeD::xdual() const {
1314   return this;
1315 }
1316 
1317 //------------------------------eq---------------------------------------------
1318 // Structural equality check for Type representations
1319 bool TypeD::eq(const Type *t) const {
1320   // Bitwise comparison to distinguish between +/-0. These values must be treated
1321   // as different to be consistent with C1 and the interpreter.
1322   return (jlong_cast(_d) == jlong_cast(t->getd()));
1323 }
1324 
1325 //------------------------------hash-------------------------------------------
1326 // Type-specific hashing function.
1327 int TypeD::hash(void) const {
1328   return *(int*)(&_d);
1329 }
1330 
1331 //------------------------------is_finite--------------------------------------
1332 // Has a finite value
1333 bool TypeD::is_finite() const {
1334   return g_isfinite(getd()) != 0;
1335 }
1336 
1337 //------------------------------is_nan-----------------------------------------
1338 // Is not a number (NaN)
1339 bool TypeD::is_nan()    const {
1340   return g_isnan(getd()) != 0;
1341 }
1342 
1343 //------------------------------dump2------------------------------------------
1344 // Dump double constant Type
1345 #ifndef PRODUCT
1346 void TypeD::dump2( Dict &d, uint depth, outputStream *st ) const {
1347   Type::dump2(d,depth,st);
1348   st->print("%f", _d);
1349 }
1350 #endif
1351 
1352 //------------------------------singleton--------------------------------------
1353 // TRUE if Type is a singleton type, FALSE otherwise.   Singletons are simple
1354 // constants (Ldi nodes).  Singletons are integer, float or double constants
1355 // or a single symbol.
1356 bool TypeD::singleton(void) const {
1357   return true;                  // Always a singleton
1358 }
1359 
1360 bool TypeD::empty(void) const {
1361   return false;                 // always exactly a singleton
1362 }
1363 
1364 //=============================================================================
1365 // Convience common pre-built types.
1366 const TypeInt *TypeInt::MINUS_1;// -1
1367 const TypeInt *TypeInt::ZERO;   // 0
1368 const TypeInt *TypeInt::ONE;    // 1
1369 const TypeInt *TypeInt::BOOL;   // 0 or 1, FALSE or TRUE.
1370 const TypeInt *TypeInt::CC;     // -1,0 or 1, condition codes
1371 const TypeInt *TypeInt::CC_LT;  // [-1]  == MINUS_1
1372 const TypeInt *TypeInt::CC_GT;  // [1]   == ONE
1373 const TypeInt *TypeInt::CC_EQ;  // [0]   == ZERO
1374 const TypeInt *TypeInt::CC_LE;  // [-1,0]
1375 const TypeInt *TypeInt::CC_GE;  // [0,1] == BOOL (!)
1376 const TypeInt *TypeInt::BYTE;   // Bytes, -128 to 127
1377 const TypeInt *TypeInt::UBYTE;  // Unsigned Bytes, 0 to 255
1378 const TypeInt *TypeInt::CHAR;   // Java chars, 0-65535
1379 const TypeInt *TypeInt::SHORT;  // Java shorts, -32768-32767
1380 const TypeInt *TypeInt::POS;    // Positive 32-bit integers or zero
1381 const TypeInt *TypeInt::POS1;   // Positive 32-bit integers
1382 const TypeInt *TypeInt::INT;    // 32-bit integers
1383 const TypeInt *TypeInt::SYMINT; // symmetric range [-max_jint..max_jint]
1384 const TypeInt *TypeInt::TYPE_DOMAIN; // alias for TypeInt::INT
1385 
1386 //------------------------------TypeInt----------------------------------------
1387 TypeInt::TypeInt( jint lo, jint hi, int w ) : Type(Int), _lo(lo), _hi(hi), _widen(w) {
1388 }
1389 
1390 //------------------------------make-------------------------------------------
1391 const TypeInt *TypeInt::make( jint lo ) {
1392   return (TypeInt*)(new TypeInt(lo,lo,WidenMin))->hashcons();
1393 }
1394 
1395 static int normalize_int_widen( jint lo, jint hi, int w ) {
1396   // Certain normalizations keep us sane when comparing types.
1397   // The 'SMALLINT' covers constants and also CC and its relatives.
1398   if (lo <= hi) {
1399     if (((juint)hi - lo) <= SMALLINT)  w = Type::WidenMin;
1400     if (((juint)hi - lo) >= max_juint) w = Type::WidenMax; // TypeInt::INT
1401   } else {
1402     if (((juint)lo - hi) <= SMALLINT)  w = Type::WidenMin;
1403     if (((juint)lo - hi) >= max_juint) w = Type::WidenMin; // dual TypeInt::INT
1404   }
1405   return w;
1406 }
1407 
1408 const TypeInt *TypeInt::make( jint lo, jint hi, int w ) {
1409   w = normalize_int_widen(lo, hi, w);
1410   return (TypeInt*)(new TypeInt(lo,hi,w))->hashcons();
1411 }
1412 
1413 //------------------------------meet-------------------------------------------
1414 // Compute the MEET of two types.  It returns a new Type representation object
1415 // with reference count equal to the number of Types pointing at it.
1416 // Caller should wrap a Types around it.
1417 const Type *TypeInt::xmeet( const Type *t ) const {
1418   // Perform a fast test for common case; meeting the same types together.
1419   if( this == t ) return this;  // Meeting same type?
1420 
1421   // Currently "this->_base" is a TypeInt
1422   switch (t->base()) {          // Switch on original type
1423   case AnyPtr:                  // Mixing with oops happens when javac
1424   case RawPtr:                  // reuses local variables
1425   case OopPtr:
1426   case InstPtr:
1427   case ValueTypePtr:
1428   case AryPtr:
1429   case MetadataPtr:
1430   case KlassPtr:
1431   case NarrowOop:
1432   case NarrowKlass:
1433   case Long:
1434   case FloatTop:
1435   case FloatCon:
1436   case FloatBot:
1437   case DoubleTop:
1438   case DoubleCon:
1439   case DoubleBot:
1440   case Bottom:                  // Ye Olde Default
1441     return Type::BOTTOM;
1442   default:                      // All else is a mistake
1443     typerr(t);
1444   case Top:                     // No change
1445     return this;
1446   case Int:                     // Int vs Int?
1447     break;
1448   }
1449 
1450   // Expand covered set
1451   const TypeInt *r = t->is_int();
1452   return make( MIN2(_lo,r->_lo), MAX2(_hi,r->_hi), MAX2(_widen,r->_widen) );
1453 }
1454 
1455 //------------------------------xdual------------------------------------------
1456 // Dual: reverse hi & lo; flip widen
1457 const Type *TypeInt::xdual() const {
1458   int w = normalize_int_widen(_hi,_lo, WidenMax-_widen);
1459   return new TypeInt(_hi,_lo,w);
1460 }
1461 
1462 //------------------------------widen------------------------------------------
1463 // Only happens for optimistic top-down optimizations.
1464 const Type *TypeInt::widen( const Type *old, const Type* limit ) const {
1465   // Coming from TOP or such; no widening
1466   if( old->base() != Int ) return this;
1467   const TypeInt *ot = old->is_int();
1468 
1469   // If new guy is equal to old guy, no widening
1470   if( _lo == ot->_lo && _hi == ot->_hi )
1471     return old;
1472 
1473   // If new guy contains old, then we widened
1474   if( _lo <= ot->_lo && _hi >= ot->_hi ) {
1475     // New contains old
1476     // If new guy is already wider than old, no widening
1477     if( _widen > ot->_widen ) return this;
1478     // If old guy was a constant, do not bother
1479     if (ot->_lo == ot->_hi)  return this;
1480     // Now widen new guy.
1481     // Check for widening too far
1482     if (_widen == WidenMax) {
1483       int max = max_jint;
1484       int min = min_jint;
1485       if (limit->isa_int()) {
1486         max = limit->is_int()->_hi;
1487         min = limit->is_int()->_lo;
1488       }
1489       if (min < _lo && _hi < max) {
1490         // If neither endpoint is extremal yet, push out the endpoint
1491         // which is closer to its respective limit.
1492         if (_lo >= 0 ||                 // easy common case
1493             (juint)(_lo - min) >= (juint)(max - _hi)) {
1494           // Try to widen to an unsigned range type of 31 bits:
1495           return make(_lo, max, WidenMax);
1496         } else {
1497           return make(min, _hi, WidenMax);
1498         }
1499       }
1500       return TypeInt::INT;
1501     }
1502     // Returned widened new guy
1503     return make(_lo,_hi,_widen+1);
1504   }
1505 
1506   // If old guy contains new, then we probably widened too far & dropped to
1507   // bottom.  Return the wider fellow.
1508   if ( ot->_lo <= _lo && ot->_hi >= _hi )
1509     return old;
1510 
1511   //fatal("Integer value range is not subset");
1512   //return this;
1513   return TypeInt::INT;
1514 }
1515 
1516 //------------------------------narrow---------------------------------------
1517 // Only happens for pessimistic optimizations.
1518 const Type *TypeInt::narrow( const Type *old ) const {
1519   if (_lo >= _hi)  return this;   // already narrow enough
1520   if (old == NULL)  return this;
1521   const TypeInt* ot = old->isa_int();
1522   if (ot == NULL)  return this;
1523   jint olo = ot->_lo;
1524   jint ohi = ot->_hi;
1525 
1526   // If new guy is equal to old guy, no narrowing
1527   if (_lo == olo && _hi == ohi)  return old;
1528 
1529   // If old guy was maximum range, allow the narrowing
1530   if (olo == min_jint && ohi == max_jint)  return this;
1531 
1532   if (_lo < olo || _hi > ohi)
1533     return this;                // doesn't narrow; pretty wierd
1534 
1535   // The new type narrows the old type, so look for a "death march".
1536   // See comments on PhaseTransform::saturate.
1537   juint nrange = (juint)_hi - _lo;
1538   juint orange = (juint)ohi - olo;
1539   if (nrange < max_juint - 1 && nrange > (orange >> 1) + (SMALLINT*2)) {
1540     // Use the new type only if the range shrinks a lot.
1541     // We do not want the optimizer computing 2^31 point by point.
1542     return old;
1543   }
1544 
1545   return this;
1546 }
1547 
1548 //-----------------------------filter------------------------------------------
1549 const Type *TypeInt::filter_helper(const Type *kills, bool include_speculative) const {
1550   const TypeInt* ft = join_helper(kills, include_speculative)->isa_int();
1551   if (ft == NULL || ft->empty())
1552     return Type::TOP;           // Canonical empty value
1553   if (ft->_widen < this->_widen) {
1554     // Do not allow the value of kill->_widen to affect the outcome.
1555     // The widen bits must be allowed to run freely through the graph.
1556     ft = TypeInt::make(ft->_lo, ft->_hi, this->_widen);
1557   }
1558   return ft;
1559 }
1560 
1561 //------------------------------eq---------------------------------------------
1562 // Structural equality check for Type representations
1563 bool TypeInt::eq( const Type *t ) const {
1564   const TypeInt *r = t->is_int(); // Handy access
1565   return r->_lo == _lo && r->_hi == _hi && r->_widen == _widen;
1566 }
1567 
1568 //------------------------------hash-------------------------------------------
1569 // Type-specific hashing function.
1570 int TypeInt::hash(void) const {
1571   return java_add(java_add(_lo, _hi), java_add(_widen, (int)Type::Int));
1572 }
1573 
1574 //------------------------------is_finite--------------------------------------
1575 // Has a finite value
1576 bool TypeInt::is_finite() const {
1577   return true;
1578 }
1579 
1580 //------------------------------dump2------------------------------------------
1581 // Dump TypeInt
1582 #ifndef PRODUCT
1583 static const char* intname(char* buf, jint n) {
1584   if (n == min_jint)
1585     return "min";
1586   else if (n < min_jint + 10000)
1587     sprintf(buf, "min+" INT32_FORMAT, n - min_jint);
1588   else if (n == max_jint)
1589     return "max";
1590   else if (n > max_jint - 10000)
1591     sprintf(buf, "max-" INT32_FORMAT, max_jint - n);
1592   else
1593     sprintf(buf, INT32_FORMAT, n);
1594   return buf;
1595 }
1596 
1597 void TypeInt::dump2( Dict &d, uint depth, outputStream *st ) const {
1598   char buf[40], buf2[40];
1599   if (_lo == min_jint && _hi == max_jint)
1600     st->print("int");
1601   else if (is_con())
1602     st->print("int:%s", intname(buf, get_con()));
1603   else if (_lo == BOOL->_lo && _hi == BOOL->_hi)
1604     st->print("bool");
1605   else if (_lo == BYTE->_lo && _hi == BYTE->_hi)
1606     st->print("byte");
1607   else if (_lo == CHAR->_lo && _hi == CHAR->_hi)
1608     st->print("char");
1609   else if (_lo == SHORT->_lo && _hi == SHORT->_hi)
1610     st->print("short");
1611   else if (_hi == max_jint)
1612     st->print("int:>=%s", intname(buf, _lo));
1613   else if (_lo == min_jint)
1614     st->print("int:<=%s", intname(buf, _hi));
1615   else
1616     st->print("int:%s..%s", intname(buf, _lo), intname(buf2, _hi));
1617 
1618   if (_widen != 0 && this != TypeInt::INT)
1619     st->print(":%.*s", _widen, "wwww");
1620 }
1621 #endif
1622 
1623 //------------------------------singleton--------------------------------------
1624 // TRUE if Type is a singleton type, FALSE otherwise.   Singletons are simple
1625 // constants.
1626 bool TypeInt::singleton(void) const {
1627   return _lo >= _hi;
1628 }
1629 
1630 bool TypeInt::empty(void) const {
1631   return _lo > _hi;
1632 }
1633 
1634 //=============================================================================
1635 // Convenience common pre-built types.
1636 const TypeLong *TypeLong::MINUS_1;// -1
1637 const TypeLong *TypeLong::ZERO; // 0
1638 const TypeLong *TypeLong::ONE;  // 1
1639 const TypeLong *TypeLong::POS;  // >=0
1640 const TypeLong *TypeLong::LONG; // 64-bit integers
1641 const TypeLong *TypeLong::INT;  // 32-bit subrange
1642 const TypeLong *TypeLong::UINT; // 32-bit unsigned subrange
1643 const TypeLong *TypeLong::TYPE_DOMAIN; // alias for TypeLong::LONG
1644 
1645 //------------------------------TypeLong---------------------------------------
1646 TypeLong::TypeLong( jlong lo, jlong hi, int w ) : Type(Long), _lo(lo), _hi(hi), _widen(w) {
1647 }
1648 
1649 //------------------------------make-------------------------------------------
1650 const TypeLong *TypeLong::make( jlong lo ) {
1651   return (TypeLong*)(new TypeLong(lo,lo,WidenMin))->hashcons();
1652 }
1653 
1654 static int normalize_long_widen( jlong lo, jlong hi, int w ) {
1655   // Certain normalizations keep us sane when comparing types.
1656   // The 'SMALLINT' covers constants.
1657   if (lo <= hi) {
1658     if (((julong)hi - lo) <= SMALLINT)   w = Type::WidenMin;
1659     if (((julong)hi - lo) >= max_julong) w = Type::WidenMax; // TypeLong::LONG
1660   } else {
1661     if (((julong)lo - hi) <= SMALLINT)   w = Type::WidenMin;
1662     if (((julong)lo - hi) >= max_julong) w = Type::WidenMin; // dual TypeLong::LONG
1663   }
1664   return w;
1665 }
1666 
1667 const TypeLong *TypeLong::make( jlong lo, jlong hi, int w ) {
1668   w = normalize_long_widen(lo, hi, w);
1669   return (TypeLong*)(new TypeLong(lo,hi,w))->hashcons();
1670 }
1671 
1672 
1673 //------------------------------meet-------------------------------------------
1674 // Compute the MEET of two types.  It returns a new Type representation object
1675 // with reference count equal to the number of Types pointing at it.
1676 // Caller should wrap a Types around it.
1677 const Type *TypeLong::xmeet( const Type *t ) const {
1678   // Perform a fast test for common case; meeting the same types together.
1679   if( this == t ) return this;  // Meeting same type?
1680 
1681   // Currently "this->_base" is a TypeLong
1682   switch (t->base()) {          // Switch on original type
1683   case AnyPtr:                  // Mixing with oops happens when javac
1684   case RawPtr:                  // reuses local variables
1685   case OopPtr:
1686   case InstPtr:
1687   case ValueTypePtr:
1688   case AryPtr:
1689   case MetadataPtr:
1690   case KlassPtr:
1691   case NarrowOop:
1692   case NarrowKlass:
1693   case Int:
1694   case FloatTop:
1695   case FloatCon:
1696   case FloatBot:
1697   case DoubleTop:
1698   case DoubleCon:
1699   case DoubleBot:
1700   case Bottom:                  // Ye Olde Default
1701     return Type::BOTTOM;
1702   default:                      // All else is a mistake
1703     typerr(t);
1704   case Top:                     // No change
1705     return this;
1706   case Long:                    // Long vs Long?
1707     break;
1708   }
1709 
1710   // Expand covered set
1711   const TypeLong *r = t->is_long(); // Turn into a TypeLong
1712   return make( MIN2(_lo,r->_lo), MAX2(_hi,r->_hi), MAX2(_widen,r->_widen) );
1713 }
1714 
1715 //------------------------------xdual------------------------------------------
1716 // Dual: reverse hi & lo; flip widen
1717 const Type *TypeLong::xdual() const {
1718   int w = normalize_long_widen(_hi,_lo, WidenMax-_widen);
1719   return new TypeLong(_hi,_lo,w);
1720 }
1721 
1722 //------------------------------widen------------------------------------------
1723 // Only happens for optimistic top-down optimizations.
1724 const Type *TypeLong::widen( const Type *old, const Type* limit ) const {
1725   // Coming from TOP or such; no widening
1726   if( old->base() != Long ) return this;
1727   const TypeLong *ot = old->is_long();
1728 
1729   // If new guy is equal to old guy, no widening
1730   if( _lo == ot->_lo && _hi == ot->_hi )
1731     return old;
1732 
1733   // If new guy contains old, then we widened
1734   if( _lo <= ot->_lo && _hi >= ot->_hi ) {
1735     // New contains old
1736     // If new guy is already wider than old, no widening
1737     if( _widen > ot->_widen ) return this;
1738     // If old guy was a constant, do not bother
1739     if (ot->_lo == ot->_hi)  return this;
1740     // Now widen new guy.
1741     // Check for widening too far
1742     if (_widen == WidenMax) {
1743       jlong max = max_jlong;
1744       jlong min = min_jlong;
1745       if (limit->isa_long()) {
1746         max = limit->is_long()->_hi;
1747         min = limit->is_long()->_lo;
1748       }
1749       if (min < _lo && _hi < max) {
1750         // If neither endpoint is extremal yet, push out the endpoint
1751         // which is closer to its respective limit.
1752         if (_lo >= 0 ||                 // easy common case
1753             ((julong)_lo - min) >= ((julong)max - _hi)) {
1754           // Try to widen to an unsigned range type of 32/63 bits:
1755           if (max >= max_juint && _hi < max_juint)
1756             return make(_lo, max_juint, WidenMax);
1757           else
1758             return make(_lo, max, WidenMax);
1759         } else {
1760           return make(min, _hi, WidenMax);
1761         }
1762       }
1763       return TypeLong::LONG;
1764     }
1765     // Returned widened new guy
1766     return make(_lo,_hi,_widen+1);
1767   }
1768 
1769   // If old guy contains new, then we probably widened too far & dropped to
1770   // bottom.  Return the wider fellow.
1771   if ( ot->_lo <= _lo && ot->_hi >= _hi )
1772     return old;
1773 
1774   //  fatal("Long value range is not subset");
1775   // return this;
1776   return TypeLong::LONG;
1777 }
1778 
1779 //------------------------------narrow----------------------------------------
1780 // Only happens for pessimistic optimizations.
1781 const Type *TypeLong::narrow( const Type *old ) const {
1782   if (_lo >= _hi)  return this;   // already narrow enough
1783   if (old == NULL)  return this;
1784   const TypeLong* ot = old->isa_long();
1785   if (ot == NULL)  return this;
1786   jlong olo = ot->_lo;
1787   jlong ohi = ot->_hi;
1788 
1789   // If new guy is equal to old guy, no narrowing
1790   if (_lo == olo && _hi == ohi)  return old;
1791 
1792   // If old guy was maximum range, allow the narrowing
1793   if (olo == min_jlong && ohi == max_jlong)  return this;
1794 
1795   if (_lo < olo || _hi > ohi)
1796     return this;                // doesn't narrow; pretty wierd
1797 
1798   // The new type narrows the old type, so look for a "death march".
1799   // See comments on PhaseTransform::saturate.
1800   julong nrange = _hi - _lo;
1801   julong orange = ohi - olo;
1802   if (nrange < max_julong - 1 && nrange > (orange >> 1) + (SMALLINT*2)) {
1803     // Use the new type only if the range shrinks a lot.
1804     // We do not want the optimizer computing 2^31 point by point.
1805     return old;
1806   }
1807 
1808   return this;
1809 }
1810 
1811 //-----------------------------filter------------------------------------------
1812 const Type *TypeLong::filter_helper(const Type *kills, bool include_speculative) const {
1813   const TypeLong* ft = join_helper(kills, include_speculative)->isa_long();
1814   if (ft == NULL || ft->empty())
1815     return Type::TOP;           // Canonical empty value
1816   if (ft->_widen < this->_widen) {
1817     // Do not allow the value of kill->_widen to affect the outcome.
1818     // The widen bits must be allowed to run freely through the graph.
1819     ft = TypeLong::make(ft->_lo, ft->_hi, this->_widen);
1820   }
1821   return ft;
1822 }
1823 
1824 //------------------------------eq---------------------------------------------
1825 // Structural equality check for Type representations
1826 bool TypeLong::eq( const Type *t ) const {
1827   const TypeLong *r = t->is_long(); // Handy access
1828   return r->_lo == _lo &&  r->_hi == _hi  && r->_widen == _widen;
1829 }
1830 
1831 //------------------------------hash-------------------------------------------
1832 // Type-specific hashing function.
1833 int TypeLong::hash(void) const {
1834   return (int)(_lo+_hi+_widen+(int)Type::Long);
1835 }
1836 
1837 //------------------------------is_finite--------------------------------------
1838 // Has a finite value
1839 bool TypeLong::is_finite() const {
1840   return true;
1841 }
1842 
1843 //------------------------------dump2------------------------------------------
1844 // Dump TypeLong
1845 #ifndef PRODUCT
1846 static const char* longnamenear(jlong x, const char* xname, char* buf, jlong n) {
1847   if (n > x) {
1848     if (n >= x + 10000)  return NULL;
1849     sprintf(buf, "%s+" JLONG_FORMAT, xname, n - x);
1850   } else if (n < x) {
1851     if (n <= x - 10000)  return NULL;
1852     sprintf(buf, "%s-" JLONG_FORMAT, xname, x - n);
1853   } else {
1854     return xname;
1855   }
1856   return buf;
1857 }
1858 
1859 static const char* longname(char* buf, jlong n) {
1860   const char* str;
1861   if (n == min_jlong)
1862     return "min";
1863   else if (n < min_jlong + 10000)
1864     sprintf(buf, "min+" JLONG_FORMAT, n - min_jlong);
1865   else if (n == max_jlong)
1866     return "max";
1867   else if (n > max_jlong - 10000)
1868     sprintf(buf, "max-" JLONG_FORMAT, max_jlong - n);
1869   else if ((str = longnamenear(max_juint, "maxuint", buf, n)) != NULL)
1870     return str;
1871   else if ((str = longnamenear(max_jint, "maxint", buf, n)) != NULL)
1872     return str;
1873   else if ((str = longnamenear(min_jint, "minint", buf, n)) != NULL)
1874     return str;
1875   else
1876     sprintf(buf, JLONG_FORMAT, n);
1877   return buf;
1878 }
1879 
1880 void TypeLong::dump2( Dict &d, uint depth, outputStream *st ) const {
1881   char buf[80], buf2[80];
1882   if (_lo == min_jlong && _hi == max_jlong)
1883     st->print("long");
1884   else if (is_con())
1885     st->print("long:%s", longname(buf, get_con()));
1886   else if (_hi == max_jlong)
1887     st->print("long:>=%s", longname(buf, _lo));
1888   else if (_lo == min_jlong)
1889     st->print("long:<=%s", longname(buf, _hi));
1890   else
1891     st->print("long:%s..%s", longname(buf, _lo), longname(buf2, _hi));
1892 
1893   if (_widen != 0 && this != TypeLong::LONG)
1894     st->print(":%.*s", _widen, "wwww");
1895 }
1896 #endif
1897 
1898 //------------------------------singleton--------------------------------------
1899 // TRUE if Type is a singleton type, FALSE otherwise.   Singletons are simple
1900 // constants
1901 bool TypeLong::singleton(void) const {
1902   return _lo >= _hi;
1903 }
1904 
1905 bool TypeLong::empty(void) const {
1906   return _lo > _hi;
1907 }
1908 
1909 //=============================================================================
1910 // Convenience common pre-built types.
1911 const TypeTuple *TypeTuple::IFBOTH;     // Return both arms of IF as reachable
1912 const TypeTuple *TypeTuple::IFFALSE;
1913 const TypeTuple *TypeTuple::IFTRUE;
1914 const TypeTuple *TypeTuple::IFNEITHER;
1915 const TypeTuple *TypeTuple::LOOPBODY;
1916 const TypeTuple *TypeTuple::MEMBAR;
1917 const TypeTuple *TypeTuple::STORECONDITIONAL;
1918 const TypeTuple *TypeTuple::START_I2C;
1919 const TypeTuple *TypeTuple::INT_PAIR;
1920 const TypeTuple *TypeTuple::LONG_PAIR;
1921 const TypeTuple *TypeTuple::INT_CC_PAIR;
1922 const TypeTuple *TypeTuple::LONG_CC_PAIR;
1923 
1924 static void collect_value_fields(ciValueKlass* vk, const Type** field_array, uint& pos) {
1925   for (int j = 0; j < vk->nof_nonstatic_fields(); j++) {
1926     ciField* f = vk->nonstatic_field_at(j);
1927     BasicType bt = f->type()->basic_type();
1928     assert(bt < T_VALUETYPE && bt >= T_BOOLEAN, "not yet supported");
1929     field_array[pos++] = Type::get_const_type(f->type());
1930     if (bt == T_LONG || bt == T_DOUBLE) {
1931       field_array[pos++] = Type::HALF;
1932     }
1933   }
1934 }
1935 
1936 //------------------------------make-------------------------------------------
1937 // Make a TypeTuple from the range of a method signature
1938 const TypeTuple *TypeTuple::make_range(ciSignature* sig, bool ret_vt_fields) {
1939   ciType* return_type = sig->return_type();
1940   return make_range(return_type, ret_vt_fields);
1941 }
1942 
1943 const TypeTuple *TypeTuple::make_range(ciType* return_type, bool ret_vt_fields) {
1944   uint arg_cnt = 0;
1945   if (ret_vt_fields) {
1946     ret_vt_fields = return_type->is_valuetype() && ((ciValueKlass*)return_type)->can_be_returned_as_fields();
1947   }
1948   if (ret_vt_fields) {
1949     ciValueKlass* vk = (ciValueKlass*)return_type;
1950     arg_cnt = vk->value_arg_slots()+1;
1951   } else {
1952     arg_cnt = return_type->size();
1953   }
1954 
1955   const Type **field_array = fields(arg_cnt);
1956   switch (return_type->basic_type()) {
1957   case T_LONG:
1958     field_array[TypeFunc::Parms]   = TypeLong::LONG;
1959     field_array[TypeFunc::Parms+1] = Type::HALF;
1960     break;
1961   case T_DOUBLE:
1962     field_array[TypeFunc::Parms]   = Type::DOUBLE;
1963     field_array[TypeFunc::Parms+1] = Type::HALF;
1964     break;
1965   case T_OBJECT:
1966   case T_ARRAY:
1967   case T_BOOLEAN:
1968   case T_CHAR:
1969   case T_FLOAT:
1970   case T_BYTE:
1971   case T_SHORT:
1972   case T_INT:
1973     field_array[TypeFunc::Parms] = get_const_type(return_type);
1974     break;
1975   case T_VALUETYPE:
1976     if (ret_vt_fields) {
1977       ciValueKlass* vk = (ciValueKlass*)return_type;
1978       uint pos = TypeFunc::Parms;
1979       field_array[pos] = TypePtr::BOTTOM;
1980       pos++;
1981       collect_value_fields(vk, field_array, pos);
1982     } else {
1983       field_array[TypeFunc::Parms] = get_const_type(return_type);
1984     }
1985     break;
1986   case T_VOID:
1987     break;
1988   default:
1989     ShouldNotReachHere();
1990   }
1991   return (TypeTuple*)(new TypeTuple(TypeFunc::Parms + arg_cnt, field_array))->hashcons();
1992 }
1993 
1994 // Make a TypeTuple from the domain of a method signature
1995 const TypeTuple *TypeTuple::make_domain(ciInstanceKlass* recv, ciSignature* sig, bool vt_fields_as_args) {
1996   uint arg_cnt = sig->size();
1997 
1998   int vt_extra = 0;
1999   if (vt_fields_as_args) {
2000     for (int i = 0; i < sig->count(); i++) {
2001       ciType* type = sig->type_at(i);
2002       if (type->basic_type() == T_VALUETYPE && type != ciEnv::current()->___Value_klass()) {
2003         assert(type->is_valuetype(), "inconsistent type");
2004         ciValueKlass* vk = (ciValueKlass*)type;
2005         vt_extra += vk->value_arg_slots()-1;
2006       }
2007     }
2008     assert(((int)arg_cnt) + vt_extra >= 0, "negative number of actual arguments?");
2009   }
2010 
2011   uint pos = TypeFunc::Parms;
2012   const Type **field_array;
2013   if (recv != NULL) {
2014     arg_cnt++;
2015     bool vt_fields_for_recv = vt_fields_as_args && recv->is_valuetype() &&
2016       recv != ciEnv::current()->___Value_klass();
2017     if (vt_fields_for_recv) {
2018       ciValueKlass* vk = (ciValueKlass*)recv;
2019       vt_extra += vk->value_arg_slots()-1;
2020     }
2021     field_array = fields(arg_cnt + vt_extra);
2022     // Use get_const_type here because it respects UseUniqueSubclasses:
2023     if (vt_fields_for_recv) {
2024       ciValueKlass* vk = (ciValueKlass*)recv;
2025       collect_value_fields(vk, field_array, pos);
2026     } else {
2027       field_array[pos++] = get_const_type(recv)->join_speculative(TypePtr::NOTNULL);
2028     }
2029   } else {
2030     field_array = fields(arg_cnt + vt_extra);
2031   }
2032 
2033   int i = 0;
2034   while (pos < TypeFunc::Parms + arg_cnt + vt_extra) {
2035     ciType* type = sig->type_at(i);
2036 
2037     switch (type->basic_type()) {
2038     case T_LONG:
2039       field_array[pos++] = TypeLong::LONG;
2040       field_array[pos++] = Type::HALF;
2041       break;
2042     case T_DOUBLE:
2043       field_array[pos++] = Type::DOUBLE;
2044       field_array[pos++] = Type::HALF;
2045       break;
2046     case T_OBJECT:
2047     case T_ARRAY:
2048     case T_FLOAT:
2049     case T_INT:
2050       field_array[pos++] = get_const_type(type);
2051       break;
2052     case T_BOOLEAN:
2053     case T_CHAR:
2054     case T_BYTE:
2055     case T_SHORT:
2056       field_array[pos++] = TypeInt::INT;
2057       break;
2058     case T_VALUETYPE: {
2059       assert(type->is_valuetype(), "inconsistent type");
2060       if (vt_fields_as_args && type != ciEnv::current()->___Value_klass()) {
2061         ciValueKlass* vk = (ciValueKlass*)type;
2062         collect_value_fields(vk, field_array, pos);
2063       } else {
2064         field_array[pos++] = get_const_type(type);
2065       }
2066       break;
2067     }
2068     default:
2069       ShouldNotReachHere();
2070     }
2071     i++;
2072   }
2073   assert(pos == TypeFunc::Parms + arg_cnt + vt_extra, "wrong number of arguments");
2074 
2075   return (TypeTuple*)(new TypeTuple(TypeFunc::Parms + arg_cnt + vt_extra, field_array))->hashcons();
2076 }
2077 
2078 const TypeTuple *TypeTuple::make( uint cnt, const Type **fields ) {
2079   return (TypeTuple*)(new TypeTuple(cnt,fields))->hashcons();
2080 }
2081 
2082 //------------------------------fields-----------------------------------------
2083 // Subroutine call type with space allocated for argument types
2084 // Memory for Control, I_O, Memory, FramePtr, and ReturnAdr is allocated implicitly
2085 const Type **TypeTuple::fields( uint arg_cnt ) {
2086   const Type **flds = (const Type **)(Compile::current()->type_arena()->Amalloc_4((TypeFunc::Parms+arg_cnt)*sizeof(Type*) ));
2087   flds[TypeFunc::Control  ] = Type::CONTROL;
2088   flds[TypeFunc::I_O      ] = Type::ABIO;
2089   flds[TypeFunc::Memory   ] = Type::MEMORY;
2090   flds[TypeFunc::FramePtr ] = TypeRawPtr::BOTTOM;
2091   flds[TypeFunc::ReturnAdr] = Type::RETURN_ADDRESS;
2092 
2093   return flds;
2094 }
2095 
2096 //------------------------------meet-------------------------------------------
2097 // Compute the MEET of two types.  It returns a new Type object.
2098 const Type *TypeTuple::xmeet( const Type *t ) const {
2099   // Perform a fast test for common case; meeting the same types together.
2100   if( this == t ) return this;  // Meeting same type-rep?
2101 
2102   // Current "this->_base" is Tuple
2103   switch (t->base()) {          // switch on original type
2104 
2105   case Bottom:                  // Ye Olde Default
2106     return t;
2107 
2108   default:                      // All else is a mistake
2109     typerr(t);
2110 
2111   case Tuple: {                 // Meeting 2 signatures?
2112     const TypeTuple *x = t->is_tuple();
2113     assert( _cnt == x->_cnt, "" );
2114     const Type **fields = (const Type **)(Compile::current()->type_arena()->Amalloc_4( _cnt*sizeof(Type*) ));
2115     for( uint i=0; i<_cnt; i++ )
2116       fields[i] = field_at(i)->xmeet( x->field_at(i) );
2117     return TypeTuple::make(_cnt,fields);
2118   }
2119   case Top:
2120     break;
2121   }
2122   return this;                  // Return the double constant
2123 }
2124 
2125 //------------------------------xdual------------------------------------------
2126 // Dual: compute field-by-field dual
2127 const Type *TypeTuple::xdual() const {
2128   const Type **fields = (const Type **)(Compile::current()->type_arena()->Amalloc_4( _cnt*sizeof(Type*) ));
2129   for( uint i=0; i<_cnt; i++ )
2130     fields[i] = _fields[i]->dual();
2131   return new TypeTuple(_cnt,fields);
2132 }
2133 
2134 //------------------------------eq---------------------------------------------
2135 // Structural equality check for Type representations
2136 bool TypeTuple::eq( const Type *t ) const {
2137   const TypeTuple *s = (const TypeTuple *)t;
2138   if (_cnt != s->_cnt)  return false;  // Unequal field counts
2139   for (uint i = 0; i < _cnt; i++)
2140     if (field_at(i) != s->field_at(i)) // POINTER COMPARE!  NO RECURSION!
2141       return false;             // Missed
2142   return true;
2143 }
2144 
2145 //------------------------------hash-------------------------------------------
2146 // Type-specific hashing function.
2147 int TypeTuple::hash(void) const {
2148   intptr_t sum = _cnt;
2149   for( uint i=0; i<_cnt; i++ )
2150     sum += (intptr_t)_fields[i];     // Hash on pointers directly
2151   return sum;
2152 }
2153 
2154 //------------------------------dump2------------------------------------------
2155 // Dump signature Type
2156 #ifndef PRODUCT
2157 void TypeTuple::dump2( Dict &d, uint depth, outputStream *st ) const {
2158   st->print("{");
2159   if( !depth || d[this] ) {     // Check for recursive print
2160     st->print("...}");
2161     return;
2162   }
2163   d.Insert((void*)this, (void*)this);   // Stop recursion
2164   if( _cnt ) {
2165     uint i;
2166     for( i=0; i<_cnt-1; i++ ) {
2167       st->print("%d:", i);
2168       _fields[i]->dump2(d, depth-1, st);
2169       st->print(", ");
2170     }
2171     st->print("%d:", i);
2172     _fields[i]->dump2(d, depth-1, st);
2173   }
2174   st->print("}");
2175 }
2176 #endif
2177 
2178 //------------------------------singleton--------------------------------------
2179 // TRUE if Type is a singleton type, FALSE otherwise.   Singletons are simple
2180 // constants (Ldi nodes).  Singletons are integer, float or double constants
2181 // or a single symbol.
2182 bool TypeTuple::singleton(void) const {
2183   return false;                 // Never a singleton
2184 }
2185 
2186 bool TypeTuple::empty(void) const {
2187   for( uint i=0; i<_cnt; i++ ) {
2188     if (_fields[i]->empty())  return true;
2189   }
2190   return false;
2191 }
2192 
2193 //=============================================================================
2194 // Convenience common pre-built types.
2195 
2196 inline const TypeInt* normalize_array_size(const TypeInt* size) {
2197   // Certain normalizations keep us sane when comparing types.
2198   // We do not want arrayOop variables to differ only by the wideness
2199   // of their index types.  Pick minimum wideness, since that is the
2200   // forced wideness of small ranges anyway.
2201   if (size->_widen != Type::WidenMin)
2202     return TypeInt::make(size->_lo, size->_hi, Type::WidenMin);
2203   else
2204     return size;
2205 }
2206 
2207 //------------------------------make-------------------------------------------
2208 const TypeAry* TypeAry::make(const Type* elem, const TypeInt* size, bool stable) {
2209   if (UseCompressedOops && elem->isa_oopptr()) {
2210     elem = elem->make_narrowoop();
2211   }
2212   size = normalize_array_size(size);
2213   return (TypeAry*)(new TypeAry(elem,size,stable))->hashcons();
2214 }
2215 
2216 //------------------------------meet-------------------------------------------
2217 // Compute the MEET of two types.  It returns a new Type object.
2218 const Type *TypeAry::xmeet( const Type *t ) const {
2219   // Perform a fast test for common case; meeting the same types together.
2220   if( this == t ) return this;  // Meeting same type-rep?
2221 
2222   // Current "this->_base" is Ary
2223   switch (t->base()) {          // switch on original type
2224 
2225   case Bottom:                  // Ye Olde Default
2226     return t;
2227 
2228   default:                      // All else is a mistake
2229     typerr(t);
2230 
2231   case Array: {                 // Meeting 2 arrays?
2232     const TypeAry *a = t->is_ary();
2233     return TypeAry::make(_elem->meet_speculative(a->_elem),
2234                          _size->xmeet(a->_size)->is_int(),
2235                          _stable & a->_stable);
2236   }
2237   case Top:
2238     break;
2239   }
2240   return this;                  // Return the double constant
2241 }
2242 
2243 //------------------------------xdual------------------------------------------
2244 // Dual: compute field-by-field dual
2245 const Type *TypeAry::xdual() const {
2246   const TypeInt* size_dual = _size->dual()->is_int();
2247   size_dual = normalize_array_size(size_dual);
2248   return new TypeAry(_elem->dual(), size_dual, !_stable);
2249 }
2250 
2251 //------------------------------eq---------------------------------------------
2252 // Structural equality check for Type representations
2253 bool TypeAry::eq( const Type *t ) const {
2254   const TypeAry *a = (const TypeAry*)t;
2255   return _elem == a->_elem &&
2256     _stable == a->_stable &&
2257     _size == a->_size;
2258 }
2259 
2260 //------------------------------hash-------------------------------------------
2261 // Type-specific hashing function.
2262 int TypeAry::hash(void) const {
2263   return (intptr_t)_elem + (intptr_t)_size + (_stable ? 43 : 0);
2264 }
2265 
2266 /**
2267  * Return same type without a speculative part in the element
2268  */
2269 const Type* TypeAry::remove_speculative() const {
2270   return make(_elem->remove_speculative(), _size, _stable);
2271 }
2272 
2273 /**
2274  * Return same type with cleaned up speculative part of element
2275  */
2276 const Type* TypeAry::cleanup_speculative() const {
2277   return make(_elem->cleanup_speculative(), _size, _stable);
2278 }
2279 
2280 /**
2281  * Return same type but with a different inline depth (used for speculation)
2282  *
2283  * @param depth  depth to meet with
2284  */
2285 const TypePtr* TypePtr::with_inline_depth(int depth) const {
2286   if (!UseInlineDepthForSpeculativeTypes) {
2287     return this;
2288   }
2289   return make(AnyPtr, _ptr, _offset, _speculative, depth);
2290 }
2291 
2292 //----------------------interface_vs_oop---------------------------------------
2293 #ifdef ASSERT
2294 bool TypeAry::interface_vs_oop(const Type *t) const {
2295   const TypeAry* t_ary = t->is_ary();
2296   if (t_ary) {
2297     const TypePtr* this_ptr = _elem->make_ptr(); // In case we have narrow_oops
2298     const TypePtr*    t_ptr = t_ary->_elem->make_ptr();
2299     if(this_ptr != NULL && t_ptr != NULL) {
2300       return this_ptr->interface_vs_oop(t_ptr);
2301     }
2302   }
2303   return false;
2304 }
2305 #endif
2306 
2307 //------------------------------dump2------------------------------------------
2308 #ifndef PRODUCT
2309 void TypeAry::dump2( Dict &d, uint depth, outputStream *st ) const {
2310   if (_stable)  st->print("stable:");
2311   _elem->dump2(d, depth, st);
2312   st->print("[");
2313   _size->dump2(d, depth, st);
2314   st->print("]");
2315 }
2316 #endif
2317 
2318 //------------------------------singleton--------------------------------------
2319 // TRUE if Type is a singleton type, FALSE otherwise.   Singletons are simple
2320 // constants (Ldi nodes).  Singletons are integer, float or double constants
2321 // or a single symbol.
2322 bool TypeAry::singleton(void) const {
2323   return false;                 // Never a singleton
2324 }
2325 
2326 bool TypeAry::empty(void) const {
2327   return _elem->empty() || _size->empty();
2328 }
2329 
2330 //--------------------------ary_must_be_exact----------------------------------
2331 bool TypeAry::ary_must_be_exact() const {
2332   if (!UseExactTypes)       return false;
2333   // This logic looks at the element type of an array, and returns true
2334   // if the element type is either a primitive or a final instance class.
2335   // In such cases, an array built on this ary must have no subclasses.
2336   if (_elem == BOTTOM)      return false;  // general array not exact
2337   if (_elem == TOP   )      return false;  // inverted general array not exact
2338   const TypeOopPtr*  toop = NULL;
2339   if (UseCompressedOops && _elem->isa_narrowoop()) {
2340     toop = _elem->make_ptr()->isa_oopptr();
2341   } else {
2342     toop = _elem->isa_oopptr();
2343   }
2344   if (!toop)                return true;   // a primitive type, like int
2345   ciKlass* tklass = toop->klass();
2346   if (tklass == NULL)       return false;  // unloaded class
2347   if (!tklass->is_loaded()) return false;  // unloaded class
2348   const TypeInstPtr* tinst;
2349   if (_elem->isa_narrowoop())
2350     tinst = _elem->make_ptr()->isa_instptr();
2351   else
2352     tinst = _elem->isa_instptr();
2353   if (tinst)
2354     return tklass->as_instance_klass()->is_final();
2355   const TypeAryPtr*  tap;
2356   if (_elem->isa_narrowoop())
2357     tap = _elem->make_ptr()->isa_aryptr();
2358   else
2359     tap = _elem->isa_aryptr();
2360   if (tap)
2361     return tap->ary()->ary_must_be_exact();
2362   return false;
2363 }
2364 
2365 //==============================TypeValueType=======================================
2366 
2367 //------------------------------make-------------------------------------------
2368 const TypeValueType* TypeValueType::make(ciValueKlass* vk) {
2369   return (TypeValueType*)(new TypeValueType(vk))->hashcons();
2370 }
2371 
2372 //------------------------------meet-------------------------------------------
2373 // Compute the MEET of two types.  It returns a new Type object.
2374 const Type* TypeValueType::xmeet(const Type* t) const {
2375   // Perform a fast test for common case; meeting the same types together.
2376   if(this == t) return this;  // Meeting same type-rep?
2377 
2378   // Current "this->_base" is ValueType
2379   switch (t->base()) {          // switch on original type
2380 
2381   case Top:
2382     break;
2383 
2384   case Bottom:
2385     return t;
2386 
2387   default:                      // All else is a mistake
2388     typerr(t);
2389 
2390   }
2391   return this;
2392 }
2393 
2394 //------------------------------xdual------------------------------------------
2395 const Type* TypeValueType::xdual() const {
2396   // FIXME
2397   return new TypeValueType(_vk);
2398 }
2399 
2400 //------------------------------eq---------------------------------------------
2401 // Structural equality check for Type representations
2402 bool TypeValueType::eq(const Type* t) const {
2403   const TypeValueType* vt = t->is_valuetype();
2404   return (_vk == vt->value_klass());
2405 }
2406 
2407 //------------------------------hash-------------------------------------------
2408 // Type-specific hashing function.
2409 int TypeValueType::hash(void) const {
2410   return (intptr_t)_vk;
2411 }
2412 
2413 //------------------------------singleton--------------------------------------
2414 // TRUE if Type is a singleton type, FALSE otherwise. Singletons are simple constants.
2415 bool TypeValueType::singleton(void) const {
2416   // FIXME
2417   return false;
2418 }
2419 
2420 //------------------------------empty------------------------------------------
2421 // TRUE if Type is a type with no values, FALSE otherwise.
2422 bool TypeValueType::empty(void) const {
2423   // FIXME
2424   return false;
2425 }
2426 
2427 //------------------------------dump2------------------------------------------
2428 #ifndef PRODUCT
2429 void TypeValueType::dump2(Dict &d, uint depth, outputStream* st) const {
2430   st->print("valuetype[%d]:{", _vk->field_count());
2431   st->print("%s", _vk->field_count() != 0 ? _vk->field_type_by_index(0)->name() : "empty");
2432   for (int i = 1; i < _vk->field_count(); ++i) {
2433     st->print(", %s", _vk->field_type_by_index(i)->name());
2434   }
2435   st->print("}");
2436 }
2437 #endif
2438 
2439 //==============================TypeVect=======================================
2440 // Convenience common pre-built types.
2441 const TypeVect *TypeVect::VECTS = NULL; //  32-bit vectors
2442 const TypeVect *TypeVect::VECTD = NULL; //  64-bit vectors
2443 const TypeVect *TypeVect::VECTX = NULL; // 128-bit vectors
2444 const TypeVect *TypeVect::VECTY = NULL; // 256-bit vectors
2445 const TypeVect *TypeVect::VECTZ = NULL; // 512-bit vectors
2446 
2447 //------------------------------make-------------------------------------------
2448 const TypeVect* TypeVect::make(const Type *elem, uint length) {
2449   BasicType elem_bt = elem->array_element_basic_type();
2450   assert(is_java_primitive(elem_bt), "only primitive types in vector");
2451   assert(length > 1 && is_power_of_2(length), "vector length is power of 2");
2452   assert(Matcher::vector_size_supported(elem_bt, length), "length in range");
2453   int size = length * type2aelembytes(elem_bt);
2454   switch (Matcher::vector_ideal_reg(size)) {
2455   case Op_VecS:
2456     return (TypeVect*)(new TypeVectS(elem, length))->hashcons();
2457   case Op_RegL:
2458   case Op_VecD:
2459   case Op_RegD:
2460     return (TypeVect*)(new TypeVectD(elem, length))->hashcons();
2461   case Op_VecX:
2462     return (TypeVect*)(new TypeVectX(elem, length))->hashcons();
2463   case Op_VecY:
2464     return (TypeVect*)(new TypeVectY(elem, length))->hashcons();
2465   case Op_VecZ:
2466     return (TypeVect*)(new TypeVectZ(elem, length))->hashcons();
2467   }
2468  ShouldNotReachHere();
2469   return NULL;
2470 }
2471 
2472 //------------------------------meet-------------------------------------------
2473 // Compute the MEET of two types.  It returns a new Type object.
2474 const Type *TypeVect::xmeet( const Type *t ) const {
2475   // Perform a fast test for common case; meeting the same types together.
2476   if( this == t ) return this;  // Meeting same type-rep?
2477 
2478   // Current "this->_base" is Vector
2479   switch (t->base()) {          // switch on original type
2480 
2481   case Bottom:                  // Ye Olde Default
2482     return t;
2483 
2484   default:                      // All else is a mistake
2485     typerr(t);
2486 
2487   case VectorS:
2488   case VectorD:
2489   case VectorX:
2490   case VectorY:
2491   case VectorZ: {                // Meeting 2 vectors?
2492     const TypeVect* v = t->is_vect();
2493     assert(  base() == v->base(), "");
2494     assert(length() == v->length(), "");
2495     assert(element_basic_type() == v->element_basic_type(), "");
2496     return TypeVect::make(_elem->xmeet(v->_elem), _length);
2497   }
2498   case Top:
2499     break;
2500   }
2501   return this;
2502 }
2503 
2504 //------------------------------xdual------------------------------------------
2505 // Dual: compute field-by-field dual
2506 const Type *TypeVect::xdual() const {
2507   return new TypeVect(base(), _elem->dual(), _length);
2508 }
2509 
2510 //------------------------------eq---------------------------------------------
2511 // Structural equality check for Type representations
2512 bool TypeVect::eq(const Type *t) const {
2513   const TypeVect *v = t->is_vect();
2514   return (_elem == v->_elem) && (_length == v->_length);
2515 }
2516 
2517 //------------------------------hash-------------------------------------------
2518 // Type-specific hashing function.
2519 int TypeVect::hash(void) const {
2520   return (intptr_t)_elem + (intptr_t)_length;
2521 }
2522 
2523 //------------------------------singleton--------------------------------------
2524 // TRUE if Type is a singleton type, FALSE otherwise.   Singletons are simple
2525 // constants (Ldi nodes).  Vector is singleton if all elements are the same
2526 // constant value (when vector is created with Replicate code).
2527 bool TypeVect::singleton(void) const {
2528 // There is no Con node for vectors yet.
2529 //  return _elem->singleton();
2530   return false;
2531 }
2532 
2533 bool TypeVect::empty(void) const {
2534   return _elem->empty();
2535 }
2536 
2537 //------------------------------dump2------------------------------------------
2538 #ifndef PRODUCT
2539 void TypeVect::dump2(Dict &d, uint depth, outputStream *st) const {
2540   switch (base()) {
2541   case VectorS:
2542     st->print("vectors["); break;
2543   case VectorD:
2544     st->print("vectord["); break;
2545   case VectorX:
2546     st->print("vectorx["); break;
2547   case VectorY:
2548     st->print("vectory["); break;
2549   case VectorZ:
2550     st->print("vectorz["); break;
2551   default:
2552     ShouldNotReachHere();
2553   }
2554   st->print("%d]:{", _length);
2555   _elem->dump2(d, depth, st);
2556   st->print("}");
2557 }
2558 #endif
2559 
2560 
2561 //=============================================================================
2562 // Convenience common pre-built types.
2563 const TypePtr *TypePtr::NULL_PTR;
2564 const TypePtr *TypePtr::NOTNULL;
2565 const TypePtr *TypePtr::BOTTOM;
2566 
2567 //------------------------------meet-------------------------------------------
2568 // Meet over the PTR enum
2569 const TypePtr::PTR TypePtr::ptr_meet[TypePtr::lastPTR][TypePtr::lastPTR] = {
2570   //              TopPTR,    AnyNull,   Constant, Null,   NotNull, BotPTR,
2571   { /* Top     */ TopPTR,    AnyNull,   Constant, Null,   NotNull, BotPTR,},
2572   { /* AnyNull */ AnyNull,   AnyNull,   Constant, BotPTR, NotNull, BotPTR,},
2573   { /* Constant*/ Constant,  Constant,  Constant, BotPTR, NotNull, BotPTR,},
2574   { /* Null    */ Null,      BotPTR,    BotPTR,   Null,   BotPTR,  BotPTR,},
2575   { /* NotNull */ NotNull,   NotNull,   NotNull,  BotPTR, NotNull, BotPTR,},
2576   { /* BotPTR  */ BotPTR,    BotPTR,    BotPTR,   BotPTR, BotPTR,  BotPTR,}
2577 };
2578 
2579 //------------------------------make-------------------------------------------
2580 const TypePtr* TypePtr::make(TYPES t, enum PTR ptr, Offset offset, const TypePtr* speculative, int inline_depth) {
2581   return (TypePtr*)(new TypePtr(t,ptr,offset, speculative, inline_depth))->hashcons();
2582 }
2583 
2584 //------------------------------cast_to_ptr_type-------------------------------
2585 const Type *TypePtr::cast_to_ptr_type(PTR ptr) const {
2586   assert(_base == AnyPtr, "subclass must override cast_to_ptr_type");
2587   if( ptr == _ptr ) return this;
2588   return make(_base, ptr, _offset, _speculative, _inline_depth);
2589 }
2590 
2591 //------------------------------get_con----------------------------------------
2592 intptr_t TypePtr::get_con() const {
2593   assert( _ptr == Null, "" );
2594   return offset();
2595 }
2596 
2597 //------------------------------meet-------------------------------------------
2598 // Compute the MEET of two types.  It returns a new Type object.
2599 const Type *TypePtr::xmeet(const Type *t) const {
2600   const Type* res = xmeet_helper(t);
2601   if (res->isa_ptr() == NULL) {
2602     return res;
2603   }
2604 
2605   const TypePtr* res_ptr = res->is_ptr();
2606   if (res_ptr->speculative() != NULL) {
2607     // type->speculative() == NULL means that speculation is no better
2608     // than type, i.e. type->speculative() == type. So there are 2
2609     // ways to represent the fact that we have no useful speculative
2610     // data and we should use a single one to be able to test for
2611     // equality between types. Check whether type->speculative() ==
2612     // type and set speculative to NULL if it is the case.
2613     if (res_ptr->remove_speculative() == res_ptr->speculative()) {
2614       return res_ptr->remove_speculative();
2615     }
2616   }
2617 
2618   return res;
2619 }
2620 
2621 const Type *TypePtr::xmeet_helper(const Type *t) const {
2622   // Perform a fast test for common case; meeting the same types together.
2623   if( this == t ) return this;  // Meeting same type-rep?
2624 
2625   // Current "this->_base" is AnyPtr
2626   switch (t->base()) {          // switch on original type
2627   case Int:                     // Mixing ints & oops happens when javac
2628   case Long:                    // reuses local variables
2629   case FloatTop:
2630   case FloatCon:
2631   case FloatBot:
2632   case DoubleTop:
2633   case DoubleCon:
2634   case DoubleBot:
2635   case NarrowOop:
2636   case NarrowKlass:
2637   case Bottom:                  // Ye Olde Default
2638     return Type::BOTTOM;
2639   case Top:
2640     return this;
2641 
2642   case AnyPtr: {                // Meeting to AnyPtrs
2643     const TypePtr *tp = t->is_ptr();
2644     const TypePtr* speculative = xmeet_speculative(tp);
2645     int depth = meet_inline_depth(tp->inline_depth());
2646     return make(AnyPtr, meet_ptr(tp->ptr()), meet_offset(tp->offset()), speculative, depth);
2647   }
2648   case RawPtr:                  // For these, flip the call around to cut down
2649   case OopPtr:
2650   case InstPtr:                 // on the cases I have to handle.
2651   case ValueTypePtr:
2652   case AryPtr:
2653   case MetadataPtr:
2654   case KlassPtr:
2655     return t->xmeet(this);      // Call in reverse direction
2656   default:                      // All else is a mistake
2657     typerr(t);
2658 
2659   }
2660   return this;
2661 }
2662 
2663 //------------------------------meet_offset------------------------------------
2664 Type::Offset TypePtr::meet_offset(int offset) const {
2665   return _offset.meet(Offset(offset));
2666 }
2667 
2668 //------------------------------dual_offset------------------------------------
2669 Type::Offset TypePtr::dual_offset() const {
2670   return _offset.dual();
2671 }
2672 
2673 //------------------------------xdual------------------------------------------
2674 // Dual: compute field-by-field dual
2675 const TypePtr::PTR TypePtr::ptr_dual[TypePtr::lastPTR] = {
2676   BotPTR, NotNull, Constant, Null, AnyNull, TopPTR
2677 };
2678 const Type *TypePtr::xdual() const {
2679   return new TypePtr(AnyPtr, dual_ptr(), dual_offset(), dual_speculative(), dual_inline_depth());
2680 }
2681 
2682 //------------------------------xadd_offset------------------------------------
2683 Type::Offset TypePtr::xadd_offset(intptr_t offset) const {
2684   return _offset.add(offset);
2685 }
2686 
2687 //------------------------------add_offset-------------------------------------
2688 const TypePtr *TypePtr::add_offset( intptr_t offset ) const {
2689   return make(AnyPtr, _ptr, xadd_offset(offset), _speculative, _inline_depth);
2690 }
2691 
2692 //------------------------------eq---------------------------------------------
2693 // Structural equality check for Type representations
2694 bool TypePtr::eq( const Type *t ) const {
2695   const TypePtr *a = (const TypePtr*)t;
2696   return _ptr == a->ptr() && _offset == a->_offset && eq_speculative(a) && _inline_depth == a->_inline_depth;
2697 }
2698 
2699 //------------------------------hash-------------------------------------------
2700 // Type-specific hashing function.
2701 int TypePtr::hash(void) const {
2702   return java_add(java_add(_ptr, offset()), java_add( hash_speculative(), _inline_depth));
2703 ;
2704 }
2705 
2706 /**
2707  * Return same type without a speculative part
2708  */
2709 const Type* TypePtr::remove_speculative() const {
2710   if (_speculative == NULL) {
2711     return this;
2712   }
2713   assert(_inline_depth == InlineDepthTop || _inline_depth == InlineDepthBottom, "non speculative type shouldn't have inline depth");
2714   return make(AnyPtr, _ptr, _offset, NULL, _inline_depth);
2715 }
2716 
2717 /**
2718  * Return same type but drop speculative part if we know we won't use
2719  * it
2720  */
2721 const Type* TypePtr::cleanup_speculative() const {
2722   if (speculative() == NULL) {
2723     return this;
2724   }
2725   const Type* no_spec = remove_speculative();
2726   // If this is NULL_PTR then we don't need the speculative type
2727   // (with_inline_depth in case the current type inline depth is
2728   // InlineDepthTop)
2729   if (no_spec == NULL_PTR->with_inline_depth(inline_depth())) {
2730     return no_spec;
2731   }
2732   if (above_centerline(speculative()->ptr())) {
2733     return no_spec;
2734   }
2735   const TypeOopPtr* spec_oopptr = speculative()->isa_oopptr();
2736   // If the speculative may be null and is an inexact klass then it
2737   // doesn't help
2738   if (speculative()->maybe_null() && (spec_oopptr == NULL || !spec_oopptr->klass_is_exact())) {
2739     return no_spec;
2740   }
2741   return this;
2742 }
2743 
2744 /**
2745  * dual of the speculative part of the type
2746  */
2747 const TypePtr* TypePtr::dual_speculative() const {
2748   if (_speculative == NULL) {
2749     return NULL;
2750   }
2751   return _speculative->dual()->is_ptr();
2752 }
2753 
2754 /**
2755  * meet of the speculative parts of 2 types
2756  *
2757  * @param other  type to meet with
2758  */
2759 const TypePtr* TypePtr::xmeet_speculative(const TypePtr* other) const {
2760   bool this_has_spec = (_speculative != NULL);
2761   bool other_has_spec = (other->speculative() != NULL);
2762 
2763   if (!this_has_spec && !other_has_spec) {
2764     return NULL;
2765   }
2766 
2767   // If we are at a point where control flow meets and one branch has
2768   // a speculative type and the other has not, we meet the speculative
2769   // type of one branch with the actual type of the other. If the
2770   // actual type is exact and the speculative is as well, then the
2771   // result is a speculative type which is exact and we can continue
2772   // speculation further.
2773   const TypePtr* this_spec = _speculative;
2774   const TypePtr* other_spec = other->speculative();
2775 
2776   if (!this_has_spec) {
2777     this_spec = this;
2778   }
2779 
2780   if (!other_has_spec) {
2781     other_spec = other;
2782   }
2783 
2784   return this_spec->meet(other_spec)->is_ptr();
2785 }
2786 
2787 /**
2788  * dual of the inline depth for this type (used for speculation)
2789  */
2790 int TypePtr::dual_inline_depth() const {
2791   return -inline_depth();
2792 }
2793 
2794 /**
2795  * meet of 2 inline depths (used for speculation)
2796  *
2797  * @param depth  depth to meet with
2798  */
2799 int TypePtr::meet_inline_depth(int depth) const {
2800   return MAX2(inline_depth(), depth);
2801 }
2802 
2803 /**
2804  * Are the speculative parts of 2 types equal?
2805  *
2806  * @param other  type to compare this one to
2807  */
2808 bool TypePtr::eq_speculative(const TypePtr* other) const {
2809   if (_speculative == NULL || other->speculative() == NULL) {
2810     return _speculative == other->speculative();
2811   }
2812 
2813   if (_speculative->base() != other->speculative()->base()) {
2814     return false;
2815   }
2816 
2817   return _speculative->eq(other->speculative());
2818 }
2819 
2820 /**
2821  * Hash of the speculative part of the type
2822  */
2823 int TypePtr::hash_speculative() const {
2824   if (_speculative == NULL) {
2825     return 0;
2826   }
2827 
2828   return _speculative->hash();
2829 }
2830 
2831 /**
2832  * add offset to the speculative part of the type
2833  *
2834  * @param offset  offset to add
2835  */
2836 const TypePtr* TypePtr::add_offset_speculative(intptr_t offset) const {
2837   if (_speculative == NULL) {
2838     return NULL;
2839   }
2840   return _speculative->add_offset(offset)->is_ptr();
2841 }
2842 
2843 /**
2844  * return exact klass from the speculative type if there's one
2845  */
2846 ciKlass* TypePtr::speculative_type() const {
2847   if (_speculative != NULL && _speculative->isa_oopptr()) {
2848     const TypeOopPtr* speculative = _speculative->join(this)->is_oopptr();
2849     if (speculative->klass_is_exact()) {
2850       return speculative->klass();
2851     }
2852   }
2853   return NULL;
2854 }
2855 
2856 /**
2857  * return true if speculative type may be null
2858  */
2859 bool TypePtr::speculative_maybe_null() const {
2860   if (_speculative != NULL) {
2861     const TypePtr* speculative = _speculative->join(this)->is_ptr();
2862     return speculative->maybe_null();
2863   }
2864   return true;
2865 }
2866 
2867 /**
2868  * Same as TypePtr::speculative_type() but return the klass only if
2869  * the speculative tells us is not null
2870  */
2871 ciKlass* TypePtr::speculative_type_not_null() const {
2872   if (speculative_maybe_null()) {
2873     return NULL;
2874   }
2875   return speculative_type();
2876 }
2877 
2878 /**
2879  * Check whether new profiling would improve speculative type
2880  *
2881  * @param   exact_kls    class from profiling
2882  * @param   inline_depth inlining depth of profile point
2883  *
2884  * @return  true if type profile is valuable
2885  */
2886 bool TypePtr::would_improve_type(ciKlass* exact_kls, int inline_depth) const {
2887   // no profiling?
2888   if (exact_kls == NULL) {
2889     return false;
2890   }
2891   // no speculative type or non exact speculative type?
2892   if (speculative_type() == NULL) {
2893     return true;
2894   }
2895   // If the node already has an exact speculative type keep it,
2896   // unless it was provided by profiling that is at a deeper
2897   // inlining level. Profiling at a higher inlining depth is
2898   // expected to be less accurate.
2899   if (_speculative->inline_depth() == InlineDepthBottom) {
2900     return false;
2901   }
2902   assert(_speculative->inline_depth() != InlineDepthTop, "can't do the comparison");
2903   return inline_depth < _speculative->inline_depth();
2904 }
2905 
2906 /**
2907  * Check whether new profiling would improve ptr (= tells us it is non
2908  * null)
2909  *
2910  * @param   maybe_null true if profiling tells the ptr may be null
2911  *
2912  * @return  true if ptr profile is valuable
2913  */
2914 bool TypePtr::would_improve_ptr(bool maybe_null) const {
2915   // profiling doesn't tell us anything useful
2916   if (maybe_null) {
2917     return false;
2918   }
2919   // We already know this is not be null
2920   if (!this->maybe_null()) {
2921     return false;
2922   }
2923   // We already know the speculative type cannot be null
2924   if (!speculative_maybe_null()) {
2925     return false;
2926   }
2927   return true;
2928 }
2929 
2930 //------------------------------dump2------------------------------------------
2931 const char *const TypePtr::ptr_msg[TypePtr::lastPTR] = {
2932   "TopPTR","AnyNull","Constant","NULL","NotNull","BotPTR"
2933 };
2934 
2935 #ifndef PRODUCT
2936 void TypePtr::dump2( Dict &d, uint depth, outputStream *st ) const {
2937   if( _ptr == Null ) st->print("NULL");
2938   else st->print("%s *", ptr_msg[_ptr]);
2939   _offset.dump2(st);
2940   dump_inline_depth(st);
2941   dump_speculative(st);
2942 }
2943 
2944 /**
2945  *dump the speculative part of the type
2946  */
2947 void TypePtr::dump_speculative(outputStream *st) const {
2948   if (_speculative != NULL) {
2949     st->print(" (speculative=");
2950     _speculative->dump_on(st);
2951     st->print(")");
2952   }
2953 }
2954 
2955 /**
2956  *dump the inline depth of the type
2957  */
2958 void TypePtr::dump_inline_depth(outputStream *st) const {
2959   if (_inline_depth != InlineDepthBottom) {
2960     if (_inline_depth == InlineDepthTop) {
2961       st->print(" (inline_depth=InlineDepthTop)");
2962     } else {
2963       st->print(" (inline_depth=%d)", _inline_depth);
2964     }
2965   }
2966 }
2967 #endif
2968 
2969 //------------------------------singleton--------------------------------------
2970 // TRUE if Type is a singleton type, FALSE otherwise.   Singletons are simple
2971 // constants
2972 bool TypePtr::singleton(void) const {
2973   // TopPTR, Null, AnyNull, Constant are all singletons
2974   return (_offset != Offset::bottom) && !below_centerline(_ptr);
2975 }
2976 
2977 bool TypePtr::empty(void) const {
2978   return (_offset == Offset::top) || above_centerline(_ptr);
2979 }
2980 
2981 //=============================================================================
2982 // Convenience common pre-built types.
2983 const TypeRawPtr *TypeRawPtr::BOTTOM;
2984 const TypeRawPtr *TypeRawPtr::NOTNULL;
2985 
2986 //------------------------------make-------------------------------------------
2987 const TypeRawPtr *TypeRawPtr::make( enum PTR ptr ) {
2988   assert( ptr != Constant, "what is the constant?" );
2989   assert( ptr != Null, "Use TypePtr for NULL" );
2990   return (TypeRawPtr*)(new TypeRawPtr(ptr,0))->hashcons();
2991 }
2992 
2993 const TypeRawPtr *TypeRawPtr::make( address bits ) {
2994   assert( bits, "Use TypePtr for NULL" );
2995   return (TypeRawPtr*)(new TypeRawPtr(Constant,bits))->hashcons();
2996 }
2997 
2998 //------------------------------cast_to_ptr_type-------------------------------
2999 const Type *TypeRawPtr::cast_to_ptr_type(PTR ptr) const {
3000   assert( ptr != Constant, "what is the constant?" );
3001   assert( ptr != Null, "Use TypePtr for NULL" );
3002   assert( _bits==0, "Why cast a constant address?");
3003   if( ptr == _ptr ) return this;
3004   return make(ptr);
3005 }
3006 
3007 //------------------------------get_con----------------------------------------
3008 intptr_t TypeRawPtr::get_con() const {
3009   assert( _ptr == Null || _ptr == Constant, "" );
3010   return (intptr_t)_bits;
3011 }
3012 
3013 //------------------------------meet-------------------------------------------
3014 // Compute the MEET of two types.  It returns a new Type object.
3015 const Type *TypeRawPtr::xmeet( const Type *t ) const {
3016   // Perform a fast test for common case; meeting the same types together.
3017   if( this == t ) return this;  // Meeting same type-rep?
3018 
3019   // Current "this->_base" is RawPtr
3020   switch( t->base() ) {         // switch on original type
3021   case Bottom:                  // Ye Olde Default
3022     return t;
3023   case Top:
3024     return this;
3025   case AnyPtr:                  // Meeting to AnyPtrs
3026     break;
3027   case RawPtr: {                // might be top, bot, any/not or constant
3028     enum PTR tptr = t->is_ptr()->ptr();
3029     enum PTR ptr = meet_ptr( tptr );
3030     if( ptr == Constant ) {     // Cannot be equal constants, so...
3031       if( tptr == Constant && _ptr != Constant)  return t;
3032       if( _ptr == Constant && tptr != Constant)  return this;
3033       ptr = NotNull;            // Fall down in lattice
3034     }
3035     return make( ptr );
3036   }
3037 
3038   case OopPtr:
3039   case InstPtr:
3040   case ValueTypePtr:
3041   case AryPtr:
3042   case MetadataPtr:
3043   case KlassPtr:
3044     return TypePtr::BOTTOM;     // Oop meet raw is not well defined
3045   default:                      // All else is a mistake
3046     typerr(t);
3047   }
3048 
3049   // Found an AnyPtr type vs self-RawPtr type
3050   const TypePtr *tp = t->is_ptr();
3051   switch (tp->ptr()) {
3052   case TypePtr::TopPTR:  return this;
3053   case TypePtr::BotPTR:  return t;
3054   case TypePtr::Null:
3055     if( _ptr == TypePtr::TopPTR ) return t;
3056     return TypeRawPtr::BOTTOM;
3057   case TypePtr::NotNull: return TypePtr::make(AnyPtr, meet_ptr(TypePtr::NotNull), tp->meet_offset(0), tp->speculative(), tp->inline_depth());
3058   case TypePtr::AnyNull:
3059     if( _ptr == TypePtr::Constant) return this;
3060     return make( meet_ptr(TypePtr::AnyNull) );
3061   default: ShouldNotReachHere();
3062   }
3063   return this;
3064 }
3065 
3066 //------------------------------xdual------------------------------------------
3067 // Dual: compute field-by-field dual
3068 const Type *TypeRawPtr::xdual() const {
3069   return new TypeRawPtr( dual_ptr(), _bits );
3070 }
3071 
3072 //------------------------------add_offset-------------------------------------
3073 const TypePtr *TypeRawPtr::add_offset( intptr_t offset ) const {
3074   if( offset == OffsetTop ) return BOTTOM; // Undefined offset-> undefined pointer
3075   if( offset == OffsetBot ) return BOTTOM; // Unknown offset-> unknown pointer
3076   if( offset == 0 ) return this; // No change
3077   switch (_ptr) {
3078   case TypePtr::TopPTR:
3079   case TypePtr::BotPTR:
3080   case TypePtr::NotNull:
3081     return this;
3082   case TypePtr::Null:
3083   case TypePtr::Constant: {
3084     address bits = _bits+offset;
3085     if ( bits == 0 ) return TypePtr::NULL_PTR;
3086     return make( bits );
3087   }
3088   default:  ShouldNotReachHere();
3089   }
3090   return NULL;                  // Lint noise
3091 }
3092 
3093 //------------------------------eq---------------------------------------------
3094 // Structural equality check for Type representations
3095 bool TypeRawPtr::eq( const Type *t ) const {
3096   const TypeRawPtr *a = (const TypeRawPtr*)t;
3097   return _bits == a->_bits && TypePtr::eq(t);
3098 }
3099 
3100 //------------------------------hash-------------------------------------------
3101 // Type-specific hashing function.
3102 int TypeRawPtr::hash(void) const {
3103   return (intptr_t)_bits + TypePtr::hash();
3104 }
3105 
3106 //------------------------------dump2------------------------------------------
3107 #ifndef PRODUCT
3108 void TypeRawPtr::dump2( Dict &d, uint depth, outputStream *st ) const {
3109   if( _ptr == Constant )
3110     st->print(INTPTR_FORMAT, p2i(_bits));
3111   else
3112     st->print("rawptr:%s", ptr_msg[_ptr]);
3113 }
3114 #endif
3115 
3116 //=============================================================================
3117 // Convenience common pre-built type.
3118 const TypeOopPtr *TypeOopPtr::BOTTOM;
3119 
3120 //------------------------------TypeOopPtr-------------------------------------
3121 TypeOopPtr::TypeOopPtr(TYPES t, PTR ptr, ciKlass* k, bool xk, ciObject* o, Offset offset, Offset field_offset,
3122                        int instance_id, const TypePtr* speculative, int inline_depth)
3123   : TypePtr(t, ptr, offset, speculative, inline_depth),
3124     _const_oop(o), _klass(k),
3125     _klass_is_exact(xk),
3126     _is_ptr_to_narrowoop(false),
3127     _is_ptr_to_narrowklass(false),
3128     _is_ptr_to_boxed_value(false),
3129     _instance_id(instance_id) {
3130   if (Compile::current()->eliminate_boxing() && (t == InstPtr) &&
3131       (offset.get() > 0) && xk && (k != 0) && k->is_instance_klass()) {
3132     _is_ptr_to_boxed_value = k->as_instance_klass()->is_boxed_value_offset(offset.get());
3133   }
3134 #ifdef _LP64
3135   if (this->offset() != 0) {
3136     if (this->offset() == oopDesc::klass_offset_in_bytes()) {
3137       _is_ptr_to_narrowklass = UseCompressedClassPointers;
3138     } else if (klass() == NULL) {
3139       // Array with unknown body type
3140       assert(this->isa_aryptr(), "only arrays without klass");
3141       _is_ptr_to_narrowoop = UseCompressedOops;
3142     } else if (UseCompressedOops && this->isa_aryptr() && this->offset() != arrayOopDesc::length_offset_in_bytes()) {
3143       if (klass()->is_obj_array_klass()) {
3144         _is_ptr_to_narrowoop = true;
3145       } else if (klass()->is_value_array_klass() && field_offset != Offset::top && field_offset != Offset::bottom) {
3146         // Check if the field of the value type array element contains oops
3147         ciValueKlass* vk = klass()->as_value_array_klass()->element_klass()->as_value_klass();
3148         int foffset = field_offset.get() + vk->first_field_offset();
3149         ciField* field = vk->get_field_by_offset(foffset, false);
3150         assert(field != NULL, "missing field");
3151         BasicType bt = field->layout_type();
3152         assert(bt != T_VALUETYPE, "should be flattened");
3153         _is_ptr_to_narrowoop = (bt == T_OBJECT || bt == T_ARRAY);
3154       }
3155     } else if (klass()->is_instance_klass()) {
3156       ciInstanceKlass* ik = klass()->as_instance_klass();
3157       ciField* field = NULL;
3158       if (this->isa_klassptr()) {
3159         // Perm objects don't use compressed references
3160       } else if (_offset == Offset::bottom || _offset == Offset::top) {
3161         // unsafe access
3162         _is_ptr_to_narrowoop = UseCompressedOops;
3163       } else { // exclude unsafe ops
3164         assert(this->isa_instptr() || this->isa_valuetypeptr(), "must be an instance ptr.");
3165 
3166         if (klass() == ciEnv::current()->Class_klass() &&
3167             (this->offset() == java_lang_Class::klass_offset_in_bytes() ||
3168              this->offset() == java_lang_Class::array_klass_offset_in_bytes())) {
3169           // Special hidden fields from the Class.
3170           assert(this->isa_instptr(), "must be an instance ptr.");
3171           _is_ptr_to_narrowoop = false;
3172         } else if (klass() == ciEnv::current()->Class_klass() &&
3173                    this->offset() >= InstanceMirrorKlass::offset_of_static_fields()) {
3174           // Static fields
3175           assert(o != NULL, "must be constant");
3176           ciInstanceKlass* k = o->as_instance()->java_lang_Class_klass()->as_instance_klass();
3177           ciField* field = k->get_field_by_offset(this->offset(), true);
3178           assert(field != NULL, "missing field");
3179           BasicType basic_elem_type = field->layout_type();
3180           _is_ptr_to_narrowoop = UseCompressedOops && (basic_elem_type == T_OBJECT ||
3181                                                        basic_elem_type == T_ARRAY);
3182         } else {
3183           // Instance fields which contains a compressed oop references.
3184           field = ik->get_field_by_offset(this->offset(), false);
3185           if (field != NULL) {
3186             BasicType basic_elem_type = field->layout_type();
3187             _is_ptr_to_narrowoop = UseCompressedOops && (basic_elem_type == T_OBJECT ||
3188                                                          basic_elem_type == T_ARRAY);
3189           } else if (klass()->equals(ciEnv::current()->Object_klass())) {
3190             // Compile::find_alias_type() cast exactness on all types to verify
3191             // that it does not affect alias type.
3192             _is_ptr_to_narrowoop = UseCompressedOops;
3193           } else {
3194             // Type for the copy start in LibraryCallKit::inline_native_clone().
3195             _is_ptr_to_narrowoop = UseCompressedOops;
3196           }
3197         }
3198       }
3199     }
3200   }
3201 #endif
3202 }
3203 
3204 //------------------------------make-------------------------------------------
3205 const TypeOopPtr *TypeOopPtr::make(PTR ptr, Offset offset, int instance_id,
3206                                    const TypePtr* speculative, int inline_depth) {
3207   assert(ptr != Constant, "no constant generic pointers");
3208   ciKlass*  k = Compile::current()->env()->Object_klass();
3209   bool      xk = false;
3210   ciObject* o = NULL;
3211   return (TypeOopPtr*)(new TypeOopPtr(OopPtr, ptr, k, xk, o, offset, Offset::bottom, instance_id, speculative, inline_depth))->hashcons();
3212 }
3213 
3214 
3215 //------------------------------cast_to_ptr_type-------------------------------
3216 const Type *TypeOopPtr::cast_to_ptr_type(PTR ptr) const {
3217   assert(_base == OopPtr, "subclass must override cast_to_ptr_type");
3218   if( ptr == _ptr ) return this;
3219   return make(ptr, _offset, _instance_id, _speculative, _inline_depth);
3220 }
3221 
3222 //-----------------------------cast_to_instance_id----------------------------
3223 const TypeOopPtr *TypeOopPtr::cast_to_instance_id(int instance_id) const {
3224   // There are no instances of a general oop.
3225   // Return self unchanged.
3226   return this;
3227 }
3228 
3229 //-----------------------------cast_to_exactness-------------------------------
3230 const Type *TypeOopPtr::cast_to_exactness(bool klass_is_exact) const {
3231   // There is no such thing as an exact general oop.
3232   // Return self unchanged.
3233   return this;
3234 }
3235 
3236 
3237 //------------------------------as_klass_type----------------------------------
3238 // Return the klass type corresponding to this instance or array type.
3239 // It is the type that is loaded from an object of this type.
3240 const TypeKlassPtr* TypeOopPtr::as_klass_type() const {
3241   ciKlass* k = klass();
3242   bool    xk = klass_is_exact();
3243   if (k == NULL)
3244     return TypeKlassPtr::OBJECT;
3245   else
3246     return TypeKlassPtr::make(xk? Constant: NotNull, k, Offset(0));
3247 }
3248 
3249 //------------------------------meet-------------------------------------------
3250 // Compute the MEET of two types.  It returns a new Type object.
3251 const Type *TypeOopPtr::xmeet_helper(const Type *t) const {
3252   // Perform a fast test for common case; meeting the same types together.
3253   if( this == t ) return this;  // Meeting same type-rep?
3254 
3255   // Current "this->_base" is OopPtr
3256   switch (t->base()) {          // switch on original type
3257 
3258   case Int:                     // Mixing ints & oops happens when javac
3259   case Long:                    // reuses local variables
3260   case FloatTop:
3261   case FloatCon:
3262   case FloatBot:
3263   case DoubleTop:
3264   case DoubleCon:
3265   case DoubleBot:
3266   case NarrowOop:
3267   case NarrowKlass:
3268   case Bottom:                  // Ye Olde Default
3269     return Type::BOTTOM;
3270   case Top:
3271     return this;
3272 
3273   default:                      // All else is a mistake
3274     typerr(t);
3275 
3276   case RawPtr:
3277   case MetadataPtr:
3278   case KlassPtr:
3279     return TypePtr::BOTTOM;     // Oop meet raw is not well defined
3280 
3281   case AnyPtr: {
3282     // Found an AnyPtr type vs self-OopPtr type
3283     const TypePtr *tp = t->is_ptr();
3284     Offset offset = meet_offset(tp->offset());
3285     PTR ptr = meet_ptr(tp->ptr());
3286     const TypePtr* speculative = xmeet_speculative(tp);
3287     int depth = meet_inline_depth(tp->inline_depth());
3288     switch (tp->ptr()) {
3289     case Null:
3290       if (ptr == Null)  return TypePtr::make(AnyPtr, ptr, offset, speculative, depth);
3291       // else fall through:
3292     case TopPTR:
3293     case AnyNull: {
3294       int instance_id = meet_instance_id(InstanceTop);
3295       return make(ptr, offset, instance_id, speculative, depth);
3296     }
3297     case BotPTR:
3298     case NotNull:
3299       return TypePtr::make(AnyPtr, ptr, offset, speculative, depth);
3300     default: typerr(t);
3301     }
3302   }
3303 
3304   case OopPtr: {                 // Meeting to other OopPtrs
3305     const TypeOopPtr *tp = t->is_oopptr();
3306     int instance_id = meet_instance_id(tp->instance_id());
3307     const TypePtr* speculative = xmeet_speculative(tp);
3308     int depth = meet_inline_depth(tp->inline_depth());
3309     return make(meet_ptr(tp->ptr()), meet_offset(tp->offset()), instance_id, speculative, depth);
3310   }
3311 
3312   case InstPtr:                  // For these, flip the call around to cut down
3313   case ValueTypePtr:
3314   case AryPtr:
3315     return t->xmeet(this);      // Call in reverse direction
3316 
3317   } // End of switch
3318   return this;                  // Return the double constant
3319 }
3320 
3321 
3322 //------------------------------xdual------------------------------------------
3323 // Dual of a pure heap pointer.  No relevant klass or oop information.
3324 const Type *TypeOopPtr::xdual() const {
3325   assert(klass() == Compile::current()->env()->Object_klass(), "no klasses here");
3326   assert(const_oop() == NULL,             "no constants here");
3327   return new TypeOopPtr(_base, dual_ptr(), klass(), klass_is_exact(), const_oop(), dual_offset(), Offset::bottom, dual_instance_id(), dual_speculative(), dual_inline_depth());
3328 }
3329 
3330 //--------------------------make_from_klass_common-----------------------------
3331 // Computes the element-type given a klass.
3332 const TypeOopPtr* TypeOopPtr::make_from_klass_common(ciKlass *klass, bool klass_change, bool try_for_exact) {
3333   if (klass->is_valuetype()) {
3334     return TypeValueTypePtr::make(TypePtr::NotNull, klass->as_value_klass());
3335   } else if (klass->is_instance_klass()) {
3336     Compile* C = Compile::current();
3337     Dependencies* deps = C->dependencies();
3338     assert((deps != NULL) == (C->method() != NULL && C->method()->code_size() > 0), "sanity");
3339     // Element is an instance
3340     bool klass_is_exact = false;
3341     if (klass->is_loaded()) {
3342       // Try to set klass_is_exact.
3343       ciInstanceKlass* ik = klass->as_instance_klass();
3344       klass_is_exact = ik->is_final();
3345       if (!klass_is_exact && klass_change
3346           && deps != NULL && UseUniqueSubclasses) {
3347         ciInstanceKlass* sub = ik->unique_concrete_subklass();
3348         if (sub != NULL) {
3349           deps->assert_abstract_with_unique_concrete_subtype(ik, sub);
3350           klass = ik = sub;
3351           klass_is_exact = sub->is_final();
3352         }
3353       }
3354       if (!klass_is_exact && try_for_exact
3355           && deps != NULL && UseExactTypes) {
3356         if (!ik->is_interface() && !ik->has_subklass()) {
3357           // Add a dependence; if concrete subclass added we need to recompile
3358           deps->assert_leaf_type(ik);
3359           klass_is_exact = true;
3360         }
3361       }
3362     }
3363     return TypeInstPtr::make(TypePtr::BotPTR, klass, klass_is_exact, NULL, Offset(0));
3364   } else if (klass->is_obj_array_klass()) {
3365     // Element is an object or value array. Recursively call ourself.
3366     const TypeOopPtr* etype = TypeOopPtr::make_from_klass_common(klass->as_array_klass()->element_klass(), false, try_for_exact);
3367     bool xk = etype->klass_is_exact();
3368     const TypeAry* arr0 = TypeAry::make(etype, TypeInt::POS);
3369     // We used to pass NotNull in here, asserting that the sub-arrays
3370     // are all not-null.  This is not true in generally, as code can
3371     // slam NULLs down in the subarrays.
3372     const TypeAryPtr* arr = TypeAryPtr::make(TypePtr::BotPTR, arr0, klass, xk, Offset(0));
3373     return arr;
3374   } else if (klass->is_type_array_klass()) {
3375     // Element is an typeArray
3376     const Type* etype = get_const_basic_type(klass->as_type_array_klass()->element_type());
3377     const TypeAry* arr0 = TypeAry::make(etype, TypeInt::POS);
3378     // We used to pass NotNull in here, asserting that the array pointer
3379     // is not-null. That was not true in general.
3380     const TypeAryPtr* arr = TypeAryPtr::make(TypePtr::BotPTR, arr0, klass, true, Offset(0));
3381     return arr;
3382   } else if (klass->is_value_array_klass()) {
3383     ciValueKlass* vk = klass->as_array_klass()->element_klass()->as_value_klass();
3384     const Type* etype = NULL;
3385     bool xk = false;
3386     if (vk->flatten_array()) {
3387       etype = TypeValueType::make(vk);
3388       xk = true;
3389     } else {
3390       const TypeOopPtr* etype_oop = TypeOopPtr::make_from_klass_common(vk, false, try_for_exact);
3391       xk = etype_oop->klass_is_exact();
3392       etype = etype_oop;
3393     }
3394     const TypeAry* arr0 = TypeAry::make(etype, TypeInt::POS);
3395     const TypeAryPtr* arr = TypeAryPtr::make(TypePtr::BotPTR, arr0, klass, xk, Offset(0));
3396     return arr;
3397   } else {
3398     ShouldNotReachHere();
3399     return NULL;
3400   }
3401 }
3402 
3403 //------------------------------make_from_constant-----------------------------
3404 // Make a java pointer from an oop constant
3405 const TypeOopPtr* TypeOopPtr::make_from_constant(ciObject* o, bool require_constant) {
3406   assert(!o->is_null_object(), "null object not yet handled here.");
3407   ciKlass* klass = o->klass();
3408   if (klass->is_valuetype()) {
3409     // Element is a value type
3410     if (require_constant) {
3411       if (!o->can_be_constant())  return NULL;
3412     } else if (!o->should_be_constant()) {
3413       return TypeValueTypePtr::make(TypePtr::NotNull, klass->as_value_klass());
3414     }
3415     return TypeValueTypePtr::make(o);
3416   } else if (klass->is_instance_klass()) {
3417     // Element is an instance
3418     if (require_constant) {
3419       if (!o->can_be_constant())  return NULL;
3420     } else if (!o->should_be_constant()) {
3421       return TypeInstPtr::make(TypePtr::NotNull, klass, true, NULL, Offset(0));
3422     }
3423     return TypeInstPtr::make(o);
3424   } else if (klass->is_obj_array_klass() || klass->is_value_array_klass()) {
3425     // Element is an object array. Recursively call ourself.
3426     const TypeOopPtr *etype =
3427       TypeOopPtr::make_from_klass_raw(klass->as_array_klass()->element_klass());
3428     const TypeAry* arr0 = TypeAry::make(etype, TypeInt::make(o->as_array()->length()));
3429     // We used to pass NotNull in here, asserting that the sub-arrays
3430     // are all not-null.  This is not true in generally, as code can
3431     // slam NULLs down in the subarrays.
3432     if (require_constant) {
3433       if (!o->can_be_constant())  return NULL;
3434     } else if (!o->should_be_constant()) {
3435       return TypeAryPtr::make(TypePtr::NotNull, arr0, klass, true, Offset(0));
3436     }
3437     const TypeAryPtr* arr = TypeAryPtr::make(TypePtr::Constant, o, arr0, klass, true, Offset(0));
3438     return arr;
3439   } else if (klass->is_type_array_klass()) {
3440     // Element is an typeArray
3441     const Type* etype =
3442       (Type*)get_const_basic_type(klass->as_type_array_klass()->element_type());
3443     const TypeAry* arr0 = TypeAry::make(etype, TypeInt::make(o->as_array()->length()));
3444     // We used to pass NotNull in here, asserting that the array pointer
3445     // is not-null. That was not true in general.
3446     if (require_constant) {
3447       if (!o->can_be_constant())  return NULL;
3448     } else if (!o->should_be_constant()) {
3449       return TypeAryPtr::make(TypePtr::NotNull, arr0, klass, true, Offset(0));
3450     }
3451     const TypeAryPtr* arr = TypeAryPtr::make(TypePtr::Constant, o, arr0, klass, true, Offset(0));
3452     return arr;
3453   }
3454 
3455   fatal("unhandled object type");
3456   return NULL;
3457 }
3458 
3459 //------------------------------get_con----------------------------------------
3460 intptr_t TypeOopPtr::get_con() const {
3461   assert( _ptr == Null || _ptr == Constant, "" );
3462   assert(offset() >= 0, "");
3463 
3464   if (offset() != 0) {
3465     // After being ported to the compiler interface, the compiler no longer
3466     // directly manipulates the addresses of oops.  Rather, it only has a pointer
3467     // to a handle at compile time.  This handle is embedded in the generated
3468     // code and dereferenced at the time the nmethod is made.  Until that time,
3469     // it is not reasonable to do arithmetic with the addresses of oops (we don't
3470     // have access to the addresses!).  This does not seem to currently happen,
3471     // but this assertion here is to help prevent its occurence.
3472     tty->print_cr("Found oop constant with non-zero offset");
3473     ShouldNotReachHere();
3474   }
3475 
3476   return (intptr_t)const_oop()->constant_encoding();
3477 }
3478 
3479 
3480 //-----------------------------filter------------------------------------------
3481 // Do not allow interface-vs.-noninterface joins to collapse to top.
3482 const Type *TypeOopPtr::filter_helper(const Type *kills, bool include_speculative) const {
3483 
3484   const Type* ft = join_helper(kills, include_speculative);
3485   const TypeInstPtr* ftip = ft->isa_instptr();
3486   const TypeInstPtr* ktip = kills->isa_instptr();
3487 
3488   if (ft->empty()) {
3489     // Check for evil case of 'this' being a class and 'kills' expecting an
3490     // interface.  This can happen because the bytecodes do not contain
3491     // enough type info to distinguish a Java-level interface variable
3492     // from a Java-level object variable.  If we meet 2 classes which
3493     // both implement interface I, but their meet is at 'j/l/O' which
3494     // doesn't implement I, we have no way to tell if the result should
3495     // be 'I' or 'j/l/O'.  Thus we'll pick 'j/l/O'.  If this then flows
3496     // into a Phi which "knows" it's an Interface type we'll have to
3497     // uplift the type.
3498     if (!empty()) {
3499       if (ktip != NULL && ktip->is_loaded() && ktip->klass()->is_interface()) {
3500         return kills;           // Uplift to interface
3501       }
3502       // Also check for evil cases of 'this' being a class array
3503       // and 'kills' expecting an array of interfaces.
3504       Type::get_arrays_base_elements(ft, kills, NULL, &ktip);
3505       if (ktip != NULL && ktip->is_loaded() && ktip->klass()->is_interface()) {
3506         return kills;           // Uplift to array of interface
3507       }
3508     }
3509 
3510     return Type::TOP;           // Canonical empty value
3511   }
3512 
3513   // If we have an interface-typed Phi or cast and we narrow to a class type,
3514   // the join should report back the class.  However, if we have a J/L/Object
3515   // class-typed Phi and an interface flows in, it's possible that the meet &
3516   // join report an interface back out.  This isn't possible but happens
3517   // because the type system doesn't interact well with interfaces.
3518   if (ftip != NULL && ktip != NULL &&
3519       ftip->is_loaded() &&  ftip->klass()->is_interface() &&
3520       ktip->is_loaded() && !ktip->klass()->is_interface()) {
3521     assert(!ftip->klass_is_exact(), "interface could not be exact");
3522     return ktip->cast_to_ptr_type(ftip->ptr());
3523   }
3524 
3525   return ft;
3526 }
3527 
3528 //------------------------------eq---------------------------------------------
3529 // Structural equality check for Type representations
3530 bool TypeOopPtr::eq( const Type *t ) const {
3531   const TypeOopPtr *a = (const TypeOopPtr*)t;
3532   if (_klass_is_exact != a->_klass_is_exact ||
3533       _instance_id != a->_instance_id)  return false;
3534   ciObject* one = const_oop();
3535   ciObject* two = a->const_oop();
3536   if (one == NULL || two == NULL) {
3537     return (one == two) && TypePtr::eq(t);
3538   } else {
3539     return one->equals(two) && TypePtr::eq(t);
3540   }
3541 }
3542 
3543 //------------------------------hash-------------------------------------------
3544 // Type-specific hashing function.
3545 int TypeOopPtr::hash(void) const {
3546   return
3547     java_add(java_add(const_oop() ? const_oop()->hash() : 0, _klass_is_exact),
3548              java_add(_instance_id, TypePtr::hash()));
3549 }
3550 
3551 //------------------------------dump2------------------------------------------
3552 #ifndef PRODUCT
3553 void TypeOopPtr::dump2( Dict &d, uint depth, outputStream *st ) const {
3554   st->print("oopptr:%s", ptr_msg[_ptr]);
3555   if( _klass_is_exact ) st->print(":exact");
3556   if( const_oop() ) st->print(INTPTR_FORMAT, p2i(const_oop()));
3557   _offset.dump2(st);
3558   if (_instance_id == InstanceTop)
3559     st->print(",iid=top");
3560   else if (_instance_id != InstanceBot)
3561     st->print(",iid=%d",_instance_id);
3562 
3563   dump_inline_depth(st);
3564   dump_speculative(st);
3565 }
3566 #endif
3567 
3568 //------------------------------singleton--------------------------------------
3569 // TRUE if Type is a singleton type, FALSE otherwise.   Singletons are simple
3570 // constants
3571 bool TypeOopPtr::singleton(void) const {
3572   // detune optimizer to not generate constant oop + constant offset as a constant!
3573   // TopPTR, Null, AnyNull, Constant are all singletons
3574   return (offset() == 0) && !below_centerline(_ptr);
3575 }
3576 
3577 //------------------------------add_offset-------------------------------------
3578 const TypePtr *TypeOopPtr::add_offset(intptr_t offset) const {
3579   return make(_ptr, xadd_offset(offset), _instance_id, add_offset_speculative(offset), _inline_depth);
3580 }
3581 
3582 /**
3583  * Return same type without a speculative part
3584  */
3585 const Type* TypeOopPtr::remove_speculative() const {
3586   if (_speculative == NULL) {
3587     return this;
3588   }
3589   assert(_inline_depth == InlineDepthTop || _inline_depth == InlineDepthBottom, "non speculative type shouldn't have inline depth");
3590   return make(_ptr, _offset, _instance_id, NULL, _inline_depth);
3591 }
3592 
3593 /**
3594  * Return same type but drop speculative part if we know we won't use
3595  * it
3596  */
3597 const Type* TypeOopPtr::cleanup_speculative() const {
3598   // If the klass is exact and the ptr is not null then there's
3599   // nothing that the speculative type can help us with
3600   if (klass_is_exact() && !maybe_null()) {
3601     return remove_speculative();
3602   }
3603   return TypePtr::cleanup_speculative();
3604 }
3605 
3606 /**
3607  * Return same type but with a different inline depth (used for speculation)
3608  *
3609  * @param depth  depth to meet with
3610  */
3611 const TypePtr* TypeOopPtr::with_inline_depth(int depth) const {
3612   if (!UseInlineDepthForSpeculativeTypes) {
3613     return this;
3614   }
3615   return make(_ptr, _offset, _instance_id, _speculative, depth);
3616 }
3617 
3618 //------------------------------meet_instance_id--------------------------------
3619 int TypeOopPtr::meet_instance_id( int instance_id ) const {
3620   // Either is 'TOP' instance?  Return the other instance!
3621   if( _instance_id == InstanceTop ) return  instance_id;
3622   if(  instance_id == InstanceTop ) return _instance_id;
3623   // If either is different, return 'BOTTOM' instance
3624   if( _instance_id != instance_id ) return InstanceBot;
3625   return _instance_id;
3626 }
3627 
3628 //------------------------------dual_instance_id--------------------------------
3629 int TypeOopPtr::dual_instance_id( ) const {
3630   if( _instance_id == InstanceTop ) return InstanceBot; // Map TOP into BOTTOM
3631   if( _instance_id == InstanceBot ) return InstanceTop; // Map BOTTOM into TOP
3632   return _instance_id;              // Map everything else into self
3633 }
3634 
3635 /**
3636  * Check whether new profiling would improve speculative type
3637  *
3638  * @param   exact_kls    class from profiling
3639  * @param   inline_depth inlining depth of profile point
3640  *
3641  * @return  true if type profile is valuable
3642  */
3643 bool TypeOopPtr::would_improve_type(ciKlass* exact_kls, int inline_depth) const {
3644   // no way to improve an already exact type
3645   if (klass_is_exact()) {
3646     return false;
3647   }
3648   return TypePtr::would_improve_type(exact_kls, inline_depth);
3649 }
3650 
3651 //=============================================================================
3652 // Convenience common pre-built types.
3653 const TypeInstPtr *TypeInstPtr::NOTNULL;
3654 const TypeInstPtr *TypeInstPtr::BOTTOM;
3655 const TypeInstPtr *TypeInstPtr::MIRROR;
3656 const TypeInstPtr *TypeInstPtr::MARK;
3657 const TypeInstPtr *TypeInstPtr::KLASS;
3658 
3659 //------------------------------TypeInstPtr-------------------------------------
3660 TypeInstPtr::TypeInstPtr(PTR ptr, ciKlass* k, bool xk, ciObject* o, Offset off,
3661                          int instance_id, const TypePtr* speculative, int inline_depth)
3662   : TypeOopPtr(InstPtr, ptr, k, xk, o, off, Offset::bottom, instance_id, speculative, inline_depth),
3663     _name(k->name()) {
3664    assert(k != NULL &&
3665           (k->is_loaded() || o == NULL),
3666           "cannot have constants with non-loaded klass");
3667 };
3668 
3669 //------------------------------make-------------------------------------------
3670 const TypeInstPtr *TypeInstPtr::make(PTR ptr,
3671                                      ciKlass* k,
3672                                      bool xk,
3673                                      ciObject* o,
3674                                      Offset offset,
3675                                      int instance_id,
3676                                      const TypePtr* speculative,
3677                                      int inline_depth) {
3678   assert( !k->is_loaded() || k->is_instance_klass(), "Must be for instance");
3679   // Either const_oop() is NULL or else ptr is Constant
3680   assert( (!o && ptr != Constant) || (o && ptr == Constant),
3681           "constant pointers must have a value supplied" );
3682   // Ptr is never Null
3683   assert( ptr != Null, "NULL pointers are not typed" );
3684 
3685   assert(instance_id <= 0 || xk || !UseExactTypes, "instances are always exactly typed");
3686   if (!UseExactTypes)  xk = false;
3687   if (ptr == Constant) {
3688     // Note:  This case includes meta-object constants, such as methods.
3689     xk = true;
3690   } else if (k->is_loaded()) {
3691     ciInstanceKlass* ik = k->as_instance_klass();
3692     if (!xk && ik->is_final())     xk = true;   // no inexact final klass
3693     if (xk && ik->is_interface())  xk = false;  // no exact interface
3694   }
3695 
3696   // Now hash this baby
3697   TypeInstPtr *result =
3698     (TypeInstPtr*)(new TypeInstPtr(ptr, k, xk, o ,offset, instance_id, speculative, inline_depth))->hashcons();
3699 
3700   return result;
3701 }
3702 
3703 /**
3704  *  Create constant type for a constant boxed value
3705  */
3706 const Type* TypeInstPtr::get_const_boxed_value() const {
3707   assert(is_ptr_to_boxed_value(), "should be called only for boxed value");
3708   assert((const_oop() != NULL), "should be called only for constant object");
3709   ciConstant constant = const_oop()->as_instance()->field_value_by_offset(offset());
3710   BasicType bt = constant.basic_type();
3711   switch (bt) {
3712     case T_BOOLEAN:  return TypeInt::make(constant.as_boolean());
3713     case T_INT:      return TypeInt::make(constant.as_int());
3714     case T_CHAR:     return TypeInt::make(constant.as_char());
3715     case T_BYTE:     return TypeInt::make(constant.as_byte());
3716     case T_SHORT:    return TypeInt::make(constant.as_short());
3717     case T_FLOAT:    return TypeF::make(constant.as_float());
3718     case T_DOUBLE:   return TypeD::make(constant.as_double());
3719     case T_LONG:     return TypeLong::make(constant.as_long());
3720     default:         break;
3721   }
3722   fatal("Invalid boxed value type '%s'", type2name(bt));
3723   return NULL;
3724 }
3725 
3726 //------------------------------cast_to_ptr_type-------------------------------
3727 const Type *TypeInstPtr::cast_to_ptr_type(PTR ptr) const {
3728   if( ptr == _ptr ) return this;
3729   // Reconstruct _sig info here since not a problem with later lazy
3730   // construction, _sig will show up on demand.
3731   return make(ptr, klass(), klass_is_exact(), const_oop(), _offset, _instance_id, _speculative, _inline_depth);
3732 }
3733 
3734 
3735 //-----------------------------cast_to_exactness-------------------------------
3736 const Type *TypeInstPtr::cast_to_exactness(bool klass_is_exact) const {
3737   if( klass_is_exact == _klass_is_exact ) return this;
3738   if (!UseExactTypes)  return this;
3739   if (!_klass->is_loaded())  return this;
3740   ciInstanceKlass* ik = _klass->as_instance_klass();
3741   if( (ik->is_final() || _const_oop) )  return this;  // cannot clear xk
3742   if( ik->is_interface() )              return this;  // cannot set xk
3743   return make(ptr(), klass(), klass_is_exact, const_oop(), _offset, _instance_id, _speculative, _inline_depth);
3744 }
3745 
3746 //-----------------------------cast_to_instance_id----------------------------
3747 const TypeOopPtr *TypeInstPtr::cast_to_instance_id(int instance_id) const {
3748   if( instance_id == _instance_id ) return this;
3749   return make(_ptr, klass(), _klass_is_exact, const_oop(), _offset, instance_id, _speculative, _inline_depth);
3750 }
3751 
3752 //------------------------------xmeet_unloaded---------------------------------
3753 // Compute the MEET of two InstPtrs when at least one is unloaded.
3754 // Assume classes are different since called after check for same name/class-loader
3755 const TypeInstPtr *TypeInstPtr::xmeet_unloaded(const TypeInstPtr *tinst) const {
3756     Offset off = meet_offset(tinst->offset());
3757     PTR ptr = meet_ptr(tinst->ptr());
3758     int instance_id = meet_instance_id(tinst->instance_id());
3759     const TypePtr* speculative = xmeet_speculative(tinst);
3760     int depth = meet_inline_depth(tinst->inline_depth());
3761 
3762     const TypeInstPtr *loaded    = is_loaded() ? this  : tinst;
3763     const TypeInstPtr *unloaded  = is_loaded() ? tinst : this;
3764     if( loaded->klass()->equals(ciEnv::current()->Object_klass()) ) {
3765       //
3766       // Meet unloaded class with java/lang/Object
3767       //
3768       // Meet
3769       //          |                     Unloaded Class
3770       //  Object  |   TOP    |   AnyNull | Constant |   NotNull |  BOTTOM   |
3771       //  ===================================================================
3772       //   TOP    | ..........................Unloaded......................|
3773       //  AnyNull |  U-AN    |................Unloaded......................|
3774       // Constant | ... O-NN .................................. |   O-BOT   |
3775       //  NotNull | ... O-NN .................................. |   O-BOT   |
3776       //  BOTTOM  | ........................Object-BOTTOM ..................|
3777       //
3778       assert(loaded->ptr() != TypePtr::Null, "insanity check");
3779       //
3780       if(      loaded->ptr() == TypePtr::TopPTR ) { return unloaded; }
3781       else if (loaded->ptr() == TypePtr::AnyNull) { return TypeInstPtr::make(ptr, unloaded->klass(), false, NULL, off, instance_id, speculative, depth); }
3782       else if (loaded->ptr() == TypePtr::BotPTR ) { return TypeInstPtr::BOTTOM; }
3783       else if (loaded->ptr() == TypePtr::Constant || loaded->ptr() == TypePtr::NotNull) {
3784         if (unloaded->ptr() == TypePtr::BotPTR  ) { return TypeInstPtr::BOTTOM;  }
3785         else                                      { return TypeInstPtr::NOTNULL; }
3786       }
3787       else if( unloaded->ptr() == TypePtr::TopPTR )  { return unloaded; }
3788 
3789       return unloaded->cast_to_ptr_type(TypePtr::AnyNull)->is_instptr();
3790     }
3791 
3792     // Both are unloaded, not the same class, not Object
3793     // Or meet unloaded with a different loaded class, not java/lang/Object
3794     if( ptr != TypePtr::BotPTR ) {
3795       return TypeInstPtr::NOTNULL;
3796     }
3797     return TypeInstPtr::BOTTOM;
3798 }
3799 
3800 
3801 //------------------------------meet-------------------------------------------
3802 // Compute the MEET of two types.  It returns a new Type object.
3803 const Type *TypeInstPtr::xmeet_helper(const Type *t) const {
3804   // Perform a fast test for common case; meeting the same types together.
3805   if( this == t ) return this;  // Meeting same type-rep?
3806 
3807   // Current "this->_base" is Pointer
3808   switch (t->base()) {          // switch on original type
3809 
3810   case Int:                     // Mixing ints & oops happens when javac
3811   case Long:                    // reuses local variables
3812   case FloatTop:
3813   case FloatCon:
3814   case FloatBot:
3815   case DoubleTop:
3816   case DoubleCon:
3817   case DoubleBot:
3818   case NarrowOop:
3819   case NarrowKlass:
3820   case Bottom:                  // Ye Olde Default
3821     return Type::BOTTOM;
3822   case Top:
3823     return this;
3824 
3825   default:                      // All else is a mistake
3826     typerr(t);
3827 
3828   case MetadataPtr:
3829   case KlassPtr:
3830   case RawPtr: return TypePtr::BOTTOM;
3831 
3832   case AryPtr: {                // All arrays inherit from Object class
3833     const TypeAryPtr *tp = t->is_aryptr();
3834     Offset offset = meet_offset(tp->offset());
3835     PTR ptr = meet_ptr(tp->ptr());
3836     int instance_id = meet_instance_id(tp->instance_id());
3837     const TypePtr* speculative = xmeet_speculative(tp);
3838     int depth = meet_inline_depth(tp->inline_depth());
3839     switch (ptr) {
3840     case TopPTR:
3841     case AnyNull:                // Fall 'down' to dual of object klass
3842       // For instances when a subclass meets a superclass we fall
3843       // below the centerline when the superclass is exact. We need to
3844       // do the same here.
3845       if (klass()->equals(ciEnv::current()->Object_klass()) && !klass_is_exact()) {
3846         return TypeAryPtr::make(ptr, tp->ary(), tp->klass(), tp->klass_is_exact(), offset, tp->field_offset(), instance_id, speculative, depth);
3847       } else {
3848         // cannot subclass, so the meet has to fall badly below the centerline
3849         ptr = NotNull;
3850         instance_id = InstanceBot;
3851         return TypeInstPtr::make( ptr, ciEnv::current()->Object_klass(), false, NULL, offset, instance_id, speculative, depth);
3852       }
3853     case Constant:
3854     case NotNull:
3855     case BotPTR:                // Fall down to object klass
3856       // LCA is object_klass, but if we subclass from the top we can do better
3857       if( above_centerline(_ptr) ) { // if( _ptr == TopPTR || _ptr == AnyNull )
3858         // If 'this' (InstPtr) is above the centerline and it is Object class
3859         // then we can subclass in the Java class hierarchy.
3860         // For instances when a subclass meets a superclass we fall
3861         // below the centerline when the superclass is exact. We need
3862         // to do the same here.
3863         if (klass()->equals(ciEnv::current()->Object_klass()) && !klass_is_exact()) {
3864           // that is, tp's array type is a subtype of my klass
3865           return TypeAryPtr::make(ptr, (ptr == Constant ? tp->const_oop() : NULL),
3866                                   tp->ary(), tp->klass(), tp->klass_is_exact(), offset, tp->field_offset(), instance_id, speculative, depth);
3867         }
3868       }
3869       // The other case cannot happen, since I cannot be a subtype of an array.
3870       // The meet falls down to Object class below centerline.
3871       if( ptr == Constant )
3872          ptr = NotNull;
3873       instance_id = InstanceBot;
3874       return make(ptr, ciEnv::current()->Object_klass(), false, NULL, offset, instance_id, speculative, depth);
3875     default: typerr(t);
3876     }
3877   }
3878 
3879   case OopPtr: {                // Meeting to OopPtrs
3880     // Found a OopPtr type vs self-InstPtr type
3881     const TypeOopPtr *tp = t->is_oopptr();
3882     Offset offset = meet_offset(tp->offset());
3883     PTR ptr = meet_ptr(tp->ptr());
3884     switch (tp->ptr()) {
3885     case TopPTR:
3886     case AnyNull: {
3887       int instance_id = meet_instance_id(InstanceTop);
3888       const TypePtr* speculative = xmeet_speculative(tp);
3889       int depth = meet_inline_depth(tp->inline_depth());
3890       return make(ptr, klass(), klass_is_exact(),
3891                   (ptr == Constant ? const_oop() : NULL), offset, instance_id, speculative, depth);
3892     }
3893     case NotNull:
3894     case BotPTR: {
3895       int instance_id = meet_instance_id(tp->instance_id());
3896       const TypePtr* speculative = xmeet_speculative(tp);
3897       int depth = meet_inline_depth(tp->inline_depth());
3898       return TypeOopPtr::make(ptr, offset, instance_id, speculative, depth);
3899     }
3900     default: typerr(t);
3901     }
3902   }
3903 
3904   case AnyPtr: {                // Meeting to AnyPtrs
3905     // Found an AnyPtr type vs self-InstPtr type
3906     const TypePtr *tp = t->is_ptr();
3907     Offset offset = meet_offset(tp->offset());
3908     PTR ptr = meet_ptr(tp->ptr());
3909     int instance_id = meet_instance_id(InstanceTop);
3910     const TypePtr* speculative = xmeet_speculative(tp);
3911     int depth = meet_inline_depth(tp->inline_depth());
3912     switch (tp->ptr()) {
3913     case Null:
3914       if( ptr == Null ) return TypePtr::make(AnyPtr, ptr, offset, speculative, depth);
3915       // else fall through to AnyNull
3916     case TopPTR:
3917     case AnyNull: {
3918       return make(ptr, klass(), klass_is_exact(),
3919                   (ptr == Constant ? const_oop() : NULL), offset, instance_id, speculative, depth);
3920     }
3921     case NotNull:
3922     case BotPTR:
3923       return TypePtr::make(AnyPtr, ptr, offset, speculative,depth);
3924     default: typerr(t);
3925     }
3926   }
3927 
3928   /*
3929                  A-top         }
3930                /   |   \       }  Tops
3931            B-top A-any C-top   }
3932               | /  |  \ |      }  Any-nulls
3933            B-any   |   C-any   }
3934               |    |    |
3935            B-con A-con C-con   } constants; not comparable across classes
3936               |    |    |
3937            B-not   |   C-not   }
3938               | \  |  / |      }  not-nulls
3939            B-bot A-not C-bot   }
3940                \   |   /       }  Bottoms
3941                  A-bot         }
3942   */
3943 
3944   case InstPtr: {                // Meeting 2 Oops?
3945     // Found an InstPtr sub-type vs self-InstPtr type
3946     const TypeInstPtr *tinst = t->is_instptr();
3947     Offset off = meet_offset( tinst->offset() );
3948     PTR ptr = meet_ptr( tinst->ptr() );
3949     int instance_id = meet_instance_id(tinst->instance_id());
3950     const TypePtr* speculative = xmeet_speculative(tinst);
3951     int depth = meet_inline_depth(tinst->inline_depth());
3952 
3953     // Check for easy case; klasses are equal (and perhaps not loaded!)
3954     // If we have constants, then we created oops so classes are loaded
3955     // and we can handle the constants further down.  This case handles
3956     // both-not-loaded or both-loaded classes
3957     if (ptr != Constant && klass()->equals(tinst->klass()) && klass_is_exact() == tinst->klass_is_exact()) {
3958       return make(ptr, klass(), klass_is_exact(), NULL, off, instance_id, speculative, depth);
3959     }
3960 
3961     // Classes require inspection in the Java klass hierarchy.  Must be loaded.
3962     ciKlass* tinst_klass = tinst->klass();
3963     ciKlass* this_klass  = this->klass();
3964     bool tinst_xk = tinst->klass_is_exact();
3965     bool this_xk  = this->klass_is_exact();
3966     if (!tinst_klass->is_loaded() || !this_klass->is_loaded() ) {
3967       // One of these classes has not been loaded
3968       const TypeInstPtr *unloaded_meet = xmeet_unloaded(tinst);
3969 #ifndef PRODUCT
3970       if( PrintOpto && Verbose ) {
3971         tty->print("meet of unloaded classes resulted in: "); unloaded_meet->dump(); tty->cr();
3972         tty->print("  this == "); this->dump(); tty->cr();
3973         tty->print(" tinst == "); tinst->dump(); tty->cr();
3974       }
3975 #endif
3976       return unloaded_meet;
3977     }
3978 
3979     // Handle mixing oops and interfaces first.
3980     if( this_klass->is_interface() && !(tinst_klass->is_interface() ||
3981                                         tinst_klass == ciEnv::current()->Object_klass())) {
3982       ciKlass *tmp = tinst_klass; // Swap interface around
3983       tinst_klass = this_klass;
3984       this_klass = tmp;
3985       bool tmp2 = tinst_xk;
3986       tinst_xk = this_xk;
3987       this_xk = tmp2;
3988     }
3989     if (tinst_klass->is_interface() &&
3990         !(this_klass->is_interface() ||
3991           // Treat java/lang/Object as an honorary interface,
3992           // because we need a bottom for the interface hierarchy.
3993           this_klass == ciEnv::current()->Object_klass())) {
3994       // Oop meets interface!
3995 
3996       // See if the oop subtypes (implements) interface.
3997       ciKlass *k;
3998       bool xk;
3999       if( this_klass->is_subtype_of( tinst_klass ) ) {
4000         // Oop indeed subtypes.  Now keep oop or interface depending
4001         // on whether we are both above the centerline or either is
4002         // below the centerline.  If we are on the centerline
4003         // (e.g., Constant vs. AnyNull interface), use the constant.
4004         k  = below_centerline(ptr) ? tinst_klass : this_klass;
4005         // If we are keeping this_klass, keep its exactness too.
4006         xk = below_centerline(ptr) ? tinst_xk    : this_xk;
4007       } else {                  // Does not implement, fall to Object
4008         // Oop does not implement interface, so mixing falls to Object
4009         // just like the verifier does (if both are above the
4010         // centerline fall to interface)
4011         k = above_centerline(ptr) ? tinst_klass : ciEnv::current()->Object_klass();
4012         xk = above_centerline(ptr) ? tinst_xk : false;
4013         // Watch out for Constant vs. AnyNull interface.
4014         if (ptr == Constant)  ptr = NotNull;   // forget it was a constant
4015         instance_id = InstanceBot;
4016       }
4017       ciObject* o = NULL;  // the Constant value, if any
4018       if (ptr == Constant) {
4019         // Find out which constant.
4020         o = (this_klass == klass()) ? const_oop() : tinst->const_oop();
4021       }
4022       return make(ptr, k, xk, o, off, instance_id, speculative, depth);
4023     }
4024 
4025     // Either oop vs oop or interface vs interface or interface vs Object
4026 
4027     // !!! Here's how the symmetry requirement breaks down into invariants:
4028     // If we split one up & one down AND they subtype, take the down man.
4029     // If we split one up & one down AND they do NOT subtype, "fall hard".
4030     // If both are up and they subtype, take the subtype class.
4031     // If both are up and they do NOT subtype, "fall hard".
4032     // If both are down and they subtype, take the supertype class.
4033     // If both are down and they do NOT subtype, "fall hard".
4034     // Constants treated as down.
4035 
4036     // Now, reorder the above list; observe that both-down+subtype is also
4037     // "fall hard"; "fall hard" becomes the default case:
4038     // If we split one up & one down AND they subtype, take the down man.
4039     // If both are up and they subtype, take the subtype class.
4040 
4041     // If both are down and they subtype, "fall hard".
4042     // If both are down and they do NOT subtype, "fall hard".
4043     // If both are up and they do NOT subtype, "fall hard".
4044     // If we split one up & one down AND they do NOT subtype, "fall hard".
4045 
4046     // If a proper subtype is exact, and we return it, we return it exactly.
4047     // If a proper supertype is exact, there can be no subtyping relationship!
4048     // If both types are equal to the subtype, exactness is and-ed below the
4049     // centerline and or-ed above it.  (N.B. Constants are always exact.)
4050 
4051     // Check for subtyping:
4052     ciKlass *subtype = NULL;
4053     bool subtype_exact = false;
4054     if( tinst_klass->equals(this_klass) ) {
4055       subtype = this_klass;
4056       subtype_exact = below_centerline(ptr) ? (this_xk & tinst_xk) : (this_xk | tinst_xk);
4057     } else if( !tinst_xk && this_klass->is_subtype_of( tinst_klass ) ) {
4058       subtype = this_klass;     // Pick subtyping class
4059       subtype_exact = this_xk;
4060     } else if( !this_xk && tinst_klass->is_subtype_of( this_klass ) ) {
4061       subtype = tinst_klass;    // Pick subtyping class
4062       subtype_exact = tinst_xk;
4063     }
4064 
4065     if( subtype ) {
4066       if( above_centerline(ptr) ) { // both are up?
4067         this_klass = tinst_klass = subtype;
4068         this_xk = tinst_xk = subtype_exact;
4069       } else if( above_centerline(this ->_ptr) && !above_centerline(tinst->_ptr) ) {
4070         this_klass = tinst_klass; // tinst is down; keep down man
4071         this_xk = tinst_xk;
4072       } else if( above_centerline(tinst->_ptr) && !above_centerline(this ->_ptr) ) {
4073         tinst_klass = this_klass; // this is down; keep down man
4074         tinst_xk = this_xk;
4075       } else {
4076         this_xk = subtype_exact;  // either they are equal, or we'll do an LCA
4077       }
4078     }
4079 
4080     // Check for classes now being equal
4081     if (tinst_klass->equals(this_klass)) {
4082       // If the klasses are equal, the constants may still differ.  Fall to
4083       // NotNull if they do (neither constant is NULL; that is a special case
4084       // handled elsewhere).
4085       ciObject* o = NULL;             // Assume not constant when done
4086       ciObject* this_oop  = const_oop();
4087       ciObject* tinst_oop = tinst->const_oop();
4088       if( ptr == Constant ) {
4089         if (this_oop != NULL && tinst_oop != NULL &&
4090             this_oop->equals(tinst_oop) )
4091           o = this_oop;
4092         else if (above_centerline(this ->_ptr))
4093           o = tinst_oop;
4094         else if (above_centerline(tinst ->_ptr))
4095           o = this_oop;
4096         else
4097           ptr = NotNull;
4098       }
4099       return make(ptr, this_klass, this_xk, o, off, instance_id, speculative, depth);
4100     } // Else classes are not equal
4101 
4102     // Since klasses are different, we require a LCA in the Java
4103     // class hierarchy - which means we have to fall to at least NotNull.
4104     if( ptr == TopPTR || ptr == AnyNull || ptr == Constant )
4105       ptr = NotNull;
4106 
4107     instance_id = InstanceBot;
4108 
4109     // Now we find the LCA of Java classes
4110     ciKlass* k = this_klass->least_common_ancestor(tinst_klass);
4111     return make(ptr, k, false, NULL, off, instance_id, speculative, depth);
4112   } // End of case InstPtr
4113 
4114   } // End of switch
4115   return this;                  // Return the double constant
4116 }
4117 
4118 
4119 //------------------------java_mirror_type--------------------------------------
4120 ciType* TypeInstPtr::java_mirror_type() const {
4121   // must be a singleton type
4122   if( const_oop() == NULL )  return NULL;
4123 
4124   // must be of type java.lang.Class
4125   if( klass() != ciEnv::current()->Class_klass() )  return NULL;
4126 
4127   return const_oop()->as_instance()->java_mirror_type();
4128 }
4129 
4130 
4131 //------------------------------xdual------------------------------------------
4132 // Dual: do NOT dual on klasses.  This means I do NOT understand the Java
4133 // inheritance mechanism.
4134 const Type *TypeInstPtr::xdual() const {
4135   return new TypeInstPtr(dual_ptr(), klass(), klass_is_exact(), const_oop(), dual_offset(), dual_instance_id(), dual_speculative(), dual_inline_depth());
4136 }
4137 
4138 //------------------------------eq---------------------------------------------
4139 // Structural equality check for Type representations
4140 bool TypeInstPtr::eq( const Type *t ) const {
4141   const TypeInstPtr *p = t->is_instptr();
4142   return
4143     klass()->equals(p->klass()) &&
4144     TypeOopPtr::eq(p);          // Check sub-type stuff
4145 }
4146 
4147 //------------------------------hash-------------------------------------------
4148 // Type-specific hashing function.
4149 int TypeInstPtr::hash(void) const {
4150   int hash = java_add(klass()->hash(), TypeOopPtr::hash());
4151   return hash;
4152 }
4153 
4154 //------------------------------dump2------------------------------------------
4155 // Dump oop Type
4156 #ifndef PRODUCT
4157 void TypeInstPtr::dump2( Dict &d, uint depth, outputStream *st ) const {
4158   // Print the name of the klass.
4159   klass()->print_name_on(st);
4160 
4161   switch( _ptr ) {
4162   case Constant:
4163     // TO DO: Make CI print the hex address of the underlying oop.
4164     if (WizardMode || Verbose) {
4165       const_oop()->print_oop(st);
4166     }
4167   case BotPTR:
4168     if (!WizardMode && !Verbose) {
4169       if( _klass_is_exact ) st->print(":exact");
4170       break;
4171     }
4172   case TopPTR:
4173   case AnyNull:
4174   case NotNull:
4175     st->print(":%s", ptr_msg[_ptr]);
4176     if( _klass_is_exact ) st->print(":exact");
4177     break;
4178   }
4179 
4180   _offset.dump2(st);
4181 
4182   st->print(" *");
4183   if (_instance_id == InstanceTop)
4184     st->print(",iid=top");
4185   else if (_instance_id != InstanceBot)
4186     st->print(",iid=%d",_instance_id);
4187 
4188   dump_inline_depth(st);
4189   dump_speculative(st);
4190 }
4191 #endif
4192 
4193 //------------------------------add_offset-------------------------------------
4194 const TypePtr *TypeInstPtr::add_offset(intptr_t offset) const {
4195   return make(_ptr, klass(), klass_is_exact(), const_oop(), xadd_offset(offset),
4196               _instance_id, add_offset_speculative(offset), _inline_depth);
4197 }
4198 
4199 const Type *TypeInstPtr::remove_speculative() const {
4200   if (_speculative == NULL) {
4201     return this;
4202   }
4203   assert(_inline_depth == InlineDepthTop || _inline_depth == InlineDepthBottom, "non speculative type shouldn't have inline depth");
4204   return make(_ptr, klass(), klass_is_exact(), const_oop(), _offset,
4205               _instance_id, NULL, _inline_depth);
4206 }
4207 
4208 const TypePtr *TypeInstPtr::with_inline_depth(int depth) const {
4209   if (!UseInlineDepthForSpeculativeTypes) {
4210     return this;
4211   }
4212   return make(_ptr, klass(), klass_is_exact(), const_oop(), _offset, _instance_id, _speculative, depth);
4213 }
4214 
4215 //=============================================================================
4216 // Convenience common pre-built types.
4217 const TypeAryPtr *TypeAryPtr::RANGE;
4218 const TypeAryPtr *TypeAryPtr::OOPS;
4219 const TypeAryPtr *TypeAryPtr::NARROWOOPS;
4220 const TypeAryPtr *TypeAryPtr::BYTES;
4221 const TypeAryPtr *TypeAryPtr::SHORTS;
4222 const TypeAryPtr *TypeAryPtr::CHARS;
4223 const TypeAryPtr *TypeAryPtr::INTS;
4224 const TypeAryPtr *TypeAryPtr::LONGS;
4225 const TypeAryPtr *TypeAryPtr::FLOATS;
4226 const TypeAryPtr *TypeAryPtr::DOUBLES;
4227 
4228 //------------------------------make-------------------------------------------
4229 const TypeAryPtr* TypeAryPtr::make(PTR ptr, const TypeAry *ary, ciKlass* k, bool xk, Offset offset, Offset field_offset,
4230                                    int instance_id, const TypePtr* speculative, int inline_depth) {
4231   assert(!(k == NULL && ary->_elem->isa_int()),
4232          "integral arrays must be pre-equipped with a class");
4233   if (!xk)  xk = ary->ary_must_be_exact();
4234   assert(instance_id <= 0 || xk || !UseExactTypes, "instances are always exactly typed");
4235   if (!UseExactTypes)  xk = (ptr == Constant);
4236   return (TypeAryPtr*)(new TypeAryPtr(ptr, NULL, ary, k, xk, offset, field_offset, instance_id, false, speculative, inline_depth))->hashcons();
4237 }
4238 
4239 //------------------------------make-------------------------------------------
4240 const TypeAryPtr* TypeAryPtr::make(PTR ptr, ciObject* o, const TypeAry *ary, ciKlass* k, bool xk, Offset offset, Offset field_offset,
4241                                    int instance_id, const TypePtr* speculative, int inline_depth,
4242                                    bool is_autobox_cache) {
4243   assert(!(k == NULL && ary->_elem->isa_int()),
4244          "integral arrays must be pre-equipped with a class");
4245   assert( (ptr==Constant && o) || (ptr!=Constant && !o), "" );
4246   if (!xk)  xk = (o != NULL) || ary->ary_must_be_exact();
4247   assert(instance_id <= 0 || xk || !UseExactTypes, "instances are always exactly typed");
4248   if (!UseExactTypes)  xk = (ptr == Constant);
4249   return (TypeAryPtr*)(new TypeAryPtr(ptr, o, ary, k, xk, offset, field_offset, instance_id, is_autobox_cache, speculative, inline_depth))->hashcons();
4250 }
4251 
4252 //------------------------------cast_to_ptr_type-------------------------------
4253 const Type *TypeAryPtr::cast_to_ptr_type(PTR ptr) const {
4254   if( ptr == _ptr ) return this;
4255   return make(ptr, const_oop(), _ary, klass(), klass_is_exact(), _offset, _field_offset, _instance_id, _speculative, _inline_depth, _is_autobox_cache);
4256 }
4257 
4258 
4259 //-----------------------------cast_to_exactness-------------------------------
4260 const Type *TypeAryPtr::cast_to_exactness(bool klass_is_exact) const {
4261   if( klass_is_exact == _klass_is_exact ) return this;
4262   if (!UseExactTypes)  return this;
4263   if (_ary->ary_must_be_exact())  return this;  // cannot clear xk
4264   return make(ptr(), const_oop(), _ary, klass(), klass_is_exact, _offset, _field_offset, _instance_id, _speculative, _inline_depth, _is_autobox_cache);
4265 }
4266 
4267 //-----------------------------cast_to_instance_id----------------------------
4268 const TypeOopPtr *TypeAryPtr::cast_to_instance_id(int instance_id) const {
4269   if( instance_id == _instance_id ) return this;
4270   return make(_ptr, const_oop(), _ary, klass(), _klass_is_exact, _offset, _field_offset, instance_id, _speculative, _inline_depth, _is_autobox_cache);
4271 }
4272 
4273 //-----------------------------narrow_size_type-------------------------------
4274 // Local cache for arrayOopDesc::max_array_length(etype),
4275 // which is kind of slow (and cached elsewhere by other users).
4276 static jint max_array_length_cache[T_CONFLICT+1];
4277 static jint max_array_length(BasicType etype) {
4278   jint& cache = max_array_length_cache[etype];
4279   jint res = cache;
4280   if (res == 0) {
4281     switch (etype) {
4282     case T_NARROWOOP:
4283       etype = T_OBJECT;
4284       break;
4285     case T_NARROWKLASS:
4286     case T_CONFLICT:
4287     case T_ILLEGAL:
4288     case T_VOID:
4289       etype = T_BYTE;           // will produce conservatively high value
4290     }
4291     cache = res = arrayOopDesc::max_array_length(etype);
4292   }
4293   return res;
4294 }
4295 
4296 // Narrow the given size type to the index range for the given array base type.
4297 // Return NULL if the resulting int type becomes empty.
4298 const TypeInt* TypeAryPtr::narrow_size_type(const TypeInt* size) const {
4299   jint hi = size->_hi;
4300   jint lo = size->_lo;
4301   jint min_lo = 0;
4302   jint max_hi = max_array_length(elem()->basic_type());
4303   //if (index_not_size)  --max_hi;     // type of a valid array index, FTR
4304   bool chg = false;
4305   if (lo < min_lo) {
4306     lo = min_lo;
4307     if (size->is_con()) {
4308       hi = lo;
4309     }
4310     chg = true;
4311   }
4312   if (hi > max_hi) {
4313     hi = max_hi;
4314     if (size->is_con()) {
4315       lo = hi;
4316     }
4317     chg = true;
4318   }
4319   // Negative length arrays will produce weird intermediate dead fast-path code
4320   if (lo > hi)
4321     return TypeInt::ZERO;
4322   if (!chg)
4323     return size;
4324   return TypeInt::make(lo, hi, Type::WidenMin);
4325 }
4326 
4327 //-------------------------------cast_to_size----------------------------------
4328 const TypeAryPtr* TypeAryPtr::cast_to_size(const TypeInt* new_size) const {
4329   assert(new_size != NULL, "");
4330   new_size = narrow_size_type(new_size);
4331   if (new_size == size())  return this;
4332   const TypeAry* new_ary = TypeAry::make(elem(), new_size, is_stable());
4333   return make(ptr(), const_oop(), new_ary, klass(), klass_is_exact(), _offset, _field_offset, _instance_id, _speculative, _inline_depth, _is_autobox_cache);
4334 }
4335 
4336 //------------------------------cast_to_stable---------------------------------
4337 const TypeAryPtr* TypeAryPtr::cast_to_stable(bool stable, int stable_dimension) const {
4338   if (stable_dimension <= 0 || (stable_dimension == 1 && stable == this->is_stable()))
4339     return this;
4340 
4341   const Type* elem = this->elem();
4342   const TypePtr* elem_ptr = elem->make_ptr();
4343 
4344   if (stable_dimension > 1 && elem_ptr != NULL && elem_ptr->isa_aryptr()) {
4345     // If this is widened from a narrow oop, TypeAry::make will re-narrow it.
4346     elem = elem_ptr = elem_ptr->is_aryptr()->cast_to_stable(stable, stable_dimension - 1);
4347   }
4348 
4349   const TypeAry* new_ary = TypeAry::make(elem, size(), stable);
4350 
4351   return make(ptr(), const_oop(), new_ary, klass(), klass_is_exact(), _offset, _field_offset, _instance_id, _speculative, _inline_depth, _is_autobox_cache);
4352 }
4353 
4354 //-----------------------------stable_dimension--------------------------------
4355 int TypeAryPtr::stable_dimension() const {
4356   if (!is_stable())  return 0;
4357   int dim = 1;
4358   const TypePtr* elem_ptr = elem()->make_ptr();
4359   if (elem_ptr != NULL && elem_ptr->isa_aryptr())
4360     dim += elem_ptr->is_aryptr()->stable_dimension();
4361   return dim;
4362 }
4363 
4364 //----------------------cast_to_autobox_cache-----------------------------------
4365 const TypeAryPtr* TypeAryPtr::cast_to_autobox_cache(bool cache) const {
4366   if (is_autobox_cache() == cache)  return this;
4367   const TypeOopPtr* etype = elem()->make_oopptr();
4368   if (etype == NULL)  return this;
4369   // The pointers in the autobox arrays are always non-null.
4370   TypePtr::PTR ptr_type = cache ? TypePtr::NotNull : TypePtr::AnyNull;
4371   etype = etype->cast_to_ptr_type(TypePtr::NotNull)->is_oopptr();
4372   const TypeAry* new_ary = TypeAry::make(etype, size(), is_stable());
4373   return make(ptr(), const_oop(), new_ary, klass(), klass_is_exact(), _offset, _field_offset, _instance_id, _speculative, _inline_depth, cache);
4374 }
4375 
4376 //------------------------------eq---------------------------------------------
4377 // Structural equality check for Type representations
4378 bool TypeAryPtr::eq( const Type *t ) const {
4379   const TypeAryPtr *p = t->is_aryptr();
4380   return
4381     _ary == p->_ary &&  // Check array
4382     TypeOopPtr::eq(p) &&// Check sub-parts
4383     _field_offset == p->_field_offset;
4384 }
4385 
4386 //------------------------------hash-------------------------------------------
4387 // Type-specific hashing function.
4388 int TypeAryPtr::hash(void) const {
4389   return (intptr_t)_ary + TypeOopPtr::hash() + _field_offset.get();
4390 }
4391 
4392 //------------------------------meet-------------------------------------------
4393 // Compute the MEET of two types.  It returns a new Type object.
4394 const Type *TypeAryPtr::xmeet_helper(const Type *t) const {
4395   // Perform a fast test for common case; meeting the same types together.
4396   if( this == t ) return this;  // Meeting same type-rep?
4397   // Current "this->_base" is Pointer
4398   switch (t->base()) {          // switch on original type
4399 
4400   // Mixing ints & oops happens when javac reuses local variables
4401   case Int:
4402   case Long:
4403   case FloatTop:
4404   case FloatCon:
4405   case FloatBot:
4406   case DoubleTop:
4407   case DoubleCon:
4408   case DoubleBot:
4409   case NarrowOop:
4410   case NarrowKlass:
4411   case Bottom:                  // Ye Olde Default
4412     return Type::BOTTOM;
4413   case Top:
4414     return this;
4415 
4416   default:                      // All else is a mistake
4417     typerr(t);
4418 
4419   case OopPtr: {                // Meeting to OopPtrs
4420     // Found a OopPtr type vs self-AryPtr type
4421     const TypeOopPtr *tp = t->is_oopptr();
4422     Offset offset = meet_offset(tp->offset());
4423     PTR ptr = meet_ptr(tp->ptr());
4424     int depth = meet_inline_depth(tp->inline_depth());
4425     const TypePtr* speculative = xmeet_speculative(tp);
4426     switch (tp->ptr()) {
4427     case TopPTR:
4428     case AnyNull: {
4429       int instance_id = meet_instance_id(InstanceTop);
4430       return make(ptr, (ptr == Constant ? const_oop() : NULL),
4431                   _ary, _klass, _klass_is_exact, offset, _field_offset, instance_id, speculative, depth);
4432     }
4433     case BotPTR:
4434     case NotNull: {
4435       int instance_id = meet_instance_id(tp->instance_id());
4436       return TypeOopPtr::make(ptr, offset, instance_id, speculative, depth);
4437     }
4438     default: ShouldNotReachHere();
4439     }
4440   }
4441 
4442   case AnyPtr: {                // Meeting two AnyPtrs
4443     // Found an AnyPtr type vs self-AryPtr type
4444     const TypePtr *tp = t->is_ptr();
4445     Offset offset = meet_offset(tp->offset());
4446     PTR ptr = meet_ptr(tp->ptr());
4447     const TypePtr* speculative = xmeet_speculative(tp);
4448     int depth = meet_inline_depth(tp->inline_depth());
4449     switch (tp->ptr()) {
4450     case TopPTR:
4451       return this;
4452     case BotPTR:
4453     case NotNull:
4454       return TypePtr::make(AnyPtr, ptr, offset, speculative, depth);
4455     case Null:
4456       if( ptr == Null ) return TypePtr::make(AnyPtr, ptr, offset, speculative, depth);
4457       // else fall through to AnyNull
4458     case AnyNull: {
4459       int instance_id = meet_instance_id(InstanceTop);
4460       return make(ptr, (ptr == Constant ? const_oop() : NULL),
4461                   _ary, _klass, _klass_is_exact, offset, _field_offset, instance_id, speculative, depth);
4462     }
4463     default: ShouldNotReachHere();
4464     }
4465   }
4466 
4467   case MetadataPtr:
4468   case KlassPtr:
4469   case RawPtr: return TypePtr::BOTTOM;
4470 
4471   case AryPtr: {                // Meeting 2 references?
4472     const TypeAryPtr *tap = t->is_aryptr();
4473     Offset off = meet_offset(tap->offset());
4474     Offset field_off = meet_field_offset(tap->field_offset());
4475     const TypeAry *tary = _ary->meet_speculative(tap->_ary)->is_ary();
4476     PTR ptr = meet_ptr(tap->ptr());
4477     int instance_id = meet_instance_id(tap->instance_id());
4478     const TypePtr* speculative = xmeet_speculative(tap);
4479     int depth = meet_inline_depth(tap->inline_depth());
4480     ciKlass* lazy_klass = NULL;
4481     if (tary->_elem->isa_int()) {
4482       // Integral array element types have irrelevant lattice relations.
4483       // It is the klass that determines array layout, not the element type.
4484       if (_klass == NULL)
4485         lazy_klass = tap->_klass;
4486       else if (tap->_klass == NULL || tap->_klass == _klass) {
4487         lazy_klass = _klass;
4488       } else {
4489         // Something like byte[int+] meets char[int+].
4490         // This must fall to bottom, not (int[-128..65535])[int+].
4491         instance_id = InstanceBot;
4492         tary = TypeAry::make(Type::BOTTOM, tary->_size, tary->_stable);
4493       }
4494     } else // Non integral arrays.
4495       // Must fall to bottom if exact klasses in upper lattice
4496       // are not equal or super klass is exact.
4497       if ((above_centerline(ptr) || ptr == Constant) && klass() != tap->klass() &&
4498           // meet with top[] and bottom[] are processed further down:
4499           tap->_klass != NULL  && this->_klass != NULL   &&
4500           // both are exact and not equal:
4501           ((tap->_klass_is_exact && this->_klass_is_exact) ||
4502            // 'tap'  is exact and super or unrelated:
4503            (tap->_klass_is_exact && !tap->klass()->is_subtype_of(klass())) ||
4504            // 'this' is exact and super or unrelated:
4505            (this->_klass_is_exact && !klass()->is_subtype_of(tap->klass())))) {
4506       if (above_centerline(ptr)) {
4507         tary = TypeAry::make(Type::BOTTOM, tary->_size, tary->_stable);
4508       }
4509       return make(NotNull, NULL, tary, lazy_klass, false, off, field_off, InstanceBot, speculative, depth);
4510     }
4511 
4512     bool xk = false;
4513     switch (tap->ptr()) {
4514     case AnyNull:
4515     case TopPTR:
4516       // Compute new klass on demand, do not use tap->_klass
4517       if (below_centerline(this->_ptr)) {
4518         xk = this->_klass_is_exact;
4519       } else {
4520         xk = (tap->_klass_is_exact | this->_klass_is_exact);
4521       }
4522       return make(ptr, const_oop(), tary, lazy_klass, xk, off, field_off, instance_id, speculative, depth);
4523     case Constant: {
4524       ciObject* o = const_oop();
4525       if( _ptr == Constant ) {
4526         if( tap->const_oop() != NULL && !o->equals(tap->const_oop()) ) {
4527           xk = (klass() == tap->klass());
4528           ptr = NotNull;
4529           o = NULL;
4530           instance_id = InstanceBot;
4531         } else {
4532           xk = true;
4533         }
4534       } else if(above_centerline(_ptr)) {
4535         o = tap->const_oop();
4536         xk = true;
4537       } else {
4538         // Only precise for identical arrays
4539         xk = this->_klass_is_exact && (klass() == tap->klass());
4540       }
4541       return TypeAryPtr::make(ptr, o, tary, lazy_klass, xk, off, field_off, instance_id, speculative, depth);
4542     }
4543     case NotNull:
4544     case BotPTR:
4545       // Compute new klass on demand, do not use tap->_klass
4546       if (above_centerline(this->_ptr))
4547             xk = tap->_klass_is_exact;
4548       else  xk = (tap->_klass_is_exact & this->_klass_is_exact) &&
4549               (klass() == tap->klass()); // Only precise for identical arrays
4550       return TypeAryPtr::make(ptr, NULL, tary, lazy_klass, xk, off, field_off, instance_id, speculative, depth);
4551     default: ShouldNotReachHere();
4552     }
4553   }
4554 
4555   // All arrays inherit from Object class
4556   case InstPtr: {
4557     const TypeInstPtr *tp = t->is_instptr();
4558     Offset offset = meet_offset(tp->offset());
4559     PTR ptr = meet_ptr(tp->ptr());
4560     int instance_id = meet_instance_id(tp->instance_id());
4561     const TypePtr* speculative = xmeet_speculative(tp);
4562     int depth = meet_inline_depth(tp->inline_depth());
4563     switch (ptr) {
4564     case TopPTR:
4565     case AnyNull:                // Fall 'down' to dual of object klass
4566       // For instances when a subclass meets a superclass we fall
4567       // below the centerline when the superclass is exact. We need to
4568       // do the same here.
4569       if (tp->klass()->equals(ciEnv::current()->Object_klass()) && !tp->klass_is_exact()) {
4570         return TypeAryPtr::make(ptr, _ary, _klass, _klass_is_exact, offset, _field_offset, instance_id, speculative, depth);
4571       } else {
4572         // cannot subclass, so the meet has to fall badly below the centerline
4573         ptr = NotNull;
4574         instance_id = InstanceBot;
4575         return TypeInstPtr::make(ptr, ciEnv::current()->Object_klass(), false, NULL, offset, instance_id, speculative, depth);
4576       }
4577     case Constant:
4578     case NotNull:
4579     case BotPTR:                // Fall down to object klass
4580       // LCA is object_klass, but if we subclass from the top we can do better
4581       if (above_centerline(tp->ptr())) {
4582         // If 'tp'  is above the centerline and it is Object class
4583         // then we can subclass in the Java class hierarchy.
4584         // For instances when a subclass meets a superclass we fall
4585         // below the centerline when the superclass is exact. We need
4586         // to do the same here.
4587         if (tp->klass()->equals(ciEnv::current()->Object_klass()) && !tp->klass_is_exact()) {
4588           // that is, my array type is a subtype of 'tp' klass
4589           return make(ptr, (ptr == Constant ? const_oop() : NULL),
4590                       _ary, _klass, _klass_is_exact, offset, _field_offset, instance_id, speculative, depth);
4591         }
4592       }
4593       // The other case cannot happen, since t cannot be a subtype of an array.
4594       // The meet falls down to Object class below centerline.
4595       if( ptr == Constant )
4596          ptr = NotNull;
4597       instance_id = InstanceBot;
4598       return TypeInstPtr::make(ptr, ciEnv::current()->Object_klass(), false, NULL,offset, instance_id, speculative, depth);
4599     default: typerr(t);
4600     }
4601   }
4602   }
4603   return this;                  // Lint noise
4604 }
4605 
4606 //------------------------------xdual------------------------------------------
4607 // Dual: compute field-by-field dual
4608 const Type *TypeAryPtr::xdual() const {
4609   return new TypeAryPtr(dual_ptr(), _const_oop, _ary->dual()->is_ary(), _klass, _klass_is_exact, dual_offset(), dual_field_offset(), dual_instance_id(), is_autobox_cache(), dual_speculative(), dual_inline_depth());
4610 }
4611 
4612 Type::Offset TypeAryPtr::meet_field_offset(const Type::Offset offset) const {
4613   return _field_offset.meet(offset);
4614 }
4615 
4616 //------------------------------dual_offset------------------------------------
4617 Type::Offset TypeAryPtr::dual_field_offset() const {
4618   return _field_offset.dual();
4619 }
4620 
4621 //----------------------interface_vs_oop---------------------------------------
4622 #ifdef ASSERT
4623 bool TypeAryPtr::interface_vs_oop(const Type *t) const {
4624   const TypeAryPtr* t_aryptr = t->isa_aryptr();
4625   if (t_aryptr) {
4626     return _ary->interface_vs_oop(t_aryptr->_ary);
4627   }
4628   return false;
4629 }
4630 #endif
4631 
4632 //------------------------------dump2------------------------------------------
4633 #ifndef PRODUCT
4634 void TypeAryPtr::dump2( Dict &d, uint depth, outputStream *st ) const {
4635   _ary->dump2(d,depth,st);
4636   switch( _ptr ) {
4637   case Constant:
4638     const_oop()->print(st);
4639     break;
4640   case BotPTR:
4641     if (!WizardMode && !Verbose) {
4642       if( _klass_is_exact ) st->print(":exact");
4643       break;
4644     }
4645   case TopPTR:
4646   case AnyNull:
4647   case NotNull:
4648     st->print(":%s", ptr_msg[_ptr]);
4649     if( _klass_is_exact ) st->print(":exact");
4650     break;
4651   }
4652 
4653   if (elem()->isa_valuetype()) {
4654     st->print("(");
4655     _field_offset.dump2(st);
4656     st->print(")");
4657   }
4658   if (offset() != 0) {
4659     int header_size = objArrayOopDesc::header_size() * wordSize;
4660     if( _offset == Offset::top )       st->print("+undefined");
4661     else if( _offset == Offset::bottom )  st->print("+any");
4662     else if( offset() < header_size ) st->print("+%d", offset());
4663     else {
4664       BasicType basic_elem_type = elem()->basic_type();
4665       int array_base = arrayOopDesc::base_offset_in_bytes(basic_elem_type);
4666       int elem_size = type2aelembytes(basic_elem_type);
4667       st->print("[%d]", (offset() - array_base)/elem_size);
4668     }
4669   }
4670   st->print(" *");
4671   if (_instance_id == InstanceTop)
4672     st->print(",iid=top");
4673   else if (_instance_id != InstanceBot)
4674     st->print(",iid=%d",_instance_id);
4675 
4676   dump_inline_depth(st);
4677   dump_speculative(st);
4678 }
4679 #endif
4680 
4681 bool TypeAryPtr::empty(void) const {
4682   if (_ary->empty())       return true;
4683   return TypeOopPtr::empty();
4684 }
4685 
4686 //------------------------------add_offset-------------------------------------
4687 const TypePtr *TypeAryPtr::add_offset(intptr_t offset) const {
4688   return make(_ptr, _const_oop, _ary, _klass, _klass_is_exact, xadd_offset(offset), _field_offset, _instance_id, add_offset_speculative(offset), _inline_depth, _is_autobox_cache);
4689 }
4690 
4691 const Type *TypeAryPtr::remove_speculative() const {
4692   if (_speculative == NULL) {
4693     return this;
4694   }
4695   assert(_inline_depth == InlineDepthTop || _inline_depth == InlineDepthBottom, "non speculative type shouldn't have inline depth");
4696   return make(_ptr, _const_oop, _ary->remove_speculative()->is_ary(), _klass, _klass_is_exact, _offset, _field_offset, _instance_id, NULL, _inline_depth, _is_autobox_cache);
4697 }
4698 
4699 const TypePtr *TypeAryPtr::with_inline_depth(int depth) const {
4700   if (!UseInlineDepthForSpeculativeTypes) {
4701     return this;
4702   }
4703   return make(_ptr, _const_oop, _ary->remove_speculative()->is_ary(), _klass, _klass_is_exact, _offset, _field_offset, _instance_id, _speculative, depth, _is_autobox_cache);
4704 }
4705 
4706 const TypeAryPtr* TypeAryPtr::with_field_offset(int offset) const {
4707   return make(_ptr, _const_oop, _ary->remove_speculative()->is_ary(), _klass, _klass_is_exact, _offset, Offset(offset), _instance_id, _speculative, _inline_depth, _is_autobox_cache);
4708 }
4709 
4710 const TypePtr* TypeAryPtr::with_field_offset_and_offset(intptr_t offset) const {
4711   if (offset != Type::OffsetBot) {
4712     const Type* elemtype = elem();
4713     if (elemtype->isa_valuetype()) {
4714       uint header = arrayOopDesc::base_offset_in_bytes(T_OBJECT);
4715       if (offset >= (intptr_t)header) {
4716         ciKlass* arytype_klass = klass();
4717         ciValueArrayKlass* vak = arytype_klass->as_value_array_klass();
4718         int shift = vak->log2_element_size();
4719         intptr_t field_offset = ((offset - header) & ((1 << shift) - 1));
4720 
4721         return with_field_offset(field_offset)->add_offset(offset - field_offset);
4722       }
4723     }
4724   }
4725   return add_offset(offset);
4726 }
4727 
4728 //=============================================================================
4729 
4730 
4731 //=============================================================================
4732 
4733 const TypeValueTypePtr* TypeValueTypePtr::NOTNULL;
4734 //------------------------------make-------------------------------------------
4735 const TypeValueTypePtr* TypeValueTypePtr::make(const TypeValueType* vt, PTR ptr, ciObject* o, Offset offset, int instance_id, const TypePtr* speculative, int inline_depth) {
4736   return (TypeValueTypePtr*)(new TypeValueTypePtr(vt, ptr, o, offset, instance_id, speculative, inline_depth))->hashcons();
4737 }
4738 
4739 const TypePtr* TypeValueTypePtr::add_offset(intptr_t offset) const {
4740   return make(_vt, _ptr, _const_oop, Offset(offset), _instance_id, _speculative, _inline_depth);
4741 }
4742 
4743 //------------------------------cast_to_ptr_type-------------------------------
4744 const Type* TypeValueTypePtr::cast_to_ptr_type(PTR ptr) const {
4745   if (ptr == _ptr) return this;
4746   return make(_vt, ptr, _const_oop, _offset, _instance_id, _speculative, _inline_depth);
4747 }
4748 
4749 //-----------------------------cast_to_instance_id----------------------------
4750 const TypeOopPtr* TypeValueTypePtr::cast_to_instance_id(int instance_id) const {
4751   if (instance_id == _instance_id) return this;
4752   return make(_vt, _ptr, _const_oop, _offset, instance_id, _speculative, _inline_depth);
4753 }
4754 
4755 //------------------------------meet-------------------------------------------
4756 // Compute the MEET of two types.  It returns a new Type object.
4757 const Type* TypeValueTypePtr::xmeet_helper(const Type* t) const {
4758   // Perform a fast test for common case; meeting the same types together.
4759   if (this == t) return this;  // Meeting same type-rep?
4760 
4761   switch (t->base()) {          // switch on original type
4762     case Int:                     // Mixing ints & oops happens when javac
4763     case Long:                    // reuses local variables
4764     case FloatTop:
4765     case FloatCon:
4766     case FloatBot:
4767     case DoubleTop:
4768     case DoubleCon:
4769     case DoubleBot:
4770     case NarrowOop:
4771     case NarrowKlass:
4772     case MetadataPtr:
4773     case KlassPtr:
4774     case RawPtr:
4775     case AryPtr:
4776     case InstPtr:
4777     case Bottom:                  // Ye Olde Default
4778       return Type::BOTTOM;
4779     case Top:
4780       return this;
4781 
4782     default:                      // All else is a mistake
4783       typerr(t);
4784 
4785     case OopPtr: {
4786       // Found a OopPtr type vs self-ValueTypePtr type
4787       const TypeOopPtr* tp = t->is_oopptr();
4788       Offset offset = meet_offset(tp->offset());
4789       PTR ptr = meet_ptr(tp->ptr());
4790       int instance_id = meet_instance_id(tp->instance_id());
4791       const TypePtr* speculative = xmeet_speculative(tp);
4792       int depth = meet_inline_depth(tp->inline_depth());
4793       switch (tp->ptr()) {
4794       case TopPTR:
4795       case AnyNull: {
4796         return make(_vt, ptr, NULL, offset, instance_id, speculative, depth);
4797       }
4798       case NotNull:
4799       case BotPTR: {
4800         return TypeOopPtr::make(ptr, offset, instance_id, speculative, depth);
4801       }
4802       default: typerr(t);
4803       }
4804     }
4805 
4806     case AnyPtr: {
4807       // Found an AnyPtr type vs self-ValueTypePtr type
4808       const TypePtr* tp = t->is_ptr();
4809       Offset offset = meet_offset(tp->offset());
4810       PTR ptr = meet_ptr(tp->ptr());
4811       int instance_id = meet_instance_id(InstanceTop);
4812       const TypePtr* speculative = xmeet_speculative(tp);
4813       int depth = meet_inline_depth(tp->inline_depth());
4814       switch (tp->ptr()) {
4815       case Null:
4816         if( ptr == Null ) return TypePtr::make(AnyPtr, ptr, offset, speculative, depth);
4817         // else fall through to AnyNull
4818       case TopPTR:
4819       case AnyNull: {
4820         return make(_vt, ptr, NULL, offset, instance_id, speculative, depth);
4821       }
4822       case NotNull:
4823       case BotPTR:
4824         return TypePtr::make(AnyPtr, ptr, offset, speculative, depth);
4825       default: typerr(t);
4826       }
4827     }
4828 
4829     case ValueTypePtr: {
4830       // Found an ValueTypePtr type vs self-ValueTypePtr type
4831       const TypeValueTypePtr* tp = t->is_valuetypeptr();
4832       Offset offset = meet_offset(tp->offset());
4833       PTR ptr = meet_ptr(tp->ptr());
4834       int instance_id = meet_instance_id(InstanceTop);
4835       const TypePtr* speculative = xmeet_speculative(tp);
4836       int depth = meet_inline_depth(tp->inline_depth());
4837       // Compute constant oop
4838       ciObject* o = NULL;
4839       ciObject* this_oop  = const_oop();
4840       ciObject* tp_oop = tp->const_oop();
4841       const TypeValueType* vt = NULL;
4842       if (_vt != tp->_vt) {
4843         ciKlass* __value_klass = ciEnv::current()->___Value_klass();
4844         assert(klass() == __value_klass || tp->klass() == __value_klass, "impossible meet");
4845         if (above_centerline(ptr)) {
4846           vt = klass() == __value_klass ? tp->_vt : _vt;
4847         } else if (above_centerline(this->_ptr) && !above_centerline(tp->_ptr)) {
4848           vt = tp->_vt;
4849         } else if (above_centerline(tp->_ptr) && !above_centerline(this->_ptr)) {
4850           vt = _vt;
4851         } else {
4852           vt = klass() == __value_klass ? _vt : tp->_vt;
4853         }
4854       } else {
4855         vt = _vt;
4856       }
4857       if (ptr == Constant) {
4858         if (this_oop != NULL && tp_oop != NULL &&
4859             this_oop->equals(tp_oop) ) {
4860           o = this_oop;
4861         } else if (above_centerline(this ->_ptr)) {
4862           o = tp_oop;
4863         } else if (above_centerline(tp ->_ptr)) {
4864           o = this_oop;
4865         } else {
4866           ptr = NotNull;
4867         }
4868       }
4869       return make(vt, ptr, o, offset, instance_id, speculative, depth);
4870     }
4871     }
4872 }
4873 
4874 // Dual: compute field-by-field dual
4875 const Type* TypeValueTypePtr::xdual() const {
4876   return new TypeValueTypePtr(_vt, dual_ptr(), const_oop(), dual_offset(), dual_instance_id(), dual_speculative(), dual_inline_depth());
4877 }
4878 
4879 //------------------------------eq---------------------------------------------
4880 // Structural equality check for Type representations
4881 bool TypeValueTypePtr::eq(const Type* t) const {
4882   const TypeValueTypePtr* p = t->is_valuetypeptr();
4883   return _vt->eq(p->value_type()) && TypeOopPtr::eq(p);
4884 }
4885 
4886 //------------------------------hash-------------------------------------------
4887 // Type-specific hashing function.
4888 int TypeValueTypePtr::hash(void) const {
4889   return java_add(_vt->hash(), TypeOopPtr::hash());
4890 }
4891 
4892 //------------------------------empty------------------------------------------
4893 // TRUE if Type is a type with no values, FALSE otherwise.
4894 bool TypeValueTypePtr::empty(void) const {
4895   // FIXME
4896   return false;
4897 }
4898 
4899 //------------------------------dump2------------------------------------------
4900 #ifndef PRODUCT
4901 void TypeValueTypePtr::dump2(Dict &d, uint depth, outputStream *st) const {
4902   st->print("valuetype* ");
4903   klass()->print_name_on(st);
4904   st->print(":%s", ptr_msg[_ptr]);
4905   _offset.dump2(st);
4906 }
4907 #endif
4908 
4909 //=============================================================================
4910 
4911 //------------------------------hash-------------------------------------------
4912 // Type-specific hashing function.
4913 int TypeNarrowPtr::hash(void) const {
4914   return _ptrtype->hash() + 7;
4915 }
4916 
4917 bool TypeNarrowPtr::singleton(void) const {    // TRUE if type is a singleton
4918   return _ptrtype->singleton();
4919 }
4920 
4921 bool TypeNarrowPtr::empty(void) const {
4922   return _ptrtype->empty();
4923 }
4924 
4925 intptr_t TypeNarrowPtr::get_con() const {
4926   return _ptrtype->get_con();
4927 }
4928 
4929 bool TypeNarrowPtr::eq( const Type *t ) const {
4930   const TypeNarrowPtr* tc = isa_same_narrowptr(t);
4931   if (tc != NULL) {
4932     if (_ptrtype->base() != tc->_ptrtype->base()) {
4933       return false;
4934     }
4935     return tc->_ptrtype->eq(_ptrtype);
4936   }
4937   return false;
4938 }
4939 
4940 const Type *TypeNarrowPtr::xdual() const {    // Compute dual right now.
4941   const TypePtr* odual = _ptrtype->dual()->is_ptr();
4942   return make_same_narrowptr(odual);
4943 }
4944 
4945 
4946 const Type *TypeNarrowPtr::filter_helper(const Type *kills, bool include_speculative) const {
4947   if (isa_same_narrowptr(kills)) {
4948     const Type* ft =_ptrtype->filter_helper(is_same_narrowptr(kills)->_ptrtype, include_speculative);
4949     if (ft->empty())
4950       return Type::TOP;           // Canonical empty value
4951     if (ft->isa_ptr()) {
4952       return make_hash_same_narrowptr(ft->isa_ptr());
4953     }
4954     return ft;
4955   } else if (kills->isa_ptr()) {
4956     const Type* ft = _ptrtype->join_helper(kills, include_speculative);
4957     if (ft->empty())
4958       return Type::TOP;           // Canonical empty value
4959     return ft;
4960   } else {
4961     return Type::TOP;
4962   }
4963 }
4964 
4965 //------------------------------xmeet------------------------------------------
4966 // Compute the MEET of two types.  It returns a new Type object.
4967 const Type *TypeNarrowPtr::xmeet( const Type *t ) const {
4968   // Perform a fast test for common case; meeting the same types together.
4969   if( this == t ) return this;  // Meeting same type-rep?
4970 
4971   if (t->base() == base()) {
4972     const Type* result = _ptrtype->xmeet(t->make_ptr());
4973     if (result->isa_ptr()) {
4974       return make_hash_same_narrowptr(result->is_ptr());
4975     }
4976     return result;
4977   }
4978 
4979   // Current "this->_base" is NarrowKlass or NarrowOop
4980   switch (t->base()) {          // switch on original type
4981 
4982   case Int:                     // Mixing ints & oops happens when javac
4983   case Long:                    // reuses local variables
4984   case FloatTop:
4985   case FloatCon:
4986   case FloatBot:
4987   case DoubleTop:
4988   case DoubleCon:
4989   case DoubleBot:
4990   case AnyPtr:
4991   case RawPtr:
4992   case OopPtr:
4993   case InstPtr:
4994   case ValueTypePtr:
4995   case AryPtr:
4996   case MetadataPtr:
4997   case KlassPtr:
4998   case NarrowOop:
4999   case NarrowKlass:
5000 
5001   case Bottom:                  // Ye Olde Default
5002     return Type::BOTTOM;
5003   case Top:
5004     return this;
5005 
5006   default:                      // All else is a mistake
5007     typerr(t);
5008 
5009   } // End of switch
5010 
5011   return this;
5012 }
5013 
5014 #ifndef PRODUCT
5015 void TypeNarrowPtr::dump2( Dict & d, uint depth, outputStream *st ) const {
5016   _ptrtype->dump2(d, depth, st);
5017 }
5018 #endif
5019 
5020 const TypeNarrowOop *TypeNarrowOop::BOTTOM;
5021 const TypeNarrowOop *TypeNarrowOop::NULL_PTR;
5022 
5023 
5024 const TypeNarrowOop* TypeNarrowOop::make(const TypePtr* type) {
5025   return (const TypeNarrowOop*)(new TypeNarrowOop(type))->hashcons();
5026 }
5027 
5028 const Type* TypeNarrowOop::remove_speculative() const {
5029   return make(_ptrtype->remove_speculative()->is_ptr());
5030 }
5031 
5032 const Type* TypeNarrowOop::cleanup_speculative() const {
5033   return make(_ptrtype->cleanup_speculative()->is_ptr());
5034 }
5035 
5036 #ifndef PRODUCT
5037 void TypeNarrowOop::dump2( Dict & d, uint depth, outputStream *st ) const {
5038   st->print("narrowoop: ");
5039   TypeNarrowPtr::dump2(d, depth, st);
5040 }
5041 #endif
5042 
5043 const TypeNarrowKlass *TypeNarrowKlass::NULL_PTR;
5044 
5045 const TypeNarrowKlass* TypeNarrowKlass::make(const TypePtr* type) {
5046   return (const TypeNarrowKlass*)(new TypeNarrowKlass(type))->hashcons();
5047 }
5048 
5049 #ifndef PRODUCT
5050 void TypeNarrowKlass::dump2( Dict & d, uint depth, outputStream *st ) const {
5051   st->print("narrowklass: ");
5052   TypeNarrowPtr::dump2(d, depth, st);
5053 }
5054 #endif
5055 
5056 
5057 //------------------------------eq---------------------------------------------
5058 // Structural equality check for Type representations
5059 bool TypeMetadataPtr::eq( const Type *t ) const {
5060   const TypeMetadataPtr *a = (const TypeMetadataPtr*)t;
5061   ciMetadata* one = metadata();
5062   ciMetadata* two = a->metadata();
5063   if (one == NULL || two == NULL) {
5064     return (one == two) && TypePtr::eq(t);
5065   } else {
5066     return one->equals(two) && TypePtr::eq(t);
5067   }
5068 }
5069 
5070 //------------------------------hash-------------------------------------------
5071 // Type-specific hashing function.
5072 int TypeMetadataPtr::hash(void) const {
5073   return
5074     (metadata() ? metadata()->hash() : 0) +
5075     TypePtr::hash();
5076 }
5077 
5078 //------------------------------singleton--------------------------------------
5079 // TRUE if Type is a singleton type, FALSE otherwise.   Singletons are simple
5080 // constants
5081 bool TypeMetadataPtr::singleton(void) const {
5082   // detune optimizer to not generate constant metadata + constant offset as a constant!
5083   // TopPTR, Null, AnyNull, Constant are all singletons
5084   return (offset() == 0) && !below_centerline(_ptr);
5085 }
5086 
5087 //------------------------------add_offset-------------------------------------
5088 const TypePtr *TypeMetadataPtr::add_offset( intptr_t offset ) const {
5089   return make( _ptr, _metadata, xadd_offset(offset));
5090 }
5091 
5092 //-----------------------------filter------------------------------------------
5093 // Do not allow interface-vs.-noninterface joins to collapse to top.
5094 const Type *TypeMetadataPtr::filter_helper(const Type *kills, bool include_speculative) const {
5095   const TypeMetadataPtr* ft = join_helper(kills, include_speculative)->isa_metadataptr();
5096   if (ft == NULL || ft->empty())
5097     return Type::TOP;           // Canonical empty value
5098   return ft;
5099 }
5100 
5101  //------------------------------get_con----------------------------------------
5102 intptr_t TypeMetadataPtr::get_con() const {
5103   assert( _ptr == Null || _ptr == Constant, "" );
5104   assert(offset() >= 0, "");
5105 
5106   if (offset() != 0) {
5107     // After being ported to the compiler interface, the compiler no longer
5108     // directly manipulates the addresses of oops.  Rather, it only has a pointer
5109     // to a handle at compile time.  This handle is embedded in the generated
5110     // code and dereferenced at the time the nmethod is made.  Until that time,
5111     // it is not reasonable to do arithmetic with the addresses of oops (we don't
5112     // have access to the addresses!).  This does not seem to currently happen,
5113     // but this assertion here is to help prevent its occurence.
5114     tty->print_cr("Found oop constant with non-zero offset");
5115     ShouldNotReachHere();
5116   }
5117 
5118   return (intptr_t)metadata()->constant_encoding();
5119 }
5120 
5121 //------------------------------cast_to_ptr_type-------------------------------
5122 const Type *TypeMetadataPtr::cast_to_ptr_type(PTR ptr) const {
5123   if( ptr == _ptr ) return this;
5124   return make(ptr, metadata(), _offset);
5125 }
5126 
5127 //------------------------------meet-------------------------------------------
5128 // Compute the MEET of two types.  It returns a new Type object.
5129 const Type *TypeMetadataPtr::xmeet( const Type *t ) const {
5130   // Perform a fast test for common case; meeting the same types together.
5131   if( this == t ) return this;  // Meeting same type-rep?
5132 
5133   // Current "this->_base" is OopPtr
5134   switch (t->base()) {          // switch on original type
5135 
5136   case Int:                     // Mixing ints & oops happens when javac
5137   case Long:                    // reuses local variables
5138   case FloatTop:
5139   case FloatCon:
5140   case FloatBot:
5141   case DoubleTop:
5142   case DoubleCon:
5143   case DoubleBot:
5144   case NarrowOop:
5145   case NarrowKlass:
5146   case Bottom:                  // Ye Olde Default
5147     return Type::BOTTOM;
5148   case Top:
5149     return this;
5150 
5151   default:                      // All else is a mistake
5152     typerr(t);
5153 
5154   case AnyPtr: {
5155     // Found an AnyPtr type vs self-OopPtr type
5156     const TypePtr *tp = t->is_ptr();
5157     Offset offset = meet_offset(tp->offset());
5158     PTR ptr = meet_ptr(tp->ptr());
5159     switch (tp->ptr()) {
5160     case Null:
5161       if (ptr == Null)  return TypePtr::make(AnyPtr, ptr, offset, tp->speculative(), tp->inline_depth());
5162       // else fall through:
5163     case TopPTR:
5164     case AnyNull: {
5165       return make(ptr, _metadata, offset);
5166     }
5167     case BotPTR:
5168     case NotNull:
5169       return TypePtr::make(AnyPtr, ptr, offset, tp->speculative(), tp->inline_depth());
5170     default: typerr(t);
5171     }
5172   }
5173 
5174   case RawPtr:
5175   case KlassPtr:
5176   case OopPtr:
5177   case InstPtr:
5178   case ValueTypePtr:
5179   case AryPtr:
5180     return TypePtr::BOTTOM;     // Oop meet raw is not well defined
5181 
5182   case MetadataPtr: {
5183     const TypeMetadataPtr *tp = t->is_metadataptr();
5184     Offset offset = meet_offset(tp->offset());
5185     PTR tptr = tp->ptr();
5186     PTR ptr = meet_ptr(tptr);
5187     ciMetadata* md = (tptr == TopPTR) ? metadata() : tp->metadata();
5188     if (tptr == TopPTR || _ptr == TopPTR ||
5189         metadata()->equals(tp->metadata())) {
5190       return make(ptr, md, offset);
5191     }
5192     // metadata is different
5193     if( ptr == Constant ) {  // Cannot be equal constants, so...
5194       if( tptr == Constant && _ptr != Constant)  return t;
5195       if( _ptr == Constant && tptr != Constant)  return this;
5196       ptr = NotNull;            // Fall down in lattice
5197     }
5198     return make(ptr, NULL, offset);
5199     break;
5200   }
5201   } // End of switch
5202   return this;                  // Return the double constant
5203 }
5204 
5205 
5206 //------------------------------xdual------------------------------------------
5207 // Dual of a pure metadata pointer.
5208 const Type *TypeMetadataPtr::xdual() const {
5209   return new TypeMetadataPtr(dual_ptr(), metadata(), dual_offset());
5210 }
5211 
5212 //------------------------------dump2------------------------------------------
5213 #ifndef PRODUCT
5214 void TypeMetadataPtr::dump2( Dict &d, uint depth, outputStream *st ) const {
5215   st->print("metadataptr:%s", ptr_msg[_ptr]);
5216   if( metadata() ) st->print(INTPTR_FORMAT, p2i(metadata()));
5217   switch (offset()) {
5218   case OffsetTop: st->print("+top"); break;
5219   case OffsetBot: st->print("+any"); break;
5220   case         0: break;
5221   default:        st->print("+%d",offset()); break;
5222   }
5223 }
5224 #endif
5225 
5226 
5227 //=============================================================================
5228 // Convenience common pre-built type.
5229 const TypeMetadataPtr *TypeMetadataPtr::BOTTOM;
5230 
5231 TypeMetadataPtr::TypeMetadataPtr(PTR ptr, ciMetadata* metadata, Offset offset):
5232   TypePtr(MetadataPtr, ptr, offset), _metadata(metadata) {
5233 }
5234 
5235 const TypeMetadataPtr* TypeMetadataPtr::make(ciMethod* m) {
5236   return make(Constant, m, Offset(0));
5237 }
5238 const TypeMetadataPtr* TypeMetadataPtr::make(ciMethodData* m) {
5239   return make(Constant, m, Offset(0));
5240 }
5241 
5242 //------------------------------make-------------------------------------------
5243 // Create a meta data constant
5244 const TypeMetadataPtr* TypeMetadataPtr::make(PTR ptr, ciMetadata* m, Offset offset) {
5245   assert(m == NULL || !m->is_klass(), "wrong type");
5246   return (TypeMetadataPtr*)(new TypeMetadataPtr(ptr, m, offset))->hashcons();
5247 }
5248 
5249 
5250 //=============================================================================
5251 // Convenience common pre-built types.
5252 
5253 // Not-null object klass or below
5254 const TypeKlassPtr *TypeKlassPtr::OBJECT;
5255 const TypeKlassPtr *TypeKlassPtr::OBJECT_OR_NULL;
5256 const TypeKlassPtr* TypeKlassPtr::BOTTOM;
5257 
5258 //------------------------------TypeKlassPtr-----------------------------------
5259 TypeKlassPtr::TypeKlassPtr( PTR ptr, ciKlass* klass, Offset offset )
5260   : TypePtr(KlassPtr, ptr, offset), _klass(klass), _klass_is_exact(ptr == Constant) {
5261 }
5262 
5263 //------------------------------make-------------------------------------------
5264 // ptr to klass 'k', if Constant, or possibly to a sub-klass if not a Constant
5265 const TypeKlassPtr* TypeKlassPtr::make(PTR ptr, ciKlass* k, Offset offset) {
5266   assert(k == NULL || k->is_instance_klass() || k->is_array_klass(), "Incorrect type of klass oop");
5267   return (TypeKlassPtr*)(new TypeKlassPtr(ptr, k, offset))->hashcons();
5268 }
5269 
5270 //------------------------------eq---------------------------------------------
5271 // Structural equality check for Type representations
5272 bool TypeKlassPtr::eq( const Type *t ) const {
5273   const TypeKlassPtr *p = t->is_klassptr();
5274   return klass() == p->klass() && TypePtr::eq(p);
5275 }
5276 
5277 //------------------------------hash-------------------------------------------
5278 // Type-specific hashing function.
5279 int TypeKlassPtr::hash(void) const {
5280   return java_add(klass() != NULL ? klass()->hash() : 0, TypePtr::hash());
5281 }
5282 
5283 //------------------------------singleton--------------------------------------
5284 // TRUE if Type is a singleton type, FALSE otherwise.   Singletons are simple
5285 // constants
5286 bool TypeKlassPtr::singleton(void) const {
5287   // detune optimizer to not generate constant klass + constant offset as a constant!
5288   // TopPTR, Null, AnyNull, Constant are all singletons
5289   return (offset() == 0) && !below_centerline(_ptr);
5290 }
5291 
5292 // Do not allow interface-vs.-noninterface joins to collapse to top.
5293 const Type *TypeKlassPtr::filter_helper(const Type *kills, bool include_speculative) const {
5294   // logic here mirrors the one from TypeOopPtr::filter. See comments
5295   // there.
5296   const Type* ft = join_helper(kills, include_speculative);
5297   const TypeKlassPtr* ftkp = ft->isa_klassptr();
5298   const TypeKlassPtr* ktkp = kills->isa_klassptr();
5299 
5300   if (ft->empty()) {
5301     if (!empty() && ktkp != NULL && ktkp->is_loaded() && ktkp->klass()->is_interface())
5302       return kills;             // Uplift to interface
5303 
5304     return Type::TOP;           // Canonical empty value
5305   }
5306 
5307   // Interface klass type could be exact in opposite to interface type,
5308   // return it here instead of incorrect Constant ptr J/L/Object (6894807).
5309   if (ftkp != NULL && ktkp != NULL &&
5310       ftkp->is_loaded() &&  ftkp->klass()->is_interface() &&
5311       !ftkp->klass_is_exact() && // Keep exact interface klass
5312       ktkp->is_loaded() && !ktkp->klass()->is_interface()) {
5313     return ktkp->cast_to_ptr_type(ftkp->ptr());
5314   }
5315 
5316   return ft;
5317 }
5318 
5319 //----------------------compute_klass------------------------------------------
5320 // Compute the defining klass for this class
5321 ciKlass* TypeAryPtr::compute_klass(DEBUG_ONLY(bool verify)) const {
5322   // Compute _klass based on element type.
5323   ciKlass* k_ary = NULL;
5324   const TypeAryPtr *tary;
5325   const Type* el = elem();
5326   if (el->isa_narrowoop()) {
5327     el = el->make_ptr();
5328   }
5329 
5330   // Get element klass
5331   if (el->isa_instptr() || el->isa_valuetypeptr()) {
5332     // Compute object array klass from element klass
5333     k_ary = ciArrayKlass::make(el->is_oopptr()->klass());
5334   } else if (el->isa_valuetype()) {
5335     k_ary = ciArrayKlass::make(el->is_valuetype()->value_klass());
5336   } else if ((tary = el->isa_aryptr()) != NULL) {
5337     // Compute array klass from element klass
5338     ciKlass* k_elem = tary->klass();
5339     // If element type is something like bottom[], k_elem will be null.
5340     if (k_elem != NULL)
5341       k_ary = ciObjArrayKlass::make(k_elem);
5342   } else if ((el->base() == Type::Top) ||
5343              (el->base() == Type::Bottom)) {
5344     // element type of Bottom occurs from meet of basic type
5345     // and object; Top occurs when doing join on Bottom.
5346     // Leave k_ary at NULL.
5347   } else {
5348     // Cannot compute array klass directly from basic type,
5349     // since subtypes of TypeInt all have basic type T_INT.
5350 #ifdef ASSERT
5351     if (verify && el->isa_int()) {
5352       // Check simple cases when verifying klass.
5353       BasicType bt = T_ILLEGAL;
5354       if (el == TypeInt::BYTE) {
5355         bt = T_BYTE;
5356       } else if (el == TypeInt::SHORT) {
5357         bt = T_SHORT;
5358       } else if (el == TypeInt::CHAR) {
5359         bt = T_CHAR;
5360       } else if (el == TypeInt::INT) {
5361         bt = T_INT;
5362       } else {
5363         return _klass; // just return specified klass
5364       }
5365       return ciTypeArrayKlass::make(bt);
5366     }
5367 #endif
5368     assert(!el->isa_int(),
5369            "integral arrays must be pre-equipped with a class");
5370     // Compute array klass directly from basic type
5371     k_ary = ciTypeArrayKlass::make(el->basic_type());
5372   }
5373   return k_ary;
5374 }
5375 
5376 //------------------------------klass------------------------------------------
5377 // Return the defining klass for this class
5378 ciKlass* TypeAryPtr::klass() const {
5379   if( _klass ) return _klass;   // Return cached value, if possible
5380 
5381   // Oops, need to compute _klass and cache it
5382   ciKlass* k_ary = compute_klass();
5383 
5384   if( this != TypeAryPtr::OOPS && this->dual() != TypeAryPtr::OOPS ) {
5385     // The _klass field acts as a cache of the underlying
5386     // ciKlass for this array type.  In order to set the field,
5387     // we need to cast away const-ness.
5388     //
5389     // IMPORTANT NOTE: we *never* set the _klass field for the
5390     // type TypeAryPtr::OOPS.  This Type is shared between all
5391     // active compilations.  However, the ciKlass which represents
5392     // this Type is *not* shared between compilations, so caching
5393     // this value would result in fetching a dangling pointer.
5394     //
5395     // Recomputing the underlying ciKlass for each request is
5396     // a bit less efficient than caching, but calls to
5397     // TypeAryPtr::OOPS->klass() are not common enough to matter.
5398     ((TypeAryPtr*)this)->_klass = k_ary;
5399     if (UseCompressedOops && k_ary != NULL && k_ary->is_obj_array_klass() &&
5400         offset() != 0 && offset() != arrayOopDesc::length_offset_in_bytes()) {
5401       ((TypeAryPtr*)this)->_is_ptr_to_narrowoop = true;
5402     }
5403   }
5404   return k_ary;
5405 }
5406 
5407 
5408 //------------------------------add_offset-------------------------------------
5409 // Access internals of klass object
5410 const TypePtr *TypeKlassPtr::add_offset( intptr_t offset ) const {
5411   return make( _ptr, klass(), xadd_offset(offset) );
5412 }
5413 
5414 //------------------------------cast_to_ptr_type-------------------------------
5415 const Type *TypeKlassPtr::cast_to_ptr_type(PTR ptr) const {
5416   assert(_base == KlassPtr, "subclass must override cast_to_ptr_type");
5417   if( ptr == _ptr ) return this;
5418   return make(ptr, _klass, _offset);
5419 }
5420 
5421 
5422 //-----------------------------cast_to_exactness-------------------------------
5423 const Type *TypeKlassPtr::cast_to_exactness(bool klass_is_exact) const {
5424   if( klass_is_exact == _klass_is_exact ) return this;
5425   if (!UseExactTypes)  return this;
5426   return make(klass_is_exact ? Constant : NotNull, _klass, _offset);
5427 }
5428 
5429 
5430 //-----------------------------as_instance_type--------------------------------
5431 // Corresponding type for an instance of the given class.
5432 // It will be NotNull, and exact if and only if the klass type is exact.
5433 const TypeOopPtr* TypeKlassPtr::as_instance_type() const {
5434   ciKlass* k = klass();
5435   assert(k != NULL, "klass should not be NULL");
5436   bool    xk = klass_is_exact();
5437   //return TypeInstPtr::make(TypePtr::NotNull, k, xk, NULL, 0);
5438   const TypeOopPtr* toop = TypeOopPtr::make_from_klass_raw(k);
5439   guarantee(toop != NULL, "need type for given klass");
5440   toop = toop->cast_to_ptr_type(TypePtr::NotNull)->is_oopptr();
5441   return toop->cast_to_exactness(xk)->is_oopptr();
5442 }
5443 
5444 
5445 //------------------------------xmeet------------------------------------------
5446 // Compute the MEET of two types, return a new Type object.
5447 const Type    *TypeKlassPtr::xmeet( const Type *t ) const {
5448   // Perform a fast test for common case; meeting the same types together.
5449   if( this == t ) return this;  // Meeting same type-rep?
5450 
5451   // Current "this->_base" is Pointer
5452   switch (t->base()) {          // switch on original type
5453 
5454   case Int:                     // Mixing ints & oops happens when javac
5455   case Long:                    // reuses local variables
5456   case FloatTop:
5457   case FloatCon:
5458   case FloatBot:
5459   case DoubleTop:
5460   case DoubleCon:
5461   case DoubleBot:
5462   case NarrowOop:
5463   case NarrowKlass:
5464   case Bottom:                  // Ye Olde Default
5465     return Type::BOTTOM;
5466   case Top:
5467     return this;
5468 
5469   default:                      // All else is a mistake
5470     typerr(t);
5471 
5472   case AnyPtr: {                // Meeting to AnyPtrs
5473     // Found an AnyPtr type vs self-KlassPtr type
5474     const TypePtr *tp = t->is_ptr();
5475     Offset offset = meet_offset(tp->offset());
5476     PTR ptr = meet_ptr(tp->ptr());
5477     switch (tp->ptr()) {
5478     case TopPTR:
5479       return this;
5480     case Null:
5481       if( ptr == Null ) return TypePtr::make(AnyPtr, ptr, offset, tp->speculative(), tp->inline_depth());
5482     case AnyNull:
5483       return make( ptr, klass(), offset );
5484     case BotPTR:
5485     case NotNull:
5486       return TypePtr::make(AnyPtr, ptr, offset, tp->speculative(), tp->inline_depth());
5487     default: typerr(t);
5488     }
5489   }
5490 
5491   case RawPtr:
5492   case MetadataPtr:
5493   case OopPtr:
5494   case AryPtr:                  // Meet with AryPtr
5495   case InstPtr:                 // Meet with InstPtr
5496   case ValueTypePtr:
5497     return TypePtr::BOTTOM;
5498 
5499   //
5500   //             A-top         }
5501   //           /   |   \       }  Tops
5502   //       B-top A-any C-top   }
5503   //          | /  |  \ |      }  Any-nulls
5504   //       B-any   |   C-any   }
5505   //          |    |    |
5506   //       B-con A-con C-con   } constants; not comparable across classes
5507   //          |    |    |
5508   //       B-not   |   C-not   }
5509   //          | \  |  / |      }  not-nulls
5510   //       B-bot A-not C-bot   }
5511   //           \   |   /       }  Bottoms
5512   //             A-bot         }
5513   //
5514 
5515   case KlassPtr: {  // Meet two KlassPtr types
5516     const TypeKlassPtr *tkls = t->is_klassptr();
5517     Offset  off  = meet_offset(tkls->offset());
5518     PTR  ptr     = meet_ptr(tkls->ptr());
5519 
5520     if (klass() == NULL || tkls->klass() == NULL) {
5521       ciKlass* k = NULL;
5522       if (ptr == Constant) {
5523         k = (klass() == NULL) ? tkls->klass() : klass();
5524       }
5525       return make(ptr, k, off);
5526     }
5527 
5528     // Check for easy case; klasses are equal (and perhaps not loaded!)
5529     // If we have constants, then we created oops so classes are loaded
5530     // and we can handle the constants further down.  This case handles
5531     // not-loaded classes
5532     if( ptr != Constant && tkls->klass()->equals(klass()) ) {
5533       return make( ptr, klass(), off );
5534     }
5535 
5536     // Classes require inspection in the Java klass hierarchy.  Must be loaded.
5537     ciKlass* tkls_klass = tkls->klass();
5538     ciKlass* this_klass = this->klass();
5539     assert( tkls_klass->is_loaded(), "This class should have been loaded.");
5540     assert( this_klass->is_loaded(), "This class should have been loaded.");
5541 
5542     // If 'this' type is above the centerline and is a superclass of the
5543     // other, we can treat 'this' as having the same type as the other.
5544     if ((above_centerline(this->ptr())) &&
5545         tkls_klass->is_subtype_of(this_klass)) {
5546       this_klass = tkls_klass;
5547     }
5548     // If 'tinst' type is above the centerline and is a superclass of the
5549     // other, we can treat 'tinst' as having the same type as the other.
5550     if ((above_centerline(tkls->ptr())) &&
5551         this_klass->is_subtype_of(tkls_klass)) {
5552       tkls_klass = this_klass;
5553     }
5554 
5555     // Check for classes now being equal
5556     if (tkls_klass->equals(this_klass)) {
5557       // If the klasses are equal, the constants may still differ.  Fall to
5558       // NotNull if they do (neither constant is NULL; that is a special case
5559       // handled elsewhere).
5560       if( ptr == Constant ) {
5561         if (this->_ptr == Constant && tkls->_ptr == Constant &&
5562             this->klass()->equals(tkls->klass()));
5563         else if (above_centerline(this->ptr()));
5564         else if (above_centerline(tkls->ptr()));
5565         else
5566           ptr = NotNull;
5567       }
5568       return make( ptr, this_klass, off );
5569     } // Else classes are not equal
5570 
5571     // Since klasses are different, we require the LCA in the Java
5572     // class hierarchy - which means we have to fall to at least NotNull.
5573     if( ptr == TopPTR || ptr == AnyNull || ptr == Constant )
5574       ptr = NotNull;
5575     // Now we find the LCA of Java classes
5576     ciKlass* k = this_klass->least_common_ancestor(tkls_klass);
5577     return   make( ptr, k, off );
5578   } // End of case KlassPtr
5579 
5580   } // End of switch
5581   return this;                  // Return the double constant
5582 }
5583 
5584 //------------------------------xdual------------------------------------------
5585 // Dual: compute field-by-field dual
5586 const Type    *TypeKlassPtr::xdual() const {
5587   return new TypeKlassPtr( dual_ptr(), klass(), dual_offset() );
5588 }
5589 
5590 //------------------------------get_con----------------------------------------
5591 intptr_t TypeKlassPtr::get_con() const {
5592   assert( _ptr == Null || _ptr == Constant, "" );
5593   assert(offset() >= 0, "");
5594 
5595   if (offset() != 0) {
5596     // After being ported to the compiler interface, the compiler no longer
5597     // directly manipulates the addresses of oops.  Rather, it only has a pointer
5598     // to a handle at compile time.  This handle is embedded in the generated
5599     // code and dereferenced at the time the nmethod is made.  Until that time,
5600     // it is not reasonable to do arithmetic with the addresses of oops (we don't
5601     // have access to the addresses!).  This does not seem to currently happen,
5602     // but this assertion here is to help prevent its occurence.
5603     tty->print_cr("Found oop constant with non-zero offset");
5604     ShouldNotReachHere();
5605   }
5606 
5607   return (intptr_t)klass()->constant_encoding();
5608 }
5609 //------------------------------dump2------------------------------------------
5610 // Dump Klass Type
5611 #ifndef PRODUCT
5612 void TypeKlassPtr::dump2( Dict & d, uint depth, outputStream *st ) const {
5613   switch( _ptr ) {
5614   case Constant:
5615     st->print("precise ");
5616   case NotNull:
5617     {
5618       if (klass() != NULL) {
5619         const char* name = klass()->name()->as_utf8();
5620         st->print("klass %s: " INTPTR_FORMAT, name, p2i(klass()));
5621       } else {
5622         st->print("klass BOTTOM");
5623       }
5624     }
5625   case BotPTR:
5626     if( !WizardMode && !Verbose && !_klass_is_exact ) break;
5627   case TopPTR:
5628   case AnyNull:
5629     st->print(":%s", ptr_msg[_ptr]);
5630     if( _klass_is_exact ) st->print(":exact");
5631     break;
5632   }
5633 
5634   _offset.dump2(st);
5635 
5636   st->print(" *");
5637 }
5638 #endif
5639 
5640 
5641 
5642 //=============================================================================
5643 // Convenience common pre-built types.
5644 
5645 //------------------------------make-------------------------------------------
5646 const TypeFunc *TypeFunc::make(const TypeTuple *domain_sig, const TypeTuple* domain_cc,
5647                                const TypeTuple *range_sig, const TypeTuple *range_cc) {
5648   return (TypeFunc*)(new TypeFunc(domain_sig, domain_cc, range_sig, range_cc))->hashcons();
5649 }
5650 
5651 const TypeFunc *TypeFunc::make(const TypeTuple *domain, const TypeTuple *range) {
5652   return make(domain, domain, range, range);
5653 }
5654 
5655 //------------------------------make-------------------------------------------
5656 const TypeFunc *TypeFunc::make(ciMethod* method) {
5657   Compile* C = Compile::current();
5658   const TypeFunc* tf = C->last_tf(method); // check cache
5659   if (tf != NULL)  return tf;  // The hit rate here is almost 50%.
5660   const TypeTuple *domain_sig, *domain_cc;
5661   // Value type arguments are not passed by reference, instead each
5662   // field of the value type is passed as an argument. We maintain 2
5663   // views of the argument list here: one based on the signature (with
5664   // a value type argument as a single slot), one based on the actual
5665   // calling convention (with a value type argument as a list of its
5666   // fields).
5667   if (method->is_static()) {
5668     domain_sig = TypeTuple::make_domain(NULL, method->signature(), false);
5669     domain_cc = TypeTuple::make_domain(NULL, method->signature(), ValueTypePassFieldsAsArgs);
5670   } else {
5671     domain_sig = TypeTuple::make_domain(method->holder(), method->signature(), false);
5672     domain_cc = TypeTuple::make_domain(method->holder(), method->signature(), ValueTypePassFieldsAsArgs);
5673   }
5674   const TypeTuple *range_sig = TypeTuple::make_range(method->signature(), false);
5675   const TypeTuple *range_cc = TypeTuple::make_range(method->signature(), ValueTypeReturnedAsFields);
5676   tf = TypeFunc::make(domain_sig, domain_cc, range_sig, range_cc);
5677   C->set_last_tf(method, tf);  // fill cache
5678   return tf;
5679 }
5680 
5681 //------------------------------meet-------------------------------------------
5682 // Compute the MEET of two types.  It returns a new Type object.
5683 const Type *TypeFunc::xmeet( const Type *t ) const {
5684   // Perform a fast test for common case; meeting the same types together.
5685   if( this == t ) return this;  // Meeting same type-rep?
5686 
5687   // Current "this->_base" is Func
5688   switch (t->base()) {          // switch on original type
5689 
5690   case Bottom:                  // Ye Olde Default
5691     return t;
5692 
5693   default:                      // All else is a mistake
5694     typerr(t);
5695 
5696   case Top:
5697     break;
5698   }
5699   return this;                  // Return the double constant
5700 }
5701 
5702 //------------------------------xdual------------------------------------------
5703 // Dual: compute field-by-field dual
5704 const Type *TypeFunc::xdual() const {
5705   return this;
5706 }
5707 
5708 //------------------------------eq---------------------------------------------
5709 // Structural equality check for Type representations
5710 bool TypeFunc::eq( const Type *t ) const {
5711   const TypeFunc *a = (const TypeFunc*)t;
5712   return _domain_sig == a->_domain_sig &&
5713     _domain_cc == a->_domain_cc &&
5714     _range_sig == a->_range_sig &&
5715     _range_cc == a->_range_cc;
5716 }
5717 
5718 //------------------------------hash-------------------------------------------
5719 // Type-specific hashing function.
5720 int TypeFunc::hash(void) const {
5721   return (intptr_t)_domain_sig + (intptr_t)_domain_cc + (intptr_t)_range_sig + (intptr_t)_range_cc;
5722 }
5723 
5724 //------------------------------dump2------------------------------------------
5725 // Dump Function Type
5726 #ifndef PRODUCT
5727 void TypeFunc::dump2( Dict &d, uint depth, outputStream *st ) const {
5728   if( _range_sig->cnt() <= Parms )
5729     st->print("void");
5730   else {
5731     uint i;
5732     for (i = Parms; i < _range_sig->cnt()-1; i++) {
5733       _range_sig->field_at(i)->dump2(d,depth,st);
5734       st->print("/");
5735     }
5736     _range_sig->field_at(i)->dump2(d,depth,st);
5737   }
5738   st->print(" ");
5739   st->print("( ");
5740   if( !depth || d[this] ) {     // Check for recursive dump
5741     st->print("...)");
5742     return;
5743   }
5744   d.Insert((void*)this,(void*)this);    // Stop recursion
5745   if (Parms < _domain_sig->cnt())
5746     _domain_sig->field_at(Parms)->dump2(d,depth-1,st);
5747   for (uint i = Parms+1; i < _domain_sig->cnt(); i++) {
5748     st->print(", ");
5749     _domain_sig->field_at(i)->dump2(d,depth-1,st);
5750   }
5751   st->print(" )");
5752 }
5753 #endif
5754 
5755 //------------------------------singleton--------------------------------------
5756 // TRUE if Type is a singleton type, FALSE otherwise.   Singletons are simple
5757 // constants (Ldi nodes).  Singletons are integer, float or double constants
5758 // or a single symbol.
5759 bool TypeFunc::singleton(void) const {
5760   return false;                 // Never a singleton
5761 }
5762 
5763 bool TypeFunc::empty(void) const {
5764   return false;                 // Never empty
5765 }
5766 
5767 
5768 BasicType TypeFunc::return_type() const{
5769   if (range_sig()->cnt() == TypeFunc::Parms) {
5770     return T_VOID;
5771   }
5772   return range_sig()->field_at(TypeFunc::Parms)->basic_type();
5773 }