src/share/vm/opto/type.cpp

Sat, 01 Sep 2012 13:25:18 -0400

author
coleenp
date
Sat, 01 Sep 2012 13:25:18 -0400
changeset 4037
da91efe96a93
parent 3901
24b9c7f4cae6
child 4040
ca11db66f9de
permissions
-rw-r--r--

6964458: Reimplement class meta-data storage to use native memory
Summary: Remove PermGen, allocate meta-data in metaspace linked to class loaders, rewrite GC walking, rewrite and rename metadata to be C++ classes
Reviewed-by: jmasa, stefank, never, coleenp, kvn, brutisso, mgerdin, dholmes, jrose, twisti, roland
Contributed-by: jmasa <jon.masamitsu@oracle.com>, stefank <stefan.karlsson@oracle.com>, mgerdin <mikael.gerdin@oracle.com>, never <tom.rodriguez@oracle.com>

     1 /*
     2  * Copyright (c) 1997, 2012, 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  */
    25 #include "precompiled.hpp"
    26 #include "ci/ciMethodData.hpp"
    27 #include "ci/ciTypeFlow.hpp"
    28 #include "classfile/symbolTable.hpp"
    29 #include "classfile/systemDictionary.hpp"
    30 #include "compiler/compileLog.hpp"
    31 #include "libadt/dict.hpp"
    32 #include "memory/gcLocker.hpp"
    33 #include "memory/oopFactory.hpp"
    34 #include "memory/resourceArea.hpp"
    35 #include "oops/instanceKlass.hpp"
    36 #include "oops/instanceMirrorKlass.hpp"
    37 #include "oops/objArrayKlass.hpp"
    38 #include "oops/typeArrayKlass.hpp"
    39 #include "opto/matcher.hpp"
    40 #include "opto/node.hpp"
    41 #include "opto/opcodes.hpp"
    42 #include "opto/type.hpp"
    44 // Portions of code courtesy of Clifford Click
    46 // Optimization - Graph Style
    48 // Dictionary of types shared among compilations.
    49 Dict* Type::_shared_type_dict = NULL;
    51 // Array which maps compiler types to Basic Types
    52 Type::TypeInfo Type::_type_info[Type::lastype] = {
    53   { Bad,             T_ILLEGAL,    "bad",           false, Node::NotAMachineReg, relocInfo::none          },  // Bad
    54   { Control,         T_ILLEGAL,    "control",       false, 0,                    relocInfo::none          },  // Control
    55   { Bottom,          T_VOID,       "top",           false, 0,                    relocInfo::none          },  // Top
    56   { Bad,             T_INT,        "int:",          false, Op_RegI,              relocInfo::none          },  // Int
    57   { Bad,             T_LONG,       "long:",         false, Op_RegL,              relocInfo::none          },  // Long
    58   { Half,            T_VOID,       "half",          false, 0,                    relocInfo::none          },  // Half
    59   { Bad,             T_NARROWOOP,  "narrowoop:",    false, Op_RegN,              relocInfo::none          },  // NarrowOop
    60   { Bad,             T_ILLEGAL,    "tuple:",        false, Node::NotAMachineReg, relocInfo::none          },  // Tuple
    61   { Bad,             T_ARRAY,      "array:",        false, Node::NotAMachineReg, relocInfo::none          },  // Array
    63 #if defined(IA32) || defined(AMD64)
    64   { Bad,             T_ILLEGAL,    "vectors:",      false, Op_VecS,              relocInfo::none          },  // VectorS
    65   { Bad,             T_ILLEGAL,    "vectord:",      false, Op_VecD,              relocInfo::none          },  // VectorD
    66   { Bad,             T_ILLEGAL,    "vectorx:",      false, Op_VecX,              relocInfo::none          },  // VectorX
    67   { Bad,             T_ILLEGAL,    "vectory:",      false, Op_VecY,              relocInfo::none          },  // VectorY
    68 #else
    69   { Bad,             T_ILLEGAL,    "vectors:",      false, 0,                    relocInfo::none          },  // VectorS
    70   { Bad,             T_ILLEGAL,    "vectord:",      false, Op_RegD,              relocInfo::none          },  // VectorD
    71   { Bad,             T_ILLEGAL,    "vectorx:",      false, 0,                    relocInfo::none          },  // VectorX
    72   { Bad,             T_ILLEGAL,    "vectory:",      false, 0,                    relocInfo::none          },  // VectorY
    73 #endif // IA32 || AMD64
    74   { Bad,             T_ADDRESS,    "anyptr:",       false, Op_RegP,              relocInfo::none          },  // AnyPtr
    75   { Bad,             T_ADDRESS,    "rawptr:",       false, Op_RegP,              relocInfo::none          },  // RawPtr
    76   { Bad,             T_OBJECT,     "oop:",          true,  Op_RegP,              relocInfo::oop_type      },  // OopPtr
    77   { Bad,             T_OBJECT,     "inst:",         true,  Op_RegP,              relocInfo::oop_type      },  // InstPtr
    78   { Bad,             T_OBJECT,     "ary:",          true,  Op_RegP,              relocInfo::oop_type      },  // AryPtr
    79   { Bad,             T_METADATA,   "metadata:",     false, Op_RegP,              relocInfo::metadata_type },  // MetadataPtr
    80   { Bad,             T_METADATA,   "klass:",        false, Op_RegP,              relocInfo::metadata_type },  // KlassPtr
    81   { Bad,             T_OBJECT,     "func",          false, 0,                    relocInfo::none          },  // Function
    82   { Abio,            T_ILLEGAL,    "abIO",          false, 0,                    relocInfo::none          },  // Abio
    83   { Return_Address,  T_ADDRESS,    "return_address",false, Op_RegP,              relocInfo::none          },  // Return_Address
    84   { Memory,          T_ILLEGAL,    "memory",        false, 0,                    relocInfo::none          },  // Memory
    85   { FloatBot,        T_FLOAT,      "float_top",     false, Op_RegF,              relocInfo::none          },  // FloatTop
    86   { FloatCon,        T_FLOAT,      "ftcon:",        false, Op_RegF,              relocInfo::none          },  // FloatCon
    87   { FloatTop,        T_FLOAT,      "float",         false, Op_RegF,              relocInfo::none          },  // FloatBot
    88   { DoubleBot,       T_DOUBLE,     "double_top",    false, Op_RegD,              relocInfo::none          },  // DoubleTop
    89   { DoubleCon,       T_DOUBLE,     "dblcon:",       false, Op_RegD,              relocInfo::none          },  // DoubleCon
    90   { DoubleTop,       T_DOUBLE,     "double",        false, Op_RegD,              relocInfo::none          },  // DoubleBot
    91   { Top,             T_ILLEGAL,    "bottom",        false, 0,                    relocInfo::none          }   // Bottom
    92 };
    94 // Map ideal registers (machine types) to ideal types
    95 const Type *Type::mreg2type[_last_machine_leaf];
    97 // Map basic types to canonical Type* pointers.
    98 const Type* Type::     _const_basic_type[T_CONFLICT+1];
   100 // Map basic types to constant-zero Types.
   101 const Type* Type::            _zero_type[T_CONFLICT+1];
   103 // Map basic types to array-body alias types.
   104 const TypeAryPtr* TypeAryPtr::_array_body_type[T_CONFLICT+1];
   106 //=============================================================================
   107 // Convenience common pre-built types.
   108 const Type *Type::ABIO;         // State-of-machine only
   109 const Type *Type::BOTTOM;       // All values
   110 const Type *Type::CONTROL;      // Control only
   111 const Type *Type::DOUBLE;       // All doubles
   112 const Type *Type::FLOAT;        // All floats
   113 const Type *Type::HALF;         // Placeholder half of doublewide type
   114 const Type *Type::MEMORY;       // Abstract store only
   115 const Type *Type::RETURN_ADDRESS;
   116 const Type *Type::TOP;          // No values in set
   118 //------------------------------get_const_type---------------------------
   119 const Type* Type::get_const_type(ciType* type) {
   120   if (type == NULL) {
   121     return NULL;
   122   } else if (type->is_primitive_type()) {
   123     return get_const_basic_type(type->basic_type());
   124   } else {
   125     return TypeOopPtr::make_from_klass(type->as_klass());
   126   }
   127 }
   129 //---------------------------array_element_basic_type---------------------------------
   130 // Mapping to the array element's basic type.
   131 BasicType Type::array_element_basic_type() const {
   132   BasicType bt = basic_type();
   133   if (bt == T_INT) {
   134     if (this == TypeInt::INT)   return T_INT;
   135     if (this == TypeInt::CHAR)  return T_CHAR;
   136     if (this == TypeInt::BYTE)  return T_BYTE;
   137     if (this == TypeInt::BOOL)  return T_BOOLEAN;
   138     if (this == TypeInt::SHORT) return T_SHORT;
   139     return T_VOID;
   140   }
   141   return bt;
   142 }
   144 //---------------------------get_typeflow_type---------------------------------
   145 // Import a type produced by ciTypeFlow.
   146 const Type* Type::get_typeflow_type(ciType* type) {
   147   switch (type->basic_type()) {
   149   case ciTypeFlow::StateVector::T_BOTTOM:
   150     assert(type == ciTypeFlow::StateVector::bottom_type(), "");
   151     return Type::BOTTOM;
   153   case ciTypeFlow::StateVector::T_TOP:
   154     assert(type == ciTypeFlow::StateVector::top_type(), "");
   155     return Type::TOP;
   157   case ciTypeFlow::StateVector::T_NULL:
   158     assert(type == ciTypeFlow::StateVector::null_type(), "");
   159     return TypePtr::NULL_PTR;
   161   case ciTypeFlow::StateVector::T_LONG2:
   162     // The ciTypeFlow pass pushes a long, then the half.
   163     // We do the same.
   164     assert(type == ciTypeFlow::StateVector::long2_type(), "");
   165     return TypeInt::TOP;
   167   case ciTypeFlow::StateVector::T_DOUBLE2:
   168     // The ciTypeFlow pass pushes double, then the half.
   169     // Our convention is the same.
   170     assert(type == ciTypeFlow::StateVector::double2_type(), "");
   171     return Type::TOP;
   173   case T_ADDRESS:
   174     assert(type->is_return_address(), "");
   175     return TypeRawPtr::make((address)(intptr_t)type->as_return_address()->bci());
   177   default:
   178     // make sure we did not mix up the cases:
   179     assert(type != ciTypeFlow::StateVector::bottom_type(), "");
   180     assert(type != ciTypeFlow::StateVector::top_type(), "");
   181     assert(type != ciTypeFlow::StateVector::null_type(), "");
   182     assert(type != ciTypeFlow::StateVector::long2_type(), "");
   183     assert(type != ciTypeFlow::StateVector::double2_type(), "");
   184     assert(!type->is_return_address(), "");
   186     return Type::get_const_type(type);
   187   }
   188 }
   191 //------------------------------make-------------------------------------------
   192 // Create a simple Type, with default empty symbol sets.  Then hashcons it
   193 // and look for an existing copy in the type dictionary.
   194 const Type *Type::make( enum TYPES t ) {
   195   return (new Type(t))->hashcons();
   196 }
   198 //------------------------------cmp--------------------------------------------
   199 int Type::cmp( const Type *const t1, const Type *const t2 ) {
   200   if( t1->_base != t2->_base )
   201     return 1;                   // Missed badly
   202   assert(t1 != t2 || t1->eq(t2), "eq must be reflexive");
   203   return !t1->eq(t2);           // Return ZERO if equal
   204 }
   206 //------------------------------hash-------------------------------------------
   207 int Type::uhash( const Type *const t ) {
   208   return t->hash();
   209 }
   211 #define SMALLINT ((juint)3)  // a value too insignificant to consider widening
   213 //--------------------------Initialize_shared----------------------------------
   214 void Type::Initialize_shared(Compile* current) {
   215   // This method does not need to be locked because the first system
   216   // compilations (stub compilations) occur serially.  If they are
   217   // changed to proceed in parallel, then this section will need
   218   // locking.
   220   Arena* save = current->type_arena();
   221   Arena* shared_type_arena = new (mtCompiler)Arena();
   223   current->set_type_arena(shared_type_arena);
   224   _shared_type_dict =
   225     new (shared_type_arena) Dict( (CmpKey)Type::cmp, (Hash)Type::uhash,
   226                                   shared_type_arena, 128 );
   227   current->set_type_dict(_shared_type_dict);
   229   // Make shared pre-built types.
   230   CONTROL = make(Control);      // Control only
   231   TOP     = make(Top);          // No values in set
   232   MEMORY  = make(Memory);       // Abstract store only
   233   ABIO    = make(Abio);         // State-of-machine only
   234   RETURN_ADDRESS=make(Return_Address);
   235   FLOAT   = make(FloatBot);     // All floats
   236   DOUBLE  = make(DoubleBot);    // All doubles
   237   BOTTOM  = make(Bottom);       // Everything
   238   HALF    = make(Half);         // Placeholder half of doublewide type
   240   TypeF::ZERO = TypeF::make(0.0); // Float 0 (positive zero)
   241   TypeF::ONE  = TypeF::make(1.0); // Float 1
   243   TypeD::ZERO = TypeD::make(0.0); // Double 0 (positive zero)
   244   TypeD::ONE  = TypeD::make(1.0); // Double 1
   246   TypeInt::MINUS_1 = TypeInt::make(-1);  // -1
   247   TypeInt::ZERO    = TypeInt::make( 0);  //  0
   248   TypeInt::ONE     = TypeInt::make( 1);  //  1
   249   TypeInt::BOOL    = TypeInt::make(0,1,   WidenMin);  // 0 or 1, FALSE or TRUE.
   250   TypeInt::CC      = TypeInt::make(-1, 1, WidenMin);  // -1, 0 or 1, condition codes
   251   TypeInt::CC_LT   = TypeInt::make(-1,-1, WidenMin);  // == TypeInt::MINUS_1
   252   TypeInt::CC_GT   = TypeInt::make( 1, 1, WidenMin);  // == TypeInt::ONE
   253   TypeInt::CC_EQ   = TypeInt::make( 0, 0, WidenMin);  // == TypeInt::ZERO
   254   TypeInt::CC_LE   = TypeInt::make(-1, 0, WidenMin);
   255   TypeInt::CC_GE   = TypeInt::make( 0, 1, WidenMin);  // == TypeInt::BOOL
   256   TypeInt::BYTE    = TypeInt::make(-128,127,     WidenMin); // Bytes
   257   TypeInt::UBYTE   = TypeInt::make(0, 255,       WidenMin); // Unsigned Bytes
   258   TypeInt::CHAR    = TypeInt::make(0,65535,      WidenMin); // Java chars
   259   TypeInt::SHORT   = TypeInt::make(-32768,32767, WidenMin); // Java shorts
   260   TypeInt::POS     = TypeInt::make(0,max_jint,   WidenMin); // Non-neg values
   261   TypeInt::POS1    = TypeInt::make(1,max_jint,   WidenMin); // Positive values
   262   TypeInt::INT     = TypeInt::make(min_jint,max_jint, WidenMax); // 32-bit integers
   263   TypeInt::SYMINT  = TypeInt::make(-max_jint,max_jint,WidenMin); // symmetric range
   264   // CmpL is overloaded both as the bytecode computation returning
   265   // a trinary (-1,0,+1) integer result AND as an efficient long
   266   // compare returning optimizer ideal-type flags.
   267   assert( TypeInt::CC_LT == TypeInt::MINUS_1, "types must match for CmpL to work" );
   268   assert( TypeInt::CC_GT == TypeInt::ONE,     "types must match for CmpL to work" );
   269   assert( TypeInt::CC_EQ == TypeInt::ZERO,    "types must match for CmpL to work" );
   270   assert( TypeInt::CC_GE == TypeInt::BOOL,    "types must match for CmpL to work" );
   271   assert( (juint)(TypeInt::CC->_hi - TypeInt::CC->_lo) <= SMALLINT, "CC is truly small");
   273   TypeLong::MINUS_1 = TypeLong::make(-1);        // -1
   274   TypeLong::ZERO    = TypeLong::make( 0);        //  0
   275   TypeLong::ONE     = TypeLong::make( 1);        //  1
   276   TypeLong::POS     = TypeLong::make(0,max_jlong, WidenMin); // Non-neg values
   277   TypeLong::LONG    = TypeLong::make(min_jlong,max_jlong,WidenMax); // 64-bit integers
   278   TypeLong::INT     = TypeLong::make((jlong)min_jint,(jlong)max_jint,WidenMin);
   279   TypeLong::UINT    = TypeLong::make(0,(jlong)max_juint,WidenMin);
   281   const Type **fboth =(const Type**)shared_type_arena->Amalloc_4(2*sizeof(Type*));
   282   fboth[0] = Type::CONTROL;
   283   fboth[1] = Type::CONTROL;
   284   TypeTuple::IFBOTH = TypeTuple::make( 2, fboth );
   286   const Type **ffalse =(const Type**)shared_type_arena->Amalloc_4(2*sizeof(Type*));
   287   ffalse[0] = Type::CONTROL;
   288   ffalse[1] = Type::TOP;
   289   TypeTuple::IFFALSE = TypeTuple::make( 2, ffalse );
   291   const Type **fneither =(const Type**)shared_type_arena->Amalloc_4(2*sizeof(Type*));
   292   fneither[0] = Type::TOP;
   293   fneither[1] = Type::TOP;
   294   TypeTuple::IFNEITHER = TypeTuple::make( 2, fneither );
   296   const Type **ftrue =(const Type**)shared_type_arena->Amalloc_4(2*sizeof(Type*));
   297   ftrue[0] = Type::TOP;
   298   ftrue[1] = Type::CONTROL;
   299   TypeTuple::IFTRUE = TypeTuple::make( 2, ftrue );
   301   const Type **floop =(const Type**)shared_type_arena->Amalloc_4(2*sizeof(Type*));
   302   floop[0] = Type::CONTROL;
   303   floop[1] = TypeInt::INT;
   304   TypeTuple::LOOPBODY = TypeTuple::make( 2, floop );
   306   TypePtr::NULL_PTR= TypePtr::make( AnyPtr, TypePtr::Null, 0 );
   307   TypePtr::NOTNULL = TypePtr::make( AnyPtr, TypePtr::NotNull, OffsetBot );
   308   TypePtr::BOTTOM  = TypePtr::make( AnyPtr, TypePtr::BotPTR, OffsetBot );
   310   TypeRawPtr::BOTTOM = TypeRawPtr::make( TypePtr::BotPTR );
   311   TypeRawPtr::NOTNULL= TypeRawPtr::make( TypePtr::NotNull );
   313   const Type **fmembar = TypeTuple::fields(0);
   314   TypeTuple::MEMBAR = TypeTuple::make(TypeFunc::Parms+0, fmembar);
   316   const Type **fsc = (const Type**)shared_type_arena->Amalloc_4(2*sizeof(Type*));
   317   fsc[0] = TypeInt::CC;
   318   fsc[1] = Type::MEMORY;
   319   TypeTuple::STORECONDITIONAL = TypeTuple::make(2, fsc);
   321   TypeInstPtr::NOTNULL = TypeInstPtr::make(TypePtr::NotNull, current->env()->Object_klass());
   322   TypeInstPtr::BOTTOM  = TypeInstPtr::make(TypePtr::BotPTR,  current->env()->Object_klass());
   323   TypeInstPtr::MIRROR  = TypeInstPtr::make(TypePtr::NotNull, current->env()->Class_klass());
   324   TypeInstPtr::MARK    = TypeInstPtr::make(TypePtr::BotPTR,  current->env()->Object_klass(),
   325                                            false, 0, oopDesc::mark_offset_in_bytes());
   326   TypeInstPtr::KLASS   = TypeInstPtr::make(TypePtr::BotPTR,  current->env()->Object_klass(),
   327                                            false, 0, oopDesc::klass_offset_in_bytes());
   328   TypeOopPtr::BOTTOM  = TypeOopPtr::make(TypePtr::BotPTR, OffsetBot, TypeOopPtr::InstanceBot);
   330   TypeMetadataPtr::BOTTOM = TypeMetadataPtr::make(TypePtr::BotPTR, NULL, OffsetBot);
   332   TypeNarrowOop::NULL_PTR = TypeNarrowOop::make( TypePtr::NULL_PTR );
   333   TypeNarrowOop::BOTTOM   = TypeNarrowOop::make( TypeInstPtr::BOTTOM );
   335   mreg2type[Op_Node] = Type::BOTTOM;
   336   mreg2type[Op_Set ] = 0;
   337   mreg2type[Op_RegN] = TypeNarrowOop::BOTTOM;
   338   mreg2type[Op_RegI] = TypeInt::INT;
   339   mreg2type[Op_RegP] = TypePtr::BOTTOM;
   340   mreg2type[Op_RegF] = Type::FLOAT;
   341   mreg2type[Op_RegD] = Type::DOUBLE;
   342   mreg2type[Op_RegL] = TypeLong::LONG;
   343   mreg2type[Op_RegFlags] = TypeInt::CC;
   345   TypeAryPtr::RANGE   = TypeAryPtr::make( TypePtr::BotPTR, TypeAry::make(Type::BOTTOM,TypeInt::POS), NULL /* current->env()->Object_klass() */, false, arrayOopDesc::length_offset_in_bytes());
   347   TypeAryPtr::NARROWOOPS = TypeAryPtr::make(TypePtr::BotPTR, TypeAry::make(TypeNarrowOop::BOTTOM, TypeInt::POS), NULL /*ciArrayKlass::make(o)*/,  false,  Type::OffsetBot);
   349 #ifdef _LP64
   350   if (UseCompressedOops) {
   351     assert(TypeAryPtr::NARROWOOPS->is_ptr_to_narrowoop(), "array of narrow oops must be ptr to narrow oop");
   352     TypeAryPtr::OOPS  = TypeAryPtr::NARROWOOPS;
   353   } else
   354 #endif
   355   {
   356     // There is no shared klass for Object[].  See note in TypeAryPtr::klass().
   357     TypeAryPtr::OOPS  = TypeAryPtr::make(TypePtr::BotPTR, TypeAry::make(TypeInstPtr::BOTTOM,TypeInt::POS), NULL /*ciArrayKlass::make(o)*/,  false,  Type::OffsetBot);
   358   }
   359   TypeAryPtr::BYTES   = TypeAryPtr::make(TypePtr::BotPTR, TypeAry::make(TypeInt::BYTE      ,TypeInt::POS), ciTypeArrayKlass::make(T_BYTE),   true,  Type::OffsetBot);
   360   TypeAryPtr::SHORTS  = TypeAryPtr::make(TypePtr::BotPTR, TypeAry::make(TypeInt::SHORT     ,TypeInt::POS), ciTypeArrayKlass::make(T_SHORT),  true,  Type::OffsetBot);
   361   TypeAryPtr::CHARS   = TypeAryPtr::make(TypePtr::BotPTR, TypeAry::make(TypeInt::CHAR      ,TypeInt::POS), ciTypeArrayKlass::make(T_CHAR),   true,  Type::OffsetBot);
   362   TypeAryPtr::INTS    = TypeAryPtr::make(TypePtr::BotPTR, TypeAry::make(TypeInt::INT       ,TypeInt::POS), ciTypeArrayKlass::make(T_INT),    true,  Type::OffsetBot);
   363   TypeAryPtr::LONGS   = TypeAryPtr::make(TypePtr::BotPTR, TypeAry::make(TypeLong::LONG     ,TypeInt::POS), ciTypeArrayKlass::make(T_LONG),   true,  Type::OffsetBot);
   364   TypeAryPtr::FLOATS  = TypeAryPtr::make(TypePtr::BotPTR, TypeAry::make(Type::FLOAT        ,TypeInt::POS), ciTypeArrayKlass::make(T_FLOAT),  true,  Type::OffsetBot);
   365   TypeAryPtr::DOUBLES = TypeAryPtr::make(TypePtr::BotPTR, TypeAry::make(Type::DOUBLE       ,TypeInt::POS), ciTypeArrayKlass::make(T_DOUBLE), true,  Type::OffsetBot);
   367   // Nobody should ask _array_body_type[T_NARROWOOP]. Use NULL as assert.
   368   TypeAryPtr::_array_body_type[T_NARROWOOP] = NULL;
   369   TypeAryPtr::_array_body_type[T_OBJECT]  = TypeAryPtr::OOPS;
   370   TypeAryPtr::_array_body_type[T_ARRAY]   = TypeAryPtr::OOPS; // arrays are stored in oop arrays
   371   TypeAryPtr::_array_body_type[T_BYTE]    = TypeAryPtr::BYTES;
   372   TypeAryPtr::_array_body_type[T_BOOLEAN] = TypeAryPtr::BYTES;  // boolean[] is a byte array
   373   TypeAryPtr::_array_body_type[T_SHORT]   = TypeAryPtr::SHORTS;
   374   TypeAryPtr::_array_body_type[T_CHAR]    = TypeAryPtr::CHARS;
   375   TypeAryPtr::_array_body_type[T_INT]     = TypeAryPtr::INTS;
   376   TypeAryPtr::_array_body_type[T_LONG]    = TypeAryPtr::LONGS;
   377   TypeAryPtr::_array_body_type[T_FLOAT]   = TypeAryPtr::FLOATS;
   378   TypeAryPtr::_array_body_type[T_DOUBLE]  = TypeAryPtr::DOUBLES;
   380   TypeKlassPtr::OBJECT = TypeKlassPtr::make( TypePtr::NotNull, current->env()->Object_klass(), 0 );
   381   TypeKlassPtr::OBJECT_OR_NULL = TypeKlassPtr::make( TypePtr::BotPTR, current->env()->Object_klass(), 0 );
   383   const Type **fi2c = TypeTuple::fields(2);
   384   fi2c[TypeFunc::Parms+0] = TypeInstPtr::BOTTOM; // Method*
   385   fi2c[TypeFunc::Parms+1] = TypeRawPtr::BOTTOM; // argument pointer
   386   TypeTuple::START_I2C = TypeTuple::make(TypeFunc::Parms+2, fi2c);
   388   const Type **intpair = TypeTuple::fields(2);
   389   intpair[0] = TypeInt::INT;
   390   intpair[1] = TypeInt::INT;
   391   TypeTuple::INT_PAIR = TypeTuple::make(2, intpair);
   393   const Type **longpair = TypeTuple::fields(2);
   394   longpair[0] = TypeLong::LONG;
   395   longpair[1] = TypeLong::LONG;
   396   TypeTuple::LONG_PAIR = TypeTuple::make(2, longpair);
   398   _const_basic_type[T_NARROWOOP] = TypeNarrowOop::BOTTOM;
   399   _const_basic_type[T_BOOLEAN] = TypeInt::BOOL;
   400   _const_basic_type[T_CHAR]    = TypeInt::CHAR;
   401   _const_basic_type[T_BYTE]    = TypeInt::BYTE;
   402   _const_basic_type[T_SHORT]   = TypeInt::SHORT;
   403   _const_basic_type[T_INT]     = TypeInt::INT;
   404   _const_basic_type[T_LONG]    = TypeLong::LONG;
   405   _const_basic_type[T_FLOAT]   = Type::FLOAT;
   406   _const_basic_type[T_DOUBLE]  = Type::DOUBLE;
   407   _const_basic_type[T_OBJECT]  = TypeInstPtr::BOTTOM;
   408   _const_basic_type[T_ARRAY]   = TypeInstPtr::BOTTOM; // there is no separate bottom for arrays
   409   _const_basic_type[T_VOID]    = TypePtr::NULL_PTR;   // reflection represents void this way
   410   _const_basic_type[T_ADDRESS] = TypeRawPtr::BOTTOM;  // both interpreter return addresses & random raw ptrs
   411   _const_basic_type[T_CONFLICT]= Type::BOTTOM;        // why not?
   413   _zero_type[T_NARROWOOP] = TypeNarrowOop::NULL_PTR;
   414   _zero_type[T_BOOLEAN] = TypeInt::ZERO;     // false == 0
   415   _zero_type[T_CHAR]    = TypeInt::ZERO;     // '\0' == 0
   416   _zero_type[T_BYTE]    = TypeInt::ZERO;     // 0x00 == 0
   417   _zero_type[T_SHORT]   = TypeInt::ZERO;     // 0x0000 == 0
   418   _zero_type[T_INT]     = TypeInt::ZERO;
   419   _zero_type[T_LONG]    = TypeLong::ZERO;
   420   _zero_type[T_FLOAT]   = TypeF::ZERO;
   421   _zero_type[T_DOUBLE]  = TypeD::ZERO;
   422   _zero_type[T_OBJECT]  = TypePtr::NULL_PTR;
   423   _zero_type[T_ARRAY]   = TypePtr::NULL_PTR; // null array is null oop
   424   _zero_type[T_ADDRESS] = TypePtr::NULL_PTR; // raw pointers use the same null
   425   _zero_type[T_VOID]    = Type::TOP;         // the only void value is no value at all
   427   // get_zero_type() should not happen for T_CONFLICT
   428   _zero_type[T_CONFLICT]= NULL;
   430   // Vector predefined types, it needs initialized _const_basic_type[].
   431   if (Matcher::vector_size_supported(T_BYTE,4)) {
   432     TypeVect::VECTS = TypeVect::make(T_BYTE,4);
   433   }
   434   if (Matcher::vector_size_supported(T_FLOAT,2)) {
   435     TypeVect::VECTD = TypeVect::make(T_FLOAT,2);
   436   }
   437   if (Matcher::vector_size_supported(T_FLOAT,4)) {
   438     TypeVect::VECTX = TypeVect::make(T_FLOAT,4);
   439   }
   440   if (Matcher::vector_size_supported(T_FLOAT,8)) {
   441     TypeVect::VECTY = TypeVect::make(T_FLOAT,8);
   442   }
   443   mreg2type[Op_VecS] = TypeVect::VECTS;
   444   mreg2type[Op_VecD] = TypeVect::VECTD;
   445   mreg2type[Op_VecX] = TypeVect::VECTX;
   446   mreg2type[Op_VecY] = TypeVect::VECTY;
   448   // Restore working type arena.
   449   current->set_type_arena(save);
   450   current->set_type_dict(NULL);
   451 }
   453 //------------------------------Initialize-------------------------------------
   454 void Type::Initialize(Compile* current) {
   455   assert(current->type_arena() != NULL, "must have created type arena");
   457   if (_shared_type_dict == NULL) {
   458     Initialize_shared(current);
   459   }
   461   Arena* type_arena = current->type_arena();
   463   // Create the hash-cons'ing dictionary with top-level storage allocation
   464   Dict *tdic = new (type_arena) Dict( (CmpKey)Type::cmp,(Hash)Type::uhash, type_arena, 128 );
   465   current->set_type_dict(tdic);
   467   // Transfer the shared types.
   468   DictI i(_shared_type_dict);
   469   for( ; i.test(); ++i ) {
   470     Type* t = (Type*)i._value;
   471     tdic->Insert(t,t);  // New Type, insert into Type table
   472   }
   473 }
   475 //------------------------------hashcons---------------------------------------
   476 // Do the hash-cons trick.  If the Type already exists in the type table,
   477 // delete the current Type and return the existing Type.  Otherwise stick the
   478 // current Type in the Type table.
   479 const Type *Type::hashcons(void) {
   480   debug_only(base());           // Check the assertion in Type::base().
   481   // Look up the Type in the Type dictionary
   482   Dict *tdic = type_dict();
   483   Type* old = (Type*)(tdic->Insert(this, this, false));
   484   if( old ) {                   // Pre-existing Type?
   485     if( old != this )           // Yes, this guy is not the pre-existing?
   486       delete this;              // Yes, Nuke this guy
   487     assert( old->_dual, "" );
   488     return old;                 // Return pre-existing
   489   }
   491   // Every type has a dual (to make my lattice symmetric).
   492   // Since we just discovered a new Type, compute its dual right now.
   493   assert( !_dual, "" );         // No dual yet
   494   _dual = xdual();              // Compute the dual
   495   if( cmp(this,_dual)==0 ) {    // Handle self-symmetric
   496     _dual = this;
   497     return this;
   498   }
   499   assert( !_dual->_dual, "" );  // No reverse dual yet
   500   assert( !(*tdic)[_dual], "" ); // Dual not in type system either
   501   // New Type, insert into Type table
   502   tdic->Insert((void*)_dual,(void*)_dual);
   503   ((Type*)_dual)->_dual = this; // Finish up being symmetric
   504 #ifdef ASSERT
   505   Type *dual_dual = (Type*)_dual->xdual();
   506   assert( eq(dual_dual), "xdual(xdual()) should be identity" );
   507   delete dual_dual;
   508 #endif
   509   return this;                  // Return new Type
   510 }
   512 //------------------------------eq---------------------------------------------
   513 // Structural equality check for Type representations
   514 bool Type::eq( const Type * ) const {
   515   return true;                  // Nothing else can go wrong
   516 }
   518 //------------------------------hash-------------------------------------------
   519 // Type-specific hashing function.
   520 int Type::hash(void) const {
   521   return _base;
   522 }
   524 //------------------------------is_finite--------------------------------------
   525 // Has a finite value
   526 bool Type::is_finite() const {
   527   return false;
   528 }
   530 //------------------------------is_nan-----------------------------------------
   531 // Is not a number (NaN)
   532 bool Type::is_nan()    const {
   533   return false;
   534 }
   536 //----------------------interface_vs_oop---------------------------------------
   537 #ifdef ASSERT
   538 bool Type::interface_vs_oop(const Type *t) const {
   539   bool result = false;
   541   const TypePtr* this_ptr = this->make_ptr(); // In case it is narrow_oop
   542   const TypePtr*    t_ptr =    t->make_ptr();
   543   if( this_ptr == NULL || t_ptr == NULL )
   544     return result;
   546   const TypeInstPtr* this_inst = this_ptr->isa_instptr();
   547   const TypeInstPtr*    t_inst =    t_ptr->isa_instptr();
   548   if( this_inst && this_inst->is_loaded() && t_inst && t_inst->is_loaded() ) {
   549     bool this_interface = this_inst->klass()->is_interface();
   550     bool    t_interface =    t_inst->klass()->is_interface();
   551     result = this_interface ^ t_interface;
   552   }
   554   return result;
   555 }
   556 #endif
   558 //------------------------------meet-------------------------------------------
   559 // Compute the MEET of two types.  NOT virtual.  It enforces that meet is
   560 // commutative and the lattice is symmetric.
   561 const Type *Type::meet( const Type *t ) const {
   562   if (isa_narrowoop() && t->isa_narrowoop()) {
   563     const Type* result = make_ptr()->meet(t->make_ptr());
   564     return result->make_narrowoop();
   565   }
   567   const Type *mt = xmeet(t);
   568   if (isa_narrowoop() || t->isa_narrowoop()) return mt;
   569 #ifdef ASSERT
   570   assert( mt == t->xmeet(this), "meet not commutative" );
   571   const Type* dual_join = mt->_dual;
   572   const Type *t2t    = dual_join->xmeet(t->_dual);
   573   const Type *t2this = dual_join->xmeet(   _dual);
   575   // Interface meet Oop is Not Symmetric:
   576   // Interface:AnyNull meet Oop:AnyNull == Interface:AnyNull
   577   // Interface:NotNull meet Oop:NotNull == java/lang/Object:NotNull
   579   if( !interface_vs_oop(t) && (t2t != t->_dual || t2this != _dual) ) {
   580     tty->print_cr("=== Meet Not Symmetric ===");
   581     tty->print("t   =                   ");         t->dump(); tty->cr();
   582     tty->print("this=                   ");            dump(); tty->cr();
   583     tty->print("mt=(t meet this)=       ");        mt->dump(); tty->cr();
   585     tty->print("t_dual=                 ");  t->_dual->dump(); tty->cr();
   586     tty->print("this_dual=              ");     _dual->dump(); tty->cr();
   587     tty->print("mt_dual=                "); mt->_dual->dump(); tty->cr();
   589     tty->print("mt_dual meet t_dual=    "); t2t      ->dump(); tty->cr();
   590     tty->print("mt_dual meet this_dual= "); t2this   ->dump(); tty->cr();
   592     fatal("meet not symmetric" );
   593   }
   594 #endif
   595   return mt;
   596 }
   598 //------------------------------xmeet------------------------------------------
   599 // Compute the MEET of two types.  It returns a new Type object.
   600 const Type *Type::xmeet( const Type *t ) const {
   601   // Perform a fast test for common case; meeting the same types together.
   602   if( this == t ) return this;  // Meeting same type-rep?
   604   // Meeting TOP with anything?
   605   if( _base == Top ) return t;
   607   // Meeting BOTTOM with anything?
   608   if( _base == Bottom ) return BOTTOM;
   610   // Current "this->_base" is one of: Bad, Multi, Control, Top,
   611   // Abio, Abstore, Floatxxx, Doublexxx, Bottom, lastype.
   612   switch (t->base()) {  // Switch on original type
   614   // Cut in half the number of cases I must handle.  Only need cases for when
   615   // the given enum "t->type" is less than or equal to the local enum "type".
   616   case FloatCon:
   617   case DoubleCon:
   618   case Int:
   619   case Long:
   620     return t->xmeet(this);
   622   case OopPtr:
   623     return t->xmeet(this);
   625   case InstPtr:
   626     return t->xmeet(this);
   628   case MetadataPtr:
   629   case KlassPtr:
   630     return t->xmeet(this);
   632   case AryPtr:
   633     return t->xmeet(this);
   635   case NarrowOop:
   636     return t->xmeet(this);
   638   case Bad:                     // Type check
   639   default:                      // Bogus type not in lattice
   640     typerr(t);
   641     return Type::BOTTOM;
   643   case Bottom:                  // Ye Olde Default
   644     return t;
   646   case FloatTop:
   647     if( _base == FloatTop ) return this;
   648   case FloatBot:                // Float
   649     if( _base == FloatBot || _base == FloatTop ) return FLOAT;
   650     if( _base == DoubleTop || _base == DoubleBot ) return Type::BOTTOM;
   651     typerr(t);
   652     return Type::BOTTOM;
   654   case DoubleTop:
   655     if( _base == DoubleTop ) return this;
   656   case DoubleBot:               // Double
   657     if( _base == DoubleBot || _base == DoubleTop ) return DOUBLE;
   658     if( _base == FloatTop || _base == FloatBot ) return Type::BOTTOM;
   659     typerr(t);
   660     return Type::BOTTOM;
   662   // These next few cases must match exactly or it is a compile-time error.
   663   case Control:                 // Control of code
   664   case Abio:                    // State of world outside of program
   665   case Memory:
   666     if( _base == t->_base )  return this;
   667     typerr(t);
   668     return Type::BOTTOM;
   670   case Top:                     // Top of the lattice
   671     return this;
   672   }
   674   // The type is unchanged
   675   return this;
   676 }
   678 //-----------------------------filter------------------------------------------
   679 const Type *Type::filter( const Type *kills ) const {
   680   const Type* ft = join(kills);
   681   if (ft->empty())
   682     return Type::TOP;           // Canonical empty value
   683   return ft;
   684 }
   686 //------------------------------xdual------------------------------------------
   687 // Compute dual right now.
   688 const Type::TYPES Type::dual_type[Type::lastype] = {
   689   Bad,          // Bad
   690   Control,      // Control
   691   Bottom,       // Top
   692   Bad,          // Int - handled in v-call
   693   Bad,          // Long - handled in v-call
   694   Half,         // Half
   695   Bad,          // NarrowOop - handled in v-call
   697   Bad,          // Tuple - handled in v-call
   698   Bad,          // Array - handled in v-call
   699   Bad,          // VectorS - handled in v-call
   700   Bad,          // VectorD - handled in v-call
   701   Bad,          // VectorX - handled in v-call
   702   Bad,          // VectorY - handled in v-call
   704   Bad,          // AnyPtr - handled in v-call
   705   Bad,          // RawPtr - handled in v-call
   706   Bad,          // OopPtr - handled in v-call
   707   Bad,          // InstPtr - handled in v-call
   708   Bad,          // AryPtr - handled in v-call
   710   Bad,          //  MetadataPtr - handled in v-call
   711   Bad,          // KlassPtr - handled in v-call
   713   Bad,          // Function - handled in v-call
   714   Abio,         // Abio
   715   Return_Address,// Return_Address
   716   Memory,       // Memory
   717   FloatBot,     // FloatTop
   718   FloatCon,     // FloatCon
   719   FloatTop,     // FloatBot
   720   DoubleBot,    // DoubleTop
   721   DoubleCon,    // DoubleCon
   722   DoubleTop,    // DoubleBot
   723   Top           // Bottom
   724 };
   726 const Type *Type::xdual() const {
   727   // Note: the base() accessor asserts the sanity of _base.
   728   assert(_type_info[base()].dual_type != Bad, "implement with v-call");
   729   return new Type(_type_info[_base].dual_type);
   730 }
   732 //------------------------------has_memory-------------------------------------
   733 bool Type::has_memory() const {
   734   Type::TYPES tx = base();
   735   if (tx == Memory) return true;
   736   if (tx == Tuple) {
   737     const TypeTuple *t = is_tuple();
   738     for (uint i=0; i < t->cnt(); i++) {
   739       tx = t->field_at(i)->base();
   740       if (tx == Memory)  return true;
   741     }
   742   }
   743   return false;
   744 }
   746 #ifndef PRODUCT
   747 //------------------------------dump2------------------------------------------
   748 void Type::dump2( Dict &d, uint depth, outputStream *st ) const {
   749   st->print(_type_info[_base].msg);
   750 }
   752 //------------------------------dump-------------------------------------------
   753 void Type::dump_on(outputStream *st) const {
   754   ResourceMark rm;
   755   Dict d(cmpkey,hashkey);       // Stop recursive type dumping
   756   dump2(d,1, st);
   757   if (is_ptr_to_narrowoop()) {
   758     st->print(" [narrow]");
   759   }
   760 }
   761 #endif
   763 //------------------------------singleton--------------------------------------
   764 // TRUE if Type is a singleton type, FALSE otherwise.   Singletons are simple
   765 // constants (Ldi nodes).  Singletons are integer, float or double constants.
   766 bool Type::singleton(void) const {
   767   return _base == Top || _base == Half;
   768 }
   770 //------------------------------empty------------------------------------------
   771 // TRUE if Type is a type with no values, FALSE otherwise.
   772 bool Type::empty(void) const {
   773   switch (_base) {
   774   case DoubleTop:
   775   case FloatTop:
   776   case Top:
   777     return true;
   779   case Half:
   780   case Abio:
   781   case Return_Address:
   782   case Memory:
   783   case Bottom:
   784   case FloatBot:
   785   case DoubleBot:
   786     return false;  // never a singleton, therefore never empty
   787   }
   789   ShouldNotReachHere();
   790   return false;
   791 }
   793 //------------------------------dump_stats-------------------------------------
   794 // Dump collected statistics to stderr
   795 #ifndef PRODUCT
   796 void Type::dump_stats() {
   797   tty->print("Types made: %d\n", type_dict()->Size());
   798 }
   799 #endif
   801 //------------------------------typerr-----------------------------------------
   802 void Type::typerr( const Type *t ) const {
   803 #ifndef PRODUCT
   804   tty->print("\nError mixing types: ");
   805   dump();
   806   tty->print(" and ");
   807   t->dump();
   808   tty->print("\n");
   809 #endif
   810   ShouldNotReachHere();
   811 }
   814 //=============================================================================
   815 // Convenience common pre-built types.
   816 const TypeF *TypeF::ZERO;       // Floating point zero
   817 const TypeF *TypeF::ONE;        // Floating point one
   819 //------------------------------make-------------------------------------------
   820 // Create a float constant
   821 const TypeF *TypeF::make(float f) {
   822   return (TypeF*)(new TypeF(f))->hashcons();
   823 }
   825 //------------------------------meet-------------------------------------------
   826 // Compute the MEET of two types.  It returns a new Type object.
   827 const Type *TypeF::xmeet( const Type *t ) const {
   828   // Perform a fast test for common case; meeting the same types together.
   829   if( this == t ) return this;  // Meeting same type-rep?
   831   // Current "this->_base" is FloatCon
   832   switch (t->base()) {          // Switch on original type
   833   case AnyPtr:                  // Mixing with oops happens when javac
   834   case RawPtr:                  // reuses local variables
   835   case OopPtr:
   836   case InstPtr:
   837   case AryPtr:
   838   case MetadataPtr:
   839   case KlassPtr:
   840   case NarrowOop:
   841   case Int:
   842   case Long:
   843   case DoubleTop:
   844   case DoubleCon:
   845   case DoubleBot:
   846   case Bottom:                  // Ye Olde Default
   847     return Type::BOTTOM;
   849   case FloatBot:
   850     return t;
   852   default:                      // All else is a mistake
   853     typerr(t);
   855   case FloatCon:                // Float-constant vs Float-constant?
   856     if( jint_cast(_f) != jint_cast(t->getf()) )         // unequal constants?
   857                                 // must compare bitwise as positive zero, negative zero and NaN have
   858                                 // all the same representation in C++
   859       return FLOAT;             // Return generic float
   860                                 // Equal constants
   861   case Top:
   862   case FloatTop:
   863     break;                      // Return the float constant
   864   }
   865   return this;                  // Return the float constant
   866 }
   868 //------------------------------xdual------------------------------------------
   869 // Dual: symmetric
   870 const Type *TypeF::xdual() const {
   871   return this;
   872 }
   874 //------------------------------eq---------------------------------------------
   875 // Structural equality check for Type representations
   876 bool TypeF::eq( const Type *t ) const {
   877   if( g_isnan(_f) ||
   878       g_isnan(t->getf()) ) {
   879     // One or both are NANs.  If both are NANs return true, else false.
   880     return (g_isnan(_f) && g_isnan(t->getf()));
   881   }
   882   if (_f == t->getf()) {
   883     // (NaN is impossible at this point, since it is not equal even to itself)
   884     if (_f == 0.0) {
   885       // difference between positive and negative zero
   886       if (jint_cast(_f) != jint_cast(t->getf()))  return false;
   887     }
   888     return true;
   889   }
   890   return false;
   891 }
   893 //------------------------------hash-------------------------------------------
   894 // Type-specific hashing function.
   895 int TypeF::hash(void) const {
   896   return *(int*)(&_f);
   897 }
   899 //------------------------------is_finite--------------------------------------
   900 // Has a finite value
   901 bool TypeF::is_finite() const {
   902   return g_isfinite(getf()) != 0;
   903 }
   905 //------------------------------is_nan-----------------------------------------
   906 // Is not a number (NaN)
   907 bool TypeF::is_nan()    const {
   908   return g_isnan(getf()) != 0;
   909 }
   911 //------------------------------dump2------------------------------------------
   912 // Dump float constant Type
   913 #ifndef PRODUCT
   914 void TypeF::dump2( Dict &d, uint depth, outputStream *st ) const {
   915   Type::dump2(d,depth, st);
   916   st->print("%f", _f);
   917 }
   918 #endif
   920 //------------------------------singleton--------------------------------------
   921 // TRUE if Type is a singleton type, FALSE otherwise.   Singletons are simple
   922 // constants (Ldi nodes).  Singletons are integer, float or double constants
   923 // or a single symbol.
   924 bool TypeF::singleton(void) const {
   925   return true;                  // Always a singleton
   926 }
   928 bool TypeF::empty(void) const {
   929   return false;                 // always exactly a singleton
   930 }
   932 //=============================================================================
   933 // Convenience common pre-built types.
   934 const TypeD *TypeD::ZERO;       // Floating point zero
   935 const TypeD *TypeD::ONE;        // Floating point one
   937 //------------------------------make-------------------------------------------
   938 const TypeD *TypeD::make(double d) {
   939   return (TypeD*)(new TypeD(d))->hashcons();
   940 }
   942 //------------------------------meet-------------------------------------------
   943 // Compute the MEET of two types.  It returns a new Type object.
   944 const Type *TypeD::xmeet( const Type *t ) const {
   945   // Perform a fast test for common case; meeting the same types together.
   946   if( this == t ) return this;  // Meeting same type-rep?
   948   // Current "this->_base" is DoubleCon
   949   switch (t->base()) {          // Switch on original type
   950   case AnyPtr:                  // Mixing with oops happens when javac
   951   case RawPtr:                  // reuses local variables
   952   case OopPtr:
   953   case InstPtr:
   954   case AryPtr:
   955   case MetadataPtr:
   956   case KlassPtr:
   957   case NarrowOop:
   958   case Int:
   959   case Long:
   960   case FloatTop:
   961   case FloatCon:
   962   case FloatBot:
   963   case Bottom:                  // Ye Olde Default
   964     return Type::BOTTOM;
   966   case DoubleBot:
   967     return t;
   969   default:                      // All else is a mistake
   970     typerr(t);
   972   case DoubleCon:               // Double-constant vs Double-constant?
   973     if( jlong_cast(_d) != jlong_cast(t->getd()) )       // unequal constants? (see comment in TypeF::xmeet)
   974       return DOUBLE;            // Return generic double
   975   case Top:
   976   case DoubleTop:
   977     break;
   978   }
   979   return this;                  // Return the double constant
   980 }
   982 //------------------------------xdual------------------------------------------
   983 // Dual: symmetric
   984 const Type *TypeD::xdual() const {
   985   return this;
   986 }
   988 //------------------------------eq---------------------------------------------
   989 // Structural equality check for Type representations
   990 bool TypeD::eq( const Type *t ) const {
   991   if( g_isnan(_d) ||
   992       g_isnan(t->getd()) ) {
   993     // One or both are NANs.  If both are NANs return true, else false.
   994     return (g_isnan(_d) && g_isnan(t->getd()));
   995   }
   996   if (_d == t->getd()) {
   997     // (NaN is impossible at this point, since it is not equal even to itself)
   998     if (_d == 0.0) {
   999       // difference between positive and negative zero
  1000       if (jlong_cast(_d) != jlong_cast(t->getd()))  return false;
  1002     return true;
  1004   return false;
  1007 //------------------------------hash-------------------------------------------
  1008 // Type-specific hashing function.
  1009 int TypeD::hash(void) const {
  1010   return *(int*)(&_d);
  1013 //------------------------------is_finite--------------------------------------
  1014 // Has a finite value
  1015 bool TypeD::is_finite() const {
  1016   return g_isfinite(getd()) != 0;
  1019 //------------------------------is_nan-----------------------------------------
  1020 // Is not a number (NaN)
  1021 bool TypeD::is_nan()    const {
  1022   return g_isnan(getd()) != 0;
  1025 //------------------------------dump2------------------------------------------
  1026 // Dump double constant Type
  1027 #ifndef PRODUCT
  1028 void TypeD::dump2( Dict &d, uint depth, outputStream *st ) const {
  1029   Type::dump2(d,depth,st);
  1030   st->print("%f", _d);
  1032 #endif
  1034 //------------------------------singleton--------------------------------------
  1035 // TRUE if Type is a singleton type, FALSE otherwise.   Singletons are simple
  1036 // constants (Ldi nodes).  Singletons are integer, float or double constants
  1037 // or a single symbol.
  1038 bool TypeD::singleton(void) const {
  1039   return true;                  // Always a singleton
  1042 bool TypeD::empty(void) const {
  1043   return false;                 // always exactly a singleton
  1046 //=============================================================================
  1047 // Convience common pre-built types.
  1048 const TypeInt *TypeInt::MINUS_1;// -1
  1049 const TypeInt *TypeInt::ZERO;   // 0
  1050 const TypeInt *TypeInt::ONE;    // 1
  1051 const TypeInt *TypeInt::BOOL;   // 0 or 1, FALSE or TRUE.
  1052 const TypeInt *TypeInt::CC;     // -1,0 or 1, condition codes
  1053 const TypeInt *TypeInt::CC_LT;  // [-1]  == MINUS_1
  1054 const TypeInt *TypeInt::CC_GT;  // [1]   == ONE
  1055 const TypeInt *TypeInt::CC_EQ;  // [0]   == ZERO
  1056 const TypeInt *TypeInt::CC_LE;  // [-1,0]
  1057 const TypeInt *TypeInt::CC_GE;  // [0,1] == BOOL (!)
  1058 const TypeInt *TypeInt::BYTE;   // Bytes, -128 to 127
  1059 const TypeInt *TypeInt::UBYTE;  // Unsigned Bytes, 0 to 255
  1060 const TypeInt *TypeInt::CHAR;   // Java chars, 0-65535
  1061 const TypeInt *TypeInt::SHORT;  // Java shorts, -32768-32767
  1062 const TypeInt *TypeInt::POS;    // Positive 32-bit integers or zero
  1063 const TypeInt *TypeInt::POS1;   // Positive 32-bit integers
  1064 const TypeInt *TypeInt::INT;    // 32-bit integers
  1065 const TypeInt *TypeInt::SYMINT; // symmetric range [-max_jint..max_jint]
  1067 //------------------------------TypeInt----------------------------------------
  1068 TypeInt::TypeInt( jint lo, jint hi, int w ) : Type(Int), _lo(lo), _hi(hi), _widen(w) {
  1071 //------------------------------make-------------------------------------------
  1072 const TypeInt *TypeInt::make( jint lo ) {
  1073   return (TypeInt*)(new TypeInt(lo,lo,WidenMin))->hashcons();
  1076 static int normalize_int_widen( jint lo, jint hi, int w ) {
  1077   // Certain normalizations keep us sane when comparing types.
  1078   // The 'SMALLINT' covers constants and also CC and its relatives.
  1079   if (lo <= hi) {
  1080     if ((juint)(hi - lo) <= SMALLINT)  w = Type::WidenMin;
  1081     if ((juint)(hi - lo) >= max_juint) w = Type::WidenMax; // TypeInt::INT
  1082   } else {
  1083     if ((juint)(lo - hi) <= SMALLINT)  w = Type::WidenMin;
  1084     if ((juint)(lo - hi) >= max_juint) w = Type::WidenMin; // dual TypeInt::INT
  1086   return w;
  1089 const TypeInt *TypeInt::make( jint lo, jint hi, int w ) {
  1090   w = normalize_int_widen(lo, hi, w);
  1091   return (TypeInt*)(new TypeInt(lo,hi,w))->hashcons();
  1094 //------------------------------meet-------------------------------------------
  1095 // Compute the MEET of two types.  It returns a new Type representation object
  1096 // with reference count equal to the number of Types pointing at it.
  1097 // Caller should wrap a Types around it.
  1098 const Type *TypeInt::xmeet( const Type *t ) const {
  1099   // Perform a fast test for common case; meeting the same types together.
  1100   if( this == t ) return this;  // Meeting same type?
  1102   // Currently "this->_base" is a TypeInt
  1103   switch (t->base()) {          // Switch on original type
  1104   case AnyPtr:                  // Mixing with oops happens when javac
  1105   case RawPtr:                  // reuses local variables
  1106   case OopPtr:
  1107   case InstPtr:
  1108   case AryPtr:
  1109   case MetadataPtr:
  1110   case KlassPtr:
  1111   case NarrowOop:
  1112   case Long:
  1113   case FloatTop:
  1114   case FloatCon:
  1115   case FloatBot:
  1116   case DoubleTop:
  1117   case DoubleCon:
  1118   case DoubleBot:
  1119   case Bottom:                  // Ye Olde Default
  1120     return Type::BOTTOM;
  1121   default:                      // All else is a mistake
  1122     typerr(t);
  1123   case Top:                     // No change
  1124     return this;
  1125   case Int:                     // Int vs Int?
  1126     break;
  1129   // Expand covered set
  1130   const TypeInt *r = t->is_int();
  1131   return make( MIN2(_lo,r->_lo), MAX2(_hi,r->_hi), MAX2(_widen,r->_widen) );
  1134 //------------------------------xdual------------------------------------------
  1135 // Dual: reverse hi & lo; flip widen
  1136 const Type *TypeInt::xdual() const {
  1137   int w = normalize_int_widen(_hi,_lo, WidenMax-_widen);
  1138   return new TypeInt(_hi,_lo,w);
  1141 //------------------------------widen------------------------------------------
  1142 // Only happens for optimistic top-down optimizations.
  1143 const Type *TypeInt::widen( const Type *old, const Type* limit ) const {
  1144   // Coming from TOP or such; no widening
  1145   if( old->base() != Int ) return this;
  1146   const TypeInt *ot = old->is_int();
  1148   // If new guy is equal to old guy, no widening
  1149   if( _lo == ot->_lo && _hi == ot->_hi )
  1150     return old;
  1152   // If new guy contains old, then we widened
  1153   if( _lo <= ot->_lo && _hi >= ot->_hi ) {
  1154     // New contains old
  1155     // If new guy is already wider than old, no widening
  1156     if( _widen > ot->_widen ) return this;
  1157     // If old guy was a constant, do not bother
  1158     if (ot->_lo == ot->_hi)  return this;
  1159     // Now widen new guy.
  1160     // Check for widening too far
  1161     if (_widen == WidenMax) {
  1162       int max = max_jint;
  1163       int min = min_jint;
  1164       if (limit->isa_int()) {
  1165         max = limit->is_int()->_hi;
  1166         min = limit->is_int()->_lo;
  1168       if (min < _lo && _hi < max) {
  1169         // If neither endpoint is extremal yet, push out the endpoint
  1170         // which is closer to its respective limit.
  1171         if (_lo >= 0 ||                 // easy common case
  1172             (juint)(_lo - min) >= (juint)(max - _hi)) {
  1173           // Try to widen to an unsigned range type of 31 bits:
  1174           return make(_lo, max, WidenMax);
  1175         } else {
  1176           return make(min, _hi, WidenMax);
  1179       return TypeInt::INT;
  1181     // Returned widened new guy
  1182     return make(_lo,_hi,_widen+1);
  1185   // If old guy contains new, then we probably widened too far & dropped to
  1186   // bottom.  Return the wider fellow.
  1187   if ( ot->_lo <= _lo && ot->_hi >= _hi )
  1188     return old;
  1190   //fatal("Integer value range is not subset");
  1191   //return this;
  1192   return TypeInt::INT;
  1195 //------------------------------narrow---------------------------------------
  1196 // Only happens for pessimistic optimizations.
  1197 const Type *TypeInt::narrow( const Type *old ) const {
  1198   if (_lo >= _hi)  return this;   // already narrow enough
  1199   if (old == NULL)  return this;
  1200   const TypeInt* ot = old->isa_int();
  1201   if (ot == NULL)  return this;
  1202   jint olo = ot->_lo;
  1203   jint ohi = ot->_hi;
  1205   // If new guy is equal to old guy, no narrowing
  1206   if (_lo == olo && _hi == ohi)  return old;
  1208   // If old guy was maximum range, allow the narrowing
  1209   if (olo == min_jint && ohi == max_jint)  return this;
  1211   if (_lo < olo || _hi > ohi)
  1212     return this;                // doesn't narrow; pretty wierd
  1214   // The new type narrows the old type, so look for a "death march".
  1215   // See comments on PhaseTransform::saturate.
  1216   juint nrange = _hi - _lo;
  1217   juint orange = ohi - olo;
  1218   if (nrange < max_juint - 1 && nrange > (orange >> 1) + (SMALLINT*2)) {
  1219     // Use the new type only if the range shrinks a lot.
  1220     // We do not want the optimizer computing 2^31 point by point.
  1221     return old;
  1224   return this;
  1227 //-----------------------------filter------------------------------------------
  1228 const Type *TypeInt::filter( const Type *kills ) const {
  1229   const TypeInt* ft = join(kills)->isa_int();
  1230   if (ft == NULL || ft->empty())
  1231     return Type::TOP;           // Canonical empty value
  1232   if (ft->_widen < this->_widen) {
  1233     // Do not allow the value of kill->_widen to affect the outcome.
  1234     // The widen bits must be allowed to run freely through the graph.
  1235     ft = TypeInt::make(ft->_lo, ft->_hi, this->_widen);
  1237   return ft;
  1240 //------------------------------eq---------------------------------------------
  1241 // Structural equality check for Type representations
  1242 bool TypeInt::eq( const Type *t ) const {
  1243   const TypeInt *r = t->is_int(); // Handy access
  1244   return r->_lo == _lo && r->_hi == _hi && r->_widen == _widen;
  1247 //------------------------------hash-------------------------------------------
  1248 // Type-specific hashing function.
  1249 int TypeInt::hash(void) const {
  1250   return _lo+_hi+_widen+(int)Type::Int;
  1253 //------------------------------is_finite--------------------------------------
  1254 // Has a finite value
  1255 bool TypeInt::is_finite() const {
  1256   return true;
  1259 //------------------------------dump2------------------------------------------
  1260 // Dump TypeInt
  1261 #ifndef PRODUCT
  1262 static const char* intname(char* buf, jint n) {
  1263   if (n == min_jint)
  1264     return "min";
  1265   else if (n < min_jint + 10000)
  1266     sprintf(buf, "min+" INT32_FORMAT, n - min_jint);
  1267   else if (n == max_jint)
  1268     return "max";
  1269   else if (n > max_jint - 10000)
  1270     sprintf(buf, "max-" INT32_FORMAT, max_jint - n);
  1271   else
  1272     sprintf(buf, INT32_FORMAT, n);
  1273   return buf;
  1276 void TypeInt::dump2( Dict &d, uint depth, outputStream *st ) const {
  1277   char buf[40], buf2[40];
  1278   if (_lo == min_jint && _hi == max_jint)
  1279     st->print("int");
  1280   else if (is_con())
  1281     st->print("int:%s", intname(buf, get_con()));
  1282   else if (_lo == BOOL->_lo && _hi == BOOL->_hi)
  1283     st->print("bool");
  1284   else if (_lo == BYTE->_lo && _hi == BYTE->_hi)
  1285     st->print("byte");
  1286   else if (_lo == CHAR->_lo && _hi == CHAR->_hi)
  1287     st->print("char");
  1288   else if (_lo == SHORT->_lo && _hi == SHORT->_hi)
  1289     st->print("short");
  1290   else if (_hi == max_jint)
  1291     st->print("int:>=%s", intname(buf, _lo));
  1292   else if (_lo == min_jint)
  1293     st->print("int:<=%s", intname(buf, _hi));
  1294   else
  1295     st->print("int:%s..%s", intname(buf, _lo), intname(buf2, _hi));
  1297   if (_widen != 0 && this != TypeInt::INT)
  1298     st->print(":%.*s", _widen, "wwww");
  1300 #endif
  1302 //------------------------------singleton--------------------------------------
  1303 // TRUE if Type is a singleton type, FALSE otherwise.   Singletons are simple
  1304 // constants.
  1305 bool TypeInt::singleton(void) const {
  1306   return _lo >= _hi;
  1309 bool TypeInt::empty(void) const {
  1310   return _lo > _hi;
  1313 //=============================================================================
  1314 // Convenience common pre-built types.
  1315 const TypeLong *TypeLong::MINUS_1;// -1
  1316 const TypeLong *TypeLong::ZERO; // 0
  1317 const TypeLong *TypeLong::ONE;  // 1
  1318 const TypeLong *TypeLong::POS;  // >=0
  1319 const TypeLong *TypeLong::LONG; // 64-bit integers
  1320 const TypeLong *TypeLong::INT;  // 32-bit subrange
  1321 const TypeLong *TypeLong::UINT; // 32-bit unsigned subrange
  1323 //------------------------------TypeLong---------------------------------------
  1324 TypeLong::TypeLong( jlong lo, jlong hi, int w ) : Type(Long), _lo(lo), _hi(hi), _widen(w) {
  1327 //------------------------------make-------------------------------------------
  1328 const TypeLong *TypeLong::make( jlong lo ) {
  1329   return (TypeLong*)(new TypeLong(lo,lo,WidenMin))->hashcons();
  1332 static int normalize_long_widen( jlong lo, jlong hi, int w ) {
  1333   // Certain normalizations keep us sane when comparing types.
  1334   // The 'SMALLINT' covers constants.
  1335   if (lo <= hi) {
  1336     if ((julong)(hi - lo) <= SMALLINT)   w = Type::WidenMin;
  1337     if ((julong)(hi - lo) >= max_julong) w = Type::WidenMax; // TypeLong::LONG
  1338   } else {
  1339     if ((julong)(lo - hi) <= SMALLINT)   w = Type::WidenMin;
  1340     if ((julong)(lo - hi) >= max_julong) w = Type::WidenMin; // dual TypeLong::LONG
  1342   return w;
  1345 const TypeLong *TypeLong::make( jlong lo, jlong hi, int w ) {
  1346   w = normalize_long_widen(lo, hi, w);
  1347   return (TypeLong*)(new TypeLong(lo,hi,w))->hashcons();
  1351 //------------------------------meet-------------------------------------------
  1352 // Compute the MEET of two types.  It returns a new Type representation object
  1353 // with reference count equal to the number of Types pointing at it.
  1354 // Caller should wrap a Types around it.
  1355 const Type *TypeLong::xmeet( const Type *t ) const {
  1356   // Perform a fast test for common case; meeting the same types together.
  1357   if( this == t ) return this;  // Meeting same type?
  1359   // Currently "this->_base" is a TypeLong
  1360   switch (t->base()) {          // Switch on original type
  1361   case AnyPtr:                  // Mixing with oops happens when javac
  1362   case RawPtr:                  // reuses local variables
  1363   case OopPtr:
  1364   case InstPtr:
  1365   case AryPtr:
  1366   case MetadataPtr:
  1367   case KlassPtr:
  1368   case NarrowOop:
  1369   case Int:
  1370   case FloatTop:
  1371   case FloatCon:
  1372   case FloatBot:
  1373   case DoubleTop:
  1374   case DoubleCon:
  1375   case DoubleBot:
  1376   case Bottom:                  // Ye Olde Default
  1377     return Type::BOTTOM;
  1378   default:                      // All else is a mistake
  1379     typerr(t);
  1380   case Top:                     // No change
  1381     return this;
  1382   case Long:                    // Long vs Long?
  1383     break;
  1386   // Expand covered set
  1387   const TypeLong *r = t->is_long(); // Turn into a TypeLong
  1388   return make( MIN2(_lo,r->_lo), MAX2(_hi,r->_hi), MAX2(_widen,r->_widen) );
  1391 //------------------------------xdual------------------------------------------
  1392 // Dual: reverse hi & lo; flip widen
  1393 const Type *TypeLong::xdual() const {
  1394   int w = normalize_long_widen(_hi,_lo, WidenMax-_widen);
  1395   return new TypeLong(_hi,_lo,w);
  1398 //------------------------------widen------------------------------------------
  1399 // Only happens for optimistic top-down optimizations.
  1400 const Type *TypeLong::widen( const Type *old, const Type* limit ) const {
  1401   // Coming from TOP or such; no widening
  1402   if( old->base() != Long ) return this;
  1403   const TypeLong *ot = old->is_long();
  1405   // If new guy is equal to old guy, no widening
  1406   if( _lo == ot->_lo && _hi == ot->_hi )
  1407     return old;
  1409   // If new guy contains old, then we widened
  1410   if( _lo <= ot->_lo && _hi >= ot->_hi ) {
  1411     // New contains old
  1412     // If new guy is already wider than old, no widening
  1413     if( _widen > ot->_widen ) return this;
  1414     // If old guy was a constant, do not bother
  1415     if (ot->_lo == ot->_hi)  return this;
  1416     // Now widen new guy.
  1417     // Check for widening too far
  1418     if (_widen == WidenMax) {
  1419       jlong max = max_jlong;
  1420       jlong min = min_jlong;
  1421       if (limit->isa_long()) {
  1422         max = limit->is_long()->_hi;
  1423         min = limit->is_long()->_lo;
  1425       if (min < _lo && _hi < max) {
  1426         // If neither endpoint is extremal yet, push out the endpoint
  1427         // which is closer to its respective limit.
  1428         if (_lo >= 0 ||                 // easy common case
  1429             (julong)(_lo - min) >= (julong)(max - _hi)) {
  1430           // Try to widen to an unsigned range type of 32/63 bits:
  1431           if (max >= max_juint && _hi < max_juint)
  1432             return make(_lo, max_juint, WidenMax);
  1433           else
  1434             return make(_lo, max, WidenMax);
  1435         } else {
  1436           return make(min, _hi, WidenMax);
  1439       return TypeLong::LONG;
  1441     // Returned widened new guy
  1442     return make(_lo,_hi,_widen+1);
  1445   // If old guy contains new, then we probably widened too far & dropped to
  1446   // bottom.  Return the wider fellow.
  1447   if ( ot->_lo <= _lo && ot->_hi >= _hi )
  1448     return old;
  1450   //  fatal("Long value range is not subset");
  1451   // return this;
  1452   return TypeLong::LONG;
  1455 //------------------------------narrow----------------------------------------
  1456 // Only happens for pessimistic optimizations.
  1457 const Type *TypeLong::narrow( const Type *old ) const {
  1458   if (_lo >= _hi)  return this;   // already narrow enough
  1459   if (old == NULL)  return this;
  1460   const TypeLong* ot = old->isa_long();
  1461   if (ot == NULL)  return this;
  1462   jlong olo = ot->_lo;
  1463   jlong ohi = ot->_hi;
  1465   // If new guy is equal to old guy, no narrowing
  1466   if (_lo == olo && _hi == ohi)  return old;
  1468   // If old guy was maximum range, allow the narrowing
  1469   if (olo == min_jlong && ohi == max_jlong)  return this;
  1471   if (_lo < olo || _hi > ohi)
  1472     return this;                // doesn't narrow; pretty wierd
  1474   // The new type narrows the old type, so look for a "death march".
  1475   // See comments on PhaseTransform::saturate.
  1476   julong nrange = _hi - _lo;
  1477   julong orange = ohi - olo;
  1478   if (nrange < max_julong - 1 && nrange > (orange >> 1) + (SMALLINT*2)) {
  1479     // Use the new type only if the range shrinks a lot.
  1480     // We do not want the optimizer computing 2^31 point by point.
  1481     return old;
  1484   return this;
  1487 //-----------------------------filter------------------------------------------
  1488 const Type *TypeLong::filter( const Type *kills ) const {
  1489   const TypeLong* ft = join(kills)->isa_long();
  1490   if (ft == NULL || ft->empty())
  1491     return Type::TOP;           // Canonical empty value
  1492   if (ft->_widen < this->_widen) {
  1493     // Do not allow the value of kill->_widen to affect the outcome.
  1494     // The widen bits must be allowed to run freely through the graph.
  1495     ft = TypeLong::make(ft->_lo, ft->_hi, this->_widen);
  1497   return ft;
  1500 //------------------------------eq---------------------------------------------
  1501 // Structural equality check for Type representations
  1502 bool TypeLong::eq( const Type *t ) const {
  1503   const TypeLong *r = t->is_long(); // Handy access
  1504   return r->_lo == _lo &&  r->_hi == _hi  && r->_widen == _widen;
  1507 //------------------------------hash-------------------------------------------
  1508 // Type-specific hashing function.
  1509 int TypeLong::hash(void) const {
  1510   return (int)(_lo+_hi+_widen+(int)Type::Long);
  1513 //------------------------------is_finite--------------------------------------
  1514 // Has a finite value
  1515 bool TypeLong::is_finite() const {
  1516   return true;
  1519 //------------------------------dump2------------------------------------------
  1520 // Dump TypeLong
  1521 #ifndef PRODUCT
  1522 static const char* longnamenear(jlong x, const char* xname, char* buf, jlong n) {
  1523   if (n > x) {
  1524     if (n >= x + 10000)  return NULL;
  1525     sprintf(buf, "%s+" INT64_FORMAT, xname, n - x);
  1526   } else if (n < x) {
  1527     if (n <= x - 10000)  return NULL;
  1528     sprintf(buf, "%s-" INT64_FORMAT, xname, x - n);
  1529   } else {
  1530     return xname;
  1532   return buf;
  1535 static const char* longname(char* buf, jlong n) {
  1536   const char* str;
  1537   if (n == min_jlong)
  1538     return "min";
  1539   else if (n < min_jlong + 10000)
  1540     sprintf(buf, "min+" INT64_FORMAT, n - min_jlong);
  1541   else if (n == max_jlong)
  1542     return "max";
  1543   else if (n > max_jlong - 10000)
  1544     sprintf(buf, "max-" INT64_FORMAT, max_jlong - n);
  1545   else if ((str = longnamenear(max_juint, "maxuint", buf, n)) != NULL)
  1546     return str;
  1547   else if ((str = longnamenear(max_jint, "maxint", buf, n)) != NULL)
  1548     return str;
  1549   else if ((str = longnamenear(min_jint, "minint", buf, n)) != NULL)
  1550     return str;
  1551   else
  1552     sprintf(buf, INT64_FORMAT, n);
  1553   return buf;
  1556 void TypeLong::dump2( Dict &d, uint depth, outputStream *st ) const {
  1557   char buf[80], buf2[80];
  1558   if (_lo == min_jlong && _hi == max_jlong)
  1559     st->print("long");
  1560   else if (is_con())
  1561     st->print("long:%s", longname(buf, get_con()));
  1562   else if (_hi == max_jlong)
  1563     st->print("long:>=%s", longname(buf, _lo));
  1564   else if (_lo == min_jlong)
  1565     st->print("long:<=%s", longname(buf, _hi));
  1566   else
  1567     st->print("long:%s..%s", longname(buf, _lo), longname(buf2, _hi));
  1569   if (_widen != 0 && this != TypeLong::LONG)
  1570     st->print(":%.*s", _widen, "wwww");
  1572 #endif
  1574 //------------------------------singleton--------------------------------------
  1575 // TRUE if Type is a singleton type, FALSE otherwise.   Singletons are simple
  1576 // constants
  1577 bool TypeLong::singleton(void) const {
  1578   return _lo >= _hi;
  1581 bool TypeLong::empty(void) const {
  1582   return _lo > _hi;
  1585 //=============================================================================
  1586 // Convenience common pre-built types.
  1587 const TypeTuple *TypeTuple::IFBOTH;     // Return both arms of IF as reachable
  1588 const TypeTuple *TypeTuple::IFFALSE;
  1589 const TypeTuple *TypeTuple::IFTRUE;
  1590 const TypeTuple *TypeTuple::IFNEITHER;
  1591 const TypeTuple *TypeTuple::LOOPBODY;
  1592 const TypeTuple *TypeTuple::MEMBAR;
  1593 const TypeTuple *TypeTuple::STORECONDITIONAL;
  1594 const TypeTuple *TypeTuple::START_I2C;
  1595 const TypeTuple *TypeTuple::INT_PAIR;
  1596 const TypeTuple *TypeTuple::LONG_PAIR;
  1599 //------------------------------make-------------------------------------------
  1600 // Make a TypeTuple from the range of a method signature
  1601 const TypeTuple *TypeTuple::make_range(ciSignature* sig) {
  1602   ciType* return_type = sig->return_type();
  1603   uint total_fields = TypeFunc::Parms + return_type->size();
  1604   const Type **field_array = fields(total_fields);
  1605   switch (return_type->basic_type()) {
  1606   case T_LONG:
  1607     field_array[TypeFunc::Parms]   = TypeLong::LONG;
  1608     field_array[TypeFunc::Parms+1] = Type::HALF;
  1609     break;
  1610   case T_DOUBLE:
  1611     field_array[TypeFunc::Parms]   = Type::DOUBLE;
  1612     field_array[TypeFunc::Parms+1] = Type::HALF;
  1613     break;
  1614   case T_OBJECT:
  1615   case T_ARRAY:
  1616   case T_BOOLEAN:
  1617   case T_CHAR:
  1618   case T_FLOAT:
  1619   case T_BYTE:
  1620   case T_SHORT:
  1621   case T_INT:
  1622     field_array[TypeFunc::Parms] = get_const_type(return_type);
  1623     break;
  1624   case T_VOID:
  1625     break;
  1626   default:
  1627     ShouldNotReachHere();
  1629   return (TypeTuple*)(new TypeTuple(total_fields,field_array))->hashcons();
  1632 // Make a TypeTuple from the domain of a method signature
  1633 const TypeTuple *TypeTuple::make_domain(ciInstanceKlass* recv, ciSignature* sig) {
  1634   uint total_fields = TypeFunc::Parms + sig->size();
  1636   uint pos = TypeFunc::Parms;
  1637   const Type **field_array;
  1638   if (recv != NULL) {
  1639     total_fields++;
  1640     field_array = fields(total_fields);
  1641     // Use get_const_type here because it respects UseUniqueSubclasses:
  1642     field_array[pos++] = get_const_type(recv)->join(TypePtr::NOTNULL);
  1643   } else {
  1644     field_array = fields(total_fields);
  1647   int i = 0;
  1648   while (pos < total_fields) {
  1649     ciType* type = sig->type_at(i);
  1651     switch (type->basic_type()) {
  1652     case T_LONG:
  1653       field_array[pos++] = TypeLong::LONG;
  1654       field_array[pos++] = Type::HALF;
  1655       break;
  1656     case T_DOUBLE:
  1657       field_array[pos++] = Type::DOUBLE;
  1658       field_array[pos++] = Type::HALF;
  1659       break;
  1660     case T_OBJECT:
  1661     case T_ARRAY:
  1662     case T_BOOLEAN:
  1663     case T_CHAR:
  1664     case T_FLOAT:
  1665     case T_BYTE:
  1666     case T_SHORT:
  1667     case T_INT:
  1668       field_array[pos++] = get_const_type(type);
  1669       break;
  1670     default:
  1671       ShouldNotReachHere();
  1673     i++;
  1675   return (TypeTuple*)(new TypeTuple(total_fields,field_array))->hashcons();
  1678 const TypeTuple *TypeTuple::make( uint cnt, const Type **fields ) {
  1679   return (TypeTuple*)(new TypeTuple(cnt,fields))->hashcons();
  1682 //------------------------------fields-----------------------------------------
  1683 // Subroutine call type with space allocated for argument types
  1684 const Type **TypeTuple::fields( uint arg_cnt ) {
  1685   const Type **flds = (const Type **)(Compile::current()->type_arena()->Amalloc_4((TypeFunc::Parms+arg_cnt)*sizeof(Type*) ));
  1686   flds[TypeFunc::Control  ] = Type::CONTROL;
  1687   flds[TypeFunc::I_O      ] = Type::ABIO;
  1688   flds[TypeFunc::Memory   ] = Type::MEMORY;
  1689   flds[TypeFunc::FramePtr ] = TypeRawPtr::BOTTOM;
  1690   flds[TypeFunc::ReturnAdr] = Type::RETURN_ADDRESS;
  1692   return flds;
  1695 //------------------------------meet-------------------------------------------
  1696 // Compute the MEET of two types.  It returns a new Type object.
  1697 const Type *TypeTuple::xmeet( const Type *t ) const {
  1698   // Perform a fast test for common case; meeting the same types together.
  1699   if( this == t ) return this;  // Meeting same type-rep?
  1701   // Current "this->_base" is Tuple
  1702   switch (t->base()) {          // switch on original type
  1704   case Bottom:                  // Ye Olde Default
  1705     return t;
  1707   default:                      // All else is a mistake
  1708     typerr(t);
  1710   case Tuple: {                 // Meeting 2 signatures?
  1711     const TypeTuple *x = t->is_tuple();
  1712     assert( _cnt == x->_cnt, "" );
  1713     const Type **fields = (const Type **)(Compile::current()->type_arena()->Amalloc_4( _cnt*sizeof(Type*) ));
  1714     for( uint i=0; i<_cnt; i++ )
  1715       fields[i] = field_at(i)->xmeet( x->field_at(i) );
  1716     return TypeTuple::make(_cnt,fields);
  1718   case Top:
  1719     break;
  1721   return this;                  // Return the double constant
  1724 //------------------------------xdual------------------------------------------
  1725 // Dual: compute field-by-field dual
  1726 const Type *TypeTuple::xdual() const {
  1727   const Type **fields = (const Type **)(Compile::current()->type_arena()->Amalloc_4( _cnt*sizeof(Type*) ));
  1728   for( uint i=0; i<_cnt; i++ )
  1729     fields[i] = _fields[i]->dual();
  1730   return new TypeTuple(_cnt,fields);
  1733 //------------------------------eq---------------------------------------------
  1734 // Structural equality check for Type representations
  1735 bool TypeTuple::eq( const Type *t ) const {
  1736   const TypeTuple *s = (const TypeTuple *)t;
  1737   if (_cnt != s->_cnt)  return false;  // Unequal field counts
  1738   for (uint i = 0; i < _cnt; i++)
  1739     if (field_at(i) != s->field_at(i)) // POINTER COMPARE!  NO RECURSION!
  1740       return false;             // Missed
  1741   return true;
  1744 //------------------------------hash-------------------------------------------
  1745 // Type-specific hashing function.
  1746 int TypeTuple::hash(void) const {
  1747   intptr_t sum = _cnt;
  1748   for( uint i=0; i<_cnt; i++ )
  1749     sum += (intptr_t)_fields[i];     // Hash on pointers directly
  1750   return sum;
  1753 //------------------------------dump2------------------------------------------
  1754 // Dump signature Type
  1755 #ifndef PRODUCT
  1756 void TypeTuple::dump2( Dict &d, uint depth, outputStream *st ) const {
  1757   st->print("{");
  1758   if( !depth || d[this] ) {     // Check for recursive print
  1759     st->print("...}");
  1760     return;
  1762   d.Insert((void*)this, (void*)this);   // Stop recursion
  1763   if( _cnt ) {
  1764     uint i;
  1765     for( i=0; i<_cnt-1; i++ ) {
  1766       st->print("%d:", i);
  1767       _fields[i]->dump2(d, depth-1, st);
  1768       st->print(", ");
  1770     st->print("%d:", i);
  1771     _fields[i]->dump2(d, depth-1, st);
  1773   st->print("}");
  1775 #endif
  1777 //------------------------------singleton--------------------------------------
  1778 // TRUE if Type is a singleton type, FALSE otherwise.   Singletons are simple
  1779 // constants (Ldi nodes).  Singletons are integer, float or double constants
  1780 // or a single symbol.
  1781 bool TypeTuple::singleton(void) const {
  1782   return false;                 // Never a singleton
  1785 bool TypeTuple::empty(void) const {
  1786   for( uint i=0; i<_cnt; i++ ) {
  1787     if (_fields[i]->empty())  return true;
  1789   return false;
  1792 //=============================================================================
  1793 // Convenience common pre-built types.
  1795 inline const TypeInt* normalize_array_size(const TypeInt* size) {
  1796   // Certain normalizations keep us sane when comparing types.
  1797   // We do not want arrayOop variables to differ only by the wideness
  1798   // of their index types.  Pick minimum wideness, since that is the
  1799   // forced wideness of small ranges anyway.
  1800   if (size->_widen != Type::WidenMin)
  1801     return TypeInt::make(size->_lo, size->_hi, Type::WidenMin);
  1802   else
  1803     return size;
  1806 //------------------------------make-------------------------------------------
  1807 const TypeAry *TypeAry::make( const Type *elem, const TypeInt *size) {
  1808   if (UseCompressedOops && elem->isa_oopptr()) {
  1809     elem = elem->make_narrowoop();
  1811   size = normalize_array_size(size);
  1812   return (TypeAry*)(new TypeAry(elem,size))->hashcons();
  1815 //------------------------------meet-------------------------------------------
  1816 // Compute the MEET of two types.  It returns a new Type object.
  1817 const Type *TypeAry::xmeet( const Type *t ) const {
  1818   // Perform a fast test for common case; meeting the same types together.
  1819   if( this == t ) return this;  // Meeting same type-rep?
  1821   // Current "this->_base" is Ary
  1822   switch (t->base()) {          // switch on original type
  1824   case Bottom:                  // Ye Olde Default
  1825     return t;
  1827   default:                      // All else is a mistake
  1828     typerr(t);
  1830   case Array: {                 // Meeting 2 arrays?
  1831     const TypeAry *a = t->is_ary();
  1832     return TypeAry::make(_elem->meet(a->_elem),
  1833                          _size->xmeet(a->_size)->is_int());
  1835   case Top:
  1836     break;
  1838   return this;                  // Return the double constant
  1841 //------------------------------xdual------------------------------------------
  1842 // Dual: compute field-by-field dual
  1843 const Type *TypeAry::xdual() const {
  1844   const TypeInt* size_dual = _size->dual()->is_int();
  1845   size_dual = normalize_array_size(size_dual);
  1846   return new TypeAry( _elem->dual(), size_dual);
  1849 //------------------------------eq---------------------------------------------
  1850 // Structural equality check for Type representations
  1851 bool TypeAry::eq( const Type *t ) const {
  1852   const TypeAry *a = (const TypeAry*)t;
  1853   return _elem == a->_elem &&
  1854     _size == a->_size;
  1857 //------------------------------hash-------------------------------------------
  1858 // Type-specific hashing function.
  1859 int TypeAry::hash(void) const {
  1860   return (intptr_t)_elem + (intptr_t)_size;
  1863 //----------------------interface_vs_oop---------------------------------------
  1864 #ifdef ASSERT
  1865 bool TypeAry::interface_vs_oop(const Type *t) const {
  1866   const TypeAry* t_ary = t->is_ary();
  1867   if (t_ary) {
  1868     return _elem->interface_vs_oop(t_ary->_elem);
  1870   return false;
  1872 #endif
  1874 //------------------------------dump2------------------------------------------
  1875 #ifndef PRODUCT
  1876 void TypeAry::dump2( Dict &d, uint depth, outputStream *st ) const {
  1877   _elem->dump2(d, depth, st);
  1878   st->print("[");
  1879   _size->dump2(d, depth, st);
  1880   st->print("]");
  1882 #endif
  1884 //------------------------------singleton--------------------------------------
  1885 // TRUE if Type is a singleton type, FALSE otherwise.   Singletons are simple
  1886 // constants (Ldi nodes).  Singletons are integer, float or double constants
  1887 // or a single symbol.
  1888 bool TypeAry::singleton(void) const {
  1889   return false;                 // Never a singleton
  1892 bool TypeAry::empty(void) const {
  1893   return _elem->empty() || _size->empty();
  1896 //--------------------------ary_must_be_exact----------------------------------
  1897 bool TypeAry::ary_must_be_exact() const {
  1898   if (!UseExactTypes)       return false;
  1899   // This logic looks at the element type of an array, and returns true
  1900   // if the element type is either a primitive or a final instance class.
  1901   // In such cases, an array built on this ary must have no subclasses.
  1902   if (_elem == BOTTOM)      return false;  // general array not exact
  1903   if (_elem == TOP   )      return false;  // inverted general array not exact
  1904   const TypeOopPtr*  toop = NULL;
  1905   if (UseCompressedOops && _elem->isa_narrowoop()) {
  1906     toop = _elem->make_ptr()->isa_oopptr();
  1907   } else {
  1908     toop = _elem->isa_oopptr();
  1910   if (!toop)                return true;   // a primitive type, like int
  1911   ciKlass* tklass = toop->klass();
  1912   if (tklass == NULL)       return false;  // unloaded class
  1913   if (!tklass->is_loaded()) return false;  // unloaded class
  1914   const TypeInstPtr* tinst;
  1915   if (_elem->isa_narrowoop())
  1916     tinst = _elem->make_ptr()->isa_instptr();
  1917   else
  1918     tinst = _elem->isa_instptr();
  1919   if (tinst)
  1920     return tklass->as_instance_klass()->is_final();
  1921   const TypeAryPtr*  tap;
  1922   if (_elem->isa_narrowoop())
  1923     tap = _elem->make_ptr()->isa_aryptr();
  1924   else
  1925     tap = _elem->isa_aryptr();
  1926   if (tap)
  1927     return tap->ary()->ary_must_be_exact();
  1928   return false;
  1931 //==============================TypeVect=======================================
  1932 // Convenience common pre-built types.
  1933 const TypeVect *TypeVect::VECTS = NULL; //  32-bit vectors
  1934 const TypeVect *TypeVect::VECTD = NULL; //  64-bit vectors
  1935 const TypeVect *TypeVect::VECTX = NULL; // 128-bit vectors
  1936 const TypeVect *TypeVect::VECTY = NULL; // 256-bit vectors
  1938 //------------------------------make-------------------------------------------
  1939 const TypeVect* TypeVect::make(const Type *elem, uint length) {
  1940   BasicType elem_bt = elem->array_element_basic_type();
  1941   assert(is_java_primitive(elem_bt), "only primitive types in vector");
  1942   assert(length > 1 && is_power_of_2(length), "vector length is power of 2");
  1943   assert(Matcher::vector_size_supported(elem_bt, length), "length in range");
  1944   int size = length * type2aelembytes(elem_bt);
  1945   switch (Matcher::vector_ideal_reg(size)) {
  1946   case Op_VecS:
  1947     return (TypeVect*)(new TypeVectS(elem, length))->hashcons();
  1948   case Op_VecD:
  1949   case Op_RegD:
  1950     return (TypeVect*)(new TypeVectD(elem, length))->hashcons();
  1951   case Op_VecX:
  1952     return (TypeVect*)(new TypeVectX(elem, length))->hashcons();
  1953   case Op_VecY:
  1954     return (TypeVect*)(new TypeVectY(elem, length))->hashcons();
  1956  ShouldNotReachHere();
  1957   return NULL;
  1960 //------------------------------meet-------------------------------------------
  1961 // Compute the MEET of two types.  It returns a new Type object.
  1962 const Type *TypeVect::xmeet( const Type *t ) const {
  1963   // Perform a fast test for common case; meeting the same types together.
  1964   if( this == t ) return this;  // Meeting same type-rep?
  1966   // Current "this->_base" is Vector
  1967   switch (t->base()) {          // switch on original type
  1969   case Bottom:                  // Ye Olde Default
  1970     return t;
  1972   default:                      // All else is a mistake
  1973     typerr(t);
  1975   case VectorS:
  1976   case VectorD:
  1977   case VectorX:
  1978   case VectorY: {                // Meeting 2 vectors?
  1979     const TypeVect* v = t->is_vect();
  1980     assert(  base() == v->base(), "");
  1981     assert(length() == v->length(), "");
  1982     assert(element_basic_type() == v->element_basic_type(), "");
  1983     return TypeVect::make(_elem->xmeet(v->_elem), _length);
  1985   case Top:
  1986     break;
  1988   return this;
  1991 //------------------------------xdual------------------------------------------
  1992 // Dual: compute field-by-field dual
  1993 const Type *TypeVect::xdual() const {
  1994   return new TypeVect(base(), _elem->dual(), _length);
  1997 //------------------------------eq---------------------------------------------
  1998 // Structural equality check for Type representations
  1999 bool TypeVect::eq(const Type *t) const {
  2000   const TypeVect *v = t->is_vect();
  2001   return (_elem == v->_elem) && (_length == v->_length);
  2004 //------------------------------hash-------------------------------------------
  2005 // Type-specific hashing function.
  2006 int TypeVect::hash(void) const {
  2007   return (intptr_t)_elem + (intptr_t)_length;
  2010 //------------------------------singleton--------------------------------------
  2011 // TRUE if Type is a singleton type, FALSE otherwise.   Singletons are simple
  2012 // constants (Ldi nodes).  Vector is singleton if all elements are the same
  2013 // constant value (when vector is created with Replicate code).
  2014 bool TypeVect::singleton(void) const {
  2015 // There is no Con node for vectors yet.
  2016 //  return _elem->singleton();
  2017   return false;
  2020 bool TypeVect::empty(void) const {
  2021   return _elem->empty();
  2024 //------------------------------dump2------------------------------------------
  2025 #ifndef PRODUCT
  2026 void TypeVect::dump2(Dict &d, uint depth, outputStream *st) const {
  2027   switch (base()) {
  2028   case VectorS:
  2029     st->print("vectors["); break;
  2030   case VectorD:
  2031     st->print("vectord["); break;
  2032   case VectorX:
  2033     st->print("vectorx["); break;
  2034   case VectorY:
  2035     st->print("vectory["); break;
  2036   default:
  2037     ShouldNotReachHere();
  2039   st->print("%d]:{", _length);
  2040   _elem->dump2(d, depth, st);
  2041   st->print("}");
  2043 #endif
  2046 //=============================================================================
  2047 // Convenience common pre-built types.
  2048 const TypePtr *TypePtr::NULL_PTR;
  2049 const TypePtr *TypePtr::NOTNULL;
  2050 const TypePtr *TypePtr::BOTTOM;
  2052 //------------------------------meet-------------------------------------------
  2053 // Meet over the PTR enum
  2054 const TypePtr::PTR TypePtr::ptr_meet[TypePtr::lastPTR][TypePtr::lastPTR] = {
  2055   //              TopPTR,    AnyNull,   Constant, Null,   NotNull, BotPTR,
  2056   { /* Top     */ TopPTR,    AnyNull,   Constant, Null,   NotNull, BotPTR,},
  2057   { /* AnyNull */ AnyNull,   AnyNull,   Constant, BotPTR, NotNull, BotPTR,},
  2058   { /* Constant*/ Constant,  Constant,  Constant, BotPTR, NotNull, BotPTR,},
  2059   { /* Null    */ Null,      BotPTR,    BotPTR,   Null,   BotPTR,  BotPTR,},
  2060   { /* NotNull */ NotNull,   NotNull,   NotNull,  BotPTR, NotNull, BotPTR,},
  2061   { /* BotPTR  */ BotPTR,    BotPTR,    BotPTR,   BotPTR, BotPTR,  BotPTR,}
  2062 };
  2064 //------------------------------make-------------------------------------------
  2065 const TypePtr *TypePtr::make( TYPES t, enum PTR ptr, int offset ) {
  2066   return (TypePtr*)(new TypePtr(t,ptr,offset))->hashcons();
  2069 //------------------------------cast_to_ptr_type-------------------------------
  2070 const Type *TypePtr::cast_to_ptr_type(PTR ptr) const {
  2071   assert(_base == AnyPtr, "subclass must override cast_to_ptr_type");
  2072   if( ptr == _ptr ) return this;
  2073   return make(_base, ptr, _offset);
  2076 //------------------------------get_con----------------------------------------
  2077 intptr_t TypePtr::get_con() const {
  2078   assert( _ptr == Null, "" );
  2079   return _offset;
  2082 //------------------------------meet-------------------------------------------
  2083 // Compute the MEET of two types.  It returns a new Type object.
  2084 const Type *TypePtr::xmeet( const Type *t ) const {
  2085   // Perform a fast test for common case; meeting the same types together.
  2086   if( this == t ) return this;  // Meeting same type-rep?
  2088   // Current "this->_base" is AnyPtr
  2089   switch (t->base()) {          // switch on original type
  2090   case Int:                     // Mixing ints & oops happens when javac
  2091   case Long:                    // reuses local variables
  2092   case FloatTop:
  2093   case FloatCon:
  2094   case FloatBot:
  2095   case DoubleTop:
  2096   case DoubleCon:
  2097   case DoubleBot:
  2098   case NarrowOop:
  2099   case Bottom:                  // Ye Olde Default
  2100     return Type::BOTTOM;
  2101   case Top:
  2102     return this;
  2104   case AnyPtr: {                // Meeting to AnyPtrs
  2105     const TypePtr *tp = t->is_ptr();
  2106     return make( AnyPtr, meet_ptr(tp->ptr()), meet_offset(tp->offset()) );
  2108   case RawPtr:                  // For these, flip the call around to cut down
  2109   case OopPtr:
  2110   case InstPtr:                 // on the cases I have to handle.
  2111   case AryPtr:
  2112   case MetadataPtr:
  2113   case KlassPtr:
  2114     return t->xmeet(this);      // Call in reverse direction
  2115   default:                      // All else is a mistake
  2116     typerr(t);
  2119   return this;
  2122 //------------------------------meet_offset------------------------------------
  2123 int TypePtr::meet_offset( int offset ) const {
  2124   // Either is 'TOP' offset?  Return the other offset!
  2125   if( _offset == OffsetTop ) return offset;
  2126   if( offset == OffsetTop ) return _offset;
  2127   // If either is different, return 'BOTTOM' offset
  2128   if( _offset != offset ) return OffsetBot;
  2129   return _offset;
  2132 //------------------------------dual_offset------------------------------------
  2133 int TypePtr::dual_offset( ) const {
  2134   if( _offset == OffsetTop ) return OffsetBot;// Map 'TOP' into 'BOTTOM'
  2135   if( _offset == OffsetBot ) return OffsetTop;// Map 'BOTTOM' into 'TOP'
  2136   return _offset;               // Map everything else into self
  2139 //------------------------------xdual------------------------------------------
  2140 // Dual: compute field-by-field dual
  2141 const TypePtr::PTR TypePtr::ptr_dual[TypePtr::lastPTR] = {
  2142   BotPTR, NotNull, Constant, Null, AnyNull, TopPTR
  2143 };
  2144 const Type *TypePtr::xdual() const {
  2145   return new TypePtr( AnyPtr, dual_ptr(), dual_offset() );
  2148 //------------------------------xadd_offset------------------------------------
  2149 int TypePtr::xadd_offset( intptr_t offset ) const {
  2150   // Adding to 'TOP' offset?  Return 'TOP'!
  2151   if( _offset == OffsetTop || offset == OffsetTop ) return OffsetTop;
  2152   // Adding to 'BOTTOM' offset?  Return 'BOTTOM'!
  2153   if( _offset == OffsetBot || offset == OffsetBot ) return OffsetBot;
  2154   // Addition overflows or "accidentally" equals to OffsetTop? Return 'BOTTOM'!
  2155   offset += (intptr_t)_offset;
  2156   if (offset != (int)offset || offset == OffsetTop) return OffsetBot;
  2158   // assert( _offset >= 0 && _offset+offset >= 0, "" );
  2159   // It is possible to construct a negative offset during PhaseCCP
  2161   return (int)offset;        // Sum valid offsets
  2164 //------------------------------add_offset-------------------------------------
  2165 const TypePtr *TypePtr::add_offset( intptr_t offset ) const {
  2166   return make( AnyPtr, _ptr, xadd_offset(offset) );
  2169 //------------------------------eq---------------------------------------------
  2170 // Structural equality check for Type representations
  2171 bool TypePtr::eq( const Type *t ) const {
  2172   const TypePtr *a = (const TypePtr*)t;
  2173   return _ptr == a->ptr() && _offset == a->offset();
  2176 //------------------------------hash-------------------------------------------
  2177 // Type-specific hashing function.
  2178 int TypePtr::hash(void) const {
  2179   return _ptr + _offset;
  2182 //------------------------------dump2------------------------------------------
  2183 const char *const TypePtr::ptr_msg[TypePtr::lastPTR] = {
  2184   "TopPTR","AnyNull","Constant","NULL","NotNull","BotPTR"
  2185 };
  2187 #ifndef PRODUCT
  2188 void TypePtr::dump2( Dict &d, uint depth, outputStream *st ) const {
  2189   if( _ptr == Null ) st->print("NULL");
  2190   else st->print("%s *", ptr_msg[_ptr]);
  2191   if( _offset == OffsetTop ) st->print("+top");
  2192   else if( _offset == OffsetBot ) st->print("+bot");
  2193   else if( _offset ) st->print("+%d", _offset);
  2195 #endif
  2197 //------------------------------singleton--------------------------------------
  2198 // TRUE if Type is a singleton type, FALSE otherwise.   Singletons are simple
  2199 // constants
  2200 bool TypePtr::singleton(void) const {
  2201   // TopPTR, Null, AnyNull, Constant are all singletons
  2202   return (_offset != OffsetBot) && !below_centerline(_ptr);
  2205 bool TypePtr::empty(void) const {
  2206   return (_offset == OffsetTop) || above_centerline(_ptr);
  2209 //=============================================================================
  2210 // Convenience common pre-built types.
  2211 const TypeRawPtr *TypeRawPtr::BOTTOM;
  2212 const TypeRawPtr *TypeRawPtr::NOTNULL;
  2214 //------------------------------make-------------------------------------------
  2215 const TypeRawPtr *TypeRawPtr::make( enum PTR ptr ) {
  2216   assert( ptr != Constant, "what is the constant?" );
  2217   assert( ptr != Null, "Use TypePtr for NULL" );
  2218   return (TypeRawPtr*)(new TypeRawPtr(ptr,0))->hashcons();
  2221 const TypeRawPtr *TypeRawPtr::make( address bits ) {
  2222   assert( bits, "Use TypePtr for NULL" );
  2223   return (TypeRawPtr*)(new TypeRawPtr(Constant,bits))->hashcons();
  2226 //------------------------------cast_to_ptr_type-------------------------------
  2227 const Type *TypeRawPtr::cast_to_ptr_type(PTR ptr) const {
  2228   assert( ptr != Constant, "what is the constant?" );
  2229   assert( ptr != Null, "Use TypePtr for NULL" );
  2230   assert( _bits==0, "Why cast a constant address?");
  2231   if( ptr == _ptr ) return this;
  2232   return make(ptr);
  2235 //------------------------------get_con----------------------------------------
  2236 intptr_t TypeRawPtr::get_con() const {
  2237   assert( _ptr == Null || _ptr == Constant, "" );
  2238   return (intptr_t)_bits;
  2241 //------------------------------meet-------------------------------------------
  2242 // Compute the MEET of two types.  It returns a new Type object.
  2243 const Type *TypeRawPtr::xmeet( const Type *t ) const {
  2244   // Perform a fast test for common case; meeting the same types together.
  2245   if( this == t ) return this;  // Meeting same type-rep?
  2247   // Current "this->_base" is RawPtr
  2248   switch( t->base() ) {         // switch on original type
  2249   case Bottom:                  // Ye Olde Default
  2250     return t;
  2251   case Top:
  2252     return this;
  2253   case AnyPtr:                  // Meeting to AnyPtrs
  2254     break;
  2255   case RawPtr: {                // might be top, bot, any/not or constant
  2256     enum PTR tptr = t->is_ptr()->ptr();
  2257     enum PTR ptr = meet_ptr( tptr );
  2258     if( ptr == Constant ) {     // Cannot be equal constants, so...
  2259       if( tptr == Constant && _ptr != Constant)  return t;
  2260       if( _ptr == Constant && tptr != Constant)  return this;
  2261       ptr = NotNull;            // Fall down in lattice
  2263     return make( ptr );
  2266   case OopPtr:
  2267   case InstPtr:
  2268   case AryPtr:
  2269   case MetadataPtr:
  2270   case KlassPtr:
  2271     return TypePtr::BOTTOM;     // Oop meet raw is not well defined
  2272   default:                      // All else is a mistake
  2273     typerr(t);
  2276   // Found an AnyPtr type vs self-RawPtr type
  2277   const TypePtr *tp = t->is_ptr();
  2278   switch (tp->ptr()) {
  2279   case TypePtr::TopPTR:  return this;
  2280   case TypePtr::BotPTR:  return t;
  2281   case TypePtr::Null:
  2282     if( _ptr == TypePtr::TopPTR ) return t;
  2283     return TypeRawPtr::BOTTOM;
  2284   case TypePtr::NotNull: return TypePtr::make( AnyPtr, meet_ptr(TypePtr::NotNull), tp->meet_offset(0) );
  2285   case TypePtr::AnyNull:
  2286     if( _ptr == TypePtr::Constant) return this;
  2287     return make( meet_ptr(TypePtr::AnyNull) );
  2288   default: ShouldNotReachHere();
  2290   return this;
  2293 //------------------------------xdual------------------------------------------
  2294 // Dual: compute field-by-field dual
  2295 const Type *TypeRawPtr::xdual() const {
  2296   return new TypeRawPtr( dual_ptr(), _bits );
  2299 //------------------------------add_offset-------------------------------------
  2300 const TypePtr *TypeRawPtr::add_offset( intptr_t offset ) const {
  2301   if( offset == OffsetTop ) return BOTTOM; // Undefined offset-> undefined pointer
  2302   if( offset == OffsetBot ) return BOTTOM; // Unknown offset-> unknown pointer
  2303   if( offset == 0 ) return this; // No change
  2304   switch (_ptr) {
  2305   case TypePtr::TopPTR:
  2306   case TypePtr::BotPTR:
  2307   case TypePtr::NotNull:
  2308     return this;
  2309   case TypePtr::Null:
  2310   case TypePtr::Constant: {
  2311     address bits = _bits+offset;
  2312     if ( bits == 0 ) return TypePtr::NULL_PTR;
  2313     return make( bits );
  2315   default:  ShouldNotReachHere();
  2317   return NULL;                  // Lint noise
  2320 //------------------------------eq---------------------------------------------
  2321 // Structural equality check for Type representations
  2322 bool TypeRawPtr::eq( const Type *t ) const {
  2323   const TypeRawPtr *a = (const TypeRawPtr*)t;
  2324   return _bits == a->_bits && TypePtr::eq(t);
  2327 //------------------------------hash-------------------------------------------
  2328 // Type-specific hashing function.
  2329 int TypeRawPtr::hash(void) const {
  2330   return (intptr_t)_bits + TypePtr::hash();
  2333 //------------------------------dump2------------------------------------------
  2334 #ifndef PRODUCT
  2335 void TypeRawPtr::dump2( Dict &d, uint depth, outputStream *st ) const {
  2336   if( _ptr == Constant )
  2337     st->print(INTPTR_FORMAT, _bits);
  2338   else
  2339     st->print("rawptr:%s", ptr_msg[_ptr]);
  2341 #endif
  2343 //=============================================================================
  2344 // Convenience common pre-built type.
  2345 const TypeOopPtr *TypeOopPtr::BOTTOM;
  2347 //------------------------------TypeOopPtr-------------------------------------
  2348 TypeOopPtr::TypeOopPtr( TYPES t, PTR ptr, ciKlass* k, bool xk, ciObject* o, int offset, int instance_id )
  2349   : TypePtr(t, ptr, offset),
  2350     _const_oop(o), _klass(k),
  2351     _klass_is_exact(xk),
  2352     _is_ptr_to_narrowoop(false),
  2353     _instance_id(instance_id) {
  2354 #ifdef _LP64
  2355   if (UseCompressedOops && _offset != 0) {
  2356     if (_offset == oopDesc::klass_offset_in_bytes()) {
  2357       _is_ptr_to_narrowoop = UseCompressedKlassPointers;
  2358     } else if (klass() == NULL) {
  2359       // Array with unknown body type
  2360       assert(this->isa_aryptr(), "only arrays without klass");
  2361       _is_ptr_to_narrowoop = true;
  2362     } else if (this->isa_aryptr()) {
  2363       _is_ptr_to_narrowoop = (klass()->is_obj_array_klass() &&
  2364                              _offset != arrayOopDesc::length_offset_in_bytes());
  2365     } else if (klass()->is_instance_klass()) {
  2366       ciInstanceKlass* ik = klass()->as_instance_klass();
  2367       ciField* field = NULL;
  2368       if (this->isa_klassptr()) {
  2369         // Perm objects don't use compressed references
  2370       } else if (_offset == OffsetBot || _offset == OffsetTop) {
  2371         // unsafe access
  2372         _is_ptr_to_narrowoop = true;
  2373       } else { // exclude unsafe ops
  2374         assert(this->isa_instptr(), "must be an instance ptr.");
  2376         if (klass() == ciEnv::current()->Class_klass() &&
  2377             (_offset == java_lang_Class::klass_offset_in_bytes() ||
  2378              _offset == java_lang_Class::array_klass_offset_in_bytes())) {
  2379           // Special hidden fields from the Class.
  2380           assert(this->isa_instptr(), "must be an instance ptr.");
  2381           _is_ptr_to_narrowoop = false;
  2382         } else if (klass() == ciEnv::current()->Class_klass() &&
  2383                    _offset >= instanceMirrorKlass::offset_of_static_fields()) {
  2384           // Static fields
  2385           assert(o != NULL, "must be constant");
  2386           ciInstanceKlass* k = o->as_instance()->java_lang_Class_klass()->as_instance_klass();
  2387           ciField* field = k->get_field_by_offset(_offset, true);
  2388           assert(field != NULL, "missing field");
  2389           BasicType basic_elem_type = field->layout_type();
  2390           _is_ptr_to_narrowoop = (basic_elem_type == T_OBJECT ||
  2391                                   basic_elem_type == T_ARRAY);
  2392         } else {
  2393           // Instance fields which contains a compressed oop references.
  2394           field = ik->get_field_by_offset(_offset, false);
  2395           if (field != NULL) {
  2396             BasicType basic_elem_type = field->layout_type();
  2397             _is_ptr_to_narrowoop = (basic_elem_type == T_OBJECT ||
  2398                                     basic_elem_type == T_ARRAY);
  2399           } else if (klass()->equals(ciEnv::current()->Object_klass())) {
  2400             // Compile::find_alias_type() cast exactness on all types to verify
  2401             // that it does not affect alias type.
  2402             _is_ptr_to_narrowoop = true;
  2403           } else {
  2404             // Type for the copy start in LibraryCallKit::inline_native_clone().
  2405             _is_ptr_to_narrowoop = true;
  2411 #endif
  2414 //------------------------------make-------------------------------------------
  2415 const TypeOopPtr *TypeOopPtr::make(PTR ptr,
  2416                                    int offset, int instance_id) {
  2417   assert(ptr != Constant, "no constant generic pointers");
  2418   ciKlass*  k = Compile::current()->env()->Object_klass();
  2419   bool      xk = false;
  2420   ciObject* o = NULL;
  2421   return (TypeOopPtr*)(new TypeOopPtr(OopPtr, ptr, k, xk, o, offset, instance_id))->hashcons();
  2425 //------------------------------cast_to_ptr_type-------------------------------
  2426 const Type *TypeOopPtr::cast_to_ptr_type(PTR ptr) const {
  2427   assert(_base == OopPtr, "subclass must override cast_to_ptr_type");
  2428   if( ptr == _ptr ) return this;
  2429   return make(ptr, _offset, _instance_id);
  2432 //-----------------------------cast_to_instance_id----------------------------
  2433 const TypeOopPtr *TypeOopPtr::cast_to_instance_id(int instance_id) const {
  2434   // There are no instances of a general oop.
  2435   // Return self unchanged.
  2436   return this;
  2439 //-----------------------------cast_to_exactness-------------------------------
  2440 const Type *TypeOopPtr::cast_to_exactness(bool klass_is_exact) const {
  2441   // There is no such thing as an exact general oop.
  2442   // Return self unchanged.
  2443   return this;
  2447 //------------------------------as_klass_type----------------------------------
  2448 // Return the klass type corresponding to this instance or array type.
  2449 // It is the type that is loaded from an object of this type.
  2450 const TypeKlassPtr* TypeOopPtr::as_klass_type() const {
  2451   ciKlass* k = klass();
  2452   bool    xk = klass_is_exact();
  2453   if (k == NULL)
  2454     return TypeKlassPtr::OBJECT;
  2455   else
  2456     return TypeKlassPtr::make(xk? Constant: NotNull, k, 0);
  2460 //------------------------------meet-------------------------------------------
  2461 // Compute the MEET of two types.  It returns a new Type object.
  2462 const Type *TypeOopPtr::xmeet( const Type *t ) const {
  2463   // Perform a fast test for common case; meeting the same types together.
  2464   if( this == t ) return this;  // Meeting same type-rep?
  2466   // Current "this->_base" is OopPtr
  2467   switch (t->base()) {          // switch on original type
  2469   case Int:                     // Mixing ints & oops happens when javac
  2470   case Long:                    // reuses local variables
  2471   case FloatTop:
  2472   case FloatCon:
  2473   case FloatBot:
  2474   case DoubleTop:
  2475   case DoubleCon:
  2476   case DoubleBot:
  2477   case NarrowOop:
  2478   case Bottom:                  // Ye Olde Default
  2479     return Type::BOTTOM;
  2480   case Top:
  2481     return this;
  2483   default:                      // All else is a mistake
  2484     typerr(t);
  2486   case RawPtr:
  2487   case MetadataPtr:
  2488   case KlassPtr:
  2489     return TypePtr::BOTTOM;     // Oop meet raw is not well defined
  2491   case AnyPtr: {
  2492     // Found an AnyPtr type vs self-OopPtr type
  2493     const TypePtr *tp = t->is_ptr();
  2494     int offset = meet_offset(tp->offset());
  2495     PTR ptr = meet_ptr(tp->ptr());
  2496     switch (tp->ptr()) {
  2497     case Null:
  2498       if (ptr == Null)  return TypePtr::make(AnyPtr, ptr, offset);
  2499       // else fall through:
  2500     case TopPTR:
  2501     case AnyNull: {
  2502       int instance_id = meet_instance_id(InstanceTop);
  2503       return make(ptr, offset, instance_id);
  2505     case BotPTR:
  2506     case NotNull:
  2507       return TypePtr::make(AnyPtr, ptr, offset);
  2508     default: typerr(t);
  2512   case OopPtr: {                 // Meeting to other OopPtrs
  2513     const TypeOopPtr *tp = t->is_oopptr();
  2514     int instance_id = meet_instance_id(tp->instance_id());
  2515     return make( meet_ptr(tp->ptr()), meet_offset(tp->offset()), instance_id );
  2518   case InstPtr:                  // For these, flip the call around to cut down
  2519   case AryPtr:
  2520     return t->xmeet(this);      // Call in reverse direction
  2522   } // End of switch
  2523   return this;                  // Return the double constant
  2527 //------------------------------xdual------------------------------------------
  2528 // Dual of a pure heap pointer.  No relevant klass or oop information.
  2529 const Type *TypeOopPtr::xdual() const {
  2530   assert(klass() == Compile::current()->env()->Object_klass(), "no klasses here");
  2531   assert(const_oop() == NULL,             "no constants here");
  2532   return new TypeOopPtr(_base, dual_ptr(), klass(), klass_is_exact(), const_oop(), dual_offset(), dual_instance_id()  );
  2535 //--------------------------make_from_klass_common-----------------------------
  2536 // Computes the element-type given a klass.
  2537 const TypeOopPtr* TypeOopPtr::make_from_klass_common(ciKlass *klass, bool klass_change, bool try_for_exact) {
  2538   if (klass->is_instance_klass()) {
  2539     Compile* C = Compile::current();
  2540     Dependencies* deps = C->dependencies();
  2541     assert((deps != NULL) == (C->method() != NULL && C->method()->code_size() > 0), "sanity");
  2542     // Element is an instance
  2543     bool klass_is_exact = false;
  2544     if (klass->is_loaded()) {
  2545       // Try to set klass_is_exact.
  2546       ciInstanceKlass* ik = klass->as_instance_klass();
  2547       klass_is_exact = ik->is_final();
  2548       if (!klass_is_exact && klass_change
  2549           && deps != NULL && UseUniqueSubclasses) {
  2550         ciInstanceKlass* sub = ik->unique_concrete_subklass();
  2551         if (sub != NULL) {
  2552           deps->assert_abstract_with_unique_concrete_subtype(ik, sub);
  2553           klass = ik = sub;
  2554           klass_is_exact = sub->is_final();
  2557       if (!klass_is_exact && try_for_exact
  2558           && deps != NULL && UseExactTypes) {
  2559         if (!ik->is_interface() && !ik->has_subklass()) {
  2560           // Add a dependence; if concrete subclass added we need to recompile
  2561           deps->assert_leaf_type(ik);
  2562           klass_is_exact = true;
  2566     return TypeInstPtr::make(TypePtr::BotPTR, klass, klass_is_exact, NULL, 0);
  2567   } else if (klass->is_obj_array_klass()) {
  2568     // Element is an object array. Recursively call ourself.
  2569     const TypeOopPtr *etype = TypeOopPtr::make_from_klass_common(klass->as_obj_array_klass()->element_klass(), false, try_for_exact);
  2570     bool xk = etype->klass_is_exact();
  2571     const TypeAry* arr0 = TypeAry::make(etype, TypeInt::POS);
  2572     // We used to pass NotNull in here, asserting that the sub-arrays
  2573     // are all not-null.  This is not true in generally, as code can
  2574     // slam NULLs down in the subarrays.
  2575     const TypeAryPtr* arr = TypeAryPtr::make(TypePtr::BotPTR, arr0, klass, xk, 0);
  2576     return arr;
  2577   } else if (klass->is_type_array_klass()) {
  2578     // Element is an typeArray
  2579     const Type* etype = get_const_basic_type(klass->as_type_array_klass()->element_type());
  2580     const TypeAry* arr0 = TypeAry::make(etype, TypeInt::POS);
  2581     // We used to pass NotNull in here, asserting that the array pointer
  2582     // is not-null. That was not true in general.
  2583     const TypeAryPtr* arr = TypeAryPtr::make(TypePtr::BotPTR, arr0, klass, true, 0);
  2584     return arr;
  2585   } else {
  2586     ShouldNotReachHere();
  2587     return NULL;
  2591 //------------------------------make_from_constant-----------------------------
  2592 // Make a java pointer from an oop constant
  2593 const TypeOopPtr* TypeOopPtr::make_from_constant(ciObject* o, bool require_constant) {
  2594     assert(!o->is_null_object(), "null object not yet handled here.");
  2595     ciKlass* klass = o->klass();
  2596     if (klass->is_instance_klass()) {
  2597       // Element is an instance
  2598       if (require_constant) {
  2599         if (!o->can_be_constant())  return NULL;
  2600       } else if (!o->should_be_constant()) {
  2601         return TypeInstPtr::make(TypePtr::NotNull, klass, true, NULL, 0);
  2603       return TypeInstPtr::make(o);
  2604     } else if (klass->is_obj_array_klass()) {
  2605       // Element is an object array. Recursively call ourself.
  2606     const Type *etype =
  2607       TypeOopPtr::make_from_klass_raw(klass->as_obj_array_klass()->element_klass());
  2608       const TypeAry* arr0 = TypeAry::make(etype, TypeInt::make(o->as_array()->length()));
  2609       // We used to pass NotNull in here, asserting that the sub-arrays
  2610       // are all not-null.  This is not true in generally, as code can
  2611       // slam NULLs down in the subarrays.
  2612       if (require_constant) {
  2613         if (!o->can_be_constant())  return NULL;
  2614       } else if (!o->should_be_constant()) {
  2615         return TypeAryPtr::make(TypePtr::NotNull, arr0, klass, true, 0);
  2617     const TypeAryPtr* arr = TypeAryPtr::make(TypePtr::Constant, o, arr0, klass, true, 0);
  2618     return arr;
  2619     } else if (klass->is_type_array_klass()) {
  2620       // Element is an typeArray
  2621     const Type* etype =
  2622       (Type*)get_const_basic_type(klass->as_type_array_klass()->element_type());
  2623       const TypeAry* arr0 = TypeAry::make(etype, TypeInt::make(o->as_array()->length()));
  2624       // We used to pass NotNull in here, asserting that the array pointer
  2625       // is not-null. That was not true in general.
  2626       if (require_constant) {
  2627         if (!o->can_be_constant())  return NULL;
  2628       } else if (!o->should_be_constant()) {
  2629         return TypeAryPtr::make(TypePtr::NotNull, arr0, klass, true, 0);
  2631     const TypeAryPtr* arr = TypeAryPtr::make(TypePtr::Constant, o, arr0, klass, true, 0);
  2632     return arr;
  2635   fatal("unhandled object type");
  2636   return NULL;
  2639 //------------------------------get_con----------------------------------------
  2640 intptr_t TypeOopPtr::get_con() const {
  2641   assert( _ptr == Null || _ptr == Constant, "" );
  2642   assert( _offset >= 0, "" );
  2644   if (_offset != 0) {
  2645     // After being ported to the compiler interface, the compiler no longer
  2646     // directly manipulates the addresses of oops.  Rather, it only has a pointer
  2647     // to a handle at compile time.  This handle is embedded in the generated
  2648     // code and dereferenced at the time the nmethod is made.  Until that time,
  2649     // it is not reasonable to do arithmetic with the addresses of oops (we don't
  2650     // have access to the addresses!).  This does not seem to currently happen,
  2651     // but this assertion here is to help prevent its occurence.
  2652     tty->print_cr("Found oop constant with non-zero offset");
  2653     ShouldNotReachHere();
  2656   return (intptr_t)const_oop()->constant_encoding();
  2660 //-----------------------------filter------------------------------------------
  2661 // Do not allow interface-vs.-noninterface joins to collapse to top.
  2662 const Type *TypeOopPtr::filter( const Type *kills ) const {
  2664   const Type* ft = join(kills);
  2665   const TypeInstPtr* ftip = ft->isa_instptr();
  2666   const TypeInstPtr* ktip = kills->isa_instptr();
  2667   const TypeKlassPtr* ftkp = ft->isa_klassptr();
  2668   const TypeKlassPtr* ktkp = kills->isa_klassptr();
  2670   if (ft->empty()) {
  2671     // Check for evil case of 'this' being a class and 'kills' expecting an
  2672     // interface.  This can happen because the bytecodes do not contain
  2673     // enough type info to distinguish a Java-level interface variable
  2674     // from a Java-level object variable.  If we meet 2 classes which
  2675     // both implement interface I, but their meet is at 'j/l/O' which
  2676     // doesn't implement I, we have no way to tell if the result should
  2677     // be 'I' or 'j/l/O'.  Thus we'll pick 'j/l/O'.  If this then flows
  2678     // into a Phi which "knows" it's an Interface type we'll have to
  2679     // uplift the type.
  2680     if (!empty() && ktip != NULL && ktip->is_loaded() && ktip->klass()->is_interface())
  2681       return kills;             // Uplift to interface
  2682     if (!empty() && ktkp != NULL && ktkp->klass()->is_loaded() && ktkp->klass()->is_interface())
  2683       return kills;             // Uplift to interface
  2685     return Type::TOP;           // Canonical empty value
  2688   // If we have an interface-typed Phi or cast and we narrow to a class type,
  2689   // the join should report back the class.  However, if we have a J/L/Object
  2690   // class-typed Phi and an interface flows in, it's possible that the meet &
  2691   // join report an interface back out.  This isn't possible but happens
  2692   // because the type system doesn't interact well with interfaces.
  2693   if (ftip != NULL && ktip != NULL &&
  2694       ftip->is_loaded() &&  ftip->klass()->is_interface() &&
  2695       ktip->is_loaded() && !ktip->klass()->is_interface()) {
  2696     // Happens in a CTW of rt.jar, 320-341, no extra flags
  2697     assert(!ftip->klass_is_exact(), "interface could not be exact");
  2698     return ktip->cast_to_ptr_type(ftip->ptr());
  2700   // Interface klass type could be exact in opposite to interface type,
  2701   // return it here instead of incorrect Constant ptr J/L/Object (6894807).
  2702   if (ftkp != NULL && ktkp != NULL &&
  2703       ftkp->is_loaded() &&  ftkp->klass()->is_interface() &&
  2704       !ftkp->klass_is_exact() && // Keep exact interface klass
  2705       ktkp->is_loaded() && !ktkp->klass()->is_interface()) {
  2706     return ktkp->cast_to_ptr_type(ftkp->ptr());
  2709   return ft;
  2712 //------------------------------eq---------------------------------------------
  2713 // Structural equality check for Type representations
  2714 bool TypeOopPtr::eq( const Type *t ) const {
  2715   const TypeOopPtr *a = (const TypeOopPtr*)t;
  2716   if (_klass_is_exact != a->_klass_is_exact ||
  2717       _instance_id != a->_instance_id)  return false;
  2718   ciObject* one = const_oop();
  2719   ciObject* two = a->const_oop();
  2720   if (one == NULL || two == NULL) {
  2721     return (one == two) && TypePtr::eq(t);
  2722   } else {
  2723     return one->equals(two) && TypePtr::eq(t);
  2727 //------------------------------hash-------------------------------------------
  2728 // Type-specific hashing function.
  2729 int TypeOopPtr::hash(void) const {
  2730   return
  2731     (const_oop() ? const_oop()->hash() : 0) +
  2732     _klass_is_exact +
  2733     _instance_id +
  2734     TypePtr::hash();
  2737 //------------------------------dump2------------------------------------------
  2738 #ifndef PRODUCT
  2739 void TypeOopPtr::dump2( Dict &d, uint depth, outputStream *st ) const {
  2740   st->print("oopptr:%s", ptr_msg[_ptr]);
  2741   if( _klass_is_exact ) st->print(":exact");
  2742   if( const_oop() ) st->print(INTPTR_FORMAT, const_oop());
  2743   switch( _offset ) {
  2744   case OffsetTop: st->print("+top"); break;
  2745   case OffsetBot: st->print("+any"); break;
  2746   case         0: break;
  2747   default:        st->print("+%d",_offset); break;
  2749   if (_instance_id == InstanceTop)
  2750     st->print(",iid=top");
  2751   else if (_instance_id != InstanceBot)
  2752     st->print(",iid=%d",_instance_id);
  2754 #endif
  2756 //------------------------------singleton--------------------------------------
  2757 // TRUE if Type is a singleton type, FALSE otherwise.   Singletons are simple
  2758 // constants
  2759 bool TypeOopPtr::singleton(void) const {
  2760   // detune optimizer to not generate constant oop + constant offset as a constant!
  2761   // TopPTR, Null, AnyNull, Constant are all singletons
  2762   return (_offset == 0) && !below_centerline(_ptr);
  2765 //------------------------------add_offset-------------------------------------
  2766 const TypePtr *TypeOopPtr::add_offset( intptr_t offset ) const {
  2767   return make( _ptr, xadd_offset(offset), _instance_id);
  2770 //------------------------------meet_instance_id--------------------------------
  2771 int TypeOopPtr::meet_instance_id( int instance_id ) const {
  2772   // Either is 'TOP' instance?  Return the other instance!
  2773   if( _instance_id == InstanceTop ) return  instance_id;
  2774   if(  instance_id == InstanceTop ) return _instance_id;
  2775   // If either is different, return 'BOTTOM' instance
  2776   if( _instance_id != instance_id ) return InstanceBot;
  2777   return _instance_id;
  2780 //------------------------------dual_instance_id--------------------------------
  2781 int TypeOopPtr::dual_instance_id( ) const {
  2782   if( _instance_id == InstanceTop ) return InstanceBot; // Map TOP into BOTTOM
  2783   if( _instance_id == InstanceBot ) return InstanceTop; // Map BOTTOM into TOP
  2784   return _instance_id;              // Map everything else into self
  2788 //=============================================================================
  2789 // Convenience common pre-built types.
  2790 const TypeInstPtr *TypeInstPtr::NOTNULL;
  2791 const TypeInstPtr *TypeInstPtr::BOTTOM;
  2792 const TypeInstPtr *TypeInstPtr::MIRROR;
  2793 const TypeInstPtr *TypeInstPtr::MARK;
  2794 const TypeInstPtr *TypeInstPtr::KLASS;
  2796 //------------------------------TypeInstPtr-------------------------------------
  2797 TypeInstPtr::TypeInstPtr(PTR ptr, ciKlass* k, bool xk, ciObject* o, int off, int instance_id)
  2798  : TypeOopPtr(InstPtr, ptr, k, xk, o, off, instance_id), _name(k->name()) {
  2799    assert(k != NULL &&
  2800           (k->is_loaded() || o == NULL),
  2801           "cannot have constants with non-loaded klass");
  2802 };
  2804 //------------------------------make-------------------------------------------
  2805 const TypeInstPtr *TypeInstPtr::make(PTR ptr,
  2806                                      ciKlass* k,
  2807                                      bool xk,
  2808                                      ciObject* o,
  2809                                      int offset,
  2810                                      int instance_id) {
  2811   assert( !k->is_loaded() || k->is_instance_klass(), "Must be for instance");
  2812   // Either const_oop() is NULL or else ptr is Constant
  2813   assert( (!o && ptr != Constant) || (o && ptr == Constant),
  2814           "constant pointers must have a value supplied" );
  2815   // Ptr is never Null
  2816   assert( ptr != Null, "NULL pointers are not typed" );
  2818   assert(instance_id <= 0 || xk || !UseExactTypes, "instances are always exactly typed");
  2819   if (!UseExactTypes)  xk = false;
  2820   if (ptr == Constant) {
  2821     // Note:  This case includes meta-object constants, such as methods.
  2822     xk = true;
  2823   } else if (k->is_loaded()) {
  2824     ciInstanceKlass* ik = k->as_instance_klass();
  2825     if (!xk && ik->is_final())     xk = true;   // no inexact final klass
  2826     if (xk && ik->is_interface())  xk = false;  // no exact interface
  2829   // Now hash this baby
  2830   TypeInstPtr *result =
  2831     (TypeInstPtr*)(new TypeInstPtr(ptr, k, xk, o ,offset, instance_id))->hashcons();
  2833   return result;
  2837 //------------------------------cast_to_ptr_type-------------------------------
  2838 const Type *TypeInstPtr::cast_to_ptr_type(PTR ptr) const {
  2839   if( ptr == _ptr ) return this;
  2840   // Reconstruct _sig info here since not a problem with later lazy
  2841   // construction, _sig will show up on demand.
  2842   return make(ptr, klass(), klass_is_exact(), const_oop(), _offset, _instance_id);
  2846 //-----------------------------cast_to_exactness-------------------------------
  2847 const Type *TypeInstPtr::cast_to_exactness(bool klass_is_exact) const {
  2848   if( klass_is_exact == _klass_is_exact ) return this;
  2849   if (!UseExactTypes)  return this;
  2850   if (!_klass->is_loaded())  return this;
  2851   ciInstanceKlass* ik = _klass->as_instance_klass();
  2852   if( (ik->is_final() || _const_oop) )  return this;  // cannot clear xk
  2853   if( ik->is_interface() )              return this;  // cannot set xk
  2854   return make(ptr(), klass(), klass_is_exact, const_oop(), _offset, _instance_id);
  2857 //-----------------------------cast_to_instance_id----------------------------
  2858 const TypeOopPtr *TypeInstPtr::cast_to_instance_id(int instance_id) const {
  2859   if( instance_id == _instance_id ) return this;
  2860   return make(_ptr, klass(), _klass_is_exact, const_oop(), _offset, instance_id);
  2863 //------------------------------xmeet_unloaded---------------------------------
  2864 // Compute the MEET of two InstPtrs when at least one is unloaded.
  2865 // Assume classes are different since called after check for same name/class-loader
  2866 const TypeInstPtr *TypeInstPtr::xmeet_unloaded(const TypeInstPtr *tinst) const {
  2867     int off = meet_offset(tinst->offset());
  2868     PTR ptr = meet_ptr(tinst->ptr());
  2869     int instance_id = meet_instance_id(tinst->instance_id());
  2871     const TypeInstPtr *loaded    = is_loaded() ? this  : tinst;
  2872     const TypeInstPtr *unloaded  = is_loaded() ? tinst : this;
  2873     if( loaded->klass()->equals(ciEnv::current()->Object_klass()) ) {
  2874       //
  2875       // Meet unloaded class with java/lang/Object
  2876       //
  2877       // Meet
  2878       //          |                     Unloaded Class
  2879       //  Object  |   TOP    |   AnyNull | Constant |   NotNull |  BOTTOM   |
  2880       //  ===================================================================
  2881       //   TOP    | ..........................Unloaded......................|
  2882       //  AnyNull |  U-AN    |................Unloaded......................|
  2883       // Constant | ... O-NN .................................. |   O-BOT   |
  2884       //  NotNull | ... O-NN .................................. |   O-BOT   |
  2885       //  BOTTOM  | ........................Object-BOTTOM ..................|
  2886       //
  2887       assert(loaded->ptr() != TypePtr::Null, "insanity check");
  2888       //
  2889       if(      loaded->ptr() == TypePtr::TopPTR ) { return unloaded; }
  2890       else if (loaded->ptr() == TypePtr::AnyNull) { return TypeInstPtr::make( ptr, unloaded->klass(), false, NULL, off, instance_id ); }
  2891       else if (loaded->ptr() == TypePtr::BotPTR ) { return TypeInstPtr::BOTTOM; }
  2892       else if (loaded->ptr() == TypePtr::Constant || loaded->ptr() == TypePtr::NotNull) {
  2893         if (unloaded->ptr() == TypePtr::BotPTR  ) { return TypeInstPtr::BOTTOM;  }
  2894         else                                      { return TypeInstPtr::NOTNULL; }
  2896       else if( unloaded->ptr() == TypePtr::TopPTR )  { return unloaded; }
  2898       return unloaded->cast_to_ptr_type(TypePtr::AnyNull)->is_instptr();
  2901     // Both are unloaded, not the same class, not Object
  2902     // Or meet unloaded with a different loaded class, not java/lang/Object
  2903     if( ptr != TypePtr::BotPTR ) {
  2904       return TypeInstPtr::NOTNULL;
  2906     return TypeInstPtr::BOTTOM;
  2910 //------------------------------meet-------------------------------------------
  2911 // Compute the MEET of two types.  It returns a new Type object.
  2912 const Type *TypeInstPtr::xmeet( const Type *t ) const {
  2913   // Perform a fast test for common case; meeting the same types together.
  2914   if( this == t ) return this;  // Meeting same type-rep?
  2916   // Current "this->_base" is Pointer
  2917   switch (t->base()) {          // switch on original type
  2919   case Int:                     // Mixing ints & oops happens when javac
  2920   case Long:                    // reuses local variables
  2921   case FloatTop:
  2922   case FloatCon:
  2923   case FloatBot:
  2924   case DoubleTop:
  2925   case DoubleCon:
  2926   case DoubleBot:
  2927   case NarrowOop:
  2928   case Bottom:                  // Ye Olde Default
  2929     return Type::BOTTOM;
  2930   case Top:
  2931     return this;
  2933   default:                      // All else is a mistake
  2934     typerr(t);
  2936   case MetadataPtr:
  2937   case KlassPtr:
  2938   case RawPtr: return TypePtr::BOTTOM;
  2940   case AryPtr: {                // All arrays inherit from Object class
  2941     const TypeAryPtr *tp = t->is_aryptr();
  2942     int offset = meet_offset(tp->offset());
  2943     PTR ptr = meet_ptr(tp->ptr());
  2944     int instance_id = meet_instance_id(tp->instance_id());
  2945     switch (ptr) {
  2946     case TopPTR:
  2947     case AnyNull:                // Fall 'down' to dual of object klass
  2948       if (klass()->equals(ciEnv::current()->Object_klass())) {
  2949         return TypeAryPtr::make(ptr, tp->ary(), tp->klass(), tp->klass_is_exact(), offset, instance_id);
  2950       } else {
  2951         // cannot subclass, so the meet has to fall badly below the centerline
  2952         ptr = NotNull;
  2953         instance_id = InstanceBot;
  2954         return TypeInstPtr::make( ptr, ciEnv::current()->Object_klass(), false, NULL, offset, instance_id);
  2956     case Constant:
  2957     case NotNull:
  2958     case BotPTR:                // Fall down to object klass
  2959       // LCA is object_klass, but if we subclass from the top we can do better
  2960       if( above_centerline(_ptr) ) { // if( _ptr == TopPTR || _ptr == AnyNull )
  2961         // If 'this' (InstPtr) is above the centerline and it is Object class
  2962         // then we can subclass in the Java class hierarchy.
  2963         if (klass()->equals(ciEnv::current()->Object_klass())) {
  2964           // that is, tp's array type is a subtype of my klass
  2965           return TypeAryPtr::make(ptr, (ptr == Constant ? tp->const_oop() : NULL),
  2966                                   tp->ary(), tp->klass(), tp->klass_is_exact(), offset, instance_id);
  2969       // The other case cannot happen, since I cannot be a subtype of an array.
  2970       // The meet falls down to Object class below centerline.
  2971       if( ptr == Constant )
  2972          ptr = NotNull;
  2973       instance_id = InstanceBot;
  2974       return make( ptr, ciEnv::current()->Object_klass(), false, NULL, offset, instance_id );
  2975     default: typerr(t);
  2979   case OopPtr: {                // Meeting to OopPtrs
  2980     // Found a OopPtr type vs self-InstPtr type
  2981     const TypeOopPtr *tp = t->is_oopptr();
  2982     int offset = meet_offset(tp->offset());
  2983     PTR ptr = meet_ptr(tp->ptr());
  2984     switch (tp->ptr()) {
  2985     case TopPTR:
  2986     case AnyNull: {
  2987       int instance_id = meet_instance_id(InstanceTop);
  2988       return make(ptr, klass(), klass_is_exact(),
  2989                   (ptr == Constant ? const_oop() : NULL), offset, instance_id);
  2991     case NotNull:
  2992     case BotPTR: {
  2993       int instance_id = meet_instance_id(tp->instance_id());
  2994       return TypeOopPtr::make(ptr, offset, instance_id);
  2996     default: typerr(t);
  3000   case AnyPtr: {                // Meeting to AnyPtrs
  3001     // Found an AnyPtr type vs self-InstPtr type
  3002     const TypePtr *tp = t->is_ptr();
  3003     int offset = meet_offset(tp->offset());
  3004     PTR ptr = meet_ptr(tp->ptr());
  3005     switch (tp->ptr()) {
  3006     case Null:
  3007       if( ptr == Null ) return TypePtr::make( AnyPtr, ptr, offset );
  3008       // else fall through to AnyNull
  3009     case TopPTR:
  3010     case AnyNull: {
  3011       int instance_id = meet_instance_id(InstanceTop);
  3012       return make( ptr, klass(), klass_is_exact(),
  3013                    (ptr == Constant ? const_oop() : NULL), offset, instance_id);
  3015     case NotNull:
  3016     case BotPTR:
  3017       return TypePtr::make( AnyPtr, ptr, offset );
  3018     default: typerr(t);
  3022   /*
  3023                  A-top         }
  3024                /   |   \       }  Tops
  3025            B-top A-any C-top   }
  3026               | /  |  \ |      }  Any-nulls
  3027            B-any   |   C-any   }
  3028               |    |    |
  3029            B-con A-con C-con   } constants; not comparable across classes
  3030               |    |    |
  3031            B-not   |   C-not   }
  3032               | \  |  / |      }  not-nulls
  3033            B-bot A-not C-bot   }
  3034                \   |   /       }  Bottoms
  3035                  A-bot         }
  3036   */
  3038   case InstPtr: {                // Meeting 2 Oops?
  3039     // Found an InstPtr sub-type vs self-InstPtr type
  3040     const TypeInstPtr *tinst = t->is_instptr();
  3041     int off = meet_offset( tinst->offset() );
  3042     PTR ptr = meet_ptr( tinst->ptr() );
  3043     int instance_id = meet_instance_id(tinst->instance_id());
  3045     // Check for easy case; klasses are equal (and perhaps not loaded!)
  3046     // If we have constants, then we created oops so classes are loaded
  3047     // and we can handle the constants further down.  This case handles
  3048     // both-not-loaded or both-loaded classes
  3049     if (ptr != Constant && klass()->equals(tinst->klass()) && klass_is_exact() == tinst->klass_is_exact()) {
  3050       return make( ptr, klass(), klass_is_exact(), NULL, off, instance_id );
  3053     // Classes require inspection in the Java klass hierarchy.  Must be loaded.
  3054     ciKlass* tinst_klass = tinst->klass();
  3055     ciKlass* this_klass  = this->klass();
  3056     bool tinst_xk = tinst->klass_is_exact();
  3057     bool this_xk  = this->klass_is_exact();
  3058     if (!tinst_klass->is_loaded() || !this_klass->is_loaded() ) {
  3059       // One of these classes has not been loaded
  3060       const TypeInstPtr *unloaded_meet = xmeet_unloaded(tinst);
  3061 #ifndef PRODUCT
  3062       if( PrintOpto && Verbose ) {
  3063         tty->print("meet of unloaded classes resulted in: "); unloaded_meet->dump(); tty->cr();
  3064         tty->print("  this == "); this->dump(); tty->cr();
  3065         tty->print(" tinst == "); tinst->dump(); tty->cr();
  3067 #endif
  3068       return unloaded_meet;
  3071     // Handle mixing oops and interfaces first.
  3072     if( this_klass->is_interface() && !tinst_klass->is_interface() ) {
  3073       ciKlass *tmp = tinst_klass; // Swap interface around
  3074       tinst_klass = this_klass;
  3075       this_klass = tmp;
  3076       bool tmp2 = tinst_xk;
  3077       tinst_xk = this_xk;
  3078       this_xk = tmp2;
  3080     if (tinst_klass->is_interface() &&
  3081         !(this_klass->is_interface() ||
  3082           // Treat java/lang/Object as an honorary interface,
  3083           // because we need a bottom for the interface hierarchy.
  3084           this_klass == ciEnv::current()->Object_klass())) {
  3085       // Oop meets interface!
  3087       // See if the oop subtypes (implements) interface.
  3088       ciKlass *k;
  3089       bool xk;
  3090       if( this_klass->is_subtype_of( tinst_klass ) ) {
  3091         // Oop indeed subtypes.  Now keep oop or interface depending
  3092         // on whether we are both above the centerline or either is
  3093         // below the centerline.  If we are on the centerline
  3094         // (e.g., Constant vs. AnyNull interface), use the constant.
  3095         k  = below_centerline(ptr) ? tinst_klass : this_klass;
  3096         // If we are keeping this_klass, keep its exactness too.
  3097         xk = below_centerline(ptr) ? tinst_xk    : this_xk;
  3098       } else {                  // Does not implement, fall to Object
  3099         // Oop does not implement interface, so mixing falls to Object
  3100         // just like the verifier does (if both are above the
  3101         // centerline fall to interface)
  3102         k = above_centerline(ptr) ? tinst_klass : ciEnv::current()->Object_klass();
  3103         xk = above_centerline(ptr) ? tinst_xk : false;
  3104         // Watch out for Constant vs. AnyNull interface.
  3105         if (ptr == Constant)  ptr = NotNull;   // forget it was a constant
  3106         instance_id = InstanceBot;
  3108       ciObject* o = NULL;  // the Constant value, if any
  3109       if (ptr == Constant) {
  3110         // Find out which constant.
  3111         o = (this_klass == klass()) ? const_oop() : tinst->const_oop();
  3113       return make( ptr, k, xk, o, off, instance_id );
  3116     // Either oop vs oop or interface vs interface or interface vs Object
  3118     // !!! Here's how the symmetry requirement breaks down into invariants:
  3119     // If we split one up & one down AND they subtype, take the down man.
  3120     // If we split one up & one down AND they do NOT subtype, "fall hard".
  3121     // If both are up and they subtype, take the subtype class.
  3122     // If both are up and they do NOT subtype, "fall hard".
  3123     // If both are down and they subtype, take the supertype class.
  3124     // If both are down and they do NOT subtype, "fall hard".
  3125     // Constants treated as down.
  3127     // Now, reorder the above list; observe that both-down+subtype is also
  3128     // "fall hard"; "fall hard" becomes the default case:
  3129     // If we split one up & one down AND they subtype, take the down man.
  3130     // If both are up and they subtype, take the subtype class.
  3132     // If both are down and they subtype, "fall hard".
  3133     // If both are down and they do NOT subtype, "fall hard".
  3134     // If both are up and they do NOT subtype, "fall hard".
  3135     // If we split one up & one down AND they do NOT subtype, "fall hard".
  3137     // If a proper subtype is exact, and we return it, we return it exactly.
  3138     // If a proper supertype is exact, there can be no subtyping relationship!
  3139     // If both types are equal to the subtype, exactness is and-ed below the
  3140     // centerline and or-ed above it.  (N.B. Constants are always exact.)
  3142     // Check for subtyping:
  3143     ciKlass *subtype = NULL;
  3144     bool subtype_exact = false;
  3145     if( tinst_klass->equals(this_klass) ) {
  3146       subtype = this_klass;
  3147       subtype_exact = below_centerline(ptr) ? (this_xk & tinst_xk) : (this_xk | tinst_xk);
  3148     } else if( !tinst_xk && this_klass->is_subtype_of( tinst_klass ) ) {
  3149       subtype = this_klass;     // Pick subtyping class
  3150       subtype_exact = this_xk;
  3151     } else if( !this_xk && tinst_klass->is_subtype_of( this_klass ) ) {
  3152       subtype = tinst_klass;    // Pick subtyping class
  3153       subtype_exact = tinst_xk;
  3156     if( subtype ) {
  3157       if( above_centerline(ptr) ) { // both are up?
  3158         this_klass = tinst_klass = subtype;
  3159         this_xk = tinst_xk = subtype_exact;
  3160       } else if( above_centerline(this ->_ptr) && !above_centerline(tinst->_ptr) ) {
  3161         this_klass = tinst_klass; // tinst is down; keep down man
  3162         this_xk = tinst_xk;
  3163       } else if( above_centerline(tinst->_ptr) && !above_centerline(this ->_ptr) ) {
  3164         tinst_klass = this_klass; // this is down; keep down man
  3165         tinst_xk = this_xk;
  3166       } else {
  3167         this_xk = subtype_exact;  // either they are equal, or we'll do an LCA
  3171     // Check for classes now being equal
  3172     if (tinst_klass->equals(this_klass)) {
  3173       // If the klasses are equal, the constants may still differ.  Fall to
  3174       // NotNull if they do (neither constant is NULL; that is a special case
  3175       // handled elsewhere).
  3176       ciObject* o = NULL;             // Assume not constant when done
  3177       ciObject* this_oop  = const_oop();
  3178       ciObject* tinst_oop = tinst->const_oop();
  3179       if( ptr == Constant ) {
  3180         if (this_oop != NULL && tinst_oop != NULL &&
  3181             this_oop->equals(tinst_oop) )
  3182           o = this_oop;
  3183         else if (above_centerline(this ->_ptr))
  3184           o = tinst_oop;
  3185         else if (above_centerline(tinst ->_ptr))
  3186           o = this_oop;
  3187         else
  3188           ptr = NotNull;
  3190       return make( ptr, this_klass, this_xk, o, off, instance_id );
  3191     } // Else classes are not equal
  3193     // Since klasses are different, we require a LCA in the Java
  3194     // class hierarchy - which means we have to fall to at least NotNull.
  3195     if( ptr == TopPTR || ptr == AnyNull || ptr == Constant )
  3196       ptr = NotNull;
  3197     instance_id = InstanceBot;
  3199     // Now we find the LCA of Java classes
  3200     ciKlass* k = this_klass->least_common_ancestor(tinst_klass);
  3201     return make( ptr, k, false, NULL, off, instance_id );
  3202   } // End of case InstPtr
  3204   } // End of switch
  3205   return this;                  // Return the double constant
  3209 //------------------------java_mirror_type--------------------------------------
  3210 ciType* TypeInstPtr::java_mirror_type() const {
  3211   // must be a singleton type
  3212   if( const_oop() == NULL )  return NULL;
  3214   // must be of type java.lang.Class
  3215   if( klass() != ciEnv::current()->Class_klass() )  return NULL;
  3217   return const_oop()->as_instance()->java_mirror_type();
  3221 //------------------------------xdual------------------------------------------
  3222 // Dual: do NOT dual on klasses.  This means I do NOT understand the Java
  3223 // inheritance mechanism.
  3224 const Type *TypeInstPtr::xdual() const {
  3225   return new TypeInstPtr( dual_ptr(), klass(), klass_is_exact(), const_oop(), dual_offset(), dual_instance_id()  );
  3228 //------------------------------eq---------------------------------------------
  3229 // Structural equality check for Type representations
  3230 bool TypeInstPtr::eq( const Type *t ) const {
  3231   const TypeInstPtr *p = t->is_instptr();
  3232   return
  3233     klass()->equals(p->klass()) &&
  3234     TypeOopPtr::eq(p);          // Check sub-type stuff
  3237 //------------------------------hash-------------------------------------------
  3238 // Type-specific hashing function.
  3239 int TypeInstPtr::hash(void) const {
  3240   int hash = klass()->hash() + TypeOopPtr::hash();
  3241   return hash;
  3244 //------------------------------dump2------------------------------------------
  3245 // Dump oop Type
  3246 #ifndef PRODUCT
  3247 void TypeInstPtr::dump2( Dict &d, uint depth, outputStream *st ) const {
  3248   // Print the name of the klass.
  3249   klass()->print_name_on(st);
  3251   switch( _ptr ) {
  3252   case Constant:
  3253     // TO DO: Make CI print the hex address of the underlying oop.
  3254     if (WizardMode || Verbose) {
  3255       const_oop()->print_oop(st);
  3257   case BotPTR:
  3258     if (!WizardMode && !Verbose) {
  3259       if( _klass_is_exact ) st->print(":exact");
  3260       break;
  3262   case TopPTR:
  3263   case AnyNull:
  3264   case NotNull:
  3265     st->print(":%s", ptr_msg[_ptr]);
  3266     if( _klass_is_exact ) st->print(":exact");
  3267     break;
  3270   if( _offset ) {               // Dump offset, if any
  3271     if( _offset == OffsetBot )      st->print("+any");
  3272     else if( _offset == OffsetTop ) st->print("+unknown");
  3273     else st->print("+%d", _offset);
  3276   st->print(" *");
  3277   if (_instance_id == InstanceTop)
  3278     st->print(",iid=top");
  3279   else if (_instance_id != InstanceBot)
  3280     st->print(",iid=%d",_instance_id);
  3282 #endif
  3284 //------------------------------add_offset-------------------------------------
  3285 const TypePtr *TypeInstPtr::add_offset( intptr_t offset ) const {
  3286   return make( _ptr, klass(), klass_is_exact(), const_oop(), xadd_offset(offset), _instance_id );
  3289 //=============================================================================
  3290 // Convenience common pre-built types.
  3291 const TypeAryPtr *TypeAryPtr::RANGE;
  3292 const TypeAryPtr *TypeAryPtr::OOPS;
  3293 const TypeAryPtr *TypeAryPtr::NARROWOOPS;
  3294 const TypeAryPtr *TypeAryPtr::BYTES;
  3295 const TypeAryPtr *TypeAryPtr::SHORTS;
  3296 const TypeAryPtr *TypeAryPtr::CHARS;
  3297 const TypeAryPtr *TypeAryPtr::INTS;
  3298 const TypeAryPtr *TypeAryPtr::LONGS;
  3299 const TypeAryPtr *TypeAryPtr::FLOATS;
  3300 const TypeAryPtr *TypeAryPtr::DOUBLES;
  3302 //------------------------------make-------------------------------------------
  3303 const TypeAryPtr *TypeAryPtr::make( PTR ptr, const TypeAry *ary, ciKlass* k, bool xk, int offset, int instance_id ) {
  3304   assert(!(k == NULL && ary->_elem->isa_int()),
  3305          "integral arrays must be pre-equipped with a class");
  3306   if (!xk)  xk = ary->ary_must_be_exact();
  3307   assert(instance_id <= 0 || xk || !UseExactTypes, "instances are always exactly typed");
  3308   if (!UseExactTypes)  xk = (ptr == Constant);
  3309   return (TypeAryPtr*)(new TypeAryPtr(ptr, NULL, ary, k, xk, offset, instance_id))->hashcons();
  3312 //------------------------------make-------------------------------------------
  3313 const TypeAryPtr *TypeAryPtr::make( PTR ptr, ciObject* o, const TypeAry *ary, ciKlass* k, bool xk, int offset, int instance_id ) {
  3314   assert(!(k == NULL && ary->_elem->isa_int()),
  3315          "integral arrays must be pre-equipped with a class");
  3316   assert( (ptr==Constant && o) || (ptr!=Constant && !o), "" );
  3317   if (!xk)  xk = (o != NULL) || ary->ary_must_be_exact();
  3318   assert(instance_id <= 0 || xk || !UseExactTypes, "instances are always exactly typed");
  3319   if (!UseExactTypes)  xk = (ptr == Constant);
  3320   return (TypeAryPtr*)(new TypeAryPtr(ptr, o, ary, k, xk, offset, instance_id))->hashcons();
  3323 //------------------------------cast_to_ptr_type-------------------------------
  3324 const Type *TypeAryPtr::cast_to_ptr_type(PTR ptr) const {
  3325   if( ptr == _ptr ) return this;
  3326   return make(ptr, const_oop(), _ary, klass(), klass_is_exact(), _offset, _instance_id);
  3330 //-----------------------------cast_to_exactness-------------------------------
  3331 const Type *TypeAryPtr::cast_to_exactness(bool klass_is_exact) const {
  3332   if( klass_is_exact == _klass_is_exact ) return this;
  3333   if (!UseExactTypes)  return this;
  3334   if (_ary->ary_must_be_exact())  return this;  // cannot clear xk
  3335   return make(ptr(), const_oop(), _ary, klass(), klass_is_exact, _offset, _instance_id);
  3338 //-----------------------------cast_to_instance_id----------------------------
  3339 const TypeOopPtr *TypeAryPtr::cast_to_instance_id(int instance_id) const {
  3340   if( instance_id == _instance_id ) return this;
  3341   return make(_ptr, const_oop(), _ary, klass(), _klass_is_exact, _offset, instance_id);
  3344 //-----------------------------narrow_size_type-------------------------------
  3345 // Local cache for arrayOopDesc::max_array_length(etype),
  3346 // which is kind of slow (and cached elsewhere by other users).
  3347 static jint max_array_length_cache[T_CONFLICT+1];
  3348 static jint max_array_length(BasicType etype) {
  3349   jint& cache = max_array_length_cache[etype];
  3350   jint res = cache;
  3351   if (res == 0) {
  3352     switch (etype) {
  3353     case T_NARROWOOP:
  3354       etype = T_OBJECT;
  3355       break;
  3356     case T_CONFLICT:
  3357     case T_ILLEGAL:
  3358     case T_VOID:
  3359       etype = T_BYTE;           // will produce conservatively high value
  3361     cache = res = arrayOopDesc::max_array_length(etype);
  3363   return res;
  3366 // Narrow the given size type to the index range for the given array base type.
  3367 // Return NULL if the resulting int type becomes empty.
  3368 const TypeInt* TypeAryPtr::narrow_size_type(const TypeInt* size) const {
  3369   jint hi = size->_hi;
  3370   jint lo = size->_lo;
  3371   jint min_lo = 0;
  3372   jint max_hi = max_array_length(elem()->basic_type());
  3373   //if (index_not_size)  --max_hi;     // type of a valid array index, FTR
  3374   bool chg = false;
  3375   if (lo < min_lo) { lo = min_lo; chg = true; }
  3376   if (hi > max_hi) { hi = max_hi; chg = true; }
  3377   // Negative length arrays will produce weird intermediate dead fast-path code
  3378   if (lo > hi)
  3379     return TypeInt::ZERO;
  3380   if (!chg)
  3381     return size;
  3382   return TypeInt::make(lo, hi, Type::WidenMin);
  3385 //-------------------------------cast_to_size----------------------------------
  3386 const TypeAryPtr* TypeAryPtr::cast_to_size(const TypeInt* new_size) const {
  3387   assert(new_size != NULL, "");
  3388   new_size = narrow_size_type(new_size);
  3389   if (new_size == size())  return this;
  3390   const TypeAry* new_ary = TypeAry::make(elem(), new_size);
  3391   return make(ptr(), const_oop(), new_ary, klass(), klass_is_exact(), _offset, _instance_id);
  3395 //------------------------------eq---------------------------------------------
  3396 // Structural equality check for Type representations
  3397 bool TypeAryPtr::eq( const Type *t ) const {
  3398   const TypeAryPtr *p = t->is_aryptr();
  3399   return
  3400     _ary == p->_ary &&  // Check array
  3401     TypeOopPtr::eq(p);  // Check sub-parts
  3404 //------------------------------hash-------------------------------------------
  3405 // Type-specific hashing function.
  3406 int TypeAryPtr::hash(void) const {
  3407   return (intptr_t)_ary + TypeOopPtr::hash();
  3410 //------------------------------meet-------------------------------------------
  3411 // Compute the MEET of two types.  It returns a new Type object.
  3412 const Type *TypeAryPtr::xmeet( const Type *t ) const {
  3413   // Perform a fast test for common case; meeting the same types together.
  3414   if( this == t ) return this;  // Meeting same type-rep?
  3415   // Current "this->_base" is Pointer
  3416   switch (t->base()) {          // switch on original type
  3418   // Mixing ints & oops happens when javac reuses local variables
  3419   case Int:
  3420   case Long:
  3421   case FloatTop:
  3422   case FloatCon:
  3423   case FloatBot:
  3424   case DoubleTop:
  3425   case DoubleCon:
  3426   case DoubleBot:
  3427   case NarrowOop:
  3428   case Bottom:                  // Ye Olde Default
  3429     return Type::BOTTOM;
  3430   case Top:
  3431     return this;
  3433   default:                      // All else is a mistake
  3434     typerr(t);
  3436   case OopPtr: {                // Meeting to OopPtrs
  3437     // Found a OopPtr type vs self-AryPtr type
  3438     const TypeOopPtr *tp = t->is_oopptr();
  3439     int offset = meet_offset(tp->offset());
  3440     PTR ptr = meet_ptr(tp->ptr());
  3441     switch (tp->ptr()) {
  3442     case TopPTR:
  3443     case AnyNull: {
  3444       int instance_id = meet_instance_id(InstanceTop);
  3445       return make(ptr, (ptr == Constant ? const_oop() : NULL),
  3446                   _ary, _klass, _klass_is_exact, offset, instance_id);
  3448     case BotPTR:
  3449     case NotNull: {
  3450       int instance_id = meet_instance_id(tp->instance_id());
  3451       return TypeOopPtr::make(ptr, offset, instance_id);
  3453     default: ShouldNotReachHere();
  3457   case AnyPtr: {                // Meeting two AnyPtrs
  3458     // Found an AnyPtr type vs self-AryPtr type
  3459     const TypePtr *tp = t->is_ptr();
  3460     int offset = meet_offset(tp->offset());
  3461     PTR ptr = meet_ptr(tp->ptr());
  3462     switch (tp->ptr()) {
  3463     case TopPTR:
  3464       return this;
  3465     case BotPTR:
  3466     case NotNull:
  3467       return TypePtr::make(AnyPtr, ptr, offset);
  3468     case Null:
  3469       if( ptr == Null ) return TypePtr::make(AnyPtr, ptr, offset);
  3470       // else fall through to AnyNull
  3471     case AnyNull: {
  3472       int instance_id = meet_instance_id(InstanceTop);
  3473       return make( ptr, (ptr == Constant ? const_oop() : NULL),
  3474                   _ary, _klass, _klass_is_exact, offset, instance_id);
  3476     default: ShouldNotReachHere();
  3480   case MetadataPtr:
  3481   case KlassPtr:
  3482   case RawPtr: return TypePtr::BOTTOM;
  3484   case AryPtr: {                // Meeting 2 references?
  3485     const TypeAryPtr *tap = t->is_aryptr();
  3486     int off = meet_offset(tap->offset());
  3487     const TypeAry *tary = _ary->meet(tap->_ary)->is_ary();
  3488     PTR ptr = meet_ptr(tap->ptr());
  3489     int instance_id = meet_instance_id(tap->instance_id());
  3490     ciKlass* lazy_klass = NULL;
  3491     if (tary->_elem->isa_int()) {
  3492       // Integral array element types have irrelevant lattice relations.
  3493       // It is the klass that determines array layout, not the element type.
  3494       if (_klass == NULL)
  3495         lazy_klass = tap->_klass;
  3496       else if (tap->_klass == NULL || tap->_klass == _klass) {
  3497         lazy_klass = _klass;
  3498       } else {
  3499         // Something like byte[int+] meets char[int+].
  3500         // This must fall to bottom, not (int[-128..65535])[int+].
  3501         instance_id = InstanceBot;
  3502         tary = TypeAry::make(Type::BOTTOM, tary->_size);
  3504     } else // Non integral arrays.
  3505     // Must fall to bottom if exact klasses in upper lattice
  3506     // are not equal or super klass is exact.
  3507     if ( above_centerline(ptr) && klass() != tap->klass() &&
  3508          // meet with top[] and bottom[] are processed further down:
  3509          tap ->_klass != NULL  && this->_klass != NULL   &&
  3510          // both are exact and not equal:
  3511         ((tap ->_klass_is_exact && this->_klass_is_exact) ||
  3512          // 'tap'  is exact and super or unrelated:
  3513          (tap ->_klass_is_exact && !tap->klass()->is_subtype_of(klass())) ||
  3514          // 'this' is exact and super or unrelated:
  3515          (this->_klass_is_exact && !klass()->is_subtype_of(tap->klass())))) {
  3516       tary = TypeAry::make(Type::BOTTOM, tary->_size);
  3517       return make( NotNull, NULL, tary, lazy_klass, false, off, InstanceBot );
  3520     bool xk = false;
  3521     switch (tap->ptr()) {
  3522     case AnyNull:
  3523     case TopPTR:
  3524       // Compute new klass on demand, do not use tap->_klass
  3525       xk = (tap->_klass_is_exact | this->_klass_is_exact);
  3526       return make( ptr, const_oop(), tary, lazy_klass, xk, off, instance_id );
  3527     case Constant: {
  3528       ciObject* o = const_oop();
  3529       if( _ptr == Constant ) {
  3530         if( tap->const_oop() != NULL && !o->equals(tap->const_oop()) ) {
  3531           xk = (klass() == tap->klass());
  3532           ptr = NotNull;
  3533           o = NULL;
  3534           instance_id = InstanceBot;
  3535         } else {
  3536           xk = true;
  3538       } else if( above_centerline(_ptr) ) {
  3539         o = tap->const_oop();
  3540         xk = true;
  3541       } else {
  3542         // Only precise for identical arrays
  3543         xk = this->_klass_is_exact && (klass() == tap->klass());
  3545       return TypeAryPtr::make( ptr, o, tary, lazy_klass, xk, off, instance_id );
  3547     case NotNull:
  3548     case BotPTR:
  3549       // Compute new klass on demand, do not use tap->_klass
  3550       if (above_centerline(this->_ptr))
  3551             xk = tap->_klass_is_exact;
  3552       else if (above_centerline(tap->_ptr))
  3553             xk = this->_klass_is_exact;
  3554       else  xk = (tap->_klass_is_exact & this->_klass_is_exact) &&
  3555               (klass() == tap->klass()); // Only precise for identical arrays
  3556       return TypeAryPtr::make( ptr, NULL, tary, lazy_klass, xk, off, instance_id );
  3557     default: ShouldNotReachHere();
  3561   // All arrays inherit from Object class
  3562   case InstPtr: {
  3563     const TypeInstPtr *tp = t->is_instptr();
  3564     int offset = meet_offset(tp->offset());
  3565     PTR ptr = meet_ptr(tp->ptr());
  3566     int instance_id = meet_instance_id(tp->instance_id());
  3567     switch (ptr) {
  3568     case TopPTR:
  3569     case AnyNull:                // Fall 'down' to dual of object klass
  3570       if( tp->klass()->equals(ciEnv::current()->Object_klass()) ) {
  3571         return TypeAryPtr::make( ptr, _ary, _klass, _klass_is_exact, offset, instance_id );
  3572       } else {
  3573         // cannot subclass, so the meet has to fall badly below the centerline
  3574         ptr = NotNull;
  3575         instance_id = InstanceBot;
  3576         return TypeInstPtr::make( ptr, ciEnv::current()->Object_klass(), false, NULL,offset, instance_id);
  3578     case Constant:
  3579     case NotNull:
  3580     case BotPTR:                // Fall down to object klass
  3581       // LCA is object_klass, but if we subclass from the top we can do better
  3582       if (above_centerline(tp->ptr())) {
  3583         // If 'tp'  is above the centerline and it is Object class
  3584         // then we can subclass in the Java class hierarchy.
  3585         if( tp->klass()->equals(ciEnv::current()->Object_klass()) ) {
  3586           // that is, my array type is a subtype of 'tp' klass
  3587           return make( ptr, (ptr == Constant ? const_oop() : NULL),
  3588                        _ary, _klass, _klass_is_exact, offset, instance_id );
  3591       // The other case cannot happen, since t cannot be a subtype of an array.
  3592       // The meet falls down to Object class below centerline.
  3593       if( ptr == Constant )
  3594          ptr = NotNull;
  3595       instance_id = InstanceBot;
  3596       return TypeInstPtr::make( ptr, ciEnv::current()->Object_klass(), false, NULL,offset, instance_id);
  3597     default: typerr(t);
  3601   return this;                  // Lint noise
  3604 //------------------------------xdual------------------------------------------
  3605 // Dual: compute field-by-field dual
  3606 const Type *TypeAryPtr::xdual() const {
  3607   return new TypeAryPtr( dual_ptr(), _const_oop, _ary->dual()->is_ary(),_klass, _klass_is_exact, dual_offset(), dual_instance_id() );
  3610 //----------------------interface_vs_oop---------------------------------------
  3611 #ifdef ASSERT
  3612 bool TypeAryPtr::interface_vs_oop(const Type *t) const {
  3613   const TypeAryPtr* t_aryptr = t->isa_aryptr();
  3614   if (t_aryptr) {
  3615     return _ary->interface_vs_oop(t_aryptr->_ary);
  3617   return false;
  3619 #endif
  3621 //------------------------------dump2------------------------------------------
  3622 #ifndef PRODUCT
  3623 void TypeAryPtr::dump2( Dict &d, uint depth, outputStream *st ) const {
  3624   _ary->dump2(d,depth,st);
  3625   switch( _ptr ) {
  3626   case Constant:
  3627     const_oop()->print(st);
  3628     break;
  3629   case BotPTR:
  3630     if (!WizardMode && !Verbose) {
  3631       if( _klass_is_exact ) st->print(":exact");
  3632       break;
  3634   case TopPTR:
  3635   case AnyNull:
  3636   case NotNull:
  3637     st->print(":%s", ptr_msg[_ptr]);
  3638     if( _klass_is_exact ) st->print(":exact");
  3639     break;
  3642   if( _offset != 0 ) {
  3643     int header_size = objArrayOopDesc::header_size() * wordSize;
  3644     if( _offset == OffsetTop )       st->print("+undefined");
  3645     else if( _offset == OffsetBot )  st->print("+any");
  3646     else if( _offset < header_size ) st->print("+%d", _offset);
  3647     else {
  3648       BasicType basic_elem_type = elem()->basic_type();
  3649       int array_base = arrayOopDesc::base_offset_in_bytes(basic_elem_type);
  3650       int elem_size = type2aelembytes(basic_elem_type);
  3651       st->print("[%d]", (_offset - array_base)/elem_size);
  3654   st->print(" *");
  3655   if (_instance_id == InstanceTop)
  3656     st->print(",iid=top");
  3657   else if (_instance_id != InstanceBot)
  3658     st->print(",iid=%d",_instance_id);
  3660 #endif
  3662 bool TypeAryPtr::empty(void) const {
  3663   if (_ary->empty())       return true;
  3664   return TypeOopPtr::empty();
  3667 //------------------------------add_offset-------------------------------------
  3668 const TypePtr *TypeAryPtr::add_offset( intptr_t offset ) const {
  3669   return make( _ptr, _const_oop, _ary, _klass, _klass_is_exact, xadd_offset(offset), _instance_id );
  3673 //=============================================================================
  3674 const TypeNarrowOop *TypeNarrowOop::BOTTOM;
  3675 const TypeNarrowOop *TypeNarrowOop::NULL_PTR;
  3678 const TypeNarrowOop* TypeNarrowOop::make(const TypePtr* type) {
  3679   return (const TypeNarrowOop*)(new TypeNarrowOop(type))->hashcons();
  3682 //------------------------------hash-------------------------------------------
  3683 // Type-specific hashing function.
  3684 int TypeNarrowOop::hash(void) const {
  3685   return _ptrtype->hash() + 7;
  3689 bool TypeNarrowOop::eq( const Type *t ) const {
  3690   const TypeNarrowOop* tc = t->isa_narrowoop();
  3691   if (tc != NULL) {
  3692     if (_ptrtype->base() != tc->_ptrtype->base()) {
  3693       return false;
  3695     return tc->_ptrtype->eq(_ptrtype);
  3697   return false;
  3700 bool TypeNarrowOop::singleton(void) const {    // TRUE if type is a singleton
  3701   return _ptrtype->singleton();
  3704 bool TypeNarrowOop::empty(void) const {
  3705   return _ptrtype->empty();
  3708 //------------------------------xmeet------------------------------------------
  3709 // Compute the MEET of two types.  It returns a new Type object.
  3710 const Type *TypeNarrowOop::xmeet( const Type *t ) const {
  3711   // Perform a fast test for common case; meeting the same types together.
  3712   if( this == t ) return this;  // Meeting same type-rep?
  3715   // Current "this->_base" is OopPtr
  3716   switch (t->base()) {          // switch on original type
  3718   case Int:                     // Mixing ints & oops happens when javac
  3719   case Long:                    // reuses local variables
  3720   case FloatTop:
  3721   case FloatCon:
  3722   case FloatBot:
  3723   case DoubleTop:
  3724   case DoubleCon:
  3725   case DoubleBot:
  3726   case AnyPtr:
  3727   case RawPtr:
  3728   case OopPtr:
  3729   case InstPtr:
  3730   case AryPtr:
  3731   case MetadataPtr:
  3732   case KlassPtr:
  3734   case Bottom:                  // Ye Olde Default
  3735     return Type::BOTTOM;
  3736   case Top:
  3737     return this;
  3739   case NarrowOop: {
  3740     const Type* result = _ptrtype->xmeet(t->make_ptr());
  3741     if (result->isa_ptr()) {
  3742       return TypeNarrowOop::make(result->is_ptr());
  3744     return result;
  3747   default:                      // All else is a mistake
  3748     typerr(t);
  3750   } // End of switch
  3752   return this;
  3755 const Type *TypeNarrowOop::xdual() const {    // Compute dual right now.
  3756   const TypePtr* odual = _ptrtype->dual()->is_ptr();
  3757   return new TypeNarrowOop(odual);
  3760 const Type *TypeNarrowOop::filter( const Type *kills ) const {
  3761   if (kills->isa_narrowoop()) {
  3762     const Type* ft =_ptrtype->filter(kills->is_narrowoop()->_ptrtype);
  3763     if (ft->empty())
  3764       return Type::TOP;           // Canonical empty value
  3765     if (ft->isa_ptr()) {
  3766       return make(ft->isa_ptr());
  3768     return ft;
  3769   } else if (kills->isa_ptr()) {
  3770     const Type* ft = _ptrtype->join(kills);
  3771     if (ft->empty())
  3772       return Type::TOP;           // Canonical empty value
  3773     return ft;
  3774   } else {
  3775     return Type::TOP;
  3780 intptr_t TypeNarrowOop::get_con() const {
  3781   return _ptrtype->get_con();
  3784 #ifndef PRODUCT
  3785 void TypeNarrowOop::dump2( Dict & d, uint depth, outputStream *st ) const {
  3786   st->print("narrowoop: ");
  3787   _ptrtype->dump2(d, depth, st);
  3789 #endif
  3793 //------------------------------eq---------------------------------------------
  3794 // Structural equality check for Type representations
  3795 bool TypeMetadataPtr::eq( const Type *t ) const {
  3796   const TypeMetadataPtr *a = (const TypeMetadataPtr*)t;
  3797   ciMetadata* one = metadata();
  3798   ciMetadata* two = a->metadata();
  3799   if (one == NULL || two == NULL) {
  3800     return (one == two) && TypePtr::eq(t);
  3801   } else {
  3802     return one->equals(two) && TypePtr::eq(t);
  3806 //------------------------------hash-------------------------------------------
  3807 // Type-specific hashing function.
  3808 int TypeMetadataPtr::hash(void) const {
  3809   return
  3810     (metadata() ? metadata()->hash() : 0) +
  3811     TypePtr::hash();
  3814 //------------------------------singleton--------------------------------------
  3815 // TRUE if Type is a singleton type, FALSE otherwise.   Singletons are simple
  3816 // constants
  3817 bool TypeMetadataPtr::singleton(void) const {
  3818   // detune optimizer to not generate constant metadta + constant offset as a constant!
  3819   // TopPTR, Null, AnyNull, Constant are all singletons
  3820   return (_offset == 0) && !below_centerline(_ptr);
  3823 //------------------------------add_offset-------------------------------------
  3824 const TypePtr *TypeMetadataPtr::add_offset( intptr_t offset ) const {
  3825   return make( _ptr, _metadata, xadd_offset(offset));
  3828 //-----------------------------filter------------------------------------------
  3829 // Do not allow interface-vs.-noninterface joins to collapse to top.
  3830 const Type *TypeMetadataPtr::filter( const Type *kills ) const {
  3831   const TypeMetadataPtr* ft = join(kills)->isa_metadataptr();
  3832   if (ft == NULL || ft->empty())
  3833     return Type::TOP;           // Canonical empty value
  3834   return ft;
  3837  //------------------------------get_con----------------------------------------
  3838 intptr_t TypeMetadataPtr::get_con() const {
  3839   assert( _ptr == Null || _ptr == Constant, "" );
  3840   assert( _offset >= 0, "" );
  3842   if (_offset != 0) {
  3843     // After being ported to the compiler interface, the compiler no longer
  3844     // directly manipulates the addresses of oops.  Rather, it only has a pointer
  3845     // to a handle at compile time.  This handle is embedded in the generated
  3846     // code and dereferenced at the time the nmethod is made.  Until that time,
  3847     // it is not reasonable to do arithmetic with the addresses of oops (we don't
  3848     // have access to the addresses!).  This does not seem to currently happen,
  3849     // but this assertion here is to help prevent its occurence.
  3850     tty->print_cr("Found oop constant with non-zero offset");
  3851     ShouldNotReachHere();
  3854   return (intptr_t)metadata()->constant_encoding();
  3857 //------------------------------cast_to_ptr_type-------------------------------
  3858 const Type *TypeMetadataPtr::cast_to_ptr_type(PTR ptr) const {
  3859   if( ptr == _ptr ) return this;
  3860   return make(ptr, metadata(), _offset);
  3863 //------------------------------meet-------------------------------------------
  3864 // Compute the MEET of two types.  It returns a new Type object.
  3865 const Type *TypeMetadataPtr::xmeet( const Type *t ) const {
  3866   // Perform a fast test for common case; meeting the same types together.
  3867   if( this == t ) return this;  // Meeting same type-rep?
  3869   // Current "this->_base" is OopPtr
  3870   switch (t->base()) {          // switch on original type
  3872   case Int:                     // Mixing ints & oops happens when javac
  3873   case Long:                    // reuses local variables
  3874   case FloatTop:
  3875   case FloatCon:
  3876   case FloatBot:
  3877   case DoubleTop:
  3878   case DoubleCon:
  3879   case DoubleBot:
  3880   case NarrowOop:
  3881   case Bottom:                  // Ye Olde Default
  3882     return Type::BOTTOM;
  3883   case Top:
  3884     return this;
  3886   default:                      // All else is a mistake
  3887     typerr(t);
  3889   case AnyPtr: {
  3890     // Found an AnyPtr type vs self-OopPtr type
  3891     const TypePtr *tp = t->is_ptr();
  3892     int offset = meet_offset(tp->offset());
  3893     PTR ptr = meet_ptr(tp->ptr());
  3894     switch (tp->ptr()) {
  3895     case Null:
  3896       if (ptr == Null)  return TypePtr::make(AnyPtr, ptr, offset);
  3897       // else fall through:
  3898     case TopPTR:
  3899     case AnyNull: {
  3900       return make(ptr, NULL, offset);
  3902     case BotPTR:
  3903     case NotNull:
  3904       return TypePtr::make(AnyPtr, ptr, offset);
  3905     default: typerr(t);
  3909   case RawPtr:
  3910   case KlassPtr:
  3911   case OopPtr:
  3912   case InstPtr:
  3913   case AryPtr:
  3914     return TypePtr::BOTTOM;     // Oop meet raw is not well defined
  3916   case MetadataPtr:
  3917     ShouldNotReachHere();
  3918     break;
  3920   } // End of switch
  3921   return this;                  // Return the double constant
  3925 //------------------------------xdual------------------------------------------
  3926 // Dual of a pure metadata pointer.
  3927 const Type *TypeMetadataPtr::xdual() const {
  3928   return new TypeMetadataPtr(dual_ptr(), metadata(), dual_offset());
  3931 //------------------------------dump2------------------------------------------
  3932 #ifndef PRODUCT
  3933 void TypeMetadataPtr::dump2( Dict &d, uint depth, outputStream *st ) const {
  3934   st->print("metadataptr:%s", ptr_msg[_ptr]);
  3935   if( metadata() ) st->print(INTPTR_FORMAT, metadata());
  3936   switch( _offset ) {
  3937   case OffsetTop: st->print("+top"); break;
  3938   case OffsetBot: st->print("+any"); break;
  3939   case         0: break;
  3940   default:        st->print("+%d",_offset); break;
  3943 #endif
  3946 //=============================================================================
  3947 // Convenience common pre-built type.
  3948 const TypeMetadataPtr *TypeMetadataPtr::BOTTOM;
  3950 TypeMetadataPtr::TypeMetadataPtr(PTR ptr, ciMetadata* metadata, int offset):
  3951   TypePtr(MetadataPtr, ptr, offset), _metadata(metadata) {
  3954 const TypeMetadataPtr* TypeMetadataPtr::make(ciMethod* m) {
  3955   return make(Constant, m, 0);
  3957 const TypeMetadataPtr* TypeMetadataPtr::make(ciMethodData* m) {
  3958   return make(Constant, m, 0);
  3961 //------------------------------make-------------------------------------------
  3962 // Create a meta data constant
  3963 const TypeMetadataPtr *TypeMetadataPtr::make(PTR ptr, ciMetadata* m, int offset) {
  3964   assert(m == NULL || !m->is_klass(), "wrong type");
  3965   return (TypeMetadataPtr*)(new TypeMetadataPtr(ptr, m, offset))->hashcons();
  3969 //=============================================================================
  3970 // Convenience common pre-built types.
  3972 // Not-null object klass or below
  3973 const TypeKlassPtr *TypeKlassPtr::OBJECT;
  3974 const TypeKlassPtr *TypeKlassPtr::OBJECT_OR_NULL;
  3976 //------------------------------TypeKlassPtr-----------------------------------
  3977 TypeKlassPtr::TypeKlassPtr( PTR ptr, ciKlass* klass, int offset )
  3978   : TypePtr(KlassPtr, ptr, offset), _klass(klass), _klass_is_exact(ptr == Constant) {
  3981 //------------------------------make-------------------------------------------
  3982 // ptr to klass 'k', if Constant, or possibly to a sub-klass if not a Constant
  3983 const TypeKlassPtr *TypeKlassPtr::make( PTR ptr, ciKlass* k, int offset ) {
  3984   assert( k != NULL, "Expect a non-NULL klass");
  3985   assert(k->is_instance_klass() || k->is_array_klass(), "Incorrect type of klass oop");
  3986   TypeKlassPtr *r =
  3987     (TypeKlassPtr*)(new TypeKlassPtr(ptr, k, offset))->hashcons();
  3989   return r;
  3992 //------------------------------eq---------------------------------------------
  3993 // Structural equality check for Type representations
  3994 bool TypeKlassPtr::eq( const Type *t ) const {
  3995   const TypeKlassPtr *p = t->is_klassptr();
  3996   return
  3997     klass()->equals(p->klass()) &&
  3998     TypePtr::eq(p);
  4001 //------------------------------hash-------------------------------------------
  4002 // Type-specific hashing function.
  4003 int TypeKlassPtr::hash(void) const {
  4004   return klass()->hash() + TypePtr::hash();
  4007 //------------------------------singleton--------------------------------------
  4008 // TRUE if Type is a singleton type, FALSE otherwise.   Singletons are simple
  4009 // constants
  4010 bool TypeKlassPtr::singleton(void) const {
  4011   // detune optimizer to not generate constant klass + constant offset as a constant!
  4012   // TopPTR, Null, AnyNull, Constant are all singletons
  4013   return (_offset == 0) && !below_centerline(_ptr);
  4016 //----------------------compute_klass------------------------------------------
  4017 // Compute the defining klass for this class
  4018 ciKlass* TypeAryPtr::compute_klass(DEBUG_ONLY(bool verify)) const {
  4019   // Compute _klass based on element type.
  4020   ciKlass* k_ary = NULL;
  4021   const TypeInstPtr *tinst;
  4022   const TypeAryPtr *tary;
  4023   const Type* el = elem();
  4024   if (el->isa_narrowoop()) {
  4025     el = el->make_ptr();
  4028   // Get element klass
  4029   if ((tinst = el->isa_instptr()) != NULL) {
  4030     // Compute array klass from element klass
  4031     k_ary = ciObjArrayKlass::make(tinst->klass());
  4032   } else if ((tary = el->isa_aryptr()) != NULL) {
  4033     // Compute array klass from element klass
  4034     ciKlass* k_elem = tary->klass();
  4035     // If element type is something like bottom[], k_elem will be null.
  4036     if (k_elem != NULL)
  4037       k_ary = ciObjArrayKlass::make(k_elem);
  4038   } else if ((el->base() == Type::Top) ||
  4039              (el->base() == Type::Bottom)) {
  4040     // element type of Bottom occurs from meet of basic type
  4041     // and object; Top occurs when doing join on Bottom.
  4042     // Leave k_ary at NULL.
  4043   } else {
  4044     // Cannot compute array klass directly from basic type,
  4045     // since subtypes of TypeInt all have basic type T_INT.
  4046 #ifdef ASSERT
  4047     if (verify && el->isa_int()) {
  4048       // Check simple cases when verifying klass.
  4049       BasicType bt = T_ILLEGAL;
  4050       if (el == TypeInt::BYTE) {
  4051         bt = T_BYTE;
  4052       } else if (el == TypeInt::SHORT) {
  4053         bt = T_SHORT;
  4054       } else if (el == TypeInt::CHAR) {
  4055         bt = T_CHAR;
  4056       } else if (el == TypeInt::INT) {
  4057         bt = T_INT;
  4058       } else {
  4059         return _klass; // just return specified klass
  4061       return ciTypeArrayKlass::make(bt);
  4063 #endif
  4064     assert(!el->isa_int(),
  4065            "integral arrays must be pre-equipped with a class");
  4066     // Compute array klass directly from basic type
  4067     k_ary = ciTypeArrayKlass::make(el->basic_type());
  4069   return k_ary;
  4072 //------------------------------klass------------------------------------------
  4073 // Return the defining klass for this class
  4074 ciKlass* TypeAryPtr::klass() const {
  4075   if( _klass ) return _klass;   // Return cached value, if possible
  4077   // Oops, need to compute _klass and cache it
  4078   ciKlass* k_ary = compute_klass();
  4080   if( this != TypeAryPtr::OOPS && this->dual() != TypeAryPtr::OOPS ) {
  4081     // The _klass field acts as a cache of the underlying
  4082     // ciKlass for this array type.  In order to set the field,
  4083     // we need to cast away const-ness.
  4084     //
  4085     // IMPORTANT NOTE: we *never* set the _klass field for the
  4086     // type TypeAryPtr::OOPS.  This Type is shared between all
  4087     // active compilations.  However, the ciKlass which represents
  4088     // this Type is *not* shared between compilations, so caching
  4089     // this value would result in fetching a dangling pointer.
  4090     //
  4091     // Recomputing the underlying ciKlass for each request is
  4092     // a bit less efficient than caching, but calls to
  4093     // TypeAryPtr::OOPS->klass() are not common enough to matter.
  4094     ((TypeAryPtr*)this)->_klass = k_ary;
  4095     if (UseCompressedOops && k_ary != NULL && k_ary->is_obj_array_klass() &&
  4096         _offset != 0 && _offset != arrayOopDesc::length_offset_in_bytes()) {
  4097       ((TypeAryPtr*)this)->_is_ptr_to_narrowoop = true;
  4100   return k_ary;
  4104 //------------------------------add_offset-------------------------------------
  4105 // Access internals of klass object
  4106 const TypePtr *TypeKlassPtr::add_offset( intptr_t offset ) const {
  4107   return make( _ptr, klass(), xadd_offset(offset) );
  4110 //------------------------------cast_to_ptr_type-------------------------------
  4111 const Type *TypeKlassPtr::cast_to_ptr_type(PTR ptr) const {
  4112   assert(_base == KlassPtr, "subclass must override cast_to_ptr_type");
  4113   if( ptr == _ptr ) return this;
  4114   return make(ptr, _klass, _offset);
  4118 //-----------------------------cast_to_exactness-------------------------------
  4119 const Type *TypeKlassPtr::cast_to_exactness(bool klass_is_exact) const {
  4120   if( klass_is_exact == _klass_is_exact ) return this;
  4121   if (!UseExactTypes)  return this;
  4122   return make(klass_is_exact ? Constant : NotNull, _klass, _offset);
  4126 //-----------------------------as_instance_type--------------------------------
  4127 // Corresponding type for an instance of the given class.
  4128 // It will be NotNull, and exact if and only if the klass type is exact.
  4129 const TypeOopPtr* TypeKlassPtr::as_instance_type() const {
  4130   ciKlass* k = klass();
  4131   bool    xk = klass_is_exact();
  4132   //return TypeInstPtr::make(TypePtr::NotNull, k, xk, NULL, 0);
  4133   const TypeOopPtr* toop = TypeOopPtr::make_from_klass_raw(k);
  4134   toop = toop->cast_to_ptr_type(TypePtr::NotNull)->is_oopptr();
  4135   return toop->cast_to_exactness(xk)->is_oopptr();
  4139 //------------------------------xmeet------------------------------------------
  4140 // Compute the MEET of two types, return a new Type object.
  4141 const Type    *TypeKlassPtr::xmeet( const Type *t ) const {
  4142   // Perform a fast test for common case; meeting the same types together.
  4143   if( this == t ) return this;  // Meeting same type-rep?
  4145   // Current "this->_base" is Pointer
  4146   switch (t->base()) {          // switch on original type
  4148   case Int:                     // Mixing ints & oops happens when javac
  4149   case Long:                    // reuses local variables
  4150   case FloatTop:
  4151   case FloatCon:
  4152   case FloatBot:
  4153   case DoubleTop:
  4154   case DoubleCon:
  4155   case DoubleBot:
  4156   case NarrowOop:
  4157   case Bottom:                  // Ye Olde Default
  4158     return Type::BOTTOM;
  4159   case Top:
  4160     return this;
  4162   default:                      // All else is a mistake
  4163     typerr(t);
  4165   case AnyPtr: {                // Meeting to AnyPtrs
  4166     // Found an AnyPtr type vs self-KlassPtr type
  4167     const TypePtr *tp = t->is_ptr();
  4168     int offset = meet_offset(tp->offset());
  4169     PTR ptr = meet_ptr(tp->ptr());
  4170     switch (tp->ptr()) {
  4171     case TopPTR:
  4172       return this;
  4173     case Null:
  4174       if( ptr == Null ) return TypePtr::make( AnyPtr, ptr, offset );
  4175     case AnyNull:
  4176       return make( ptr, klass(), offset );
  4177     case BotPTR:
  4178     case NotNull:
  4179       return TypePtr::make(AnyPtr, ptr, offset);
  4180     default: typerr(t);
  4184   case RawPtr:
  4185   case MetadataPtr:
  4186   case OopPtr:
  4187   case AryPtr:                  // Meet with AryPtr
  4188   case InstPtr:                 // Meet with InstPtr
  4189     return TypePtr::BOTTOM;
  4191   //
  4192   //             A-top         }
  4193   //           /   |   \       }  Tops
  4194   //       B-top A-any C-top   }
  4195   //          | /  |  \ |      }  Any-nulls
  4196   //       B-any   |   C-any   }
  4197   //          |    |    |
  4198   //       B-con A-con C-con   } constants; not comparable across classes
  4199   //          |    |    |
  4200   //       B-not   |   C-not   }
  4201   //          | \  |  / |      }  not-nulls
  4202   //       B-bot A-not C-bot   }
  4203   //           \   |   /       }  Bottoms
  4204   //             A-bot         }
  4205   //
  4207   case KlassPtr: {  // Meet two KlassPtr types
  4208     const TypeKlassPtr *tkls = t->is_klassptr();
  4209     int  off     = meet_offset(tkls->offset());
  4210     PTR  ptr     = meet_ptr(tkls->ptr());
  4212     // Check for easy case; klasses are equal (and perhaps not loaded!)
  4213     // If we have constants, then we created oops so classes are loaded
  4214     // and we can handle the constants further down.  This case handles
  4215     // not-loaded classes
  4216     if( ptr != Constant && tkls->klass()->equals(klass()) ) {
  4217       return make( ptr, klass(), off );
  4220     // Classes require inspection in the Java klass hierarchy.  Must be loaded.
  4221     ciKlass* tkls_klass = tkls->klass();
  4222     ciKlass* this_klass = this->klass();
  4223     assert( tkls_klass->is_loaded(), "This class should have been loaded.");
  4224     assert( this_klass->is_loaded(), "This class should have been loaded.");
  4226     // If 'this' type is above the centerline and is a superclass of the
  4227     // other, we can treat 'this' as having the same type as the other.
  4228     if ((above_centerline(this->ptr())) &&
  4229         tkls_klass->is_subtype_of(this_klass)) {
  4230       this_klass = tkls_klass;
  4232     // If 'tinst' type is above the centerline and is a superclass of the
  4233     // other, we can treat 'tinst' as having the same type as the other.
  4234     if ((above_centerline(tkls->ptr())) &&
  4235         this_klass->is_subtype_of(tkls_klass)) {
  4236       tkls_klass = this_klass;
  4239     // Check for classes now being equal
  4240     if (tkls_klass->equals(this_klass)) {
  4241       // If the klasses are equal, the constants may still differ.  Fall to
  4242       // NotNull if they do (neither constant is NULL; that is a special case
  4243       // handled elsewhere).
  4244       if( ptr == Constant ) {
  4245         if (this->_ptr == Constant && tkls->_ptr == Constant &&
  4246             this->klass()->equals(tkls->klass()));
  4247         else if (above_centerline(this->ptr()));
  4248         else if (above_centerline(tkls->ptr()));
  4249         else
  4250           ptr = NotNull;
  4252       return make( ptr, this_klass, off );
  4253     } // Else classes are not equal
  4255     // Since klasses are different, we require the LCA in the Java
  4256     // class hierarchy - which means we have to fall to at least NotNull.
  4257     if( ptr == TopPTR || ptr == AnyNull || ptr == Constant )
  4258       ptr = NotNull;
  4259     // Now we find the LCA of Java classes
  4260     ciKlass* k = this_klass->least_common_ancestor(tkls_klass);
  4261     return   make( ptr, k, off );
  4262   } // End of case KlassPtr
  4264   } // End of switch
  4265   return this;                  // Return the double constant
  4268 //------------------------------xdual------------------------------------------
  4269 // Dual: compute field-by-field dual
  4270 const Type    *TypeKlassPtr::xdual() const {
  4271   return new TypeKlassPtr( dual_ptr(), klass(), dual_offset() );
  4274 //------------------------------get_con----------------------------------------
  4275 intptr_t TypeKlassPtr::get_con() const {
  4276   assert( _ptr == Null || _ptr == Constant, "" );
  4277   assert( _offset >= 0, "" );
  4279   if (_offset != 0) {
  4280     // After being ported to the compiler interface, the compiler no longer
  4281     // directly manipulates the addresses of oops.  Rather, it only has a pointer
  4282     // to a handle at compile time.  This handle is embedded in the generated
  4283     // code and dereferenced at the time the nmethod is made.  Until that time,
  4284     // it is not reasonable to do arithmetic with the addresses of oops (we don't
  4285     // have access to the addresses!).  This does not seem to currently happen,
  4286     // but this assertion here is to help prevent its occurence.
  4287     tty->print_cr("Found oop constant with non-zero offset");
  4288     ShouldNotReachHere();
  4291   return (intptr_t)klass()->constant_encoding();
  4293 //------------------------------dump2------------------------------------------
  4294 // Dump Klass Type
  4295 #ifndef PRODUCT
  4296 void TypeKlassPtr::dump2( Dict & d, uint depth, outputStream *st ) const {
  4297   switch( _ptr ) {
  4298   case Constant:
  4299     st->print("precise ");
  4300   case NotNull:
  4302       const char *name = klass()->name()->as_utf8();
  4303       if( name ) {
  4304         st->print("klass %s: " INTPTR_FORMAT, name, klass());
  4305       } else {
  4306         ShouldNotReachHere();
  4309   case BotPTR:
  4310     if( !WizardMode && !Verbose && !_klass_is_exact ) break;
  4311   case TopPTR:
  4312   case AnyNull:
  4313     st->print(":%s", ptr_msg[_ptr]);
  4314     if( _klass_is_exact ) st->print(":exact");
  4315     break;
  4318   if( _offset ) {               // Dump offset, if any
  4319     if( _offset == OffsetBot )      { st->print("+any"); }
  4320     else if( _offset == OffsetTop ) { st->print("+unknown"); }
  4321     else                            { st->print("+%d", _offset); }
  4324   st->print(" *");
  4326 #endif
  4330 //=============================================================================
  4331 // Convenience common pre-built types.
  4333 //------------------------------make-------------------------------------------
  4334 const TypeFunc *TypeFunc::make( const TypeTuple *domain, const TypeTuple *range ) {
  4335   return (TypeFunc*)(new TypeFunc(domain,range))->hashcons();
  4338 //------------------------------make-------------------------------------------
  4339 const TypeFunc *TypeFunc::make(ciMethod* method) {
  4340   Compile* C = Compile::current();
  4341   const TypeFunc* tf = C->last_tf(method); // check cache
  4342   if (tf != NULL)  return tf;  // The hit rate here is almost 50%.
  4343   const TypeTuple *domain;
  4344   if (method->is_static()) {
  4345     domain = TypeTuple::make_domain(NULL, method->signature());
  4346   } else {
  4347     domain = TypeTuple::make_domain(method->holder(), method->signature());
  4349   const TypeTuple *range  = TypeTuple::make_range(method->signature());
  4350   tf = TypeFunc::make(domain, range);
  4351   C->set_last_tf(method, tf);  // fill cache
  4352   return tf;
  4355 //------------------------------meet-------------------------------------------
  4356 // Compute the MEET of two types.  It returns a new Type object.
  4357 const Type *TypeFunc::xmeet( const Type *t ) const {
  4358   // Perform a fast test for common case; meeting the same types together.
  4359   if( this == t ) return this;  // Meeting same type-rep?
  4361   // Current "this->_base" is Func
  4362   switch (t->base()) {          // switch on original type
  4364   case Bottom:                  // Ye Olde Default
  4365     return t;
  4367   default:                      // All else is a mistake
  4368     typerr(t);
  4370   case Top:
  4371     break;
  4373   return this;                  // Return the double constant
  4376 //------------------------------xdual------------------------------------------
  4377 // Dual: compute field-by-field dual
  4378 const Type *TypeFunc::xdual() const {
  4379   return this;
  4382 //------------------------------eq---------------------------------------------
  4383 // Structural equality check for Type representations
  4384 bool TypeFunc::eq( const Type *t ) const {
  4385   const TypeFunc *a = (const TypeFunc*)t;
  4386   return _domain == a->_domain &&
  4387     _range == a->_range;
  4390 //------------------------------hash-------------------------------------------
  4391 // Type-specific hashing function.
  4392 int TypeFunc::hash(void) const {
  4393   return (intptr_t)_domain + (intptr_t)_range;
  4396 //------------------------------dump2------------------------------------------
  4397 // Dump Function Type
  4398 #ifndef PRODUCT
  4399 void TypeFunc::dump2( Dict &d, uint depth, outputStream *st ) const {
  4400   if( _range->_cnt <= Parms )
  4401     st->print("void");
  4402   else {
  4403     uint i;
  4404     for (i = Parms; i < _range->_cnt-1; i++) {
  4405       _range->field_at(i)->dump2(d,depth,st);
  4406       st->print("/");
  4408     _range->field_at(i)->dump2(d,depth,st);
  4410   st->print(" ");
  4411   st->print("( ");
  4412   if( !depth || d[this] ) {     // Check for recursive dump
  4413     st->print("...)");
  4414     return;
  4416   d.Insert((void*)this,(void*)this);    // Stop recursion
  4417   if (Parms < _domain->_cnt)
  4418     _domain->field_at(Parms)->dump2(d,depth-1,st);
  4419   for (uint i = Parms+1; i < _domain->_cnt; i++) {
  4420     st->print(", ");
  4421     _domain->field_at(i)->dump2(d,depth-1,st);
  4423   st->print(" )");
  4425 #endif
  4427 //------------------------------singleton--------------------------------------
  4428 // TRUE if Type is a singleton type, FALSE otherwise.   Singletons are simple
  4429 // constants (Ldi nodes).  Singletons are integer, float or double constants
  4430 // or a single symbol.
  4431 bool TypeFunc::singleton(void) const {
  4432   return false;                 // Never a singleton
  4435 bool TypeFunc::empty(void) const {
  4436   return false;                 // Never empty
  4440 BasicType TypeFunc::return_type() const{
  4441   if (range()->cnt() == TypeFunc::Parms) {
  4442     return T_VOID;
  4444   return range()->field_at(TypeFunc::Parms)->basic_type();

mercurial