type.cpp revision 6010:abec000618bf
157109Speter/*
222514Sdarrenr * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
322514Sdarrenr * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
422514Sdarrenr *
522514Sdarrenr * This code is free software; you can redistribute it and/or modify it
622514Sdarrenr * under the terms of the GNU General Public License version 2 only, as
722514Sdarrenr * published by the Free Software Foundation.
8363526Scy *
922514Sdarrenr * This code is distributed in the hope that it will be useful, but WITHOUT
10145519Sdarrenr * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1160845Sdarrenr * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1260845Sdarrenr * version 2 for more details (a copy is included in the LICENSE file that
13145519Sdarrenr * accompanied this code).
1460845Sdarrenr *
1560845Sdarrenr * You should have received a copy of the GNU General Public License version
1660845Sdarrenr * 2 along with this work; if not, write to the Free Software Foundation,
1760845Sdarrenr * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1860845Sdarrenr *
1960845Sdarrenr * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2060845Sdarrenr * or visit www.oracle.com if you need additional information or have any
2160845Sdarrenr * questions.
2260845Sdarrenr *
2360845Sdarrenr */
2460845Sdarrenr
2560845Sdarrenr#include "precompiled.hpp"
2660845Sdarrenr#include "ci/ciMethodData.hpp"
2722514Sdarrenr#include "ci/ciTypeFlow.hpp"
2822514Sdarrenr#include "classfile/symbolTable.hpp"
2922514Sdarrenr#include "classfile/systemDictionary.hpp"
3022514Sdarrenr#include "compiler/compileLog.hpp"
31146669Seivind#include "libadt/dict.hpp"
3222514Sdarrenr#include "memory/gcLocker.hpp"
3322514Sdarrenr#include "memory/oopFactory.hpp"
3422514Sdarrenr#include "memory/resourceArea.hpp"
3522514Sdarrenr#include "oops/instanceKlass.hpp"
3622514Sdarrenr#include "oops/instanceMirrorKlass.hpp"
3722514Sdarrenr#include "oops/objArrayKlass.hpp"
38363526Scy#include "oops/typeArrayKlass.hpp"
39363526Scy#include "opto/matcher.hpp"
40363526Scy#include "opto/node.hpp"
41363526Scy#include "opto/opcodes.hpp"
42363526Scy#include "opto/type.hpp"
4360845Sdarrenr
44145519Sdarrenr// Portions of code courtesy of Clifford Click
4560845Sdarrenr
4622514Sdarrenr// Optimization - Graph Style
4722514Sdarrenr
4822514Sdarrenr// Dictionary of types shared among compilations.
4931183SpeterDict* Type::_shared_type_dict = NULL;
5031183Speter
51255332Scy// Array which maps compiler types to Basic Types
5260845SdarrenrType::TypeInfo Type::_type_info[Type::lastype] = {
5360845Sdarrenr  { Bad,             T_ILLEGAL,    "bad",           false, Node::NotAMachineReg, relocInfo::none          },  // Bad
5460845Sdarrenr  { Control,         T_ILLEGAL,    "control",       false, 0,                    relocInfo::none          },  // Control
5560845Sdarrenr  { Bottom,          T_VOID,       "top",           false, 0,                    relocInfo::none          },  // Top
5660845Sdarrenr  { Bad,             T_INT,        "int:",          false, Op_RegI,              relocInfo::none          },  // Int
5731183Speter  { Bad,             T_LONG,       "long:",         false, Op_RegL,              relocInfo::none          },  // Long
58145519Sdarrenr  { Half,            T_VOID,       "half",          false, 0,                    relocInfo::none          },  // Half
59145519Sdarrenr  { Bad,             T_NARROWOOP,  "narrowoop:",    false, Op_RegN,              relocInfo::none          },  // NarrowOop
6022514Sdarrenr  { Bad,             T_NARROWKLASS,"narrowklass:",  false, Op_RegN,              relocInfo::none          },  // NarrowKlass
6160845Sdarrenr  { Bad,             T_ILLEGAL,    "tuple:",        false, Node::NotAMachineReg, relocInfo::none          },  // Tuple
6260845Sdarrenr  { Bad,             T_ARRAY,      "array:",        false, Node::NotAMachineReg, relocInfo::none          },  // Array
6360845Sdarrenr
64145519Sdarrenr#ifdef SPARC
6560845Sdarrenr  { Bad,             T_ILLEGAL,    "vectors:",      false, 0,                    relocInfo::none          },  // VectorS
66130890Sdarrenr  { Bad,             T_ILLEGAL,    "vectord:",      false, Op_RegD,              relocInfo::none          },  // VectorD
6760845Sdarrenr  { Bad,             T_ILLEGAL,    "vectorx:",      false, 0,                    relocInfo::none          },  // VectorX
6860845Sdarrenr  { Bad,             T_ILLEGAL,    "vectory:",      false, 0,                    relocInfo::none          },  // VectorY
6922514Sdarrenr#elif defined(PPC64)
7022514Sdarrenr  { Bad,             T_ILLEGAL,    "vectors:",      false, 0,                    relocInfo::none          },  // VectorS
7122514Sdarrenr  { Bad,             T_ILLEGAL,    "vectord:",      false, Op_RegL,              relocInfo::none          },  // VectorD
7222514Sdarrenr  { Bad,             T_ILLEGAL,    "vectorx:",      false, 0,                    relocInfo::none          },  // VectorX
7357109Speter  { Bad,             T_ILLEGAL,    "vectory:",      false, 0,                    relocInfo::none          },  // VectorY
7457109Speter#else // all other
7557109Speter  { Bad,             T_ILLEGAL,    "vectors:",      false, Op_VecS,              relocInfo::none          },  // VectorS
7622514Sdarrenr  { Bad,             T_ILLEGAL,    "vectord:",      false, Op_VecD,              relocInfo::none          },  // VectorD
77361928Scy  { Bad,             T_ILLEGAL,    "vectorx:",      false, Op_VecX,              relocInfo::none          },  // VectorX
7822514Sdarrenr  { Bad,             T_ILLEGAL,    "vectory:",      false, Op_VecY,              relocInfo::none          },  // VectorY
7922514Sdarrenr#endif
8022514Sdarrenr  { Bad,             T_ADDRESS,    "anyptr:",       false, Op_RegP,              relocInfo::none          },  // AnyPtr
8122514Sdarrenr  { Bad,             T_ADDRESS,    "rawptr:",       false, Op_RegP,              relocInfo::none          },  // RawPtr
8222514Sdarrenr  { Bad,             T_OBJECT,     "oop:",          true,  Op_RegP,              relocInfo::oop_type      },  // OopPtr
8322514Sdarrenr  { Bad,             T_OBJECT,     "inst:",         true,  Op_RegP,              relocInfo::oop_type      },  // InstPtr
8422514Sdarrenr  { Bad,             T_OBJECT,     "ary:",          true,  Op_RegP,              relocInfo::oop_type      },  // AryPtr
8522514Sdarrenr  { Bad,             T_METADATA,   "metadata:",     false, Op_RegP,              relocInfo::metadata_type },  // MetadataPtr
8622514Sdarrenr  { Bad,             T_METADATA,   "klass:",        false, Op_RegP,              relocInfo::metadata_type },  // KlassPtr
8722514Sdarrenr  { Bad,             T_OBJECT,     "func",          false, 0,                    relocInfo::none          },  // Function
8822514Sdarrenr  { Abio,            T_ILLEGAL,    "abIO",          false, 0,                    relocInfo::none          },  // Abio
8922514Sdarrenr  { Return_Address,  T_ADDRESS,    "return_address",false, Op_RegP,              relocInfo::none          },  // Return_Address
9022514Sdarrenr  { Memory,          T_ILLEGAL,    "memory",        false, 0,                    relocInfo::none          },  // Memory
9122514Sdarrenr  { FloatBot,        T_FLOAT,      "float_top",     false, Op_RegF,              relocInfo::none          },  // FloatTop
9260845Sdarrenr  { FloatCon,        T_FLOAT,      "ftcon:",        false, Op_RegF,              relocInfo::none          },  // FloatCon
9360845Sdarrenr  { FloatTop,        T_FLOAT,      "float",         false, Op_RegF,              relocInfo::none          },  // FloatBot
9460845Sdarrenr  { DoubleBot,       T_DOUBLE,     "double_top",    false, Op_RegD,              relocInfo::none          },  // DoubleTop
9560845Sdarrenr  { DoubleCon,       T_DOUBLE,     "dblcon:",       false, Op_RegD,              relocInfo::none          },  // DoubleCon
9660845Sdarrenr  { DoubleTop,       T_DOUBLE,     "double",        false, Op_RegD,              relocInfo::none          },  // DoubleBot
9760845Sdarrenr  { Top,             T_ILLEGAL,    "bottom",        false, 0,                    relocInfo::none          }   // Bottom
9860845Sdarrenr};
99145519Sdarrenr
100145519Sdarrenr// Map ideal registers (machine types) to ideal types
101145519Sdarrenrconst Type *Type::mreg2type[_last_machine_leaf];
102145519Sdarrenr
10322514Sdarrenr// Map basic types to canonical Type* pointers.
10463520Sdarrenrconst Type* Type::     _const_basic_type[T_CONFLICT+1];
10522514Sdarrenr
10663520Sdarrenr// Map basic types to constant-zero Types.
10763520Sdarrenrconst Type* Type::            _zero_type[T_CONFLICT+1];
10863520Sdarrenr
10960845Sdarrenr// Map basic types to array-body alias types.
11060845Sdarrenrconst TypeAryPtr* TypeAryPtr::_array_body_type[T_CONFLICT+1];
11160845Sdarrenr
112145519Sdarrenr//=============================================================================
11360845Sdarrenr// Convenience common pre-built types.
114145519Sdarrenrconst Type *Type::ABIO;         // State-of-machine only
11560845Sdarrenrconst Type *Type::BOTTOM;       // All values
11660845Sdarrenrconst Type *Type::CONTROL;      // Control only
11760845Sdarrenrconst Type *Type::DOUBLE;       // All doubles
118145519Sdarrenrconst Type *Type::FLOAT;        // All floats
119145519Sdarrenrconst Type *Type::HALF;         // Placeholder half of doublewide type
120145519Sdarrenrconst Type *Type::MEMORY;       // Abstract store only
12160845Sdarrenrconst Type *Type::RETURN_ADDRESS;
12260845Sdarrenrconst Type *Type::TOP;          // No values in set
12360845Sdarrenr
12460845Sdarrenr//------------------------------get_const_type---------------------------
12560845Sdarrenrconst Type* Type::get_const_type(ciType* type) {
12660845Sdarrenr  if (type == NULL) {
127110920Sdarrenr    return NULL;
12860845Sdarrenr  } else if (type->is_primitive_type()) {
12960845Sdarrenr    return get_const_basic_type(type->basic_type());
13022514Sdarrenr  } else {
131170268Sdarrenr    return TypeOopPtr::make_from_klass(type->as_klass());
132170268Sdarrenr  }
133170268Sdarrenr}
134170268Sdarrenr
135170268Sdarrenr//---------------------------array_element_basic_type---------------------------------
13622514Sdarrenr// Mapping to the array element's basic type.
13722514SdarrenrBasicType Type::array_element_basic_type() const {
13822514Sdarrenr  BasicType bt = basic_type();
13922514Sdarrenr  if (bt == T_INT) {
14022514Sdarrenr    if (this == TypeInt::INT)   return T_INT;
14122514Sdarrenr    if (this == TypeInt::CHAR)  return T_CHAR;
14222514Sdarrenr    if (this == TypeInt::BYTE)  return T_BYTE;
14322514Sdarrenr    if (this == TypeInt::BOOL)  return T_BOOLEAN;
14422514Sdarrenr    if (this == TypeInt::SHORT) return T_SHORT;
145145519Sdarrenr    return T_VOID;
146145519Sdarrenr  }
147145519Sdarrenr  return bt;
148145519Sdarrenr}
14960845Sdarrenr
15060845Sdarrenr//---------------------------get_typeflow_type---------------------------------
15160845Sdarrenr// Import a type produced by ciTypeFlow.
152255332Scyconst Type* Type::get_typeflow_type(ciType* type) {
153255332Scy  switch (type->basic_type()) {
15460845Sdarrenr
15560845Sdarrenr  case ciTypeFlow::StateVector::T_BOTTOM:
15660845Sdarrenr    assert(type == ciTypeFlow::StateVector::bottom_type(), "");
15772006Sdarrenr    return Type::BOTTOM;
15872006Sdarrenr
159145519Sdarrenr  case ciTypeFlow::StateVector::T_TOP:
16072006Sdarrenr    assert(type == ciTypeFlow::StateVector::top_type(), "");
161145519Sdarrenr    return Type::TOP;
162145519Sdarrenr
16372006Sdarrenr  case ciTypeFlow::StateVector::T_NULL:
16472006Sdarrenr    assert(type == ciTypeFlow::StateVector::null_type(), "");
165255332Scy    return TypePtr::NULL_PTR;
16672006Sdarrenr
16772006Sdarrenr  case ciTypeFlow::StateVector::T_LONG2:
16872006Sdarrenr    // The ciTypeFlow pass pushes a long, then the half.
16972006Sdarrenr    // We do the same.
17060845Sdarrenr    assert(type == ciTypeFlow::StateVector::long2_type(), "");
17160845Sdarrenr    return TypeInt::TOP;
17260845Sdarrenr
17360845Sdarrenr  case ciTypeFlow::StateVector::T_DOUBLE2:
17460845Sdarrenr    // The ciTypeFlow pass pushes double, then the half.
17560845Sdarrenr    // Our convention is the same.
17660845Sdarrenr    assert(type == ciTypeFlow::StateVector::double2_type(), "");
177110920Sdarrenr    return Type::TOP;
17860845Sdarrenr
17960845Sdarrenr  case T_ADDRESS:
18060845Sdarrenr    assert(type->is_return_address(), "");
181145519Sdarrenr    return TypeRawPtr::make((address)(intptr_t)type->as_return_address()->bci());
182145519Sdarrenr
18360845Sdarrenr  default:
18460845Sdarrenr    // make sure we did not mix up the cases:
185145519Sdarrenr    assert(type != ciTypeFlow::StateVector::bottom_type(), "");
186145519Sdarrenr    assert(type != ciTypeFlow::StateVector::top_type(), "");
18722514Sdarrenr    assert(type != ciTypeFlow::StateVector::null_type(), "");
18822514Sdarrenr    assert(type != ciTypeFlow::StateVector::long2_type(), "");
18922514Sdarrenr    assert(type != ciTypeFlow::StateVector::double2_type(), "");
19037074Speter    assert(!type->is_return_address(), "");
19137074Speter
19237074Speter    return Type::get_const_type(type);
19337074Speter  }
19441197Snectar}
19522514Sdarrenr
19634739Speter
19722514Sdarrenr//-----------------------make_from_constant------------------------------------
198363526Scyconst Type* Type::make_from_constant(ciConstant constant,
199363526Scy                                     bool require_constant, bool is_autobox_cache) {
200  switch (constant.basic_type()) {
201  case T_BOOLEAN:  return TypeInt::make(constant.as_boolean());
202  case T_CHAR:     return TypeInt::make(constant.as_char());
203  case T_BYTE:     return TypeInt::make(constant.as_byte());
204  case T_SHORT:    return TypeInt::make(constant.as_short());
205  case T_INT:      return TypeInt::make(constant.as_int());
206  case T_LONG:     return TypeLong::make(constant.as_long());
207  case T_FLOAT:    return TypeF::make(constant.as_float());
208  case T_DOUBLE:   return TypeD::make(constant.as_double());
209  case T_ARRAY:
210  case T_OBJECT:
211    {
212      // cases:
213      //   can_be_constant    = (oop not scavengable || ScavengeRootsInCode != 0)
214      //   should_be_constant = (oop not scavengable || ScavengeRootsInCode >= 2)
215      // An oop is not scavengable if it is in the perm gen.
216      ciObject* oop_constant = constant.as_object();
217      if (oop_constant->is_null_object()) {
218        return Type::get_zero_type(T_OBJECT);
219      } else if (require_constant || oop_constant->should_be_constant()) {
220        return TypeOopPtr::make_from_constant(oop_constant, require_constant, is_autobox_cache);
221      }
222    }
223  }
224  // Fall through to failure
225  return NULL;
226}
227
228
229//------------------------------make-------------------------------------------
230// Create a simple Type, with default empty symbol sets.  Then hashcons it
231// and look for an existing copy in the type dictionary.
232const Type *Type::make( enum TYPES t ) {
233  return (new Type(t))->hashcons();
234}
235
236//------------------------------cmp--------------------------------------------
237int Type::cmp( const Type *const t1, const Type *const t2 ) {
238  if( t1->_base != t2->_base )
239    return 1;                   // Missed badly
240  assert(t1 != t2 || t1->eq(t2), "eq must be reflexive");
241  return !t1->eq(t2);           // Return ZERO if equal
242}
243
244//------------------------------hash-------------------------------------------
245int Type::uhash( const Type *const t ) {
246  return t->hash();
247}
248
249#define SMALLINT ((juint)3)  // a value too insignificant to consider widening
250
251//--------------------------Initialize_shared----------------------------------
252void Type::Initialize_shared(Compile* current) {
253  // This method does not need to be locked because the first system
254  // compilations (stub compilations) occur serially.  If they are
255  // changed to proceed in parallel, then this section will need
256  // locking.
257
258  Arena* save = current->type_arena();
259  Arena* shared_type_arena = new (mtCompiler)Arena();
260
261  current->set_type_arena(shared_type_arena);
262  _shared_type_dict =
263    new (shared_type_arena) Dict( (CmpKey)Type::cmp, (Hash)Type::uhash,
264                                  shared_type_arena, 128 );
265  current->set_type_dict(_shared_type_dict);
266
267  // Make shared pre-built types.
268  CONTROL = make(Control);      // Control only
269  TOP     = make(Top);          // No values in set
270  MEMORY  = make(Memory);       // Abstract store only
271  ABIO    = make(Abio);         // State-of-machine only
272  RETURN_ADDRESS=make(Return_Address);
273  FLOAT   = make(FloatBot);     // All floats
274  DOUBLE  = make(DoubleBot);    // All doubles
275  BOTTOM  = make(Bottom);       // Everything
276  HALF    = make(Half);         // Placeholder half of doublewide type
277
278  TypeF::ZERO = TypeF::make(0.0); // Float 0 (positive zero)
279  TypeF::ONE  = TypeF::make(1.0); // Float 1
280
281  TypeD::ZERO = TypeD::make(0.0); // Double 0 (positive zero)
282  TypeD::ONE  = TypeD::make(1.0); // Double 1
283
284  TypeInt::MINUS_1 = TypeInt::make(-1);  // -1
285  TypeInt::ZERO    = TypeInt::make( 0);  //  0
286  TypeInt::ONE     = TypeInt::make( 1);  //  1
287  TypeInt::BOOL    = TypeInt::make(0,1,   WidenMin);  // 0 or 1, FALSE or TRUE.
288  TypeInt::CC      = TypeInt::make(-1, 1, WidenMin);  // -1, 0 or 1, condition codes
289  TypeInt::CC_LT   = TypeInt::make(-1,-1, WidenMin);  // == TypeInt::MINUS_1
290  TypeInt::CC_GT   = TypeInt::make( 1, 1, WidenMin);  // == TypeInt::ONE
291  TypeInt::CC_EQ   = TypeInt::make( 0, 0, WidenMin);  // == TypeInt::ZERO
292  TypeInt::CC_LE   = TypeInt::make(-1, 0, WidenMin);
293  TypeInt::CC_GE   = TypeInt::make( 0, 1, WidenMin);  // == TypeInt::BOOL
294  TypeInt::BYTE    = TypeInt::make(-128,127,     WidenMin); // Bytes
295  TypeInt::UBYTE   = TypeInt::make(0, 255,       WidenMin); // Unsigned Bytes
296  TypeInt::CHAR    = TypeInt::make(0,65535,      WidenMin); // Java chars
297  TypeInt::SHORT   = TypeInt::make(-32768,32767, WidenMin); // Java shorts
298  TypeInt::POS     = TypeInt::make(0,max_jint,   WidenMin); // Non-neg values
299  TypeInt::POS1    = TypeInt::make(1,max_jint,   WidenMin); // Positive values
300  TypeInt::INT     = TypeInt::make(min_jint,max_jint, WidenMax); // 32-bit integers
301  TypeInt::SYMINT  = TypeInt::make(-max_jint,max_jint,WidenMin); // symmetric range
302  // CmpL is overloaded both as the bytecode computation returning
303  // a trinary (-1,0,+1) integer result AND as an efficient long
304  // compare returning optimizer ideal-type flags.
305  assert( TypeInt::CC_LT == TypeInt::MINUS_1, "types must match for CmpL to work" );
306  assert( TypeInt::CC_GT == TypeInt::ONE,     "types must match for CmpL to work" );
307  assert( TypeInt::CC_EQ == TypeInt::ZERO,    "types must match for CmpL to work" );
308  assert( TypeInt::CC_GE == TypeInt::BOOL,    "types must match for CmpL to work" );
309  assert( (juint)(TypeInt::CC->_hi - TypeInt::CC->_lo) <= SMALLINT, "CC is truly small");
310
311  TypeLong::MINUS_1 = TypeLong::make(-1);        // -1
312  TypeLong::ZERO    = TypeLong::make( 0);        //  0
313  TypeLong::ONE     = TypeLong::make( 1);        //  1
314  TypeLong::POS     = TypeLong::make(0,max_jlong, WidenMin); // Non-neg values
315  TypeLong::LONG    = TypeLong::make(min_jlong,max_jlong,WidenMax); // 64-bit integers
316  TypeLong::INT     = TypeLong::make((jlong)min_jint,(jlong)max_jint,WidenMin);
317  TypeLong::UINT    = TypeLong::make(0,(jlong)max_juint,WidenMin);
318
319  const Type **fboth =(const Type**)shared_type_arena->Amalloc_4(2*sizeof(Type*));
320  fboth[0] = Type::CONTROL;
321  fboth[1] = Type::CONTROL;
322  TypeTuple::IFBOTH = TypeTuple::make( 2, fboth );
323
324  const Type **ffalse =(const Type**)shared_type_arena->Amalloc_4(2*sizeof(Type*));
325  ffalse[0] = Type::CONTROL;
326  ffalse[1] = Type::TOP;
327  TypeTuple::IFFALSE = TypeTuple::make( 2, ffalse );
328
329  const Type **fneither =(const Type**)shared_type_arena->Amalloc_4(2*sizeof(Type*));
330  fneither[0] = Type::TOP;
331  fneither[1] = Type::TOP;
332  TypeTuple::IFNEITHER = TypeTuple::make( 2, fneither );
333
334  const Type **ftrue =(const Type**)shared_type_arena->Amalloc_4(2*sizeof(Type*));
335  ftrue[0] = Type::TOP;
336  ftrue[1] = Type::CONTROL;
337  TypeTuple::IFTRUE = TypeTuple::make( 2, ftrue );
338
339  const Type **floop =(const Type**)shared_type_arena->Amalloc_4(2*sizeof(Type*));
340  floop[0] = Type::CONTROL;
341  floop[1] = TypeInt::INT;
342  TypeTuple::LOOPBODY = TypeTuple::make( 2, floop );
343
344  TypePtr::NULL_PTR= TypePtr::make( AnyPtr, TypePtr::Null, 0 );
345  TypePtr::NOTNULL = TypePtr::make( AnyPtr, TypePtr::NotNull, OffsetBot );
346  TypePtr::BOTTOM  = TypePtr::make( AnyPtr, TypePtr::BotPTR, OffsetBot );
347
348  TypeRawPtr::BOTTOM = TypeRawPtr::make( TypePtr::BotPTR );
349  TypeRawPtr::NOTNULL= TypeRawPtr::make( TypePtr::NotNull );
350
351  const Type **fmembar = TypeTuple::fields(0);
352  TypeTuple::MEMBAR = TypeTuple::make(TypeFunc::Parms+0, fmembar);
353
354  const Type **fsc = (const Type**)shared_type_arena->Amalloc_4(2*sizeof(Type*));
355  fsc[0] = TypeInt::CC;
356  fsc[1] = Type::MEMORY;
357  TypeTuple::STORECONDITIONAL = TypeTuple::make(2, fsc);
358
359  TypeInstPtr::NOTNULL = TypeInstPtr::make(TypePtr::NotNull, current->env()->Object_klass());
360  TypeInstPtr::BOTTOM  = TypeInstPtr::make(TypePtr::BotPTR,  current->env()->Object_klass());
361  TypeInstPtr::MIRROR  = TypeInstPtr::make(TypePtr::NotNull, current->env()->Class_klass());
362  TypeInstPtr::MARK    = TypeInstPtr::make(TypePtr::BotPTR,  current->env()->Object_klass(),
363                                           false, 0, oopDesc::mark_offset_in_bytes());
364  TypeInstPtr::KLASS   = TypeInstPtr::make(TypePtr::BotPTR,  current->env()->Object_klass(),
365                                           false, 0, oopDesc::klass_offset_in_bytes());
366  TypeOopPtr::BOTTOM  = TypeOopPtr::make(TypePtr::BotPTR, OffsetBot, TypeOopPtr::InstanceBot, NULL);
367
368  TypeMetadataPtr::BOTTOM = TypeMetadataPtr::make(TypePtr::BotPTR, NULL, OffsetBot);
369
370  TypeNarrowOop::NULL_PTR = TypeNarrowOop::make( TypePtr::NULL_PTR );
371  TypeNarrowOop::BOTTOM   = TypeNarrowOop::make( TypeInstPtr::BOTTOM );
372
373  TypeNarrowKlass::NULL_PTR = TypeNarrowKlass::make( TypePtr::NULL_PTR );
374
375  mreg2type[Op_Node] = Type::BOTTOM;
376  mreg2type[Op_Set ] = 0;
377  mreg2type[Op_RegN] = TypeNarrowOop::BOTTOM;
378  mreg2type[Op_RegI] = TypeInt::INT;
379  mreg2type[Op_RegP] = TypePtr::BOTTOM;
380  mreg2type[Op_RegF] = Type::FLOAT;
381  mreg2type[Op_RegD] = Type::DOUBLE;
382  mreg2type[Op_RegL] = TypeLong::LONG;
383  mreg2type[Op_RegFlags] = TypeInt::CC;
384
385  TypeAryPtr::RANGE   = TypeAryPtr::make( TypePtr::BotPTR, TypeAry::make(Type::BOTTOM,TypeInt::POS), NULL /* current->env()->Object_klass() */, false, arrayOopDesc::length_offset_in_bytes());
386
387  TypeAryPtr::NARROWOOPS = TypeAryPtr::make(TypePtr::BotPTR, TypeAry::make(TypeNarrowOop::BOTTOM, TypeInt::POS), NULL /*ciArrayKlass::make(o)*/,  false,  Type::OffsetBot);
388
389#ifdef _LP64
390  if (UseCompressedOops) {
391    assert(TypeAryPtr::NARROWOOPS->is_ptr_to_narrowoop(), "array of narrow oops must be ptr to narrow oop");
392    TypeAryPtr::OOPS  = TypeAryPtr::NARROWOOPS;
393  } else
394#endif
395  {
396    // There is no shared klass for Object[].  See note in TypeAryPtr::klass().
397    TypeAryPtr::OOPS  = TypeAryPtr::make(TypePtr::BotPTR, TypeAry::make(TypeInstPtr::BOTTOM,TypeInt::POS), NULL /*ciArrayKlass::make(o)*/,  false,  Type::OffsetBot);
398  }
399  TypeAryPtr::BYTES   = TypeAryPtr::make(TypePtr::BotPTR, TypeAry::make(TypeInt::BYTE      ,TypeInt::POS), ciTypeArrayKlass::make(T_BYTE),   true,  Type::OffsetBot);
400  TypeAryPtr::SHORTS  = TypeAryPtr::make(TypePtr::BotPTR, TypeAry::make(TypeInt::SHORT     ,TypeInt::POS), ciTypeArrayKlass::make(T_SHORT),  true,  Type::OffsetBot);
401  TypeAryPtr::CHARS   = TypeAryPtr::make(TypePtr::BotPTR, TypeAry::make(TypeInt::CHAR      ,TypeInt::POS), ciTypeArrayKlass::make(T_CHAR),   true,  Type::OffsetBot);
402  TypeAryPtr::INTS    = TypeAryPtr::make(TypePtr::BotPTR, TypeAry::make(TypeInt::INT       ,TypeInt::POS), ciTypeArrayKlass::make(T_INT),    true,  Type::OffsetBot);
403  TypeAryPtr::LONGS   = TypeAryPtr::make(TypePtr::BotPTR, TypeAry::make(TypeLong::LONG     ,TypeInt::POS), ciTypeArrayKlass::make(T_LONG),   true,  Type::OffsetBot);
404  TypeAryPtr::FLOATS  = TypeAryPtr::make(TypePtr::BotPTR, TypeAry::make(Type::FLOAT        ,TypeInt::POS), ciTypeArrayKlass::make(T_FLOAT),  true,  Type::OffsetBot);
405  TypeAryPtr::DOUBLES = TypeAryPtr::make(TypePtr::BotPTR, TypeAry::make(Type::DOUBLE       ,TypeInt::POS), ciTypeArrayKlass::make(T_DOUBLE), true,  Type::OffsetBot);
406
407  // Nobody should ask _array_body_type[T_NARROWOOP]. Use NULL as assert.
408  TypeAryPtr::_array_body_type[T_NARROWOOP] = NULL;
409  TypeAryPtr::_array_body_type[T_OBJECT]  = TypeAryPtr::OOPS;
410  TypeAryPtr::_array_body_type[T_ARRAY]   = TypeAryPtr::OOPS; // arrays are stored in oop arrays
411  TypeAryPtr::_array_body_type[T_BYTE]    = TypeAryPtr::BYTES;
412  TypeAryPtr::_array_body_type[T_BOOLEAN] = TypeAryPtr::BYTES;  // boolean[] is a byte array
413  TypeAryPtr::_array_body_type[T_SHORT]   = TypeAryPtr::SHORTS;
414  TypeAryPtr::_array_body_type[T_CHAR]    = TypeAryPtr::CHARS;
415  TypeAryPtr::_array_body_type[T_INT]     = TypeAryPtr::INTS;
416  TypeAryPtr::_array_body_type[T_LONG]    = TypeAryPtr::LONGS;
417  TypeAryPtr::_array_body_type[T_FLOAT]   = TypeAryPtr::FLOATS;
418  TypeAryPtr::_array_body_type[T_DOUBLE]  = TypeAryPtr::DOUBLES;
419
420  TypeKlassPtr::OBJECT = TypeKlassPtr::make( TypePtr::NotNull, current->env()->Object_klass(), 0 );
421  TypeKlassPtr::OBJECT_OR_NULL = TypeKlassPtr::make( TypePtr::BotPTR, current->env()->Object_klass(), 0 );
422
423  const Type **fi2c = TypeTuple::fields(2);
424  fi2c[TypeFunc::Parms+0] = TypeInstPtr::BOTTOM; // Method*
425  fi2c[TypeFunc::Parms+1] = TypeRawPtr::BOTTOM; // argument pointer
426  TypeTuple::START_I2C = TypeTuple::make(TypeFunc::Parms+2, fi2c);
427
428  const Type **intpair = TypeTuple::fields(2);
429  intpair[0] = TypeInt::INT;
430  intpair[1] = TypeInt::INT;
431  TypeTuple::INT_PAIR = TypeTuple::make(2, intpair);
432
433  const Type **longpair = TypeTuple::fields(2);
434  longpair[0] = TypeLong::LONG;
435  longpair[1] = TypeLong::LONG;
436  TypeTuple::LONG_PAIR = TypeTuple::make(2, longpair);
437
438  const Type **intccpair = TypeTuple::fields(2);
439  intccpair[0] = TypeInt::INT;
440  intccpair[1] = TypeInt::CC;
441  TypeTuple::INT_CC_PAIR = TypeTuple::make(2, intccpair);
442
443  const Type **longccpair = TypeTuple::fields(2);
444  longccpair[0] = TypeLong::LONG;
445  longccpair[1] = TypeInt::CC;
446  TypeTuple::LONG_CC_PAIR = TypeTuple::make(2, longccpair);
447
448  _const_basic_type[T_NARROWOOP]   = TypeNarrowOop::BOTTOM;
449  _const_basic_type[T_NARROWKLASS] = Type::BOTTOM;
450  _const_basic_type[T_BOOLEAN]     = TypeInt::BOOL;
451  _const_basic_type[T_CHAR]        = TypeInt::CHAR;
452  _const_basic_type[T_BYTE]        = TypeInt::BYTE;
453  _const_basic_type[T_SHORT]       = TypeInt::SHORT;
454  _const_basic_type[T_INT]         = TypeInt::INT;
455  _const_basic_type[T_LONG]        = TypeLong::LONG;
456  _const_basic_type[T_FLOAT]       = Type::FLOAT;
457  _const_basic_type[T_DOUBLE]      = Type::DOUBLE;
458  _const_basic_type[T_OBJECT]      = TypeInstPtr::BOTTOM;
459  _const_basic_type[T_ARRAY]       = TypeInstPtr::BOTTOM; // there is no separate bottom for arrays
460  _const_basic_type[T_VOID]        = TypePtr::NULL_PTR;   // reflection represents void this way
461  _const_basic_type[T_ADDRESS]     = TypeRawPtr::BOTTOM;  // both interpreter return addresses & random raw ptrs
462  _const_basic_type[T_CONFLICT]    = Type::BOTTOM;        // why not?
463
464  _zero_type[T_NARROWOOP]   = TypeNarrowOop::NULL_PTR;
465  _zero_type[T_NARROWKLASS] = TypeNarrowKlass::NULL_PTR;
466  _zero_type[T_BOOLEAN]     = TypeInt::ZERO;     // false == 0
467  _zero_type[T_CHAR]        = TypeInt::ZERO;     // '\0' == 0
468  _zero_type[T_BYTE]        = TypeInt::ZERO;     // 0x00 == 0
469  _zero_type[T_SHORT]       = TypeInt::ZERO;     // 0x0000 == 0
470  _zero_type[T_INT]         = TypeInt::ZERO;
471  _zero_type[T_LONG]        = TypeLong::ZERO;
472  _zero_type[T_FLOAT]       = TypeF::ZERO;
473  _zero_type[T_DOUBLE]      = TypeD::ZERO;
474  _zero_type[T_OBJECT]      = TypePtr::NULL_PTR;
475  _zero_type[T_ARRAY]       = TypePtr::NULL_PTR; // null array is null oop
476  _zero_type[T_ADDRESS]     = TypePtr::NULL_PTR; // raw pointers use the same null
477  _zero_type[T_VOID]        = Type::TOP;         // the only void value is no value at all
478
479  // get_zero_type() should not happen for T_CONFLICT
480  _zero_type[T_CONFLICT]= NULL;
481
482  // Vector predefined types, it needs initialized _const_basic_type[].
483  if (Matcher::vector_size_supported(T_BYTE,4)) {
484    TypeVect::VECTS = TypeVect::make(T_BYTE,4);
485  }
486  if (Matcher::vector_size_supported(T_FLOAT,2)) {
487    TypeVect::VECTD = TypeVect::make(T_FLOAT,2);
488  }
489  if (Matcher::vector_size_supported(T_FLOAT,4)) {
490    TypeVect::VECTX = TypeVect::make(T_FLOAT,4);
491  }
492  if (Matcher::vector_size_supported(T_FLOAT,8)) {
493    TypeVect::VECTY = TypeVect::make(T_FLOAT,8);
494  }
495  mreg2type[Op_VecS] = TypeVect::VECTS;
496  mreg2type[Op_VecD] = TypeVect::VECTD;
497  mreg2type[Op_VecX] = TypeVect::VECTX;
498  mreg2type[Op_VecY] = TypeVect::VECTY;
499
500  // Restore working type arena.
501  current->set_type_arena(save);
502  current->set_type_dict(NULL);
503}
504
505//------------------------------Initialize-------------------------------------
506void Type::Initialize(Compile* current) {
507  assert(current->type_arena() != NULL, "must have created type arena");
508
509  if (_shared_type_dict == NULL) {
510    Initialize_shared(current);
511  }
512
513  Arena* type_arena = current->type_arena();
514
515  // Create the hash-cons'ing dictionary with top-level storage allocation
516  Dict *tdic = new (type_arena) Dict( (CmpKey)Type::cmp,(Hash)Type::uhash, type_arena, 128 );
517  current->set_type_dict(tdic);
518
519  // Transfer the shared types.
520  DictI i(_shared_type_dict);
521  for( ; i.test(); ++i ) {
522    Type* t = (Type*)i._value;
523    tdic->Insert(t,t);  // New Type, insert into Type table
524  }
525}
526
527//------------------------------hashcons---------------------------------------
528// Do the hash-cons trick.  If the Type already exists in the type table,
529// delete the current Type and return the existing Type.  Otherwise stick the
530// current Type in the Type table.
531const Type *Type::hashcons(void) {
532  debug_only(base());           // Check the assertion in Type::base().
533  // Look up the Type in the Type dictionary
534  Dict *tdic = type_dict();
535  Type* old = (Type*)(tdic->Insert(this, this, false));
536  if( old ) {                   // Pre-existing Type?
537    if( old != this )           // Yes, this guy is not the pre-existing?
538      delete this;              // Yes, Nuke this guy
539    assert( old->_dual, "" );
540    return old;                 // Return pre-existing
541  }
542
543  // Every type has a dual (to make my lattice symmetric).
544  // Since we just discovered a new Type, compute its dual right now.
545  assert( !_dual, "" );         // No dual yet
546  _dual = xdual();              // Compute the dual
547  if( cmp(this,_dual)==0 ) {    // Handle self-symmetric
548    _dual = this;
549    return this;
550  }
551  assert( !_dual->_dual, "" );  // No reverse dual yet
552  assert( !(*tdic)[_dual], "" ); // Dual not in type system either
553  // New Type, insert into Type table
554  tdic->Insert((void*)_dual,(void*)_dual);
555  ((Type*)_dual)->_dual = this; // Finish up being symmetric
556#ifdef ASSERT
557  Type *dual_dual = (Type*)_dual->xdual();
558  assert( eq(dual_dual), "xdual(xdual()) should be identity" );
559  delete dual_dual;
560#endif
561  return this;                  // Return new Type
562}
563
564//------------------------------eq---------------------------------------------
565// Structural equality check for Type representations
566bool Type::eq( const Type * ) const {
567  return true;                  // Nothing else can go wrong
568}
569
570//------------------------------hash-------------------------------------------
571// Type-specific hashing function.
572int Type::hash(void) const {
573  return _base;
574}
575
576//------------------------------is_finite--------------------------------------
577// Has a finite value
578bool Type::is_finite() const {
579  return false;
580}
581
582//------------------------------is_nan-----------------------------------------
583// Is not a number (NaN)
584bool Type::is_nan()    const {
585  return false;
586}
587
588//----------------------interface_vs_oop---------------------------------------
589#ifdef ASSERT
590bool Type::interface_vs_oop_helper(const Type *t) const {
591  bool result = false;
592
593  const TypePtr* this_ptr = this->make_ptr(); // In case it is narrow_oop
594  const TypePtr*    t_ptr =    t->make_ptr();
595  if( this_ptr == NULL || t_ptr == NULL )
596    return result;
597
598  const TypeInstPtr* this_inst = this_ptr->isa_instptr();
599  const TypeInstPtr*    t_inst =    t_ptr->isa_instptr();
600  if( this_inst && this_inst->is_loaded() && t_inst && t_inst->is_loaded() ) {
601    bool this_interface = this_inst->klass()->is_interface();
602    bool    t_interface =    t_inst->klass()->is_interface();
603    result = this_interface ^ t_interface;
604  }
605
606  return result;
607}
608
609bool Type::interface_vs_oop(const Type *t) const {
610  if (interface_vs_oop_helper(t)) {
611    return true;
612  }
613  // Now check the speculative parts as well
614  const TypeOopPtr* this_spec = isa_oopptr() != NULL ? isa_oopptr()->speculative() : NULL;
615  const TypeOopPtr* t_spec = t->isa_oopptr() != NULL ? t->isa_oopptr()->speculative() : NULL;
616  if (this_spec != NULL && t_spec != NULL) {
617    if (this_spec->interface_vs_oop_helper(t_spec)) {
618      return true;
619    }
620    return false;
621  }
622  if (this_spec != NULL && this_spec->interface_vs_oop_helper(t)) {
623    return true;
624  }
625  if (t_spec != NULL && interface_vs_oop_helper(t_spec)) {
626    return true;
627  }
628  return false;
629}
630
631#endif
632
633//------------------------------meet-------------------------------------------
634// Compute the MEET of two types.  NOT virtual.  It enforces that meet is
635// commutative and the lattice is symmetric.
636const Type *Type::meet( const Type *t ) const {
637  if (isa_narrowoop() && t->isa_narrowoop()) {
638    const Type* result = make_ptr()->meet(t->make_ptr());
639    return result->make_narrowoop();
640  }
641  if (isa_narrowklass() && t->isa_narrowklass()) {
642    const Type* result = make_ptr()->meet(t->make_ptr());
643    return result->make_narrowklass();
644  }
645
646  const Type *mt = xmeet(t);
647  if (isa_narrowoop() || t->isa_narrowoop()) return mt;
648  if (isa_narrowklass() || t->isa_narrowklass()) return mt;
649#ifdef ASSERT
650  assert( mt == t->xmeet(this), "meet not commutative" );
651  const Type* dual_join = mt->_dual;
652  const Type *t2t    = dual_join->xmeet(t->_dual);
653  const Type *t2this = dual_join->xmeet(   _dual);
654
655  // Interface meet Oop is Not Symmetric:
656  // Interface:AnyNull meet Oop:AnyNull == Interface:AnyNull
657  // Interface:NotNull meet Oop:NotNull == java/lang/Object:NotNull
658
659  if( !interface_vs_oop(t) && (t2t != t->_dual || t2this != _dual) ) {
660    tty->print_cr("=== Meet Not Symmetric ===");
661    tty->print("t   =                   ");         t->dump(); tty->cr();
662    tty->print("this=                   ");            dump(); tty->cr();
663    tty->print("mt=(t meet this)=       ");        mt->dump(); tty->cr();
664
665    tty->print("t_dual=                 ");  t->_dual->dump(); tty->cr();
666    tty->print("this_dual=              ");     _dual->dump(); tty->cr();
667    tty->print("mt_dual=                "); mt->_dual->dump(); tty->cr();
668
669    tty->print("mt_dual meet t_dual=    "); t2t      ->dump(); tty->cr();
670    tty->print("mt_dual meet this_dual= "); t2this   ->dump(); tty->cr();
671
672    fatal("meet not symmetric" );
673  }
674#endif
675  return mt;
676}
677
678//------------------------------xmeet------------------------------------------
679// Compute the MEET of two types.  It returns a new Type object.
680const Type *Type::xmeet( const Type *t ) const {
681  // Perform a fast test for common case; meeting the same types together.
682  if( this == t ) return this;  // Meeting same type-rep?
683
684  // Meeting TOP with anything?
685  if( _base == Top ) return t;
686
687  // Meeting BOTTOM with anything?
688  if( _base == Bottom ) return BOTTOM;
689
690  // Current "this->_base" is one of: Bad, Multi, Control, Top,
691  // Abio, Abstore, Floatxxx, Doublexxx, Bottom, lastype.
692  switch (t->base()) {  // Switch on original type
693
694  // Cut in half the number of cases I must handle.  Only need cases for when
695  // the given enum "t->type" is less than or equal to the local enum "type".
696  case FloatCon:
697  case DoubleCon:
698  case Int:
699  case Long:
700    return t->xmeet(this);
701
702  case OopPtr:
703    return t->xmeet(this);
704
705  case InstPtr:
706    return t->xmeet(this);
707
708  case MetadataPtr:
709  case KlassPtr:
710    return t->xmeet(this);
711
712  case AryPtr:
713    return t->xmeet(this);
714
715  case NarrowOop:
716    return t->xmeet(this);
717
718  case NarrowKlass:
719    return t->xmeet(this);
720
721  case Bad:                     // Type check
722  default:                      // Bogus type not in lattice
723    typerr(t);
724    return Type::BOTTOM;
725
726  case Bottom:                  // Ye Olde Default
727    return t;
728
729  case FloatTop:
730    if( _base == FloatTop ) return this;
731  case FloatBot:                // Float
732    if( _base == FloatBot || _base == FloatTop ) return FLOAT;
733    if( _base == DoubleTop || _base == DoubleBot ) return Type::BOTTOM;
734    typerr(t);
735    return Type::BOTTOM;
736
737  case DoubleTop:
738    if( _base == DoubleTop ) return this;
739  case DoubleBot:               // Double
740    if( _base == DoubleBot || _base == DoubleTop ) return DOUBLE;
741    if( _base == FloatTop || _base == FloatBot ) return Type::BOTTOM;
742    typerr(t);
743    return Type::BOTTOM;
744
745  // These next few cases must match exactly or it is a compile-time error.
746  case Control:                 // Control of code
747  case Abio:                    // State of world outside of program
748  case Memory:
749    if( _base == t->_base )  return this;
750    typerr(t);
751    return Type::BOTTOM;
752
753  case Top:                     // Top of the lattice
754    return this;
755  }
756
757  // The type is unchanged
758  return this;
759}
760
761//-----------------------------filter------------------------------------------
762const Type *Type::filter( const Type *kills ) const {
763  const Type* ft = join(kills);
764  if (ft->empty())
765    return Type::TOP;           // Canonical empty value
766  return ft;
767}
768
769//------------------------------xdual------------------------------------------
770// Compute dual right now.
771const Type::TYPES Type::dual_type[Type::lastype] = {
772  Bad,          // Bad
773  Control,      // Control
774  Bottom,       // Top
775  Bad,          // Int - handled in v-call
776  Bad,          // Long - handled in v-call
777  Half,         // Half
778  Bad,          // NarrowOop - handled in v-call
779  Bad,          // NarrowKlass - handled in v-call
780
781  Bad,          // Tuple - handled in v-call
782  Bad,          // Array - handled in v-call
783  Bad,          // VectorS - handled in v-call
784  Bad,          // VectorD - handled in v-call
785  Bad,          // VectorX - handled in v-call
786  Bad,          // VectorY - handled in v-call
787
788  Bad,          // AnyPtr - handled in v-call
789  Bad,          // RawPtr - handled in v-call
790  Bad,          // OopPtr - handled in v-call
791  Bad,          // InstPtr - handled in v-call
792  Bad,          // AryPtr - handled in v-call
793
794  Bad,          //  MetadataPtr - handled in v-call
795  Bad,          // KlassPtr - handled in v-call
796
797  Bad,          // Function - handled in v-call
798  Abio,         // Abio
799  Return_Address,// Return_Address
800  Memory,       // Memory
801  FloatBot,     // FloatTop
802  FloatCon,     // FloatCon
803  FloatTop,     // FloatBot
804  DoubleBot,    // DoubleTop
805  DoubleCon,    // DoubleCon
806  DoubleTop,    // DoubleBot
807  Top           // Bottom
808};
809
810const Type *Type::xdual() const {
811  // Note: the base() accessor asserts the sanity of _base.
812  assert(_type_info[base()].dual_type != Bad, "implement with v-call");
813  return new Type(_type_info[_base].dual_type);
814}
815
816//------------------------------has_memory-------------------------------------
817bool Type::has_memory() const {
818  Type::TYPES tx = base();
819  if (tx == Memory) return true;
820  if (tx == Tuple) {
821    const TypeTuple *t = is_tuple();
822    for (uint i=0; i < t->cnt(); i++) {
823      tx = t->field_at(i)->base();
824      if (tx == Memory)  return true;
825    }
826  }
827  return false;
828}
829
830#ifndef PRODUCT
831//------------------------------dump2------------------------------------------
832void Type::dump2( Dict &d, uint depth, outputStream *st ) const {
833  st->print(_type_info[_base].msg);
834}
835
836//------------------------------dump-------------------------------------------
837void Type::dump_on(outputStream *st) const {
838  ResourceMark rm;
839  Dict d(cmpkey,hashkey);       // Stop recursive type dumping
840  dump2(d,1, st);
841  if (is_ptr_to_narrowoop()) {
842    st->print(" [narrow]");
843  } else if (is_ptr_to_narrowklass()) {
844    st->print(" [narrowklass]");
845  }
846}
847#endif
848
849//------------------------------singleton--------------------------------------
850// TRUE if Type is a singleton type, FALSE otherwise.   Singletons are simple
851// constants (Ldi nodes).  Singletons are integer, float or double constants.
852bool Type::singleton(void) const {
853  return _base == Top || _base == Half;
854}
855
856//------------------------------empty------------------------------------------
857// TRUE if Type is a type with no values, FALSE otherwise.
858bool Type::empty(void) const {
859  switch (_base) {
860  case DoubleTop:
861  case FloatTop:
862  case Top:
863    return true;
864
865  case Half:
866  case Abio:
867  case Return_Address:
868  case Memory:
869  case Bottom:
870  case FloatBot:
871  case DoubleBot:
872    return false;  // never a singleton, therefore never empty
873  }
874
875  ShouldNotReachHere();
876  return false;
877}
878
879//------------------------------dump_stats-------------------------------------
880// Dump collected statistics to stderr
881#ifndef PRODUCT
882void Type::dump_stats() {
883  tty->print("Types made: %d\n", type_dict()->Size());
884}
885#endif
886
887//------------------------------typerr-----------------------------------------
888void Type::typerr( const Type *t ) const {
889#ifndef PRODUCT
890  tty->print("\nError mixing types: ");
891  dump();
892  tty->print(" and ");
893  t->dump();
894  tty->print("\n");
895#endif
896  ShouldNotReachHere();
897}
898
899
900//=============================================================================
901// Convenience common pre-built types.
902const TypeF *TypeF::ZERO;       // Floating point zero
903const TypeF *TypeF::ONE;        // Floating point one
904
905//------------------------------make-------------------------------------------
906// Create a float constant
907const TypeF *TypeF::make(float f) {
908  return (TypeF*)(new TypeF(f))->hashcons();
909}
910
911//------------------------------meet-------------------------------------------
912// Compute the MEET of two types.  It returns a new Type object.
913const Type *TypeF::xmeet( const Type *t ) const {
914  // Perform a fast test for common case; meeting the same types together.
915  if( this == t ) return this;  // Meeting same type-rep?
916
917  // Current "this->_base" is FloatCon
918  switch (t->base()) {          // Switch on original type
919  case AnyPtr:                  // Mixing with oops happens when javac
920  case RawPtr:                  // reuses local variables
921  case OopPtr:
922  case InstPtr:
923  case AryPtr:
924  case MetadataPtr:
925  case KlassPtr:
926  case NarrowOop:
927  case NarrowKlass:
928  case Int:
929  case Long:
930  case DoubleTop:
931  case DoubleCon:
932  case DoubleBot:
933  case Bottom:                  // Ye Olde Default
934    return Type::BOTTOM;
935
936  case FloatBot:
937    return t;
938
939  default:                      // All else is a mistake
940    typerr(t);
941
942  case FloatCon:                // Float-constant vs Float-constant?
943    if( jint_cast(_f) != jint_cast(t->getf()) )         // unequal constants?
944                                // must compare bitwise as positive zero, negative zero and NaN have
945                                // all the same representation in C++
946      return FLOAT;             // Return generic float
947                                // Equal constants
948  case Top:
949  case FloatTop:
950    break;                      // Return the float constant
951  }
952  return this;                  // Return the float constant
953}
954
955//------------------------------xdual------------------------------------------
956// Dual: symmetric
957const Type *TypeF::xdual() const {
958  return this;
959}
960
961//------------------------------eq---------------------------------------------
962// Structural equality check for Type representations
963bool TypeF::eq( const Type *t ) const {
964  if( g_isnan(_f) ||
965      g_isnan(t->getf()) ) {
966    // One or both are NANs.  If both are NANs return true, else false.
967    return (g_isnan(_f) && g_isnan(t->getf()));
968  }
969  if (_f == t->getf()) {
970    // (NaN is impossible at this point, since it is not equal even to itself)
971    if (_f == 0.0) {
972      // difference between positive and negative zero
973      if (jint_cast(_f) != jint_cast(t->getf()))  return false;
974    }
975    return true;
976  }
977  return false;
978}
979
980//------------------------------hash-------------------------------------------
981// Type-specific hashing function.
982int TypeF::hash(void) const {
983  return *(int*)(&_f);
984}
985
986//------------------------------is_finite--------------------------------------
987// Has a finite value
988bool TypeF::is_finite() const {
989  return g_isfinite(getf()) != 0;
990}
991
992//------------------------------is_nan-----------------------------------------
993// Is not a number (NaN)
994bool TypeF::is_nan()    const {
995  return g_isnan(getf()) != 0;
996}
997
998//------------------------------dump2------------------------------------------
999// Dump float constant Type
1000#ifndef PRODUCT
1001void TypeF::dump2( Dict &d, uint depth, outputStream *st ) const {
1002  Type::dump2(d,depth, st);
1003  st->print("%f", _f);
1004}
1005#endif
1006
1007//------------------------------singleton--------------------------------------
1008// TRUE if Type is a singleton type, FALSE otherwise.   Singletons are simple
1009// constants (Ldi nodes).  Singletons are integer, float or double constants
1010// or a single symbol.
1011bool TypeF::singleton(void) const {
1012  return true;                  // Always a singleton
1013}
1014
1015bool TypeF::empty(void) const {
1016  return false;                 // always exactly a singleton
1017}
1018
1019//=============================================================================
1020// Convenience common pre-built types.
1021const TypeD *TypeD::ZERO;       // Floating point zero
1022const TypeD *TypeD::ONE;        // Floating point one
1023
1024//------------------------------make-------------------------------------------
1025const TypeD *TypeD::make(double d) {
1026  return (TypeD*)(new TypeD(d))->hashcons();
1027}
1028
1029//------------------------------meet-------------------------------------------
1030// Compute the MEET of two types.  It returns a new Type object.
1031const Type *TypeD::xmeet( const Type *t ) const {
1032  // Perform a fast test for common case; meeting the same types together.
1033  if( this == t ) return this;  // Meeting same type-rep?
1034
1035  // Current "this->_base" is DoubleCon
1036  switch (t->base()) {          // Switch on original type
1037  case AnyPtr:                  // Mixing with oops happens when javac
1038  case RawPtr:                  // reuses local variables
1039  case OopPtr:
1040  case InstPtr:
1041  case AryPtr:
1042  case MetadataPtr:
1043  case KlassPtr:
1044  case NarrowOop:
1045  case NarrowKlass:
1046  case Int:
1047  case Long:
1048  case FloatTop:
1049  case FloatCon:
1050  case FloatBot:
1051  case Bottom:                  // Ye Olde Default
1052    return Type::BOTTOM;
1053
1054  case DoubleBot:
1055    return t;
1056
1057  default:                      // All else is a mistake
1058    typerr(t);
1059
1060  case DoubleCon:               // Double-constant vs Double-constant?
1061    if( jlong_cast(_d) != jlong_cast(t->getd()) )       // unequal constants? (see comment in TypeF::xmeet)
1062      return DOUBLE;            // Return generic double
1063  case Top:
1064  case DoubleTop:
1065    break;
1066  }
1067  return this;                  // Return the double constant
1068}
1069
1070//------------------------------xdual------------------------------------------
1071// Dual: symmetric
1072const Type *TypeD::xdual() const {
1073  return this;
1074}
1075
1076//------------------------------eq---------------------------------------------
1077// Structural equality check for Type representations
1078bool TypeD::eq( const Type *t ) const {
1079  if( g_isnan(_d) ||
1080      g_isnan(t->getd()) ) {
1081    // One or both are NANs.  If both are NANs return true, else false.
1082    return (g_isnan(_d) && g_isnan(t->getd()));
1083  }
1084  if (_d == t->getd()) {
1085    // (NaN is impossible at this point, since it is not equal even to itself)
1086    if (_d == 0.0) {
1087      // difference between positive and negative zero
1088      if (jlong_cast(_d) != jlong_cast(t->getd()))  return false;
1089    }
1090    return true;
1091  }
1092  return false;
1093}
1094
1095//------------------------------hash-------------------------------------------
1096// Type-specific hashing function.
1097int TypeD::hash(void) const {
1098  return *(int*)(&_d);
1099}
1100
1101//------------------------------is_finite--------------------------------------
1102// Has a finite value
1103bool TypeD::is_finite() const {
1104  return g_isfinite(getd()) != 0;
1105}
1106
1107//------------------------------is_nan-----------------------------------------
1108// Is not a number (NaN)
1109bool TypeD::is_nan()    const {
1110  return g_isnan(getd()) != 0;
1111}
1112
1113//------------------------------dump2------------------------------------------
1114// Dump double constant Type
1115#ifndef PRODUCT
1116void TypeD::dump2( Dict &d, uint depth, outputStream *st ) const {
1117  Type::dump2(d,depth,st);
1118  st->print("%f", _d);
1119}
1120#endif
1121
1122//------------------------------singleton--------------------------------------
1123// TRUE if Type is a singleton type, FALSE otherwise.   Singletons are simple
1124// constants (Ldi nodes).  Singletons are integer, float or double constants
1125// or a single symbol.
1126bool TypeD::singleton(void) const {
1127  return true;                  // Always a singleton
1128}
1129
1130bool TypeD::empty(void) const {
1131  return false;                 // always exactly a singleton
1132}
1133
1134//=============================================================================
1135// Convience common pre-built types.
1136const TypeInt *TypeInt::MINUS_1;// -1
1137const TypeInt *TypeInt::ZERO;   // 0
1138const TypeInt *TypeInt::ONE;    // 1
1139const TypeInt *TypeInt::BOOL;   // 0 or 1, FALSE or TRUE.
1140const TypeInt *TypeInt::CC;     // -1,0 or 1, condition codes
1141const TypeInt *TypeInt::CC_LT;  // [-1]  == MINUS_1
1142const TypeInt *TypeInt::CC_GT;  // [1]   == ONE
1143const TypeInt *TypeInt::CC_EQ;  // [0]   == ZERO
1144const TypeInt *TypeInt::CC_LE;  // [-1,0]
1145const TypeInt *TypeInt::CC_GE;  // [0,1] == BOOL (!)
1146const TypeInt *TypeInt::BYTE;   // Bytes, -128 to 127
1147const TypeInt *TypeInt::UBYTE;  // Unsigned Bytes, 0 to 255
1148const TypeInt *TypeInt::CHAR;   // Java chars, 0-65535
1149const TypeInt *TypeInt::SHORT;  // Java shorts, -32768-32767
1150const TypeInt *TypeInt::POS;    // Positive 32-bit integers or zero
1151const TypeInt *TypeInt::POS1;   // Positive 32-bit integers
1152const TypeInt *TypeInt::INT;    // 32-bit integers
1153const TypeInt *TypeInt::SYMINT; // symmetric range [-max_jint..max_jint]
1154
1155//------------------------------TypeInt----------------------------------------
1156TypeInt::TypeInt( jint lo, jint hi, int w ) : Type(Int), _lo(lo), _hi(hi), _widen(w) {
1157}
1158
1159//------------------------------make-------------------------------------------
1160const TypeInt *TypeInt::make( jint lo ) {
1161  return (TypeInt*)(new TypeInt(lo,lo,WidenMin))->hashcons();
1162}
1163
1164static int normalize_int_widen( jint lo, jint hi, int w ) {
1165  // Certain normalizations keep us sane when comparing types.
1166  // The 'SMALLINT' covers constants and also CC and its relatives.
1167  if (lo <= hi) {
1168    if ((juint)(hi - lo) <= SMALLINT)  w = Type::WidenMin;
1169    if ((juint)(hi - lo) >= max_juint) w = Type::WidenMax; // TypeInt::INT
1170  } else {
1171    if ((juint)(lo - hi) <= SMALLINT)  w = Type::WidenMin;
1172    if ((juint)(lo - hi) >= max_juint) w = Type::WidenMin; // dual TypeInt::INT
1173  }
1174  return w;
1175}
1176
1177const TypeInt *TypeInt::make( jint lo, jint hi, int w ) {
1178  w = normalize_int_widen(lo, hi, w);
1179  return (TypeInt*)(new TypeInt(lo,hi,w))->hashcons();
1180}
1181
1182//------------------------------meet-------------------------------------------
1183// Compute the MEET of two types.  It returns a new Type representation object
1184// with reference count equal to the number of Types pointing at it.
1185// Caller should wrap a Types around it.
1186const Type *TypeInt::xmeet( const Type *t ) const {
1187  // Perform a fast test for common case; meeting the same types together.
1188  if( this == t ) return this;  // Meeting same type?
1189
1190  // Currently "this->_base" is a TypeInt
1191  switch (t->base()) {          // Switch on original type
1192  case AnyPtr:                  // Mixing with oops happens when javac
1193  case RawPtr:                  // reuses local variables
1194  case OopPtr:
1195  case InstPtr:
1196  case AryPtr:
1197  case MetadataPtr:
1198  case KlassPtr:
1199  case NarrowOop:
1200  case NarrowKlass:
1201  case Long:
1202  case FloatTop:
1203  case FloatCon:
1204  case FloatBot:
1205  case DoubleTop:
1206  case DoubleCon:
1207  case DoubleBot:
1208  case Bottom:                  // Ye Olde Default
1209    return Type::BOTTOM;
1210  default:                      // All else is a mistake
1211    typerr(t);
1212  case Top:                     // No change
1213    return this;
1214  case Int:                     // Int vs Int?
1215    break;
1216  }
1217
1218  // Expand covered set
1219  const TypeInt *r = t->is_int();
1220  return make( MIN2(_lo,r->_lo), MAX2(_hi,r->_hi), MAX2(_widen,r->_widen) );
1221}
1222
1223//------------------------------xdual------------------------------------------
1224// Dual: reverse hi & lo; flip widen
1225const Type *TypeInt::xdual() const {
1226  int w = normalize_int_widen(_hi,_lo, WidenMax-_widen);
1227  return new TypeInt(_hi,_lo,w);
1228}
1229
1230//------------------------------widen------------------------------------------
1231// Only happens for optimistic top-down optimizations.
1232const Type *TypeInt::widen( const Type *old, const Type* limit ) const {
1233  // Coming from TOP or such; no widening
1234  if( old->base() != Int ) return this;
1235  const TypeInt *ot = old->is_int();
1236
1237  // If new guy is equal to old guy, no widening
1238  if( _lo == ot->_lo && _hi == ot->_hi )
1239    return old;
1240
1241  // If new guy contains old, then we widened
1242  if( _lo <= ot->_lo && _hi >= ot->_hi ) {
1243    // New contains old
1244    // If new guy is already wider than old, no widening
1245    if( _widen > ot->_widen ) return this;
1246    // If old guy was a constant, do not bother
1247    if (ot->_lo == ot->_hi)  return this;
1248    // Now widen new guy.
1249    // Check for widening too far
1250    if (_widen == WidenMax) {
1251      int max = max_jint;
1252      int min = min_jint;
1253      if (limit->isa_int()) {
1254        max = limit->is_int()->_hi;
1255        min = limit->is_int()->_lo;
1256      }
1257      if (min < _lo && _hi < max) {
1258        // If neither endpoint is extremal yet, push out the endpoint
1259        // which is closer to its respective limit.
1260        if (_lo >= 0 ||                 // easy common case
1261            (juint)(_lo - min) >= (juint)(max - _hi)) {
1262          // Try to widen to an unsigned range type of 31 bits:
1263          return make(_lo, max, WidenMax);
1264        } else {
1265          return make(min, _hi, WidenMax);
1266        }
1267      }
1268      return TypeInt::INT;
1269    }
1270    // Returned widened new guy
1271    return make(_lo,_hi,_widen+1);
1272  }
1273
1274  // If old guy contains new, then we probably widened too far & dropped to
1275  // bottom.  Return the wider fellow.
1276  if ( ot->_lo <= _lo && ot->_hi >= _hi )
1277    return old;
1278
1279  //fatal("Integer value range is not subset");
1280  //return this;
1281  return TypeInt::INT;
1282}
1283
1284//------------------------------narrow---------------------------------------
1285// Only happens for pessimistic optimizations.
1286const Type *TypeInt::narrow( const Type *old ) const {
1287  if (_lo >= _hi)  return this;   // already narrow enough
1288  if (old == NULL)  return this;
1289  const TypeInt* ot = old->isa_int();
1290  if (ot == NULL)  return this;
1291  jint olo = ot->_lo;
1292  jint ohi = ot->_hi;
1293
1294  // If new guy is equal to old guy, no narrowing
1295  if (_lo == olo && _hi == ohi)  return old;
1296
1297  // If old guy was maximum range, allow the narrowing
1298  if (olo == min_jint && ohi == max_jint)  return this;
1299
1300  if (_lo < olo || _hi > ohi)
1301    return this;                // doesn't narrow; pretty wierd
1302
1303  // The new type narrows the old type, so look for a "death march".
1304  // See comments on PhaseTransform::saturate.
1305  juint nrange = _hi - _lo;
1306  juint orange = ohi - olo;
1307  if (nrange < max_juint - 1 && nrange > (orange >> 1) + (SMALLINT*2)) {
1308    // Use the new type only if the range shrinks a lot.
1309    // We do not want the optimizer computing 2^31 point by point.
1310    return old;
1311  }
1312
1313  return this;
1314}
1315
1316//-----------------------------filter------------------------------------------
1317const Type *TypeInt::filter( const Type *kills ) const {
1318  const TypeInt* ft = join(kills)->isa_int();
1319  if (ft == NULL || ft->empty())
1320    return Type::TOP;           // Canonical empty value
1321  if (ft->_widen < this->_widen) {
1322    // Do not allow the value of kill->_widen to affect the outcome.
1323    // The widen bits must be allowed to run freely through the graph.
1324    ft = TypeInt::make(ft->_lo, ft->_hi, this->_widen);
1325  }
1326  return ft;
1327}
1328
1329//------------------------------eq---------------------------------------------
1330// Structural equality check for Type representations
1331bool TypeInt::eq( const Type *t ) const {
1332  const TypeInt *r = t->is_int(); // Handy access
1333  return r->_lo == _lo && r->_hi == _hi && r->_widen == _widen;
1334}
1335
1336//------------------------------hash-------------------------------------------
1337// Type-specific hashing function.
1338int TypeInt::hash(void) const {
1339  return _lo+_hi+_widen+(int)Type::Int;
1340}
1341
1342//------------------------------is_finite--------------------------------------
1343// Has a finite value
1344bool TypeInt::is_finite() const {
1345  return true;
1346}
1347
1348//------------------------------dump2------------------------------------------
1349// Dump TypeInt
1350#ifndef PRODUCT
1351static const char* intname(char* buf, jint n) {
1352  if (n == min_jint)
1353    return "min";
1354  else if (n < min_jint + 10000)
1355    sprintf(buf, "min+" INT32_FORMAT, n - min_jint);
1356  else if (n == max_jint)
1357    return "max";
1358  else if (n > max_jint - 10000)
1359    sprintf(buf, "max-" INT32_FORMAT, max_jint - n);
1360  else
1361    sprintf(buf, INT32_FORMAT, n);
1362  return buf;
1363}
1364
1365void TypeInt::dump2( Dict &d, uint depth, outputStream *st ) const {
1366  char buf[40], buf2[40];
1367  if (_lo == min_jint && _hi == max_jint)
1368    st->print("int");
1369  else if (is_con())
1370    st->print("int:%s", intname(buf, get_con()));
1371  else if (_lo == BOOL->_lo && _hi == BOOL->_hi)
1372    st->print("bool");
1373  else if (_lo == BYTE->_lo && _hi == BYTE->_hi)
1374    st->print("byte");
1375  else if (_lo == CHAR->_lo && _hi == CHAR->_hi)
1376    st->print("char");
1377  else if (_lo == SHORT->_lo && _hi == SHORT->_hi)
1378    st->print("short");
1379  else if (_hi == max_jint)
1380    st->print("int:>=%s", intname(buf, _lo));
1381  else if (_lo == min_jint)
1382    st->print("int:<=%s", intname(buf, _hi));
1383  else
1384    st->print("int:%s..%s", intname(buf, _lo), intname(buf2, _hi));
1385
1386  if (_widen != 0 && this != TypeInt::INT)
1387    st->print(":%.*s", _widen, "wwww");
1388}
1389#endif
1390
1391//------------------------------singleton--------------------------------------
1392// TRUE if Type is a singleton type, FALSE otherwise.   Singletons are simple
1393// constants.
1394bool TypeInt::singleton(void) const {
1395  return _lo >= _hi;
1396}
1397
1398bool TypeInt::empty(void) const {
1399  return _lo > _hi;
1400}
1401
1402//=============================================================================
1403// Convenience common pre-built types.
1404const TypeLong *TypeLong::MINUS_1;// -1
1405const TypeLong *TypeLong::ZERO; // 0
1406const TypeLong *TypeLong::ONE;  // 1
1407const TypeLong *TypeLong::POS;  // >=0
1408const TypeLong *TypeLong::LONG; // 64-bit integers
1409const TypeLong *TypeLong::INT;  // 32-bit subrange
1410const TypeLong *TypeLong::UINT; // 32-bit unsigned subrange
1411
1412//------------------------------TypeLong---------------------------------------
1413TypeLong::TypeLong( jlong lo, jlong hi, int w ) : Type(Long), _lo(lo), _hi(hi), _widen(w) {
1414}
1415
1416//------------------------------make-------------------------------------------
1417const TypeLong *TypeLong::make( jlong lo ) {
1418  return (TypeLong*)(new TypeLong(lo,lo,WidenMin))->hashcons();
1419}
1420
1421static int normalize_long_widen( jlong lo, jlong hi, int w ) {
1422  // Certain normalizations keep us sane when comparing types.
1423  // The 'SMALLINT' covers constants.
1424  if (lo <= hi) {
1425    if ((julong)(hi - lo) <= SMALLINT)   w = Type::WidenMin;
1426    if ((julong)(hi - lo) >= max_julong) w = Type::WidenMax; // TypeLong::LONG
1427  } else {
1428    if ((julong)(lo - hi) <= SMALLINT)   w = Type::WidenMin;
1429    if ((julong)(lo - hi) >= max_julong) w = Type::WidenMin; // dual TypeLong::LONG
1430  }
1431  return w;
1432}
1433
1434const TypeLong *TypeLong::make( jlong lo, jlong hi, int w ) {
1435  w = normalize_long_widen(lo, hi, w);
1436  return (TypeLong*)(new TypeLong(lo,hi,w))->hashcons();
1437}
1438
1439
1440//------------------------------meet-------------------------------------------
1441// Compute the MEET of two types.  It returns a new Type representation object
1442// with reference count equal to the number of Types pointing at it.
1443// Caller should wrap a Types around it.
1444const Type *TypeLong::xmeet( const Type *t ) const {
1445  // Perform a fast test for common case; meeting the same types together.
1446  if( this == t ) return this;  // Meeting same type?
1447
1448  // Currently "this->_base" is a TypeLong
1449  switch (t->base()) {          // Switch on original type
1450  case AnyPtr:                  // Mixing with oops happens when javac
1451  case RawPtr:                  // reuses local variables
1452  case OopPtr:
1453  case InstPtr:
1454  case AryPtr:
1455  case MetadataPtr:
1456  case KlassPtr:
1457  case NarrowOop:
1458  case NarrowKlass:
1459  case Int:
1460  case FloatTop:
1461  case FloatCon:
1462  case FloatBot:
1463  case DoubleTop:
1464  case DoubleCon:
1465  case DoubleBot:
1466  case Bottom:                  // Ye Olde Default
1467    return Type::BOTTOM;
1468  default:                      // All else is a mistake
1469    typerr(t);
1470  case Top:                     // No change
1471    return this;
1472  case Long:                    // Long vs Long?
1473    break;
1474  }
1475
1476  // Expand covered set
1477  const TypeLong *r = t->is_long(); // Turn into a TypeLong
1478  return make( MIN2(_lo,r->_lo), MAX2(_hi,r->_hi), MAX2(_widen,r->_widen) );
1479}
1480
1481//------------------------------xdual------------------------------------------
1482// Dual: reverse hi & lo; flip widen
1483const Type *TypeLong::xdual() const {
1484  int w = normalize_long_widen(_hi,_lo, WidenMax-_widen);
1485  return new TypeLong(_hi,_lo,w);
1486}
1487
1488//------------------------------widen------------------------------------------
1489// Only happens for optimistic top-down optimizations.
1490const Type *TypeLong::widen( const Type *old, const Type* limit ) const {
1491  // Coming from TOP or such; no widening
1492  if( old->base() != Long ) return this;
1493  const TypeLong *ot = old->is_long();
1494
1495  // If new guy is equal to old guy, no widening
1496  if( _lo == ot->_lo && _hi == ot->_hi )
1497    return old;
1498
1499  // If new guy contains old, then we widened
1500  if( _lo <= ot->_lo && _hi >= ot->_hi ) {
1501    // New contains old
1502    // If new guy is already wider than old, no widening
1503    if( _widen > ot->_widen ) return this;
1504    // If old guy was a constant, do not bother
1505    if (ot->_lo == ot->_hi)  return this;
1506    // Now widen new guy.
1507    // Check for widening too far
1508    if (_widen == WidenMax) {
1509      jlong max = max_jlong;
1510      jlong min = min_jlong;
1511      if (limit->isa_long()) {
1512        max = limit->is_long()->_hi;
1513        min = limit->is_long()->_lo;
1514      }
1515      if (min < _lo && _hi < max) {
1516        // If neither endpoint is extremal yet, push out the endpoint
1517        // which is closer to its respective limit.
1518        if (_lo >= 0 ||                 // easy common case
1519            (julong)(_lo - min) >= (julong)(max - _hi)) {
1520          // Try to widen to an unsigned range type of 32/63 bits:
1521          if (max >= max_juint && _hi < max_juint)
1522            return make(_lo, max_juint, WidenMax);
1523          else
1524            return make(_lo, max, WidenMax);
1525        } else {
1526          return make(min, _hi, WidenMax);
1527        }
1528      }
1529      return TypeLong::LONG;
1530    }
1531    // Returned widened new guy
1532    return make(_lo,_hi,_widen+1);
1533  }
1534
1535  // If old guy contains new, then we probably widened too far & dropped to
1536  // bottom.  Return the wider fellow.
1537  if ( ot->_lo <= _lo && ot->_hi >= _hi )
1538    return old;
1539
1540  //  fatal("Long value range is not subset");
1541  // return this;
1542  return TypeLong::LONG;
1543}
1544
1545//------------------------------narrow----------------------------------------
1546// Only happens for pessimistic optimizations.
1547const Type *TypeLong::narrow( const Type *old ) const {
1548  if (_lo >= _hi)  return this;   // already narrow enough
1549  if (old == NULL)  return this;
1550  const TypeLong* ot = old->isa_long();
1551  if (ot == NULL)  return this;
1552  jlong olo = ot->_lo;
1553  jlong ohi = ot->_hi;
1554
1555  // If new guy is equal to old guy, no narrowing
1556  if (_lo == olo && _hi == ohi)  return old;
1557
1558  // If old guy was maximum range, allow the narrowing
1559  if (olo == min_jlong && ohi == max_jlong)  return this;
1560
1561  if (_lo < olo || _hi > ohi)
1562    return this;                // doesn't narrow; pretty wierd
1563
1564  // The new type narrows the old type, so look for a "death march".
1565  // See comments on PhaseTransform::saturate.
1566  julong nrange = _hi - _lo;
1567  julong orange = ohi - olo;
1568  if (nrange < max_julong - 1 && nrange > (orange >> 1) + (SMALLINT*2)) {
1569    // Use the new type only if the range shrinks a lot.
1570    // We do not want the optimizer computing 2^31 point by point.
1571    return old;
1572  }
1573
1574  return this;
1575}
1576
1577//-----------------------------filter------------------------------------------
1578const Type *TypeLong::filter( const Type *kills ) const {
1579  const TypeLong* ft = join(kills)->isa_long();
1580  if (ft == NULL || ft->empty())
1581    return Type::TOP;           // Canonical empty value
1582  if (ft->_widen < this->_widen) {
1583    // Do not allow the value of kill->_widen to affect the outcome.
1584    // The widen bits must be allowed to run freely through the graph.
1585    ft = TypeLong::make(ft->_lo, ft->_hi, this->_widen);
1586  }
1587  return ft;
1588}
1589
1590//------------------------------eq---------------------------------------------
1591// Structural equality check for Type representations
1592bool TypeLong::eq( const Type *t ) const {
1593  const TypeLong *r = t->is_long(); // Handy access
1594  return r->_lo == _lo &&  r->_hi == _hi  && r->_widen == _widen;
1595}
1596
1597//------------------------------hash-------------------------------------------
1598// Type-specific hashing function.
1599int TypeLong::hash(void) const {
1600  return (int)(_lo+_hi+_widen+(int)Type::Long);
1601}
1602
1603//------------------------------is_finite--------------------------------------
1604// Has a finite value
1605bool TypeLong::is_finite() const {
1606  return true;
1607}
1608
1609//------------------------------dump2------------------------------------------
1610// Dump TypeLong
1611#ifndef PRODUCT
1612static const char* longnamenear(jlong x, const char* xname, char* buf, jlong n) {
1613  if (n > x) {
1614    if (n >= x + 10000)  return NULL;
1615    sprintf(buf, "%s+" JLONG_FORMAT, xname, n - x);
1616  } else if (n < x) {
1617    if (n <= x - 10000)  return NULL;
1618    sprintf(buf, "%s-" JLONG_FORMAT, xname, x - n);
1619  } else {
1620    return xname;
1621  }
1622  return buf;
1623}
1624
1625static const char* longname(char* buf, jlong n) {
1626  const char* str;
1627  if (n == min_jlong)
1628    return "min";
1629  else if (n < min_jlong + 10000)
1630    sprintf(buf, "min+" JLONG_FORMAT, n - min_jlong);
1631  else if (n == max_jlong)
1632    return "max";
1633  else if (n > max_jlong - 10000)
1634    sprintf(buf, "max-" JLONG_FORMAT, max_jlong - n);
1635  else if ((str = longnamenear(max_juint, "maxuint", buf, n)) != NULL)
1636    return str;
1637  else if ((str = longnamenear(max_jint, "maxint", buf, n)) != NULL)
1638    return str;
1639  else if ((str = longnamenear(min_jint, "minint", buf, n)) != NULL)
1640    return str;
1641  else
1642    sprintf(buf, JLONG_FORMAT, n);
1643  return buf;
1644}
1645
1646void TypeLong::dump2( Dict &d, uint depth, outputStream *st ) const {
1647  char buf[80], buf2[80];
1648  if (_lo == min_jlong && _hi == max_jlong)
1649    st->print("long");
1650  else if (is_con())
1651    st->print("long:%s", longname(buf, get_con()));
1652  else if (_hi == max_jlong)
1653    st->print("long:>=%s", longname(buf, _lo));
1654  else if (_lo == min_jlong)
1655    st->print("long:<=%s", longname(buf, _hi));
1656  else
1657    st->print("long:%s..%s", longname(buf, _lo), longname(buf2, _hi));
1658
1659  if (_widen != 0 && this != TypeLong::LONG)
1660    st->print(":%.*s", _widen, "wwww");
1661}
1662#endif
1663
1664//------------------------------singleton--------------------------------------
1665// TRUE if Type is a singleton type, FALSE otherwise.   Singletons are simple
1666// constants
1667bool TypeLong::singleton(void) const {
1668  return _lo >= _hi;
1669}
1670
1671bool TypeLong::empty(void) const {
1672  return _lo > _hi;
1673}
1674
1675//=============================================================================
1676// Convenience common pre-built types.
1677const TypeTuple *TypeTuple::IFBOTH;     // Return both arms of IF as reachable
1678const TypeTuple *TypeTuple::IFFALSE;
1679const TypeTuple *TypeTuple::IFTRUE;
1680const TypeTuple *TypeTuple::IFNEITHER;
1681const TypeTuple *TypeTuple::LOOPBODY;
1682const TypeTuple *TypeTuple::MEMBAR;
1683const TypeTuple *TypeTuple::STORECONDITIONAL;
1684const TypeTuple *TypeTuple::START_I2C;
1685const TypeTuple *TypeTuple::INT_PAIR;
1686const TypeTuple *TypeTuple::LONG_PAIR;
1687const TypeTuple *TypeTuple::INT_CC_PAIR;
1688const TypeTuple *TypeTuple::LONG_CC_PAIR;
1689
1690
1691//------------------------------make-------------------------------------------
1692// Make a TypeTuple from the range of a method signature
1693const TypeTuple *TypeTuple::make_range(ciSignature* sig) {
1694  ciType* return_type = sig->return_type();
1695  uint total_fields = TypeFunc::Parms + return_type->size();
1696  const Type **field_array = fields(total_fields);
1697  switch (return_type->basic_type()) {
1698  case T_LONG:
1699    field_array[TypeFunc::Parms]   = TypeLong::LONG;
1700    field_array[TypeFunc::Parms+1] = Type::HALF;
1701    break;
1702  case T_DOUBLE:
1703    field_array[TypeFunc::Parms]   = Type::DOUBLE;
1704    field_array[TypeFunc::Parms+1] = Type::HALF;
1705    break;
1706  case T_OBJECT:
1707  case T_ARRAY:
1708  case T_BOOLEAN:
1709  case T_CHAR:
1710  case T_FLOAT:
1711  case T_BYTE:
1712  case T_SHORT:
1713  case T_INT:
1714    field_array[TypeFunc::Parms] = get_const_type(return_type);
1715    break;
1716  case T_VOID:
1717    break;
1718  default:
1719    ShouldNotReachHere();
1720  }
1721  return (TypeTuple*)(new TypeTuple(total_fields,field_array))->hashcons();
1722}
1723
1724// Make a TypeTuple from the domain of a method signature
1725const TypeTuple *TypeTuple::make_domain(ciInstanceKlass* recv, ciSignature* sig) {
1726  uint total_fields = TypeFunc::Parms + sig->size();
1727
1728  uint pos = TypeFunc::Parms;
1729  const Type **field_array;
1730  if (recv != NULL) {
1731    total_fields++;
1732    field_array = fields(total_fields);
1733    // Use get_const_type here because it respects UseUniqueSubclasses:
1734    field_array[pos++] = get_const_type(recv)->join(TypePtr::NOTNULL);
1735  } else {
1736    field_array = fields(total_fields);
1737  }
1738
1739  int i = 0;
1740  while (pos < total_fields) {
1741    ciType* type = sig->type_at(i);
1742
1743    switch (type->basic_type()) {
1744    case T_LONG:
1745      field_array[pos++] = TypeLong::LONG;
1746      field_array[pos++] = Type::HALF;
1747      break;
1748    case T_DOUBLE:
1749      field_array[pos++] = Type::DOUBLE;
1750      field_array[pos++] = Type::HALF;
1751      break;
1752    case T_OBJECT:
1753    case T_ARRAY:
1754    case T_BOOLEAN:
1755    case T_CHAR:
1756    case T_FLOAT:
1757    case T_BYTE:
1758    case T_SHORT:
1759    case T_INT:
1760      field_array[pos++] = get_const_type(type);
1761      break;
1762    default:
1763      ShouldNotReachHere();
1764    }
1765    i++;
1766  }
1767  return (TypeTuple*)(new TypeTuple(total_fields,field_array))->hashcons();
1768}
1769
1770const TypeTuple *TypeTuple::make( uint cnt, const Type **fields ) {
1771  return (TypeTuple*)(new TypeTuple(cnt,fields))->hashcons();
1772}
1773
1774//------------------------------fields-----------------------------------------
1775// Subroutine call type with space allocated for argument types
1776const Type **TypeTuple::fields( uint arg_cnt ) {
1777  const Type **flds = (const Type **)(Compile::current()->type_arena()->Amalloc_4((TypeFunc::Parms+arg_cnt)*sizeof(Type*) ));
1778  flds[TypeFunc::Control  ] = Type::CONTROL;
1779  flds[TypeFunc::I_O      ] = Type::ABIO;
1780  flds[TypeFunc::Memory   ] = Type::MEMORY;
1781  flds[TypeFunc::FramePtr ] = TypeRawPtr::BOTTOM;
1782  flds[TypeFunc::ReturnAdr] = Type::RETURN_ADDRESS;
1783
1784  return flds;
1785}
1786
1787//------------------------------meet-------------------------------------------
1788// Compute the MEET of two types.  It returns a new Type object.
1789const Type *TypeTuple::xmeet( const Type *t ) const {
1790  // Perform a fast test for common case; meeting the same types together.
1791  if( this == t ) return this;  // Meeting same type-rep?
1792
1793  // Current "this->_base" is Tuple
1794  switch (t->base()) {          // switch on original type
1795
1796  case Bottom:                  // Ye Olde Default
1797    return t;
1798
1799  default:                      // All else is a mistake
1800    typerr(t);
1801
1802  case Tuple: {                 // Meeting 2 signatures?
1803    const TypeTuple *x = t->is_tuple();
1804    assert( _cnt == x->_cnt, "" );
1805    const Type **fields = (const Type **)(Compile::current()->type_arena()->Amalloc_4( _cnt*sizeof(Type*) ));
1806    for( uint i=0; i<_cnt; i++ )
1807      fields[i] = field_at(i)->xmeet( x->field_at(i) );
1808    return TypeTuple::make(_cnt,fields);
1809  }
1810  case Top:
1811    break;
1812  }
1813  return this;                  // Return the double constant
1814}
1815
1816//------------------------------xdual------------------------------------------
1817// Dual: compute field-by-field dual
1818const Type *TypeTuple::xdual() const {
1819  const Type **fields = (const Type **)(Compile::current()->type_arena()->Amalloc_4( _cnt*sizeof(Type*) ));
1820  for( uint i=0; i<_cnt; i++ )
1821    fields[i] = _fields[i]->dual();
1822  return new TypeTuple(_cnt,fields);
1823}
1824
1825//------------------------------eq---------------------------------------------
1826// Structural equality check for Type representations
1827bool TypeTuple::eq( const Type *t ) const {
1828  const TypeTuple *s = (const TypeTuple *)t;
1829  if (_cnt != s->_cnt)  return false;  // Unequal field counts
1830  for (uint i = 0; i < _cnt; i++)
1831    if (field_at(i) != s->field_at(i)) // POINTER COMPARE!  NO RECURSION!
1832      return false;             // Missed
1833  return true;
1834}
1835
1836//------------------------------hash-------------------------------------------
1837// Type-specific hashing function.
1838int TypeTuple::hash(void) const {
1839  intptr_t sum = _cnt;
1840  for( uint i=0; i<_cnt; i++ )
1841    sum += (intptr_t)_fields[i];     // Hash on pointers directly
1842  return sum;
1843}
1844
1845//------------------------------dump2------------------------------------------
1846// Dump signature Type
1847#ifndef PRODUCT
1848void TypeTuple::dump2( Dict &d, uint depth, outputStream *st ) const {
1849  st->print("{");
1850  if( !depth || d[this] ) {     // Check for recursive print
1851    st->print("...}");
1852    return;
1853  }
1854  d.Insert((void*)this, (void*)this);   // Stop recursion
1855  if( _cnt ) {
1856    uint i;
1857    for( i=0; i<_cnt-1; i++ ) {
1858      st->print("%d:", i);
1859      _fields[i]->dump2(d, depth-1, st);
1860      st->print(", ");
1861    }
1862    st->print("%d:", i);
1863    _fields[i]->dump2(d, depth-1, st);
1864  }
1865  st->print("}");
1866}
1867#endif
1868
1869//------------------------------singleton--------------------------------------
1870// TRUE if Type is a singleton type, FALSE otherwise.   Singletons are simple
1871// constants (Ldi nodes).  Singletons are integer, float or double constants
1872// or a single symbol.
1873bool TypeTuple::singleton(void) const {
1874  return false;                 // Never a singleton
1875}
1876
1877bool TypeTuple::empty(void) const {
1878  for( uint i=0; i<_cnt; i++ ) {
1879    if (_fields[i]->empty())  return true;
1880  }
1881  return false;
1882}
1883
1884//=============================================================================
1885// Convenience common pre-built types.
1886
1887inline const TypeInt* normalize_array_size(const TypeInt* size) {
1888  // Certain normalizations keep us sane when comparing types.
1889  // We do not want arrayOop variables to differ only by the wideness
1890  // of their index types.  Pick minimum wideness, since that is the
1891  // forced wideness of small ranges anyway.
1892  if (size->_widen != Type::WidenMin)
1893    return TypeInt::make(size->_lo, size->_hi, Type::WidenMin);
1894  else
1895    return size;
1896}
1897
1898//------------------------------make-------------------------------------------
1899const TypeAry* TypeAry::make(const Type* elem, const TypeInt* size, bool stable) {
1900  if (UseCompressedOops && elem->isa_oopptr()) {
1901    elem = elem->make_narrowoop();
1902  }
1903  size = normalize_array_size(size);
1904  return (TypeAry*)(new TypeAry(elem,size,stable))->hashcons();
1905}
1906
1907//------------------------------meet-------------------------------------------
1908// Compute the MEET of two types.  It returns a new Type object.
1909const Type *TypeAry::xmeet( const Type *t ) const {
1910  // Perform a fast test for common case; meeting the same types together.
1911  if( this == t ) return this;  // Meeting same type-rep?
1912
1913  // Current "this->_base" is Ary
1914  switch (t->base()) {          // switch on original type
1915
1916  case Bottom:                  // Ye Olde Default
1917    return t;
1918
1919  default:                      // All else is a mistake
1920    typerr(t);
1921
1922  case Array: {                 // Meeting 2 arrays?
1923    const TypeAry *a = t->is_ary();
1924    return TypeAry::make(_elem->meet(a->_elem),
1925                         _size->xmeet(a->_size)->is_int(),
1926                         _stable & a->_stable);
1927  }
1928  case Top:
1929    break;
1930  }
1931  return this;                  // Return the double constant
1932}
1933
1934//------------------------------xdual------------------------------------------
1935// Dual: compute field-by-field dual
1936const Type *TypeAry::xdual() const {
1937  const TypeInt* size_dual = _size->dual()->is_int();
1938  size_dual = normalize_array_size(size_dual);
1939  return new TypeAry(_elem->dual(), size_dual, !_stable);
1940}
1941
1942//------------------------------eq---------------------------------------------
1943// Structural equality check for Type representations
1944bool TypeAry::eq( const Type *t ) const {
1945  const TypeAry *a = (const TypeAry*)t;
1946  return _elem == a->_elem &&
1947    _stable == a->_stable &&
1948    _size == a->_size;
1949}
1950
1951//------------------------------hash-------------------------------------------
1952// Type-specific hashing function.
1953int TypeAry::hash(void) const {
1954  return (intptr_t)_elem + (intptr_t)_size + (_stable ? 43 : 0);
1955}
1956
1957//----------------------interface_vs_oop---------------------------------------
1958#ifdef ASSERT
1959bool TypeAry::interface_vs_oop(const Type *t) const {
1960  const TypeAry* t_ary = t->is_ary();
1961  if (t_ary) {
1962    return _elem->interface_vs_oop(t_ary->_elem);
1963  }
1964  return false;
1965}
1966#endif
1967
1968//------------------------------dump2------------------------------------------
1969#ifndef PRODUCT
1970void TypeAry::dump2( Dict &d, uint depth, outputStream *st ) const {
1971  if (_stable)  st->print("stable:");
1972  _elem->dump2(d, depth, st);
1973  st->print("[");
1974  _size->dump2(d, depth, st);
1975  st->print("]");
1976}
1977#endif
1978
1979//------------------------------singleton--------------------------------------
1980// TRUE if Type is a singleton type, FALSE otherwise.   Singletons are simple
1981// constants (Ldi nodes).  Singletons are integer, float or double constants
1982// or a single symbol.
1983bool TypeAry::singleton(void) const {
1984  return false;                 // Never a singleton
1985}
1986
1987bool TypeAry::empty(void) const {
1988  return _elem->empty() || _size->empty();
1989}
1990
1991//--------------------------ary_must_be_exact----------------------------------
1992bool TypeAry::ary_must_be_exact() const {
1993  if (!UseExactTypes)       return false;
1994  // This logic looks at the element type of an array, and returns true
1995  // if the element type is either a primitive or a final instance class.
1996  // In such cases, an array built on this ary must have no subclasses.
1997  if (_elem == BOTTOM)      return false;  // general array not exact
1998  if (_elem == TOP   )      return false;  // inverted general array not exact
1999  const TypeOopPtr*  toop = NULL;
2000  if (UseCompressedOops && _elem->isa_narrowoop()) {
2001    toop = _elem->make_ptr()->isa_oopptr();
2002  } else {
2003    toop = _elem->isa_oopptr();
2004  }
2005  if (!toop)                return true;   // a primitive type, like int
2006  ciKlass* tklass = toop->klass();
2007  if (tklass == NULL)       return false;  // unloaded class
2008  if (!tklass->is_loaded()) return false;  // unloaded class
2009  const TypeInstPtr* tinst;
2010  if (_elem->isa_narrowoop())
2011    tinst = _elem->make_ptr()->isa_instptr();
2012  else
2013    tinst = _elem->isa_instptr();
2014  if (tinst)
2015    return tklass->as_instance_klass()->is_final();
2016  const TypeAryPtr*  tap;
2017  if (_elem->isa_narrowoop())
2018    tap = _elem->make_ptr()->isa_aryptr();
2019  else
2020    tap = _elem->isa_aryptr();
2021  if (tap)
2022    return tap->ary()->ary_must_be_exact();
2023  return false;
2024}
2025
2026//==============================TypeVect=======================================
2027// Convenience common pre-built types.
2028const TypeVect *TypeVect::VECTS = NULL; //  32-bit vectors
2029const TypeVect *TypeVect::VECTD = NULL; //  64-bit vectors
2030const TypeVect *TypeVect::VECTX = NULL; // 128-bit vectors
2031const TypeVect *TypeVect::VECTY = NULL; // 256-bit vectors
2032
2033//------------------------------make-------------------------------------------
2034const TypeVect* TypeVect::make(const Type *elem, uint length) {
2035  BasicType elem_bt = elem->array_element_basic_type();
2036  assert(is_java_primitive(elem_bt), "only primitive types in vector");
2037  assert(length > 1 && is_power_of_2(length), "vector length is power of 2");
2038  assert(Matcher::vector_size_supported(elem_bt, length), "length in range");
2039  int size = length * type2aelembytes(elem_bt);
2040  switch (Matcher::vector_ideal_reg(size)) {
2041  case Op_VecS:
2042    return (TypeVect*)(new TypeVectS(elem, length))->hashcons();
2043  case Op_RegL:
2044  case Op_VecD:
2045  case Op_RegD:
2046    return (TypeVect*)(new TypeVectD(elem, length))->hashcons();
2047  case Op_VecX:
2048    return (TypeVect*)(new TypeVectX(elem, length))->hashcons();
2049  case Op_VecY:
2050    return (TypeVect*)(new TypeVectY(elem, length))->hashcons();
2051  }
2052 ShouldNotReachHere();
2053  return NULL;
2054}
2055
2056//------------------------------meet-------------------------------------------
2057// Compute the MEET of two types.  It returns a new Type object.
2058const Type *TypeVect::xmeet( const Type *t ) const {
2059  // Perform a fast test for common case; meeting the same types together.
2060  if( this == t ) return this;  // Meeting same type-rep?
2061
2062  // Current "this->_base" is Vector
2063  switch (t->base()) {          // switch on original type
2064
2065  case Bottom:                  // Ye Olde Default
2066    return t;
2067
2068  default:                      // All else is a mistake
2069    typerr(t);
2070
2071  case VectorS:
2072  case VectorD:
2073  case VectorX:
2074  case VectorY: {                // Meeting 2 vectors?
2075    const TypeVect* v = t->is_vect();
2076    assert(  base() == v->base(), "");
2077    assert(length() == v->length(), "");
2078    assert(element_basic_type() == v->element_basic_type(), "");
2079    return TypeVect::make(_elem->xmeet(v->_elem), _length);
2080  }
2081  case Top:
2082    break;
2083  }
2084  return this;
2085}
2086
2087//------------------------------xdual------------------------------------------
2088// Dual: compute field-by-field dual
2089const Type *TypeVect::xdual() const {
2090  return new TypeVect(base(), _elem->dual(), _length);
2091}
2092
2093//------------------------------eq---------------------------------------------
2094// Structural equality check for Type representations
2095bool TypeVect::eq(const Type *t) const {
2096  const TypeVect *v = t->is_vect();
2097  return (_elem == v->_elem) && (_length == v->_length);
2098}
2099
2100//------------------------------hash-------------------------------------------
2101// Type-specific hashing function.
2102int TypeVect::hash(void) const {
2103  return (intptr_t)_elem + (intptr_t)_length;
2104}
2105
2106//------------------------------singleton--------------------------------------
2107// TRUE if Type is a singleton type, FALSE otherwise.   Singletons are simple
2108// constants (Ldi nodes).  Vector is singleton if all elements are the same
2109// constant value (when vector is created with Replicate code).
2110bool TypeVect::singleton(void) const {
2111// There is no Con node for vectors yet.
2112//  return _elem->singleton();
2113  return false;
2114}
2115
2116bool TypeVect::empty(void) const {
2117  return _elem->empty();
2118}
2119
2120//------------------------------dump2------------------------------------------
2121#ifndef PRODUCT
2122void TypeVect::dump2(Dict &d, uint depth, outputStream *st) const {
2123  switch (base()) {
2124  case VectorS:
2125    st->print("vectors["); break;
2126  case VectorD:
2127    st->print("vectord["); break;
2128  case VectorX:
2129    st->print("vectorx["); break;
2130  case VectorY:
2131    st->print("vectory["); break;
2132  default:
2133    ShouldNotReachHere();
2134  }
2135  st->print("%d]:{", _length);
2136  _elem->dump2(d, depth, st);
2137  st->print("}");
2138}
2139#endif
2140
2141
2142//=============================================================================
2143// Convenience common pre-built types.
2144const TypePtr *TypePtr::NULL_PTR;
2145const TypePtr *TypePtr::NOTNULL;
2146const TypePtr *TypePtr::BOTTOM;
2147
2148//------------------------------meet-------------------------------------------
2149// Meet over the PTR enum
2150const TypePtr::PTR TypePtr::ptr_meet[TypePtr::lastPTR][TypePtr::lastPTR] = {
2151  //              TopPTR,    AnyNull,   Constant, Null,   NotNull, BotPTR,
2152  { /* Top     */ TopPTR,    AnyNull,   Constant, Null,   NotNull, BotPTR,},
2153  { /* AnyNull */ AnyNull,   AnyNull,   Constant, BotPTR, NotNull, BotPTR,},
2154  { /* Constant*/ Constant,  Constant,  Constant, BotPTR, NotNull, BotPTR,},
2155  { /* Null    */ Null,      BotPTR,    BotPTR,   Null,   BotPTR,  BotPTR,},
2156  { /* NotNull */ NotNull,   NotNull,   NotNull,  BotPTR, NotNull, BotPTR,},
2157  { /* BotPTR  */ BotPTR,    BotPTR,    BotPTR,   BotPTR, BotPTR,  BotPTR,}
2158};
2159
2160//------------------------------make-------------------------------------------
2161const TypePtr *TypePtr::make( TYPES t, enum PTR ptr, int offset ) {
2162  return (TypePtr*)(new TypePtr(t,ptr,offset))->hashcons();
2163}
2164
2165//------------------------------cast_to_ptr_type-------------------------------
2166const Type *TypePtr::cast_to_ptr_type(PTR ptr) const {
2167  assert(_base == AnyPtr, "subclass must override cast_to_ptr_type");
2168  if( ptr == _ptr ) return this;
2169  return make(_base, ptr, _offset);
2170}
2171
2172//------------------------------get_con----------------------------------------
2173intptr_t TypePtr::get_con() const {
2174  assert( _ptr == Null, "" );
2175  return _offset;
2176}
2177
2178//------------------------------meet-------------------------------------------
2179// Compute the MEET of two types.  It returns a new Type object.
2180const Type *TypePtr::xmeet( const Type *t ) const {
2181  // Perform a fast test for common case; meeting the same types together.
2182  if( this == t ) return this;  // Meeting same type-rep?
2183
2184  // Current "this->_base" is AnyPtr
2185  switch (t->base()) {          // switch on original type
2186  case Int:                     // Mixing ints & oops happens when javac
2187  case Long:                    // reuses local variables
2188  case FloatTop:
2189  case FloatCon:
2190  case FloatBot:
2191  case DoubleTop:
2192  case DoubleCon:
2193  case DoubleBot:
2194  case NarrowOop:
2195  case NarrowKlass:
2196  case Bottom:                  // Ye Olde Default
2197    return Type::BOTTOM;
2198  case Top:
2199    return this;
2200
2201  case AnyPtr: {                // Meeting to AnyPtrs
2202    const TypePtr *tp = t->is_ptr();
2203    return make( AnyPtr, meet_ptr(tp->ptr()), meet_offset(tp->offset()) );
2204  }
2205  case RawPtr:                  // For these, flip the call around to cut down
2206  case OopPtr:
2207  case InstPtr:                 // on the cases I have to handle.
2208  case AryPtr:
2209  case MetadataPtr:
2210  case KlassPtr:
2211    return t->xmeet(this);      // Call in reverse direction
2212  default:                      // All else is a mistake
2213    typerr(t);
2214
2215  }
2216  return this;
2217}
2218
2219//------------------------------meet_offset------------------------------------
2220int TypePtr::meet_offset( int offset ) const {
2221  // Either is 'TOP' offset?  Return the other offset!
2222  if( _offset == OffsetTop ) return offset;
2223  if( offset == OffsetTop ) return _offset;
2224  // If either is different, return 'BOTTOM' offset
2225  if( _offset != offset ) return OffsetBot;
2226  return _offset;
2227}
2228
2229//------------------------------dual_offset------------------------------------
2230int TypePtr::dual_offset( ) const {
2231  if( _offset == OffsetTop ) return OffsetBot;// Map 'TOP' into 'BOTTOM'
2232  if( _offset == OffsetBot ) return OffsetTop;// Map 'BOTTOM' into 'TOP'
2233  return _offset;               // Map everything else into self
2234}
2235
2236//------------------------------xdual------------------------------------------
2237// Dual: compute field-by-field dual
2238const TypePtr::PTR TypePtr::ptr_dual[TypePtr::lastPTR] = {
2239  BotPTR, NotNull, Constant, Null, AnyNull, TopPTR
2240};
2241const Type *TypePtr::xdual() const {
2242  return new TypePtr( AnyPtr, dual_ptr(), dual_offset() );
2243}
2244
2245//------------------------------xadd_offset------------------------------------
2246int TypePtr::xadd_offset( intptr_t offset ) const {
2247  // Adding to 'TOP' offset?  Return 'TOP'!
2248  if( _offset == OffsetTop || offset == OffsetTop ) return OffsetTop;
2249  // Adding to 'BOTTOM' offset?  Return 'BOTTOM'!
2250  if( _offset == OffsetBot || offset == OffsetBot ) return OffsetBot;
2251  // Addition overflows or "accidentally" equals to OffsetTop? Return 'BOTTOM'!
2252  offset += (intptr_t)_offset;
2253  if (offset != (int)offset || offset == OffsetTop) return OffsetBot;
2254
2255  // assert( _offset >= 0 && _offset+offset >= 0, "" );
2256  // It is possible to construct a negative offset during PhaseCCP
2257
2258  return (int)offset;        // Sum valid offsets
2259}
2260
2261//------------------------------add_offset-------------------------------------
2262const TypePtr *TypePtr::add_offset( intptr_t offset ) const {
2263  return make( AnyPtr, _ptr, xadd_offset(offset) );
2264}
2265
2266//------------------------------eq---------------------------------------------
2267// Structural equality check for Type representations
2268bool TypePtr::eq( const Type *t ) const {
2269  const TypePtr *a = (const TypePtr*)t;
2270  return _ptr == a->ptr() && _offset == a->offset();
2271}
2272
2273//------------------------------hash-------------------------------------------
2274// Type-specific hashing function.
2275int TypePtr::hash(void) const {
2276  return _ptr + _offset;
2277}
2278
2279//------------------------------dump2------------------------------------------
2280const char *const TypePtr::ptr_msg[TypePtr::lastPTR] = {
2281  "TopPTR","AnyNull","Constant","NULL","NotNull","BotPTR"
2282};
2283
2284#ifndef PRODUCT
2285void TypePtr::dump2( Dict &d, uint depth, outputStream *st ) const {
2286  if( _ptr == Null ) st->print("NULL");
2287  else st->print("%s *", ptr_msg[_ptr]);
2288  if( _offset == OffsetTop ) st->print("+top");
2289  else if( _offset == OffsetBot ) st->print("+bot");
2290  else if( _offset ) st->print("+%d", _offset);
2291}
2292#endif
2293
2294//------------------------------singleton--------------------------------------
2295// TRUE if Type is a singleton type, FALSE otherwise.   Singletons are simple
2296// constants
2297bool TypePtr::singleton(void) const {
2298  // TopPTR, Null, AnyNull, Constant are all singletons
2299  return (_offset != OffsetBot) && !below_centerline(_ptr);
2300}
2301
2302bool TypePtr::empty(void) const {
2303  return (_offset == OffsetTop) || above_centerline(_ptr);
2304}
2305
2306//=============================================================================
2307// Convenience common pre-built types.
2308const TypeRawPtr *TypeRawPtr::BOTTOM;
2309const TypeRawPtr *TypeRawPtr::NOTNULL;
2310
2311//------------------------------make-------------------------------------------
2312const TypeRawPtr *TypeRawPtr::make( enum PTR ptr ) {
2313  assert( ptr != Constant, "what is the constant?" );
2314  assert( ptr != Null, "Use TypePtr for NULL" );
2315  return (TypeRawPtr*)(new TypeRawPtr(ptr,0))->hashcons();
2316}
2317
2318const TypeRawPtr *TypeRawPtr::make( address bits ) {
2319  assert( bits, "Use TypePtr for NULL" );
2320  return (TypeRawPtr*)(new TypeRawPtr(Constant,bits))->hashcons();
2321}
2322
2323//------------------------------cast_to_ptr_type-------------------------------
2324const Type *TypeRawPtr::cast_to_ptr_type(PTR ptr) const {
2325  assert( ptr != Constant, "what is the constant?" );
2326  assert( ptr != Null, "Use TypePtr for NULL" );
2327  assert( _bits==0, "Why cast a constant address?");
2328  if( ptr == _ptr ) return this;
2329  return make(ptr);
2330}
2331
2332//------------------------------get_con----------------------------------------
2333intptr_t TypeRawPtr::get_con() const {
2334  assert( _ptr == Null || _ptr == Constant, "" );
2335  return (intptr_t)_bits;
2336}
2337
2338//------------------------------meet-------------------------------------------
2339// Compute the MEET of two types.  It returns a new Type object.
2340const Type *TypeRawPtr::xmeet( const Type *t ) const {
2341  // Perform a fast test for common case; meeting the same types together.
2342  if( this == t ) return this;  // Meeting same type-rep?
2343
2344  // Current "this->_base" is RawPtr
2345  switch( t->base() ) {         // switch on original type
2346  case Bottom:                  // Ye Olde Default
2347    return t;
2348  case Top:
2349    return this;
2350  case AnyPtr:                  // Meeting to AnyPtrs
2351    break;
2352  case RawPtr: {                // might be top, bot, any/not or constant
2353    enum PTR tptr = t->is_ptr()->ptr();
2354    enum PTR ptr = meet_ptr( tptr );
2355    if( ptr == Constant ) {     // Cannot be equal constants, so...
2356      if( tptr == Constant && _ptr != Constant)  return t;
2357      if( _ptr == Constant && tptr != Constant)  return this;
2358      ptr = NotNull;            // Fall down in lattice
2359    }
2360    return make( ptr );
2361  }
2362
2363  case OopPtr:
2364  case InstPtr:
2365  case AryPtr:
2366  case MetadataPtr:
2367  case KlassPtr:
2368    return TypePtr::BOTTOM;     // Oop meet raw is not well defined
2369  default:                      // All else is a mistake
2370    typerr(t);
2371  }
2372
2373  // Found an AnyPtr type vs self-RawPtr type
2374  const TypePtr *tp = t->is_ptr();
2375  switch (tp->ptr()) {
2376  case TypePtr::TopPTR:  return this;
2377  case TypePtr::BotPTR:  return t;
2378  case TypePtr::Null:
2379    if( _ptr == TypePtr::TopPTR ) return t;
2380    return TypeRawPtr::BOTTOM;
2381  case TypePtr::NotNull: return TypePtr::make( AnyPtr, meet_ptr(TypePtr::NotNull), tp->meet_offset(0) );
2382  case TypePtr::AnyNull:
2383    if( _ptr == TypePtr::Constant) return this;
2384    return make( meet_ptr(TypePtr::AnyNull) );
2385  default: ShouldNotReachHere();
2386  }
2387  return this;
2388}
2389
2390//------------------------------xdual------------------------------------------
2391// Dual: compute field-by-field dual
2392const Type *TypeRawPtr::xdual() const {
2393  return new TypeRawPtr( dual_ptr(), _bits );
2394}
2395
2396//------------------------------add_offset-------------------------------------
2397const TypePtr *TypeRawPtr::add_offset( intptr_t offset ) const {
2398  if( offset == OffsetTop ) return BOTTOM; // Undefined offset-> undefined pointer
2399  if( offset == OffsetBot ) return BOTTOM; // Unknown offset-> unknown pointer
2400  if( offset == 0 ) return this; // No change
2401  switch (_ptr) {
2402  case TypePtr::TopPTR:
2403  case TypePtr::BotPTR:
2404  case TypePtr::NotNull:
2405    return this;
2406  case TypePtr::Null:
2407  case TypePtr::Constant: {
2408    address bits = _bits+offset;
2409    if ( bits == 0 ) return TypePtr::NULL_PTR;
2410    return make( bits );
2411  }
2412  default:  ShouldNotReachHere();
2413  }
2414  return NULL;                  // Lint noise
2415}
2416
2417//------------------------------eq---------------------------------------------
2418// Structural equality check for Type representations
2419bool TypeRawPtr::eq( const Type *t ) const {
2420  const TypeRawPtr *a = (const TypeRawPtr*)t;
2421  return _bits == a->_bits && TypePtr::eq(t);
2422}
2423
2424//------------------------------hash-------------------------------------------
2425// Type-specific hashing function.
2426int TypeRawPtr::hash(void) const {
2427  return (intptr_t)_bits + TypePtr::hash();
2428}
2429
2430//------------------------------dump2------------------------------------------
2431#ifndef PRODUCT
2432void TypeRawPtr::dump2( Dict &d, uint depth, outputStream *st ) const {
2433  if( _ptr == Constant )
2434    st->print(INTPTR_FORMAT, _bits);
2435  else
2436    st->print("rawptr:%s", ptr_msg[_ptr]);
2437}
2438#endif
2439
2440//=============================================================================
2441// Convenience common pre-built type.
2442const TypeOopPtr *TypeOopPtr::BOTTOM;
2443
2444//------------------------------TypeOopPtr-------------------------------------
2445TypeOopPtr::TypeOopPtr(TYPES t, PTR ptr, ciKlass* k, bool xk, ciObject* o, int offset, int instance_id, const TypeOopPtr* speculative)
2446  : TypePtr(t, ptr, offset),
2447    _const_oop(o), _klass(k),
2448    _klass_is_exact(xk),
2449    _is_ptr_to_narrowoop(false),
2450    _is_ptr_to_narrowklass(false),
2451    _is_ptr_to_boxed_value(false),
2452    _instance_id(instance_id),
2453    _speculative(speculative) {
2454  if (Compile::current()->eliminate_boxing() && (t == InstPtr) &&
2455      (offset > 0) && xk && (k != 0) && k->is_instance_klass()) {
2456    _is_ptr_to_boxed_value = k->as_instance_klass()->is_boxed_value_offset(offset);
2457  }
2458#ifdef _LP64
2459  if (_offset != 0) {
2460    if (_offset == oopDesc::klass_offset_in_bytes()) {
2461      _is_ptr_to_narrowklass = UseCompressedClassPointers;
2462    } else if (klass() == NULL) {
2463      // Array with unknown body type
2464      assert(this->isa_aryptr(), "only arrays without klass");
2465      _is_ptr_to_narrowoop = UseCompressedOops;
2466    } else if (this->isa_aryptr()) {
2467      _is_ptr_to_narrowoop = (UseCompressedOops && klass()->is_obj_array_klass() &&
2468                             _offset != arrayOopDesc::length_offset_in_bytes());
2469    } else if (klass()->is_instance_klass()) {
2470      ciInstanceKlass* ik = klass()->as_instance_klass();
2471      ciField* field = NULL;
2472      if (this->isa_klassptr()) {
2473        // Perm objects don't use compressed references
2474      } else if (_offset == OffsetBot || _offset == OffsetTop) {
2475        // unsafe access
2476        _is_ptr_to_narrowoop = UseCompressedOops;
2477      } else { // exclude unsafe ops
2478        assert(this->isa_instptr(), "must be an instance ptr.");
2479
2480        if (klass() == ciEnv::current()->Class_klass() &&
2481            (_offset == java_lang_Class::klass_offset_in_bytes() ||
2482             _offset == java_lang_Class::array_klass_offset_in_bytes())) {
2483          // Special hidden fields from the Class.
2484          assert(this->isa_instptr(), "must be an instance ptr.");
2485          _is_ptr_to_narrowoop = false;
2486        } else if (klass() == ciEnv::current()->Class_klass() &&
2487                   _offset >= InstanceMirrorKlass::offset_of_static_fields()) {
2488          // Static fields
2489          assert(o != NULL, "must be constant");
2490          ciInstanceKlass* k = o->as_instance()->java_lang_Class_klass()->as_instance_klass();
2491          ciField* field = k->get_field_by_offset(_offset, true);
2492          assert(field != NULL, "missing field");
2493          BasicType basic_elem_type = field->layout_type();
2494          _is_ptr_to_narrowoop = UseCompressedOops && (basic_elem_type == T_OBJECT ||
2495                                                       basic_elem_type == T_ARRAY);
2496        } else {
2497          // Instance fields which contains a compressed oop references.
2498          field = ik->get_field_by_offset(_offset, false);
2499          if (field != NULL) {
2500            BasicType basic_elem_type = field->layout_type();
2501            _is_ptr_to_narrowoop = UseCompressedOops && (basic_elem_type == T_OBJECT ||
2502                                                         basic_elem_type == T_ARRAY);
2503          } else if (klass()->equals(ciEnv::current()->Object_klass())) {
2504            // Compile::find_alias_type() cast exactness on all types to verify
2505            // that it does not affect alias type.
2506            _is_ptr_to_narrowoop = UseCompressedOops;
2507          } else {
2508            // Type for the copy start in LibraryCallKit::inline_native_clone().
2509            _is_ptr_to_narrowoop = UseCompressedOops;
2510          }
2511        }
2512      }
2513    }
2514  }
2515#endif
2516}
2517
2518//------------------------------make-------------------------------------------
2519const TypeOopPtr *TypeOopPtr::make(PTR ptr,
2520                                   int offset, int instance_id, const TypeOopPtr* speculative) {
2521  assert(ptr != Constant, "no constant generic pointers");
2522  ciKlass*  k = Compile::current()->env()->Object_klass();
2523  bool      xk = false;
2524  ciObject* o = NULL;
2525  return (TypeOopPtr*)(new TypeOopPtr(OopPtr, ptr, k, xk, o, offset, instance_id, speculative))->hashcons();
2526}
2527
2528
2529//------------------------------cast_to_ptr_type-------------------------------
2530const Type *TypeOopPtr::cast_to_ptr_type(PTR ptr) const {
2531  assert(_base == OopPtr, "subclass must override cast_to_ptr_type");
2532  if( ptr == _ptr ) return this;
2533  return make(ptr, _offset, _instance_id, _speculative);
2534}
2535
2536//-----------------------------cast_to_instance_id----------------------------
2537const TypeOopPtr *TypeOopPtr::cast_to_instance_id(int instance_id) const {
2538  // There are no instances of a general oop.
2539  // Return self unchanged.
2540  return this;
2541}
2542
2543//-----------------------------cast_to_exactness-------------------------------
2544const Type *TypeOopPtr::cast_to_exactness(bool klass_is_exact) const {
2545  // There is no such thing as an exact general oop.
2546  // Return self unchanged.
2547  return this;
2548}
2549
2550
2551//------------------------------as_klass_type----------------------------------
2552// Return the klass type corresponding to this instance or array type.
2553// It is the type that is loaded from an object of this type.
2554const TypeKlassPtr* TypeOopPtr::as_klass_type() const {
2555  ciKlass* k = klass();
2556  bool    xk = klass_is_exact();
2557  if (k == NULL)
2558    return TypeKlassPtr::OBJECT;
2559  else
2560    return TypeKlassPtr::make(xk? Constant: NotNull, k, 0);
2561}
2562
2563const Type *TypeOopPtr::xmeet(const Type *t) const {
2564  const Type* res = xmeet_helper(t);
2565  if (res->isa_oopptr() == NULL) {
2566    return res;
2567  }
2568
2569  if (res->isa_oopptr() != NULL) {
2570    // type->speculative() == NULL means that speculation is no better
2571    // than type, i.e. type->speculative() == type. So there are 2
2572    // ways to represent the fact that we have no useful speculative
2573    // data and we should use a single one to be able to test for
2574    // equality between types. Check whether type->speculative() ==
2575    // type and set speculative to NULL if it is the case.
2576    const TypeOopPtr* res_oopptr = res->is_oopptr();
2577    if (res_oopptr->remove_speculative() == res_oopptr->speculative()) {
2578      return res_oopptr->remove_speculative();
2579    }
2580  }
2581
2582  return res;
2583}
2584
2585//------------------------------meet-------------------------------------------
2586// Compute the MEET of two types.  It returns a new Type object.
2587const Type *TypeOopPtr::xmeet_helper(const Type *t) const {
2588  // Perform a fast test for common case; meeting the same types together.
2589  if( this == t ) return this;  // Meeting same type-rep?
2590
2591  // Current "this->_base" is OopPtr
2592  switch (t->base()) {          // switch on original type
2593
2594  case Int:                     // Mixing ints & oops happens when javac
2595  case Long:                    // reuses local variables
2596  case FloatTop:
2597  case FloatCon:
2598  case FloatBot:
2599  case DoubleTop:
2600  case DoubleCon:
2601  case DoubleBot:
2602  case NarrowOop:
2603  case NarrowKlass:
2604  case Bottom:                  // Ye Olde Default
2605    return Type::BOTTOM;
2606  case Top:
2607    return this;
2608
2609  default:                      // All else is a mistake
2610    typerr(t);
2611
2612  case RawPtr:
2613  case MetadataPtr:
2614  case KlassPtr:
2615    return TypePtr::BOTTOM;     // Oop meet raw is not well defined
2616
2617  case AnyPtr: {
2618    // Found an AnyPtr type vs self-OopPtr type
2619    const TypePtr *tp = t->is_ptr();
2620    int offset = meet_offset(tp->offset());
2621    PTR ptr = meet_ptr(tp->ptr());
2622    switch (tp->ptr()) {
2623    case Null:
2624      if (ptr == Null)  return TypePtr::make(AnyPtr, ptr, offset);
2625      // else fall through:
2626    case TopPTR:
2627    case AnyNull: {
2628      int instance_id = meet_instance_id(InstanceTop);
2629      const TypeOopPtr* speculative = _speculative;
2630      return make(ptr, offset, instance_id, speculative);
2631    }
2632    case BotPTR:
2633    case NotNull:
2634      return TypePtr::make(AnyPtr, ptr, offset);
2635    default: typerr(t);
2636    }
2637  }
2638
2639  case OopPtr: {                 // Meeting to other OopPtrs
2640    const TypeOopPtr *tp = t->is_oopptr();
2641    int instance_id = meet_instance_id(tp->instance_id());
2642    const TypeOopPtr* speculative = meet_speculative(tp);
2643    return make(meet_ptr(tp->ptr()), meet_offset(tp->offset()), instance_id, speculative);
2644  }
2645
2646  case InstPtr:                  // For these, flip the call around to cut down
2647  case AryPtr:
2648    return t->xmeet(this);      // Call in reverse direction
2649
2650  } // End of switch
2651  return this;                  // Return the double constant
2652}
2653
2654
2655//------------------------------xdual------------------------------------------
2656// Dual of a pure heap pointer.  No relevant klass or oop information.
2657const Type *TypeOopPtr::xdual() const {
2658  assert(klass() == Compile::current()->env()->Object_klass(), "no klasses here");
2659  assert(const_oop() == NULL,             "no constants here");
2660  return new TypeOopPtr(_base, dual_ptr(), klass(), klass_is_exact(), const_oop(), dual_offset(), dual_instance_id(), dual_speculative());
2661}
2662
2663//--------------------------make_from_klass_common-----------------------------
2664// Computes the element-type given a klass.
2665const TypeOopPtr* TypeOopPtr::make_from_klass_common(ciKlass *klass, bool klass_change, bool try_for_exact) {
2666  if (klass->is_instance_klass()) {
2667    Compile* C = Compile::current();
2668    Dependencies* deps = C->dependencies();
2669    assert((deps != NULL) == (C->method() != NULL && C->method()->code_size() > 0), "sanity");
2670    // Element is an instance
2671    bool klass_is_exact = false;
2672    if (klass->is_loaded()) {
2673      // Try to set klass_is_exact.
2674      ciInstanceKlass* ik = klass->as_instance_klass();
2675      klass_is_exact = ik->is_final();
2676      if (!klass_is_exact && klass_change
2677          && deps != NULL && UseUniqueSubclasses) {
2678        ciInstanceKlass* sub = ik->unique_concrete_subklass();
2679        if (sub != NULL) {
2680          deps->assert_abstract_with_unique_concrete_subtype(ik, sub);
2681          klass = ik = sub;
2682          klass_is_exact = sub->is_final();
2683        }
2684      }
2685      if (!klass_is_exact && try_for_exact
2686          && deps != NULL && UseExactTypes) {
2687        if (!ik->is_interface() && !ik->has_subklass()) {
2688          // Add a dependence; if concrete subclass added we need to recompile
2689          deps->assert_leaf_type(ik);
2690          klass_is_exact = true;
2691        }
2692      }
2693    }
2694    return TypeInstPtr::make(TypePtr::BotPTR, klass, klass_is_exact, NULL, 0);
2695  } else if (klass->is_obj_array_klass()) {
2696    // Element is an object array. Recursively call ourself.
2697    const TypeOopPtr *etype = TypeOopPtr::make_from_klass_common(klass->as_obj_array_klass()->element_klass(), false, try_for_exact);
2698    bool xk = etype->klass_is_exact();
2699    const TypeAry* arr0 = TypeAry::make(etype, TypeInt::POS);
2700    // We used to pass NotNull in here, asserting that the sub-arrays
2701    // are all not-null.  This is not true in generally, as code can
2702    // slam NULLs down in the subarrays.
2703    const TypeAryPtr* arr = TypeAryPtr::make(TypePtr::BotPTR, arr0, klass, xk, 0);
2704    return arr;
2705  } else if (klass->is_type_array_klass()) {
2706    // Element is an typeArray
2707    const Type* etype = get_const_basic_type(klass->as_type_array_klass()->element_type());
2708    const TypeAry* arr0 = TypeAry::make(etype, TypeInt::POS);
2709    // We used to pass NotNull in here, asserting that the array pointer
2710    // is not-null. That was not true in general.
2711    const TypeAryPtr* arr = TypeAryPtr::make(TypePtr::BotPTR, arr0, klass, true, 0);
2712    return arr;
2713  } else {
2714    ShouldNotReachHere();
2715    return NULL;
2716  }
2717}
2718
2719//------------------------------make_from_constant-----------------------------
2720// Make a java pointer from an oop constant
2721const TypeOopPtr* TypeOopPtr::make_from_constant(ciObject* o,
2722                                                 bool require_constant,
2723                                                 bool is_autobox_cache) {
2724  assert(!o->is_null_object(), "null object not yet handled here.");
2725  ciKlass* klass = o->klass();
2726  if (klass->is_instance_klass()) {
2727    // Element is an instance
2728    if (require_constant) {
2729      if (!o->can_be_constant())  return NULL;
2730    } else if (!o->should_be_constant()) {
2731      return TypeInstPtr::make(TypePtr::NotNull, klass, true, NULL, 0);
2732    }
2733    return TypeInstPtr::make(o);
2734  } else if (klass->is_obj_array_klass()) {
2735    // Element is an object array. Recursively call ourself.
2736    const TypeOopPtr *etype =
2737      TypeOopPtr::make_from_klass_raw(klass->as_obj_array_klass()->element_klass());
2738    if (is_autobox_cache) {
2739      // The pointers in the autobox arrays are always non-null.
2740      etype = etype->cast_to_ptr_type(TypePtr::NotNull)->is_oopptr();
2741    }
2742    const TypeAry* arr0 = TypeAry::make(etype, TypeInt::make(o->as_array()->length()));
2743    // We used to pass NotNull in here, asserting that the sub-arrays
2744    // are all not-null.  This is not true in generally, as code can
2745    // slam NULLs down in the subarrays.
2746    if (require_constant) {
2747      if (!o->can_be_constant())  return NULL;
2748    } else if (!o->should_be_constant()) {
2749      return TypeAryPtr::make(TypePtr::NotNull, arr0, klass, true, 0);
2750    }
2751    const TypeAryPtr* arr = TypeAryPtr::make(TypePtr::Constant, o, arr0, klass, true, 0, InstanceBot, NULL, is_autobox_cache);
2752    return arr;
2753  } else if (klass->is_type_array_klass()) {
2754    // Element is an typeArray
2755    const Type* etype =
2756      (Type*)get_const_basic_type(klass->as_type_array_klass()->element_type());
2757    const TypeAry* arr0 = TypeAry::make(etype, TypeInt::make(o->as_array()->length()));
2758    // We used to pass NotNull in here, asserting that the array pointer
2759    // is not-null. That was not true in general.
2760    if (require_constant) {
2761      if (!o->can_be_constant())  return NULL;
2762    } else if (!o->should_be_constant()) {
2763      return TypeAryPtr::make(TypePtr::NotNull, arr0, klass, true, 0);
2764    }
2765    const TypeAryPtr* arr = TypeAryPtr::make(TypePtr::Constant, o, arr0, klass, true, 0);
2766    return arr;
2767  }
2768
2769  fatal("unhandled object type");
2770  return NULL;
2771}
2772
2773//------------------------------get_con----------------------------------------
2774intptr_t TypeOopPtr::get_con() const {
2775  assert( _ptr == Null || _ptr == Constant, "" );
2776  assert( _offset >= 0, "" );
2777
2778  if (_offset != 0) {
2779    // After being ported to the compiler interface, the compiler no longer
2780    // directly manipulates the addresses of oops.  Rather, it only has a pointer
2781    // to a handle at compile time.  This handle is embedded in the generated
2782    // code and dereferenced at the time the nmethod is made.  Until that time,
2783    // it is not reasonable to do arithmetic with the addresses of oops (we don't
2784    // have access to the addresses!).  This does not seem to currently happen,
2785    // but this assertion here is to help prevent its occurence.
2786    tty->print_cr("Found oop constant with non-zero offset");
2787    ShouldNotReachHere();
2788  }
2789
2790  return (intptr_t)const_oop()->constant_encoding();
2791}
2792
2793
2794//-----------------------------filter------------------------------------------
2795// Do not allow interface-vs.-noninterface joins to collapse to top.
2796const Type *TypeOopPtr::filter(const Type *kills) const {
2797
2798  const Type* ft = join(kills);
2799  const TypeInstPtr* ftip = ft->isa_instptr();
2800  const TypeInstPtr* ktip = kills->isa_instptr();
2801
2802  if (ft->empty()) {
2803    // Check for evil case of 'this' being a class and 'kills' expecting an
2804    // interface.  This can happen because the bytecodes do not contain
2805    // enough type info to distinguish a Java-level interface variable
2806    // from a Java-level object variable.  If we meet 2 classes which
2807    // both implement interface I, but their meet is at 'j/l/O' which
2808    // doesn't implement I, we have no way to tell if the result should
2809    // be 'I' or 'j/l/O'.  Thus we'll pick 'j/l/O'.  If this then flows
2810    // into a Phi which "knows" it's an Interface type we'll have to
2811    // uplift the type.
2812    if (!empty() && ktip != NULL && ktip->is_loaded() && ktip->klass()->is_interface())
2813      return kills;             // Uplift to interface
2814
2815    return Type::TOP;           // Canonical empty value
2816  }
2817
2818  // If we have an interface-typed Phi or cast and we narrow to a class type,
2819  // the join should report back the class.  However, if we have a J/L/Object
2820  // class-typed Phi and an interface flows in, it's possible that the meet &
2821  // join report an interface back out.  This isn't possible but happens
2822  // because the type system doesn't interact well with interfaces.
2823  if (ftip != NULL && ktip != NULL &&
2824      ftip->is_loaded() &&  ftip->klass()->is_interface() &&
2825      ktip->is_loaded() && !ktip->klass()->is_interface()) {
2826    // Happens in a CTW of rt.jar, 320-341, no extra flags
2827    assert(!ftip->klass_is_exact(), "interface could not be exact");
2828    return ktip->cast_to_ptr_type(ftip->ptr());
2829  }
2830
2831  return ft;
2832}
2833
2834//------------------------------eq---------------------------------------------
2835// Structural equality check for Type representations
2836bool TypeOopPtr::eq( const Type *t ) const {
2837  const TypeOopPtr *a = (const TypeOopPtr*)t;
2838  if (_klass_is_exact != a->_klass_is_exact ||
2839      _instance_id != a->_instance_id ||
2840      !eq_speculative(a))  return false;
2841  ciObject* one = const_oop();
2842  ciObject* two = a->const_oop();
2843  if (one == NULL || two == NULL) {
2844    return (one == two) && TypePtr::eq(t);
2845  } else {
2846    return one->equals(two) && TypePtr::eq(t);
2847  }
2848}
2849
2850//------------------------------hash-------------------------------------------
2851// Type-specific hashing function.
2852int TypeOopPtr::hash(void) const {
2853  return
2854    (const_oop() ? const_oop()->hash() : 0) +
2855    _klass_is_exact +
2856    _instance_id +
2857    hash_speculative() +
2858    TypePtr::hash();
2859}
2860
2861//------------------------------dump2------------------------------------------
2862#ifndef PRODUCT
2863void TypeOopPtr::dump2( Dict &d, uint depth, outputStream *st ) const {
2864  st->print("oopptr:%s", ptr_msg[_ptr]);
2865  if( _klass_is_exact ) st->print(":exact");
2866  if( const_oop() ) st->print(INTPTR_FORMAT, const_oop());
2867  switch( _offset ) {
2868  case OffsetTop: st->print("+top"); break;
2869  case OffsetBot: st->print("+any"); break;
2870  case         0: break;
2871  default:        st->print("+%d",_offset); break;
2872  }
2873  if (_instance_id == InstanceTop)
2874    st->print(",iid=top");
2875  else if (_instance_id != InstanceBot)
2876    st->print(",iid=%d",_instance_id);
2877
2878  dump_speculative(st);
2879}
2880
2881/**
2882 *dump the speculative part of the type
2883 */
2884void TypeOopPtr::dump_speculative(outputStream *st) const {
2885  if (_speculative != NULL) {
2886    st->print(" (speculative=");
2887    _speculative->dump_on(st);
2888    st->print(")");
2889  }
2890}
2891#endif
2892
2893//------------------------------singleton--------------------------------------
2894// TRUE if Type is a singleton type, FALSE otherwise.   Singletons are simple
2895// constants
2896bool TypeOopPtr::singleton(void) const {
2897  // detune optimizer to not generate constant oop + constant offset as a constant!
2898  // TopPTR, Null, AnyNull, Constant are all singletons
2899  return (_offset == 0) && !below_centerline(_ptr);
2900}
2901
2902//------------------------------add_offset-------------------------------------
2903const TypePtr *TypeOopPtr::add_offset(intptr_t offset) const {
2904  return make(_ptr, xadd_offset(offset), _instance_id, add_offset_speculative(offset));
2905}
2906
2907/**
2908 * Return same type without a speculative part
2909 */
2910const TypeOopPtr* TypeOopPtr::remove_speculative() const {
2911  return make(_ptr, _offset, _instance_id, NULL);
2912}
2913
2914//------------------------------meet_instance_id--------------------------------
2915int TypeOopPtr::meet_instance_id( int instance_id ) const {
2916  // Either is 'TOP' instance?  Return the other instance!
2917  if( _instance_id == InstanceTop ) return  instance_id;
2918  if(  instance_id == InstanceTop ) return _instance_id;
2919  // If either is different, return 'BOTTOM' instance
2920  if( _instance_id != instance_id ) return InstanceBot;
2921  return _instance_id;
2922}
2923
2924//------------------------------dual_instance_id--------------------------------
2925int TypeOopPtr::dual_instance_id( ) const {
2926  if( _instance_id == InstanceTop ) return InstanceBot; // Map TOP into BOTTOM
2927  if( _instance_id == InstanceBot ) return InstanceTop; // Map BOTTOM into TOP
2928  return _instance_id;              // Map everything else into self
2929}
2930
2931/**
2932 * meet of the speculative parts of 2 types
2933 *
2934 * @param other  type to meet with
2935 */
2936const TypeOopPtr* TypeOopPtr::meet_speculative(const TypeOopPtr* other) const {
2937  bool this_has_spec = (_speculative != NULL);
2938  bool other_has_spec = (other->speculative() != NULL);
2939
2940  if (!this_has_spec && !other_has_spec) {
2941    return NULL;
2942  }
2943
2944  // If we are at a point where control flow meets and one branch has
2945  // a speculative type and the other has not, we meet the speculative
2946  // type of one branch with the actual type of the other. If the
2947  // actual type is exact and the speculative is as well, then the
2948  // result is a speculative type which is exact and we can continue
2949  // speculation further.
2950  const TypeOopPtr* this_spec = _speculative;
2951  const TypeOopPtr* other_spec = other->speculative();
2952
2953  if (!this_has_spec) {
2954    this_spec = this;
2955  }
2956
2957  if (!other_has_spec) {
2958    other_spec = other;
2959  }
2960
2961  return this_spec->meet(other_spec)->is_oopptr();
2962}
2963
2964/**
2965 * dual of the speculative part of the type
2966 */
2967const TypeOopPtr* TypeOopPtr::dual_speculative() const {
2968  if (_speculative == NULL) {
2969    return NULL;
2970  }
2971  return _speculative->dual()->is_oopptr();
2972}
2973
2974/**
2975 * add offset to the speculative part of the type
2976 *
2977 * @param offset  offset to add
2978 */
2979const TypeOopPtr* TypeOopPtr::add_offset_speculative(intptr_t offset) const {
2980  if (_speculative == NULL) {
2981    return NULL;
2982  }
2983  return _speculative->add_offset(offset)->is_oopptr();
2984}
2985
2986/**
2987 * Are the speculative parts of 2 types equal?
2988 *
2989 * @param other  type to compare this one to
2990 */
2991bool TypeOopPtr::eq_speculative(const TypeOopPtr* other) const {
2992  if (_speculative == NULL || other->speculative() == NULL) {
2993    return _speculative == other->speculative();
2994  }
2995
2996  if (_speculative->base() != other->speculative()->base()) {
2997    return false;
2998  }
2999
3000  return _speculative->eq(other->speculative());
3001}
3002
3003/**
3004 * Hash of the speculative part of the type
3005 */
3006int TypeOopPtr::hash_speculative() const {
3007  if (_speculative == NULL) {
3008    return 0;
3009  }
3010
3011  return _speculative->hash();
3012}
3013
3014
3015//=============================================================================
3016// Convenience common pre-built types.
3017const TypeInstPtr *TypeInstPtr::NOTNULL;
3018const TypeInstPtr *TypeInstPtr::BOTTOM;
3019const TypeInstPtr *TypeInstPtr::MIRROR;
3020const TypeInstPtr *TypeInstPtr::MARK;
3021const TypeInstPtr *TypeInstPtr::KLASS;
3022
3023//------------------------------TypeInstPtr-------------------------------------
3024TypeInstPtr::TypeInstPtr(PTR ptr, ciKlass* k, bool xk, ciObject* o, int off, int instance_id, const TypeOopPtr* speculative)
3025  : TypeOopPtr(InstPtr, ptr, k, xk, o, off, instance_id, speculative), _name(k->name()) {
3026   assert(k != NULL &&
3027          (k->is_loaded() || o == NULL),
3028          "cannot have constants with non-loaded klass");
3029};
3030
3031//------------------------------make-------------------------------------------
3032const TypeInstPtr *TypeInstPtr::make(PTR ptr,
3033                                     ciKlass* k,
3034                                     bool xk,
3035                                     ciObject* o,
3036                                     int offset,
3037                                     int instance_id,
3038                                     const TypeOopPtr* speculative) {
3039  assert( !k->is_loaded() || k->is_instance_klass(), "Must be for instance");
3040  // Either const_oop() is NULL or else ptr is Constant
3041  assert( (!o && ptr != Constant) || (o && ptr == Constant),
3042          "constant pointers must have a value supplied" );
3043  // Ptr is never Null
3044  assert( ptr != Null, "NULL pointers are not typed" );
3045
3046  assert(instance_id <= 0 || xk || !UseExactTypes, "instances are always exactly typed");
3047  if (!UseExactTypes)  xk = false;
3048  if (ptr == Constant) {
3049    // Note:  This case includes meta-object constants, such as methods.
3050    xk = true;
3051  } else if (k->is_loaded()) {
3052    ciInstanceKlass* ik = k->as_instance_klass();
3053    if (!xk && ik->is_final())     xk = true;   // no inexact final klass
3054    if (xk && ik->is_interface())  xk = false;  // no exact interface
3055  }
3056
3057  // Now hash this baby
3058  TypeInstPtr *result =
3059    (TypeInstPtr*)(new TypeInstPtr(ptr, k, xk, o ,offset, instance_id, speculative))->hashcons();
3060
3061  return result;
3062}
3063
3064/**
3065 *  Create constant type for a constant boxed value
3066 */
3067const Type* TypeInstPtr::get_const_boxed_value() const {
3068  assert(is_ptr_to_boxed_value(), "should be called only for boxed value");
3069  assert((const_oop() != NULL), "should be called only for constant object");
3070  ciConstant constant = const_oop()->as_instance()->field_value_by_offset(offset());
3071  BasicType bt = constant.basic_type();
3072  switch (bt) {
3073    case T_BOOLEAN:  return TypeInt::make(constant.as_boolean());
3074    case T_INT:      return TypeInt::make(constant.as_int());
3075    case T_CHAR:     return TypeInt::make(constant.as_char());
3076    case T_BYTE:     return TypeInt::make(constant.as_byte());
3077    case T_SHORT:    return TypeInt::make(constant.as_short());
3078    case T_FLOAT:    return TypeF::make(constant.as_float());
3079    case T_DOUBLE:   return TypeD::make(constant.as_double());
3080    case T_LONG:     return TypeLong::make(constant.as_long());
3081    default:         break;
3082  }
3083  fatal(err_msg_res("Invalid boxed value type '%s'", type2name(bt)));
3084  return NULL;
3085}
3086
3087//------------------------------cast_to_ptr_type-------------------------------
3088const Type *TypeInstPtr::cast_to_ptr_type(PTR ptr) const {
3089  if( ptr == _ptr ) return this;
3090  // Reconstruct _sig info here since not a problem with later lazy
3091  // construction, _sig will show up on demand.
3092  return make(ptr, klass(), klass_is_exact(), const_oop(), _offset, _instance_id, _speculative);
3093}
3094
3095
3096//-----------------------------cast_to_exactness-------------------------------
3097const Type *TypeInstPtr::cast_to_exactness(bool klass_is_exact) const {
3098  if( klass_is_exact == _klass_is_exact ) return this;
3099  if (!UseExactTypes)  return this;
3100  if (!_klass->is_loaded())  return this;
3101  ciInstanceKlass* ik = _klass->as_instance_klass();
3102  if( (ik->is_final() || _const_oop) )  return this;  // cannot clear xk
3103  if( ik->is_interface() )              return this;  // cannot set xk
3104  return make(ptr(), klass(), klass_is_exact, const_oop(), _offset, _instance_id, _speculative);
3105}
3106
3107//-----------------------------cast_to_instance_id----------------------------
3108const TypeOopPtr *TypeInstPtr::cast_to_instance_id(int instance_id) const {
3109  if( instance_id == _instance_id ) return this;
3110  return make(_ptr, klass(), _klass_is_exact, const_oop(), _offset, instance_id, _speculative);
3111}
3112
3113//------------------------------xmeet_unloaded---------------------------------
3114// Compute the MEET of two InstPtrs when at least one is unloaded.
3115// Assume classes are different since called after check for same name/class-loader
3116const TypeInstPtr *TypeInstPtr::xmeet_unloaded(const TypeInstPtr *tinst) const {
3117    int off = meet_offset(tinst->offset());
3118    PTR ptr = meet_ptr(tinst->ptr());
3119    int instance_id = meet_instance_id(tinst->instance_id());
3120    const TypeOopPtr* speculative = meet_speculative(tinst);
3121
3122    const TypeInstPtr *loaded    = is_loaded() ? this  : tinst;
3123    const TypeInstPtr *unloaded  = is_loaded() ? tinst : this;
3124    if( loaded->klass()->equals(ciEnv::current()->Object_klass()) ) {
3125      //
3126      // Meet unloaded class with java/lang/Object
3127      //
3128      // Meet
3129      //          |                     Unloaded Class
3130      //  Object  |   TOP    |   AnyNull | Constant |   NotNull |  BOTTOM   |
3131      //  ===================================================================
3132      //   TOP    | ..........................Unloaded......................|
3133      //  AnyNull |  U-AN    |................Unloaded......................|
3134      // Constant | ... O-NN .................................. |   O-BOT   |
3135      //  NotNull | ... O-NN .................................. |   O-BOT   |
3136      //  BOTTOM  | ........................Object-BOTTOM ..................|
3137      //
3138      assert(loaded->ptr() != TypePtr::Null, "insanity check");
3139      //
3140      if(      loaded->ptr() == TypePtr::TopPTR ) { return unloaded; }
3141      else if (loaded->ptr() == TypePtr::AnyNull) { return TypeInstPtr::make(ptr, unloaded->klass(), false, NULL, off, instance_id, speculative); }
3142      else if (loaded->ptr() == TypePtr::BotPTR ) { return TypeInstPtr::BOTTOM; }
3143      else if (loaded->ptr() == TypePtr::Constant || loaded->ptr() == TypePtr::NotNull) {
3144        if (unloaded->ptr() == TypePtr::BotPTR  ) { return TypeInstPtr::BOTTOM;  }
3145        else                                      { return TypeInstPtr::NOTNULL; }
3146      }
3147      else if( unloaded->ptr() == TypePtr::TopPTR )  { return unloaded; }
3148
3149      return unloaded->cast_to_ptr_type(TypePtr::AnyNull)->is_instptr();
3150    }
3151
3152    // Both are unloaded, not the same class, not Object
3153    // Or meet unloaded with a different loaded class, not java/lang/Object
3154    if( ptr != TypePtr::BotPTR ) {
3155      return TypeInstPtr::NOTNULL;
3156    }
3157    return TypeInstPtr::BOTTOM;
3158}
3159
3160
3161//------------------------------meet-------------------------------------------
3162// Compute the MEET of two types.  It returns a new Type object.
3163const Type *TypeInstPtr::xmeet_helper(const Type *t) const {
3164  // Perform a fast test for common case; meeting the same types together.
3165  if( this == t ) return this;  // Meeting same type-rep?
3166
3167  // Current "this->_base" is Pointer
3168  switch (t->base()) {          // switch on original type
3169
3170  case Int:                     // Mixing ints & oops happens when javac
3171  case Long:                    // reuses local variables
3172  case FloatTop:
3173  case FloatCon:
3174  case FloatBot:
3175  case DoubleTop:
3176  case DoubleCon:
3177  case DoubleBot:
3178  case NarrowOop:
3179  case NarrowKlass:
3180  case Bottom:                  // Ye Olde Default
3181    return Type::BOTTOM;
3182  case Top:
3183    return this;
3184
3185  default:                      // All else is a mistake
3186    typerr(t);
3187
3188  case MetadataPtr:
3189  case KlassPtr:
3190  case RawPtr: return TypePtr::BOTTOM;
3191
3192  case AryPtr: {                // All arrays inherit from Object class
3193    const TypeAryPtr *tp = t->is_aryptr();
3194    int offset = meet_offset(tp->offset());
3195    PTR ptr = meet_ptr(tp->ptr());
3196    int instance_id = meet_instance_id(tp->instance_id());
3197    const TypeOopPtr* speculative = meet_speculative(tp);
3198    switch (ptr) {
3199    case TopPTR:
3200    case AnyNull:                // Fall 'down' to dual of object klass
3201      // For instances when a subclass meets a superclass we fall
3202      // below the centerline when the superclass is exact. We need to
3203      // do the same here.
3204      if (klass()->equals(ciEnv::current()->Object_klass()) && !klass_is_exact()) {
3205        return TypeAryPtr::make(ptr, tp->ary(), tp->klass(), tp->klass_is_exact(), offset, instance_id, speculative);
3206      } else {
3207        // cannot subclass, so the meet has to fall badly below the centerline
3208        ptr = NotNull;
3209        instance_id = InstanceBot;
3210        return TypeInstPtr::make( ptr, ciEnv::current()->Object_klass(), false, NULL, offset, instance_id, speculative);
3211      }
3212    case Constant:
3213    case NotNull:
3214    case BotPTR:                // Fall down to object klass
3215      // LCA is object_klass, but if we subclass from the top we can do better
3216      if( above_centerline(_ptr) ) { // if( _ptr == TopPTR || _ptr == AnyNull )
3217        // If 'this' (InstPtr) is above the centerline and it is Object class
3218        // then we can subclass in the Java class hierarchy.
3219        // For instances when a subclass meets a superclass we fall
3220        // below the centerline when the superclass is exact. We need
3221        // to do the same here.
3222        if (klass()->equals(ciEnv::current()->Object_klass()) && !klass_is_exact()) {
3223          // that is, tp's array type is a subtype of my klass
3224          return TypeAryPtr::make(ptr, (ptr == Constant ? tp->const_oop() : NULL),
3225                                  tp->ary(), tp->klass(), tp->klass_is_exact(), offset, instance_id, speculative);
3226        }
3227      }
3228      // The other case cannot happen, since I cannot be a subtype of an array.
3229      // The meet falls down to Object class below centerline.
3230      if( ptr == Constant )
3231         ptr = NotNull;
3232      instance_id = InstanceBot;
3233      return make(ptr, ciEnv::current()->Object_klass(), false, NULL, offset, instance_id, speculative);
3234    default: typerr(t);
3235    }
3236  }
3237
3238  case OopPtr: {                // Meeting to OopPtrs
3239    // Found a OopPtr type vs self-InstPtr type
3240    const TypeOopPtr *tp = t->is_oopptr();
3241    int offset = meet_offset(tp->offset());
3242    PTR ptr = meet_ptr(tp->ptr());
3243    switch (tp->ptr()) {
3244    case TopPTR:
3245    case AnyNull: {
3246      int instance_id = meet_instance_id(InstanceTop);
3247      const TypeOopPtr* speculative = meet_speculative(tp);
3248      return make(ptr, klass(), klass_is_exact(),
3249                  (ptr == Constant ? const_oop() : NULL), offset, instance_id, speculative);
3250    }
3251    case NotNull:
3252    case BotPTR: {
3253      int instance_id = meet_instance_id(tp->instance_id());
3254      const TypeOopPtr* speculative = meet_speculative(tp);
3255      return TypeOopPtr::make(ptr, offset, instance_id, speculative);
3256    }
3257    default: typerr(t);
3258    }
3259  }
3260
3261  case AnyPtr: {                // Meeting to AnyPtrs
3262    // Found an AnyPtr type vs self-InstPtr type
3263    const TypePtr *tp = t->is_ptr();
3264    int offset = meet_offset(tp->offset());
3265    PTR ptr = meet_ptr(tp->ptr());
3266    switch (tp->ptr()) {
3267    case Null:
3268      if( ptr == Null ) return TypePtr::make(AnyPtr, ptr, offset);
3269      // else fall through to AnyNull
3270    case TopPTR:
3271    case AnyNull: {
3272      int instance_id = meet_instance_id(InstanceTop);
3273      const TypeOopPtr* speculative = _speculative;
3274      return make(ptr, klass(), klass_is_exact(),
3275                  (ptr == Constant ? const_oop() : NULL), offset, instance_id, speculative);
3276    }
3277    case NotNull:
3278    case BotPTR:
3279      return TypePtr::make(AnyPtr, ptr, offset);
3280    default: typerr(t);
3281    }
3282  }
3283
3284  /*
3285                 A-top         }
3286               /   |   \       }  Tops
3287           B-top A-any C-top   }
3288              | /  |  \ |      }  Any-nulls
3289           B-any   |   C-any   }
3290              |    |    |
3291           B-con A-con C-con   } constants; not comparable across classes
3292              |    |    |
3293           B-not   |   C-not   }
3294              | \  |  / |      }  not-nulls
3295           B-bot A-not C-bot   }
3296               \   |   /       }  Bottoms
3297                 A-bot         }
3298  */
3299
3300  case InstPtr: {                // Meeting 2 Oops?
3301    // Found an InstPtr sub-type vs self-InstPtr type
3302    const TypeInstPtr *tinst = t->is_instptr();
3303    int off = meet_offset( tinst->offset() );
3304    PTR ptr = meet_ptr( tinst->ptr() );
3305    int instance_id = meet_instance_id(tinst->instance_id());
3306    const TypeOopPtr* speculative = meet_speculative(tinst);
3307
3308    // Check for easy case; klasses are equal (and perhaps not loaded!)
3309    // If we have constants, then we created oops so classes are loaded
3310    // and we can handle the constants further down.  This case handles
3311    // both-not-loaded or both-loaded classes
3312    if (ptr != Constant && klass()->equals(tinst->klass()) && klass_is_exact() == tinst->klass_is_exact()) {
3313      return make(ptr, klass(), klass_is_exact(), NULL, off, instance_id, speculative);
3314    }
3315
3316    // Classes require inspection in the Java klass hierarchy.  Must be loaded.
3317    ciKlass* tinst_klass = tinst->klass();
3318    ciKlass* this_klass  = this->klass();
3319    bool tinst_xk = tinst->klass_is_exact();
3320    bool this_xk  = this->klass_is_exact();
3321    if (!tinst_klass->is_loaded() || !this_klass->is_loaded() ) {
3322      // One of these classes has not been loaded
3323      const TypeInstPtr *unloaded_meet = xmeet_unloaded(tinst);
3324#ifndef PRODUCT
3325      if( PrintOpto && Verbose ) {
3326        tty->print("meet of unloaded classes resulted in: "); unloaded_meet->dump(); tty->cr();
3327        tty->print("  this == "); this->dump(); tty->cr();
3328        tty->print(" tinst == "); tinst->dump(); tty->cr();
3329      }
3330#endif
3331      return unloaded_meet;
3332    }
3333
3334    // Handle mixing oops and interfaces first.
3335    if( this_klass->is_interface() && !(tinst_klass->is_interface() ||
3336                                        tinst_klass == ciEnv::current()->Object_klass())) {
3337      ciKlass *tmp = tinst_klass; // Swap interface around
3338      tinst_klass = this_klass;
3339      this_klass = tmp;
3340      bool tmp2 = tinst_xk;
3341      tinst_xk = this_xk;
3342      this_xk = tmp2;
3343    }
3344    if (tinst_klass->is_interface() &&
3345        !(this_klass->is_interface() ||
3346          // Treat java/lang/Object as an honorary interface,
3347          // because we need a bottom for the interface hierarchy.
3348          this_klass == ciEnv::current()->Object_klass())) {
3349      // Oop meets interface!
3350
3351      // See if the oop subtypes (implements) interface.
3352      ciKlass *k;
3353      bool xk;
3354      if( this_klass->is_subtype_of( tinst_klass ) ) {
3355        // Oop indeed subtypes.  Now keep oop or interface depending
3356        // on whether we are both above the centerline or either is
3357        // below the centerline.  If we are on the centerline
3358        // (e.g., Constant vs. AnyNull interface), use the constant.
3359        k  = below_centerline(ptr) ? tinst_klass : this_klass;
3360        // If we are keeping this_klass, keep its exactness too.
3361        xk = below_centerline(ptr) ? tinst_xk    : this_xk;
3362      } else {                  // Does not implement, fall to Object
3363        // Oop does not implement interface, so mixing falls to Object
3364        // just like the verifier does (if both are above the
3365        // centerline fall to interface)
3366        k = above_centerline(ptr) ? tinst_klass : ciEnv::current()->Object_klass();
3367        xk = above_centerline(ptr) ? tinst_xk : false;
3368        // Watch out for Constant vs. AnyNull interface.
3369        if (ptr == Constant)  ptr = NotNull;   // forget it was a constant
3370        instance_id = InstanceBot;
3371      }
3372      ciObject* o = NULL;  // the Constant value, if any
3373      if (ptr == Constant) {
3374        // Find out which constant.
3375        o = (this_klass == klass()) ? const_oop() : tinst->const_oop();
3376      }
3377      return make(ptr, k, xk, o, off, instance_id, speculative);
3378    }
3379
3380    // Either oop vs oop or interface vs interface or interface vs Object
3381
3382    // !!! Here's how the symmetry requirement breaks down into invariants:
3383    // If we split one up & one down AND they subtype, take the down man.
3384    // If we split one up & one down AND they do NOT subtype, "fall hard".
3385    // If both are up and they subtype, take the subtype class.
3386    // If both are up and they do NOT subtype, "fall hard".
3387    // If both are down and they subtype, take the supertype class.
3388    // If both are down and they do NOT subtype, "fall hard".
3389    // Constants treated as down.
3390
3391    // Now, reorder the above list; observe that both-down+subtype is also
3392    // "fall hard"; "fall hard" becomes the default case:
3393    // If we split one up & one down AND they subtype, take the down man.
3394    // If both are up and they subtype, take the subtype class.
3395
3396    // If both are down and they subtype, "fall hard".
3397    // If both are down and they do NOT subtype, "fall hard".
3398    // If both are up and they do NOT subtype, "fall hard".
3399    // If we split one up & one down AND they do NOT subtype, "fall hard".
3400
3401    // If a proper subtype is exact, and we return it, we return it exactly.
3402    // If a proper supertype is exact, there can be no subtyping relationship!
3403    // If both types are equal to the subtype, exactness is and-ed below the
3404    // centerline and or-ed above it.  (N.B. Constants are always exact.)
3405
3406    // Check for subtyping:
3407    ciKlass *subtype = NULL;
3408    bool subtype_exact = false;
3409    if( tinst_klass->equals(this_klass) ) {
3410      subtype = this_klass;
3411      subtype_exact = below_centerline(ptr) ? (this_xk & tinst_xk) : (this_xk | tinst_xk);
3412    } else if( !tinst_xk && this_klass->is_subtype_of( tinst_klass ) ) {
3413      subtype = this_klass;     // Pick subtyping class
3414      subtype_exact = this_xk;
3415    } else if( !this_xk && tinst_klass->is_subtype_of( this_klass ) ) {
3416      subtype = tinst_klass;    // Pick subtyping class
3417      subtype_exact = tinst_xk;
3418    }
3419
3420    if( subtype ) {
3421      if( above_centerline(ptr) ) { // both are up?
3422        this_klass = tinst_klass = subtype;
3423        this_xk = tinst_xk = subtype_exact;
3424      } else if( above_centerline(this ->_ptr) && !above_centerline(tinst->_ptr) ) {
3425        this_klass = tinst_klass; // tinst is down; keep down man
3426        this_xk = tinst_xk;
3427      } else if( above_centerline(tinst->_ptr) && !above_centerline(this ->_ptr) ) {
3428        tinst_klass = this_klass; // this is down; keep down man
3429        tinst_xk = this_xk;
3430      } else {
3431        this_xk = subtype_exact;  // either they are equal, or we'll do an LCA
3432      }
3433    }
3434
3435    // Check for classes now being equal
3436    if (tinst_klass->equals(this_klass)) {
3437      // If the klasses are equal, the constants may still differ.  Fall to
3438      // NotNull if they do (neither constant is NULL; that is a special case
3439      // handled elsewhere).
3440      ciObject* o = NULL;             // Assume not constant when done
3441      ciObject* this_oop  = const_oop();
3442      ciObject* tinst_oop = tinst->const_oop();
3443      if( ptr == Constant ) {
3444        if (this_oop != NULL && tinst_oop != NULL &&
3445            this_oop->equals(tinst_oop) )
3446          o = this_oop;
3447        else if (above_centerline(this ->_ptr))
3448          o = tinst_oop;
3449        else if (above_centerline(tinst ->_ptr))
3450          o = this_oop;
3451        else
3452          ptr = NotNull;
3453      }
3454      return make(ptr, this_klass, this_xk, o, off, instance_id, speculative);
3455    } // Else classes are not equal
3456
3457    // Since klasses are different, we require a LCA in the Java
3458    // class hierarchy - which means we have to fall to at least NotNull.
3459    if( ptr == TopPTR || ptr == AnyNull || ptr == Constant )
3460      ptr = NotNull;
3461    instance_id = InstanceBot;
3462
3463    // Now we find the LCA of Java classes
3464    ciKlass* k = this_klass->least_common_ancestor(tinst_klass);
3465    return make(ptr, k, false, NULL, off, instance_id, speculative);
3466  } // End of case InstPtr
3467
3468  } // End of switch
3469  return this;                  // Return the double constant
3470}
3471
3472
3473//------------------------java_mirror_type--------------------------------------
3474ciType* TypeInstPtr::java_mirror_type() const {
3475  // must be a singleton type
3476  if( const_oop() == NULL )  return NULL;
3477
3478  // must be of type java.lang.Class
3479  if( klass() != ciEnv::current()->Class_klass() )  return NULL;
3480
3481  return const_oop()->as_instance()->java_mirror_type();
3482}
3483
3484
3485//------------------------------xdual------------------------------------------
3486// Dual: do NOT dual on klasses.  This means I do NOT understand the Java
3487// inheritance mechanism.
3488const Type *TypeInstPtr::xdual() const {
3489  return new TypeInstPtr(dual_ptr(), klass(), klass_is_exact(), const_oop(), dual_offset(), dual_instance_id(), dual_speculative());
3490}
3491
3492//------------------------------eq---------------------------------------------
3493// Structural equality check for Type representations
3494bool TypeInstPtr::eq( const Type *t ) const {
3495  const TypeInstPtr *p = t->is_instptr();
3496  return
3497    klass()->equals(p->klass()) &&
3498    TypeOopPtr::eq(p);          // Check sub-type stuff
3499}
3500
3501//------------------------------hash-------------------------------------------
3502// Type-specific hashing function.
3503int TypeInstPtr::hash(void) const {
3504  int hash = klass()->hash() + TypeOopPtr::hash();
3505  return hash;
3506}
3507
3508//------------------------------dump2------------------------------------------
3509// Dump oop Type
3510#ifndef PRODUCT
3511void TypeInstPtr::dump2( Dict &d, uint depth, outputStream *st ) const {
3512  // Print the name of the klass.
3513  klass()->print_name_on(st);
3514
3515  switch( _ptr ) {
3516  case Constant:
3517    // TO DO: Make CI print the hex address of the underlying oop.
3518    if (WizardMode || Verbose) {
3519      const_oop()->print_oop(st);
3520    }
3521  case BotPTR:
3522    if (!WizardMode && !Verbose) {
3523      if( _klass_is_exact ) st->print(":exact");
3524      break;
3525    }
3526  case TopPTR:
3527  case AnyNull:
3528  case NotNull:
3529    st->print(":%s", ptr_msg[_ptr]);
3530    if( _klass_is_exact ) st->print(":exact");
3531    break;
3532  }
3533
3534  if( _offset ) {               // Dump offset, if any
3535    if( _offset == OffsetBot )      st->print("+any");
3536    else if( _offset == OffsetTop ) st->print("+unknown");
3537    else st->print("+%d", _offset);
3538  }
3539
3540  st->print(" *");
3541  if (_instance_id == InstanceTop)
3542    st->print(",iid=top");
3543  else if (_instance_id != InstanceBot)
3544    st->print(",iid=%d",_instance_id);
3545
3546  dump_speculative(st);
3547}
3548#endif
3549
3550//------------------------------add_offset-------------------------------------
3551const TypePtr *TypeInstPtr::add_offset(intptr_t offset) const {
3552  return make(_ptr, klass(), klass_is_exact(), const_oop(), xadd_offset(offset), _instance_id, add_offset_speculative(offset));
3553}
3554
3555const TypeOopPtr *TypeInstPtr::remove_speculative() const {
3556  return make(_ptr, klass(), klass_is_exact(), const_oop(), _offset, _instance_id, NULL);
3557}
3558
3559//=============================================================================
3560// Convenience common pre-built types.
3561const TypeAryPtr *TypeAryPtr::RANGE;
3562const TypeAryPtr *TypeAryPtr::OOPS;
3563const TypeAryPtr *TypeAryPtr::NARROWOOPS;
3564const TypeAryPtr *TypeAryPtr::BYTES;
3565const TypeAryPtr *TypeAryPtr::SHORTS;
3566const TypeAryPtr *TypeAryPtr::CHARS;
3567const TypeAryPtr *TypeAryPtr::INTS;
3568const TypeAryPtr *TypeAryPtr::LONGS;
3569const TypeAryPtr *TypeAryPtr::FLOATS;
3570const TypeAryPtr *TypeAryPtr::DOUBLES;
3571
3572//------------------------------make-------------------------------------------
3573const TypeAryPtr *TypeAryPtr::make(PTR ptr, const TypeAry *ary, ciKlass* k, bool xk, int offset, int instance_id, const TypeOopPtr* speculative) {
3574  assert(!(k == NULL && ary->_elem->isa_int()),
3575         "integral arrays must be pre-equipped with a class");
3576  if (!xk)  xk = ary->ary_must_be_exact();
3577  assert(instance_id <= 0 || xk || !UseExactTypes, "instances are always exactly typed");
3578  if (!UseExactTypes)  xk = (ptr == Constant);
3579  return (TypeAryPtr*)(new TypeAryPtr(ptr, NULL, ary, k, xk, offset, instance_id, false, speculative))->hashcons();
3580}
3581
3582//------------------------------make-------------------------------------------
3583const TypeAryPtr *TypeAryPtr::make(PTR ptr, ciObject* o, const TypeAry *ary, ciKlass* k, bool xk, int offset, int instance_id, const TypeOopPtr* speculative, bool is_autobox_cache) {
3584  assert(!(k == NULL && ary->_elem->isa_int()),
3585         "integral arrays must be pre-equipped with a class");
3586  assert( (ptr==Constant && o) || (ptr!=Constant && !o), "" );
3587  if (!xk)  xk = (o != NULL) || ary->ary_must_be_exact();
3588  assert(instance_id <= 0 || xk || !UseExactTypes, "instances are always exactly typed");
3589  if (!UseExactTypes)  xk = (ptr == Constant);
3590  return (TypeAryPtr*)(new TypeAryPtr(ptr, o, ary, k, xk, offset, instance_id, is_autobox_cache, speculative))->hashcons();
3591}
3592
3593//------------------------------cast_to_ptr_type-------------------------------
3594const Type *TypeAryPtr::cast_to_ptr_type(PTR ptr) const {
3595  if( ptr == _ptr ) return this;
3596  return make(ptr, const_oop(), _ary, klass(), klass_is_exact(), _offset, _instance_id, _speculative);
3597}
3598
3599
3600//-----------------------------cast_to_exactness-------------------------------
3601const Type *TypeAryPtr::cast_to_exactness(bool klass_is_exact) const {
3602  if( klass_is_exact == _klass_is_exact ) return this;
3603  if (!UseExactTypes)  return this;
3604  if (_ary->ary_must_be_exact())  return this;  // cannot clear xk
3605  return make(ptr(), const_oop(), _ary, klass(), klass_is_exact, _offset, _instance_id, _speculative);
3606}
3607
3608//-----------------------------cast_to_instance_id----------------------------
3609const TypeOopPtr *TypeAryPtr::cast_to_instance_id(int instance_id) const {
3610  if( instance_id == _instance_id ) return this;
3611  return make(_ptr, const_oop(), _ary, klass(), _klass_is_exact, _offset, instance_id, _speculative);
3612}
3613
3614//-----------------------------narrow_size_type-------------------------------
3615// Local cache for arrayOopDesc::max_array_length(etype),
3616// which is kind of slow (and cached elsewhere by other users).
3617static jint max_array_length_cache[T_CONFLICT+1];
3618static jint max_array_length(BasicType etype) {
3619  jint& cache = max_array_length_cache[etype];
3620  jint res = cache;
3621  if (res == 0) {
3622    switch (etype) {
3623    case T_NARROWOOP:
3624      etype = T_OBJECT;
3625      break;
3626    case T_NARROWKLASS:
3627    case T_CONFLICT:
3628    case T_ILLEGAL:
3629    case T_VOID:
3630      etype = T_BYTE;           // will produce conservatively high value
3631    }
3632    cache = res = arrayOopDesc::max_array_length(etype);
3633  }
3634  return res;
3635}
3636
3637// Narrow the given size type to the index range for the given array base type.
3638// Return NULL if the resulting int type becomes empty.
3639const TypeInt* TypeAryPtr::narrow_size_type(const TypeInt* size) const {
3640  jint hi = size->_hi;
3641  jint lo = size->_lo;
3642  jint min_lo = 0;
3643  jint max_hi = max_array_length(elem()->basic_type());
3644  //if (index_not_size)  --max_hi;     // type of a valid array index, FTR
3645  bool chg = false;
3646  if (lo < min_lo) {
3647    lo = min_lo;
3648    if (size->is_con()) {
3649      hi = lo;
3650    }
3651    chg = true;
3652  }
3653  if (hi > max_hi) {
3654    hi = max_hi;
3655    if (size->is_con()) {
3656      lo = hi;
3657    }
3658    chg = true;
3659  }
3660  // Negative length arrays will produce weird intermediate dead fast-path code
3661  if (lo > hi)
3662    return TypeInt::ZERO;
3663  if (!chg)
3664    return size;
3665  return TypeInt::make(lo, hi, Type::WidenMin);
3666}
3667
3668//-------------------------------cast_to_size----------------------------------
3669const TypeAryPtr* TypeAryPtr::cast_to_size(const TypeInt* new_size) const {
3670  assert(new_size != NULL, "");
3671  new_size = narrow_size_type(new_size);
3672  if (new_size == size())  return this;
3673  const TypeAry* new_ary = TypeAry::make(elem(), new_size, is_stable());
3674  return make(ptr(), const_oop(), new_ary, klass(), klass_is_exact(), _offset, _instance_id, _speculative);
3675}
3676
3677
3678//------------------------------cast_to_stable---------------------------------
3679const TypeAryPtr* TypeAryPtr::cast_to_stable(bool stable, int stable_dimension) const {
3680  if (stable_dimension <= 0 || (stable_dimension == 1 && stable == this->is_stable()))
3681    return this;
3682
3683  const Type* elem = this->elem();
3684  const TypePtr* elem_ptr = elem->make_ptr();
3685
3686  if (stable_dimension > 1 && elem_ptr != NULL && elem_ptr->isa_aryptr()) {
3687    // If this is widened from a narrow oop, TypeAry::make will re-narrow it.
3688    elem = elem_ptr = elem_ptr->is_aryptr()->cast_to_stable(stable, stable_dimension - 1);
3689  }
3690
3691  const TypeAry* new_ary = TypeAry::make(elem, size(), stable);
3692
3693  return make(ptr(), const_oop(), new_ary, klass(), klass_is_exact(), _offset, _instance_id);
3694}
3695
3696//-----------------------------stable_dimension--------------------------------
3697int TypeAryPtr::stable_dimension() const {
3698  if (!is_stable())  return 0;
3699  int dim = 1;
3700  const TypePtr* elem_ptr = elem()->make_ptr();
3701  if (elem_ptr != NULL && elem_ptr->isa_aryptr())
3702    dim += elem_ptr->is_aryptr()->stable_dimension();
3703  return dim;
3704}
3705
3706//------------------------------eq---------------------------------------------
3707// Structural equality check for Type representations
3708bool TypeAryPtr::eq( const Type *t ) const {
3709  const TypeAryPtr *p = t->is_aryptr();
3710  return
3711    _ary == p->_ary &&  // Check array
3712    TypeOopPtr::eq(p);  // Check sub-parts
3713}
3714
3715//------------------------------hash-------------------------------------------
3716// Type-specific hashing function.
3717int TypeAryPtr::hash(void) const {
3718  return (intptr_t)_ary + TypeOopPtr::hash();
3719}
3720
3721//------------------------------meet-------------------------------------------
3722// Compute the MEET of two types.  It returns a new Type object.
3723const Type *TypeAryPtr::xmeet_helper(const Type *t) const {
3724  // Perform a fast test for common case; meeting the same types together.
3725  if( this == t ) return this;  // Meeting same type-rep?
3726  // Current "this->_base" is Pointer
3727  switch (t->base()) {          // switch on original type
3728
3729  // Mixing ints & oops happens when javac reuses local variables
3730  case Int:
3731  case Long:
3732  case FloatTop:
3733  case FloatCon:
3734  case FloatBot:
3735  case DoubleTop:
3736  case DoubleCon:
3737  case DoubleBot:
3738  case NarrowOop:
3739  case NarrowKlass:
3740  case Bottom:                  // Ye Olde Default
3741    return Type::BOTTOM;
3742  case Top:
3743    return this;
3744
3745  default:                      // All else is a mistake
3746    typerr(t);
3747
3748  case OopPtr: {                // Meeting to OopPtrs
3749    // Found a OopPtr type vs self-AryPtr type
3750    const TypeOopPtr *tp = t->is_oopptr();
3751    int offset = meet_offset(tp->offset());
3752    PTR ptr = meet_ptr(tp->ptr());
3753    switch (tp->ptr()) {
3754    case TopPTR:
3755    case AnyNull: {
3756      int instance_id = meet_instance_id(InstanceTop);
3757      const TypeOopPtr* speculative = meet_speculative(tp);
3758      return make(ptr, (ptr == Constant ? const_oop() : NULL),
3759                  _ary, _klass, _klass_is_exact, offset, instance_id, speculative);
3760    }
3761    case BotPTR:
3762    case NotNull: {
3763      int instance_id = meet_instance_id(tp->instance_id());
3764      const TypeOopPtr* speculative = meet_speculative(tp);
3765      return TypeOopPtr::make(ptr, offset, instance_id, speculative);
3766    }
3767    default: ShouldNotReachHere();
3768    }
3769  }
3770
3771  case AnyPtr: {                // Meeting two AnyPtrs
3772    // Found an AnyPtr type vs self-AryPtr type
3773    const TypePtr *tp = t->is_ptr();
3774    int offset = meet_offset(tp->offset());
3775    PTR ptr = meet_ptr(tp->ptr());
3776    switch (tp->ptr()) {
3777    case TopPTR:
3778      return this;
3779    case BotPTR:
3780    case NotNull:
3781      return TypePtr::make(AnyPtr, ptr, offset);
3782    case Null:
3783      if( ptr == Null ) return TypePtr::make(AnyPtr, ptr, offset);
3784      // else fall through to AnyNull
3785    case AnyNull: {
3786      int instance_id = meet_instance_id(InstanceTop);
3787      const TypeOopPtr* speculative = _speculative;
3788      return make(ptr, (ptr == Constant ? const_oop() : NULL),
3789                  _ary, _klass, _klass_is_exact, offset, instance_id, speculative);
3790    }
3791    default: ShouldNotReachHere();
3792    }
3793  }
3794
3795  case MetadataPtr:
3796  case KlassPtr:
3797  case RawPtr: return TypePtr::BOTTOM;
3798
3799  case AryPtr: {                // Meeting 2 references?
3800    const TypeAryPtr *tap = t->is_aryptr();
3801    int off = meet_offset(tap->offset());
3802    const TypeAry *tary = _ary->meet(tap->_ary)->is_ary();
3803    PTR ptr = meet_ptr(tap->ptr());
3804    int instance_id = meet_instance_id(tap->instance_id());
3805    const TypeOopPtr* speculative = meet_speculative(tap);
3806    ciKlass* lazy_klass = NULL;
3807    if (tary->_elem->isa_int()) {
3808      // Integral array element types have irrelevant lattice relations.
3809      // It is the klass that determines array layout, not the element type.
3810      if (_klass == NULL)
3811        lazy_klass = tap->_klass;
3812      else if (tap->_klass == NULL || tap->_klass == _klass) {
3813        lazy_klass = _klass;
3814      } else {
3815        // Something like byte[int+] meets char[int+].
3816        // This must fall to bottom, not (int[-128..65535])[int+].
3817        instance_id = InstanceBot;
3818        tary = TypeAry::make(Type::BOTTOM, tary->_size, tary->_stable);
3819      }
3820    } else // Non integral arrays.
3821      // Must fall to bottom if exact klasses in upper lattice
3822      // are not equal or super klass is exact.
3823      if ((above_centerline(ptr) || ptr == Constant) && klass() != tap->klass() &&
3824          // meet with top[] and bottom[] are processed further down:
3825          tap->_klass != NULL  && this->_klass != NULL   &&
3826          // both are exact and not equal:
3827          ((tap->_klass_is_exact && this->_klass_is_exact) ||
3828           // 'tap'  is exact and super or unrelated:
3829           (tap->_klass_is_exact && !tap->klass()->is_subtype_of(klass())) ||
3830           // 'this' is exact and super or unrelated:
3831           (this->_klass_is_exact && !klass()->is_subtype_of(tap->klass())))) {
3832      tary = TypeAry::make(Type::BOTTOM, tary->_size, tary->_stable);
3833      return make(NotNull, NULL, tary, lazy_klass, false, off, InstanceBot);
3834    }
3835
3836    bool xk = false;
3837    switch (tap->ptr()) {
3838    case AnyNull:
3839    case TopPTR:
3840      // Compute new klass on demand, do not use tap->_klass
3841      if (below_centerline(this->_ptr)) {
3842        xk = this->_klass_is_exact;
3843      } else {
3844        xk = (tap->_klass_is_exact | this->_klass_is_exact);
3845      }
3846      return make(ptr, const_oop(), tary, lazy_klass, xk, off, instance_id, speculative);
3847    case Constant: {
3848      ciObject* o = const_oop();
3849      if( _ptr == Constant ) {
3850        if( tap->const_oop() != NULL && !o->equals(tap->const_oop()) ) {
3851          xk = (klass() == tap->klass());
3852          ptr = NotNull;
3853          o = NULL;
3854          instance_id = InstanceBot;
3855        } else {
3856          xk = true;
3857        }
3858      } else if(above_centerline(_ptr)) {
3859        o = tap->const_oop();
3860        xk = true;
3861      } else {
3862        // Only precise for identical arrays
3863        xk = this->_klass_is_exact && (klass() == tap->klass());
3864      }
3865      return TypeAryPtr::make(ptr, o, tary, lazy_klass, xk, off, instance_id, speculative);
3866    }
3867    case NotNull:
3868    case BotPTR:
3869      // Compute new klass on demand, do not use tap->_klass
3870      if (above_centerline(this->_ptr))
3871            xk = tap->_klass_is_exact;
3872      else  xk = (tap->_klass_is_exact & this->_klass_is_exact) &&
3873              (klass() == tap->klass()); // Only precise for identical arrays
3874      return TypeAryPtr::make(ptr, NULL, tary, lazy_klass, xk, off, instance_id, speculative);
3875    default: ShouldNotReachHere();
3876    }
3877  }
3878
3879  // All arrays inherit from Object class
3880  case InstPtr: {
3881    const TypeInstPtr *tp = t->is_instptr();
3882    int offset = meet_offset(tp->offset());
3883    PTR ptr = meet_ptr(tp->ptr());
3884    int instance_id = meet_instance_id(tp->instance_id());
3885    const TypeOopPtr* speculative = meet_speculative(tp);
3886    switch (ptr) {
3887    case TopPTR:
3888    case AnyNull:                // Fall 'down' to dual of object klass
3889      // For instances when a subclass meets a superclass we fall
3890      // below the centerline when the superclass is exact. We need to
3891      // do the same here.
3892      if (tp->klass()->equals(ciEnv::current()->Object_klass()) && !tp->klass_is_exact()) {
3893        return TypeAryPtr::make(ptr, _ary, _klass, _klass_is_exact, offset, instance_id, speculative);
3894      } else {
3895        // cannot subclass, so the meet has to fall badly below the centerline
3896        ptr = NotNull;
3897        instance_id = InstanceBot;
3898        return TypeInstPtr::make(ptr, ciEnv::current()->Object_klass(), false, NULL,offset, instance_id, speculative);
3899      }
3900    case Constant:
3901    case NotNull:
3902    case BotPTR:                // Fall down to object klass
3903      // LCA is object_klass, but if we subclass from the top we can do better
3904      if (above_centerline(tp->ptr())) {
3905        // If 'tp'  is above the centerline and it is Object class
3906        // then we can subclass in the Java class hierarchy.
3907        // For instances when a subclass meets a superclass we fall
3908        // below the centerline when the superclass is exact. We need
3909        // to do the same here.
3910        if (tp->klass()->equals(ciEnv::current()->Object_klass()) && !tp->klass_is_exact()) {
3911          // that is, my array type is a subtype of 'tp' klass
3912          return make(ptr, (ptr == Constant ? const_oop() : NULL),
3913                      _ary, _klass, _klass_is_exact, offset, instance_id, speculative);
3914        }
3915      }
3916      // The other case cannot happen, since t cannot be a subtype of an array.
3917      // The meet falls down to Object class below centerline.
3918      if( ptr == Constant )
3919         ptr = NotNull;
3920      instance_id = InstanceBot;
3921      return TypeInstPtr::make(ptr, ciEnv::current()->Object_klass(), false, NULL,offset, instance_id, speculative);
3922    default: typerr(t);
3923    }
3924  }
3925  }
3926  return this;                  // Lint noise
3927}
3928
3929//------------------------------xdual------------------------------------------
3930// Dual: compute field-by-field dual
3931const Type *TypeAryPtr::xdual() const {
3932  return new TypeAryPtr(dual_ptr(), _const_oop, _ary->dual()->is_ary(),_klass, _klass_is_exact, dual_offset(), dual_instance_id(), is_autobox_cache(), dual_speculative());
3933}
3934
3935//----------------------interface_vs_oop---------------------------------------
3936#ifdef ASSERT
3937bool TypeAryPtr::interface_vs_oop(const Type *t) const {
3938  const TypeAryPtr* t_aryptr = t->isa_aryptr();
3939  if (t_aryptr) {
3940    return _ary->interface_vs_oop(t_aryptr->_ary);
3941  }
3942  return false;
3943}
3944#endif
3945
3946//------------------------------dump2------------------------------------------
3947#ifndef PRODUCT
3948void TypeAryPtr::dump2( Dict &d, uint depth, outputStream *st ) const {
3949  _ary->dump2(d,depth,st);
3950  switch( _ptr ) {
3951  case Constant:
3952    const_oop()->print(st);
3953    break;
3954  case BotPTR:
3955    if (!WizardMode && !Verbose) {
3956      if( _klass_is_exact ) st->print(":exact");
3957      break;
3958    }
3959  case TopPTR:
3960  case AnyNull:
3961  case NotNull:
3962    st->print(":%s", ptr_msg[_ptr]);
3963    if( _klass_is_exact ) st->print(":exact");
3964    break;
3965  }
3966
3967  if( _offset != 0 ) {
3968    int header_size = objArrayOopDesc::header_size() * wordSize;
3969    if( _offset == OffsetTop )       st->print("+undefined");
3970    else if( _offset == OffsetBot )  st->print("+any");
3971    else if( _offset < header_size ) st->print("+%d", _offset);
3972    else {
3973      BasicType basic_elem_type = elem()->basic_type();
3974      int array_base = arrayOopDesc::base_offset_in_bytes(basic_elem_type);
3975      int elem_size = type2aelembytes(basic_elem_type);
3976      st->print("[%d]", (_offset - array_base)/elem_size);
3977    }
3978  }
3979  st->print(" *");
3980  if (_instance_id == InstanceTop)
3981    st->print(",iid=top");
3982  else if (_instance_id != InstanceBot)
3983    st->print(",iid=%d",_instance_id);
3984
3985  dump_speculative(st);
3986}
3987#endif
3988
3989bool TypeAryPtr::empty(void) const {
3990  if (_ary->empty())       return true;
3991  return TypeOopPtr::empty();
3992}
3993
3994//------------------------------add_offset-------------------------------------
3995const TypePtr *TypeAryPtr::add_offset(intptr_t offset) const {
3996  return make(_ptr, _const_oop, _ary, _klass, _klass_is_exact, xadd_offset(offset), _instance_id, add_offset_speculative(offset));
3997}
3998
3999const TypeOopPtr *TypeAryPtr::remove_speculative() const {
4000  return make(_ptr, _const_oop, _ary, _klass, _klass_is_exact, _offset, _instance_id, NULL);
4001}
4002
4003//=============================================================================
4004
4005//------------------------------hash-------------------------------------------
4006// Type-specific hashing function.
4007int TypeNarrowPtr::hash(void) const {
4008  return _ptrtype->hash() + 7;
4009}
4010
4011bool TypeNarrowPtr::singleton(void) const {    // TRUE if type is a singleton
4012  return _ptrtype->singleton();
4013}
4014
4015bool TypeNarrowPtr::empty(void) const {
4016  return _ptrtype->empty();
4017}
4018
4019intptr_t TypeNarrowPtr::get_con() const {
4020  return _ptrtype->get_con();
4021}
4022
4023bool TypeNarrowPtr::eq( const Type *t ) const {
4024  const TypeNarrowPtr* tc = isa_same_narrowptr(t);
4025  if (tc != NULL) {
4026    if (_ptrtype->base() != tc->_ptrtype->base()) {
4027      return false;
4028    }
4029    return tc->_ptrtype->eq(_ptrtype);
4030  }
4031  return false;
4032}
4033
4034const Type *TypeNarrowPtr::xdual() const {    // Compute dual right now.
4035  const TypePtr* odual = _ptrtype->dual()->is_ptr();
4036  return make_same_narrowptr(odual);
4037}
4038
4039
4040const Type *TypeNarrowPtr::filter( const Type *kills ) const {
4041  if (isa_same_narrowptr(kills)) {
4042    const Type* ft =_ptrtype->filter(is_same_narrowptr(kills)->_ptrtype);
4043    if (ft->empty())
4044      return Type::TOP;           // Canonical empty value
4045    if (ft->isa_ptr()) {
4046      return make_hash_same_narrowptr(ft->isa_ptr());
4047    }
4048    return ft;
4049  } else if (kills->isa_ptr()) {
4050    const Type* ft = _ptrtype->join(kills);
4051    if (ft->empty())
4052      return Type::TOP;           // Canonical empty value
4053    return ft;
4054  } else {
4055    return Type::TOP;
4056  }
4057}
4058
4059//------------------------------xmeet------------------------------------------
4060// Compute the MEET of two types.  It returns a new Type object.
4061const Type *TypeNarrowPtr::xmeet( const Type *t ) const {
4062  // Perform a fast test for common case; meeting the same types together.
4063  if( this == t ) return this;  // Meeting same type-rep?
4064
4065  if (t->base() == base()) {
4066    const Type* result = _ptrtype->xmeet(t->make_ptr());
4067    if (result->isa_ptr()) {
4068      return make_hash_same_narrowptr(result->is_ptr());
4069    }
4070    return result;
4071  }
4072
4073  // Current "this->_base" is NarrowKlass or NarrowOop
4074  switch (t->base()) {          // switch on original type
4075
4076  case Int:                     // Mixing ints & oops happens when javac
4077  case Long:                    // reuses local variables
4078  case FloatTop:
4079  case FloatCon:
4080  case FloatBot:
4081  case DoubleTop:
4082  case DoubleCon:
4083  case DoubleBot:
4084  case AnyPtr:
4085  case RawPtr:
4086  case OopPtr:
4087  case InstPtr:
4088  case AryPtr:
4089  case MetadataPtr:
4090  case KlassPtr:
4091  case NarrowOop:
4092  case NarrowKlass:
4093
4094  case Bottom:                  // Ye Olde Default
4095    return Type::BOTTOM;
4096  case Top:
4097    return this;
4098
4099  default:                      // All else is a mistake
4100    typerr(t);
4101
4102  } // End of switch
4103
4104  return this;
4105}
4106
4107#ifndef PRODUCT
4108void TypeNarrowPtr::dump2( Dict & d, uint depth, outputStream *st ) const {
4109  _ptrtype->dump2(d, depth, st);
4110}
4111#endif
4112
4113const TypeNarrowOop *TypeNarrowOop::BOTTOM;
4114const TypeNarrowOop *TypeNarrowOop::NULL_PTR;
4115
4116
4117const TypeNarrowOop* TypeNarrowOop::make(const TypePtr* type) {
4118  return (const TypeNarrowOop*)(new TypeNarrowOop(type))->hashcons();
4119}
4120
4121
4122#ifndef PRODUCT
4123void TypeNarrowOop::dump2( Dict & d, uint depth, outputStream *st ) const {
4124  st->print("narrowoop: ");
4125  TypeNarrowPtr::dump2(d, depth, st);
4126}
4127#endif
4128
4129const TypeNarrowKlass *TypeNarrowKlass::NULL_PTR;
4130
4131const TypeNarrowKlass* TypeNarrowKlass::make(const TypePtr* type) {
4132  return (const TypeNarrowKlass*)(new TypeNarrowKlass(type))->hashcons();
4133}
4134
4135#ifndef PRODUCT
4136void TypeNarrowKlass::dump2( Dict & d, uint depth, outputStream *st ) const {
4137  st->print("narrowklass: ");
4138  TypeNarrowPtr::dump2(d, depth, st);
4139}
4140#endif
4141
4142
4143//------------------------------eq---------------------------------------------
4144// Structural equality check for Type representations
4145bool TypeMetadataPtr::eq( const Type *t ) const {
4146  const TypeMetadataPtr *a = (const TypeMetadataPtr*)t;
4147  ciMetadata* one = metadata();
4148  ciMetadata* two = a->metadata();
4149  if (one == NULL || two == NULL) {
4150    return (one == two) && TypePtr::eq(t);
4151  } else {
4152    return one->equals(two) && TypePtr::eq(t);
4153  }
4154}
4155
4156//------------------------------hash-------------------------------------------
4157// Type-specific hashing function.
4158int TypeMetadataPtr::hash(void) const {
4159  return
4160    (metadata() ? metadata()->hash() : 0) +
4161    TypePtr::hash();
4162}
4163
4164//------------------------------singleton--------------------------------------
4165// TRUE if Type is a singleton type, FALSE otherwise.   Singletons are simple
4166// constants
4167bool TypeMetadataPtr::singleton(void) const {
4168  // detune optimizer to not generate constant metadta + constant offset as a constant!
4169  // TopPTR, Null, AnyNull, Constant are all singletons
4170  return (_offset == 0) && !below_centerline(_ptr);
4171}
4172
4173//------------------------------add_offset-------------------------------------
4174const TypePtr *TypeMetadataPtr::add_offset( intptr_t offset ) const {
4175  return make( _ptr, _metadata, xadd_offset(offset));
4176}
4177
4178//-----------------------------filter------------------------------------------
4179// Do not allow interface-vs.-noninterface joins to collapse to top.
4180const Type *TypeMetadataPtr::filter( const Type *kills ) const {
4181  const TypeMetadataPtr* ft = join(kills)->isa_metadataptr();
4182  if (ft == NULL || ft->empty())
4183    return Type::TOP;           // Canonical empty value
4184  return ft;
4185}
4186
4187 //------------------------------get_con----------------------------------------
4188intptr_t TypeMetadataPtr::get_con() const {
4189  assert( _ptr == Null || _ptr == Constant, "" );
4190  assert( _offset >= 0, "" );
4191
4192  if (_offset != 0) {
4193    // After being ported to the compiler interface, the compiler no longer
4194    // directly manipulates the addresses of oops.  Rather, it only has a pointer
4195    // to a handle at compile time.  This handle is embedded in the generated
4196    // code and dereferenced at the time the nmethod is made.  Until that time,
4197    // it is not reasonable to do arithmetic with the addresses of oops (we don't
4198    // have access to the addresses!).  This does not seem to currently happen,
4199    // but this assertion here is to help prevent its occurence.
4200    tty->print_cr("Found oop constant with non-zero offset");
4201    ShouldNotReachHere();
4202  }
4203
4204  return (intptr_t)metadata()->constant_encoding();
4205}
4206
4207//------------------------------cast_to_ptr_type-------------------------------
4208const Type *TypeMetadataPtr::cast_to_ptr_type(PTR ptr) const {
4209  if( ptr == _ptr ) return this;
4210  return make(ptr, metadata(), _offset);
4211}
4212
4213//------------------------------meet-------------------------------------------
4214// Compute the MEET of two types.  It returns a new Type object.
4215const Type *TypeMetadataPtr::xmeet( const Type *t ) const {
4216  // Perform a fast test for common case; meeting the same types together.
4217  if( this == t ) return this;  // Meeting same type-rep?
4218
4219  // Current "this->_base" is OopPtr
4220  switch (t->base()) {          // switch on original type
4221
4222  case Int:                     // Mixing ints & oops happens when javac
4223  case Long:                    // reuses local variables
4224  case FloatTop:
4225  case FloatCon:
4226  case FloatBot:
4227  case DoubleTop:
4228  case DoubleCon:
4229  case DoubleBot:
4230  case NarrowOop:
4231  case NarrowKlass:
4232  case Bottom:                  // Ye Olde Default
4233    return Type::BOTTOM;
4234  case Top:
4235    return this;
4236
4237  default:                      // All else is a mistake
4238    typerr(t);
4239
4240  case AnyPtr: {
4241    // Found an AnyPtr type vs self-OopPtr type
4242    const TypePtr *tp = t->is_ptr();
4243    int offset = meet_offset(tp->offset());
4244    PTR ptr = meet_ptr(tp->ptr());
4245    switch (tp->ptr()) {
4246    case Null:
4247      if (ptr == Null)  return TypePtr::make(AnyPtr, ptr, offset);
4248      // else fall through:
4249    case TopPTR:
4250    case AnyNull: {
4251      return make(ptr, NULL, offset);
4252    }
4253    case BotPTR:
4254    case NotNull:
4255      return TypePtr::make(AnyPtr, ptr, offset);
4256    default: typerr(t);
4257    }
4258  }
4259
4260  case RawPtr:
4261  case KlassPtr:
4262  case OopPtr:
4263  case InstPtr:
4264  case AryPtr:
4265    return TypePtr::BOTTOM;     // Oop meet raw is not well defined
4266
4267  case MetadataPtr: {
4268    const TypeMetadataPtr *tp = t->is_metadataptr();
4269    int offset = meet_offset(tp->offset());
4270    PTR tptr = tp->ptr();
4271    PTR ptr = meet_ptr(tptr);
4272    ciMetadata* md = (tptr == TopPTR) ? metadata() : tp->metadata();
4273    if (tptr == TopPTR || _ptr == TopPTR ||
4274        metadata()->equals(tp->metadata())) {
4275      return make(ptr, md, offset);
4276    }
4277    // metadata is different
4278    if( ptr == Constant ) {  // Cannot be equal constants, so...
4279      if( tptr == Constant && _ptr != Constant)  return t;
4280      if( _ptr == Constant && tptr != Constant)  return this;
4281      ptr = NotNull;            // Fall down in lattice
4282    }
4283    return make(ptr, NULL, offset);
4284    break;
4285  }
4286  } // End of switch
4287  return this;                  // Return the double constant
4288}
4289
4290
4291//------------------------------xdual------------------------------------------
4292// Dual of a pure metadata pointer.
4293const Type *TypeMetadataPtr::xdual() const {
4294  return new TypeMetadataPtr(dual_ptr(), metadata(), dual_offset());
4295}
4296
4297//------------------------------dump2------------------------------------------
4298#ifndef PRODUCT
4299void TypeMetadataPtr::dump2( Dict &d, uint depth, outputStream *st ) const {
4300  st->print("metadataptr:%s", ptr_msg[_ptr]);
4301  if( metadata() ) st->print(INTPTR_FORMAT, metadata());
4302  switch( _offset ) {
4303  case OffsetTop: st->print("+top"); break;
4304  case OffsetBot: st->print("+any"); break;
4305  case         0: break;
4306  default:        st->print("+%d",_offset); break;
4307  }
4308}
4309#endif
4310
4311
4312//=============================================================================
4313// Convenience common pre-built type.
4314const TypeMetadataPtr *TypeMetadataPtr::BOTTOM;
4315
4316TypeMetadataPtr::TypeMetadataPtr(PTR ptr, ciMetadata* metadata, int offset):
4317  TypePtr(MetadataPtr, ptr, offset), _metadata(metadata) {
4318}
4319
4320const TypeMetadataPtr* TypeMetadataPtr::make(ciMethod* m) {
4321  return make(Constant, m, 0);
4322}
4323const TypeMetadataPtr* TypeMetadataPtr::make(ciMethodData* m) {
4324  return make(Constant, m, 0);
4325}
4326
4327//------------------------------make-------------------------------------------
4328// Create a meta data constant
4329const TypeMetadataPtr *TypeMetadataPtr::make(PTR ptr, ciMetadata* m, int offset) {
4330  assert(m == NULL || !m->is_klass(), "wrong type");
4331  return (TypeMetadataPtr*)(new TypeMetadataPtr(ptr, m, offset))->hashcons();
4332}
4333
4334
4335//=============================================================================
4336// Convenience common pre-built types.
4337
4338// Not-null object klass or below
4339const TypeKlassPtr *TypeKlassPtr::OBJECT;
4340const TypeKlassPtr *TypeKlassPtr::OBJECT_OR_NULL;
4341
4342//------------------------------TypeKlassPtr-----------------------------------
4343TypeKlassPtr::TypeKlassPtr( PTR ptr, ciKlass* klass, int offset )
4344  : TypePtr(KlassPtr, ptr, offset), _klass(klass), _klass_is_exact(ptr == Constant) {
4345}
4346
4347//------------------------------make-------------------------------------------
4348// ptr to klass 'k', if Constant, or possibly to a sub-klass if not a Constant
4349const TypeKlassPtr *TypeKlassPtr::make( PTR ptr, ciKlass* k, int offset ) {
4350  assert( k != NULL, "Expect a non-NULL klass");
4351  assert(k->is_instance_klass() || k->is_array_klass(), "Incorrect type of klass oop");
4352  TypeKlassPtr *r =
4353    (TypeKlassPtr*)(new TypeKlassPtr(ptr, k, offset))->hashcons();
4354
4355  return r;
4356}
4357
4358//------------------------------eq---------------------------------------------
4359// Structural equality check for Type representations
4360bool TypeKlassPtr::eq( const Type *t ) const {
4361  const TypeKlassPtr *p = t->is_klassptr();
4362  return
4363    klass()->equals(p->klass()) &&
4364    TypePtr::eq(p);
4365}
4366
4367//------------------------------hash-------------------------------------------
4368// Type-specific hashing function.
4369int TypeKlassPtr::hash(void) const {
4370  return klass()->hash() + TypePtr::hash();
4371}
4372
4373//------------------------------singleton--------------------------------------
4374// TRUE if Type is a singleton type, FALSE otherwise.   Singletons are simple
4375// constants
4376bool TypeKlassPtr::singleton(void) const {
4377  // detune optimizer to not generate constant klass + constant offset as a constant!
4378  // TopPTR, Null, AnyNull, Constant are all singletons
4379  return (_offset == 0) && !below_centerline(_ptr);
4380}
4381
4382// Do not allow interface-vs.-noninterface joins to collapse to top.
4383const Type *TypeKlassPtr::filter(const Type *kills) const {
4384  // logic here mirrors the one from TypeOopPtr::filter. See comments
4385  // there.
4386  const Type* ft = join(kills);
4387  const TypeKlassPtr* ftkp = ft->isa_klassptr();
4388  const TypeKlassPtr* ktkp = kills->isa_klassptr();
4389
4390  if (ft->empty()) {
4391    if (!empty() && ktkp != NULL && ktkp->klass()->is_loaded() && ktkp->klass()->is_interface())
4392      return kills;             // Uplift to interface
4393
4394    return Type::TOP;           // Canonical empty value
4395  }
4396
4397  // Interface klass type could be exact in opposite to interface type,
4398  // return it here instead of incorrect Constant ptr J/L/Object (6894807).
4399  if (ftkp != NULL && ktkp != NULL &&
4400      ftkp->is_loaded() &&  ftkp->klass()->is_interface() &&
4401      !ftkp->klass_is_exact() && // Keep exact interface klass
4402      ktkp->is_loaded() && !ktkp->klass()->is_interface()) {
4403    return ktkp->cast_to_ptr_type(ftkp->ptr());
4404  }
4405
4406  return ft;
4407}
4408
4409//----------------------compute_klass------------------------------------------
4410// Compute the defining klass for this class
4411ciKlass* TypeAryPtr::compute_klass(DEBUG_ONLY(bool verify)) const {
4412  // Compute _klass based on element type.
4413  ciKlass* k_ary = NULL;
4414  const TypeInstPtr *tinst;
4415  const TypeAryPtr *tary;
4416  const Type* el = elem();
4417  if (el->isa_narrowoop()) {
4418    el = el->make_ptr();
4419  }
4420
4421  // Get element klass
4422  if ((tinst = el->isa_instptr()) != NULL) {
4423    // Compute array klass from element klass
4424    k_ary = ciObjArrayKlass::make(tinst->klass());
4425  } else if ((tary = el->isa_aryptr()) != NULL) {
4426    // Compute array klass from element klass
4427    ciKlass* k_elem = tary->klass();
4428    // If element type is something like bottom[], k_elem will be null.
4429    if (k_elem != NULL)
4430      k_ary = ciObjArrayKlass::make(k_elem);
4431  } else if ((el->base() == Type::Top) ||
4432             (el->base() == Type::Bottom)) {
4433    // element type of Bottom occurs from meet of basic type
4434    // and object; Top occurs when doing join on Bottom.
4435    // Leave k_ary at NULL.
4436  } else {
4437    // Cannot compute array klass directly from basic type,
4438    // since subtypes of TypeInt all have basic type T_INT.
4439#ifdef ASSERT
4440    if (verify && el->isa_int()) {
4441      // Check simple cases when verifying klass.
4442      BasicType bt = T_ILLEGAL;
4443      if (el == TypeInt::BYTE) {
4444        bt = T_BYTE;
4445      } else if (el == TypeInt::SHORT) {
4446        bt = T_SHORT;
4447      } else if (el == TypeInt::CHAR) {
4448        bt = T_CHAR;
4449      } else if (el == TypeInt::INT) {
4450        bt = T_INT;
4451      } else {
4452        return _klass; // just return specified klass
4453      }
4454      return ciTypeArrayKlass::make(bt);
4455    }
4456#endif
4457    assert(!el->isa_int(),
4458           "integral arrays must be pre-equipped with a class");
4459    // Compute array klass directly from basic type
4460    k_ary = ciTypeArrayKlass::make(el->basic_type());
4461  }
4462  return k_ary;
4463}
4464
4465//------------------------------klass------------------------------------------
4466// Return the defining klass for this class
4467ciKlass* TypeAryPtr::klass() const {
4468  if( _klass ) return _klass;   // Return cached value, if possible
4469
4470  // Oops, need to compute _klass and cache it
4471  ciKlass* k_ary = compute_klass();
4472
4473  if( this != TypeAryPtr::OOPS && this->dual() != TypeAryPtr::OOPS ) {
4474    // The _klass field acts as a cache of the underlying
4475    // ciKlass for this array type.  In order to set the field,
4476    // we need to cast away const-ness.
4477    //
4478    // IMPORTANT NOTE: we *never* set the _klass field for the
4479    // type TypeAryPtr::OOPS.  This Type is shared between all
4480    // active compilations.  However, the ciKlass which represents
4481    // this Type is *not* shared between compilations, so caching
4482    // this value would result in fetching a dangling pointer.
4483    //
4484    // Recomputing the underlying ciKlass for each request is
4485    // a bit less efficient than caching, but calls to
4486    // TypeAryPtr::OOPS->klass() are not common enough to matter.
4487    ((TypeAryPtr*)this)->_klass = k_ary;
4488    if (UseCompressedOops && k_ary != NULL && k_ary->is_obj_array_klass() &&
4489        _offset != 0 && _offset != arrayOopDesc::length_offset_in_bytes()) {
4490      ((TypeAryPtr*)this)->_is_ptr_to_narrowoop = true;
4491    }
4492  }
4493  return k_ary;
4494}
4495
4496
4497//------------------------------add_offset-------------------------------------
4498// Access internals of klass object
4499const TypePtr *TypeKlassPtr::add_offset( intptr_t offset ) const {
4500  return make( _ptr, klass(), xadd_offset(offset) );
4501}
4502
4503//------------------------------cast_to_ptr_type-------------------------------
4504const Type *TypeKlassPtr::cast_to_ptr_type(PTR ptr) const {
4505  assert(_base == KlassPtr, "subclass must override cast_to_ptr_type");
4506  if( ptr == _ptr ) return this;
4507  return make(ptr, _klass, _offset);
4508}
4509
4510
4511//-----------------------------cast_to_exactness-------------------------------
4512const Type *TypeKlassPtr::cast_to_exactness(bool klass_is_exact) const {
4513  if( klass_is_exact == _klass_is_exact ) return this;
4514  if (!UseExactTypes)  return this;
4515  return make(klass_is_exact ? Constant : NotNull, _klass, _offset);
4516}
4517
4518
4519//-----------------------------as_instance_type--------------------------------
4520// Corresponding type for an instance of the given class.
4521// It will be NotNull, and exact if and only if the klass type is exact.
4522const TypeOopPtr* TypeKlassPtr::as_instance_type() const {
4523  ciKlass* k = klass();
4524  bool    xk = klass_is_exact();
4525  //return TypeInstPtr::make(TypePtr::NotNull, k, xk, NULL, 0);
4526  const TypeOopPtr* toop = TypeOopPtr::make_from_klass_raw(k);
4527  guarantee(toop != NULL, "need type for given klass");
4528  toop = toop->cast_to_ptr_type(TypePtr::NotNull)->is_oopptr();
4529  return toop->cast_to_exactness(xk)->is_oopptr();
4530}
4531
4532
4533//------------------------------xmeet------------------------------------------
4534// Compute the MEET of two types, return a new Type object.
4535const Type    *TypeKlassPtr::xmeet( const Type *t ) const {
4536  // Perform a fast test for common case; meeting the same types together.
4537  if( this == t ) return this;  // Meeting same type-rep?
4538
4539  // Current "this->_base" is Pointer
4540  switch (t->base()) {          // switch on original type
4541
4542  case Int:                     // Mixing ints & oops happens when javac
4543  case Long:                    // reuses local variables
4544  case FloatTop:
4545  case FloatCon:
4546  case FloatBot:
4547  case DoubleTop:
4548  case DoubleCon:
4549  case DoubleBot:
4550  case NarrowOop:
4551  case NarrowKlass:
4552  case Bottom:                  // Ye Olde Default
4553    return Type::BOTTOM;
4554  case Top:
4555    return this;
4556
4557  default:                      // All else is a mistake
4558    typerr(t);
4559
4560  case AnyPtr: {                // Meeting to AnyPtrs
4561    // Found an AnyPtr type vs self-KlassPtr type
4562    const TypePtr *tp = t->is_ptr();
4563    int offset = meet_offset(tp->offset());
4564    PTR ptr = meet_ptr(tp->ptr());
4565    switch (tp->ptr()) {
4566    case TopPTR:
4567      return this;
4568    case Null:
4569      if( ptr == Null ) return TypePtr::make( AnyPtr, ptr, offset );
4570    case AnyNull:
4571      return make( ptr, klass(), offset );
4572    case BotPTR:
4573    case NotNull:
4574      return TypePtr::make(AnyPtr, ptr, offset);
4575    default: typerr(t);
4576    }
4577  }
4578
4579  case RawPtr:
4580  case MetadataPtr:
4581  case OopPtr:
4582  case AryPtr:                  // Meet with AryPtr
4583  case InstPtr:                 // Meet with InstPtr
4584    return TypePtr::BOTTOM;
4585
4586  //
4587  //             A-top         }
4588  //           /   |   \       }  Tops
4589  //       B-top A-any C-top   }
4590  //          | /  |  \ |      }  Any-nulls
4591  //       B-any   |   C-any   }
4592  //          |    |    |
4593  //       B-con A-con C-con   } constants; not comparable across classes
4594  //          |    |    |
4595  //       B-not   |   C-not   }
4596  //          | \  |  / |      }  not-nulls
4597  //       B-bot A-not C-bot   }
4598  //           \   |   /       }  Bottoms
4599  //             A-bot         }
4600  //
4601
4602  case KlassPtr: {  // Meet two KlassPtr types
4603    const TypeKlassPtr *tkls = t->is_klassptr();
4604    int  off     = meet_offset(tkls->offset());
4605    PTR  ptr     = meet_ptr(tkls->ptr());
4606
4607    // Check for easy case; klasses are equal (and perhaps not loaded!)
4608    // If we have constants, then we created oops so classes are loaded
4609    // and we can handle the constants further down.  This case handles
4610    // not-loaded classes
4611    if( ptr != Constant && tkls->klass()->equals(klass()) ) {
4612      return make( ptr, klass(), off );
4613    }
4614
4615    // Classes require inspection in the Java klass hierarchy.  Must be loaded.
4616    ciKlass* tkls_klass = tkls->klass();
4617    ciKlass* this_klass = this->klass();
4618    assert( tkls_klass->is_loaded(), "This class should have been loaded.");
4619    assert( this_klass->is_loaded(), "This class should have been loaded.");
4620
4621    // If 'this' type is above the centerline and is a superclass of the
4622    // other, we can treat 'this' as having the same type as the other.
4623    if ((above_centerline(this->ptr())) &&
4624        tkls_klass->is_subtype_of(this_klass)) {
4625      this_klass = tkls_klass;
4626    }
4627    // If 'tinst' type is above the centerline and is a superclass of the
4628    // other, we can treat 'tinst' as having the same type as the other.
4629    if ((above_centerline(tkls->ptr())) &&
4630        this_klass->is_subtype_of(tkls_klass)) {
4631      tkls_klass = this_klass;
4632    }
4633
4634    // Check for classes now being equal
4635    if (tkls_klass->equals(this_klass)) {
4636      // If the klasses are equal, the constants may still differ.  Fall to
4637      // NotNull if they do (neither constant is NULL; that is a special case
4638      // handled elsewhere).
4639      if( ptr == Constant ) {
4640        if (this->_ptr == Constant && tkls->_ptr == Constant &&
4641            this->klass()->equals(tkls->klass()));
4642        else if (above_centerline(this->ptr()));
4643        else if (above_centerline(tkls->ptr()));
4644        else
4645          ptr = NotNull;
4646      }
4647      return make( ptr, this_klass, off );
4648    } // Else classes are not equal
4649
4650    // Since klasses are different, we require the LCA in the Java
4651    // class hierarchy - which means we have to fall to at least NotNull.
4652    if( ptr == TopPTR || ptr == AnyNull || ptr == Constant )
4653      ptr = NotNull;
4654    // Now we find the LCA of Java classes
4655    ciKlass* k = this_klass->least_common_ancestor(tkls_klass);
4656    return   make( ptr, k, off );
4657  } // End of case KlassPtr
4658
4659  } // End of switch
4660  return this;                  // Return the double constant
4661}
4662
4663//------------------------------xdual------------------------------------------
4664// Dual: compute field-by-field dual
4665const Type    *TypeKlassPtr::xdual() const {
4666  return new TypeKlassPtr( dual_ptr(), klass(), dual_offset() );
4667}
4668
4669//------------------------------get_con----------------------------------------
4670intptr_t TypeKlassPtr::get_con() const {
4671  assert( _ptr == Null || _ptr == Constant, "" );
4672  assert( _offset >= 0, "" );
4673
4674  if (_offset != 0) {
4675    // After being ported to the compiler interface, the compiler no longer
4676    // directly manipulates the addresses of oops.  Rather, it only has a pointer
4677    // to a handle at compile time.  This handle is embedded in the generated
4678    // code and dereferenced at the time the nmethod is made.  Until that time,
4679    // it is not reasonable to do arithmetic with the addresses of oops (we don't
4680    // have access to the addresses!).  This does not seem to currently happen,
4681    // but this assertion here is to help prevent its occurence.
4682    tty->print_cr("Found oop constant with non-zero offset");
4683    ShouldNotReachHere();
4684  }
4685
4686  return (intptr_t)klass()->constant_encoding();
4687}
4688//------------------------------dump2------------------------------------------
4689// Dump Klass Type
4690#ifndef PRODUCT
4691void TypeKlassPtr::dump2( Dict & d, uint depth, outputStream *st ) const {
4692  switch( _ptr ) {
4693  case Constant:
4694    st->print("precise ");
4695  case NotNull:
4696    {
4697      const char *name = klass()->name()->as_utf8();
4698      if( name ) {
4699        st->print("klass %s: " INTPTR_FORMAT, name, klass());
4700      } else {
4701        ShouldNotReachHere();
4702      }
4703    }
4704  case BotPTR:
4705    if( !WizardMode && !Verbose && !_klass_is_exact ) break;
4706  case TopPTR:
4707  case AnyNull:
4708    st->print(":%s", ptr_msg[_ptr]);
4709    if( _klass_is_exact ) st->print(":exact");
4710    break;
4711  }
4712
4713  if( _offset ) {               // Dump offset, if any
4714    if( _offset == OffsetBot )      { st->print("+any"); }
4715    else if( _offset == OffsetTop ) { st->print("+unknown"); }
4716    else                            { st->print("+%d", _offset); }
4717  }
4718
4719  st->print(" *");
4720}
4721#endif
4722
4723
4724
4725//=============================================================================
4726// Convenience common pre-built types.
4727
4728//------------------------------make-------------------------------------------
4729const TypeFunc *TypeFunc::make( const TypeTuple *domain, const TypeTuple *range ) {
4730  return (TypeFunc*)(new TypeFunc(domain,range))->hashcons();
4731}
4732
4733//------------------------------make-------------------------------------------
4734const TypeFunc *TypeFunc::make(ciMethod* method) {
4735  Compile* C = Compile::current();
4736  const TypeFunc* tf = C->last_tf(method); // check cache
4737  if (tf != NULL)  return tf;  // The hit rate here is almost 50%.
4738  const TypeTuple *domain;
4739  if (method->is_static()) {
4740    domain = TypeTuple::make_domain(NULL, method->signature());
4741  } else {
4742    domain = TypeTuple::make_domain(method->holder(), method->signature());
4743  }
4744  const TypeTuple *range  = TypeTuple::make_range(method->signature());
4745  tf = TypeFunc::make(domain, range);
4746  C->set_last_tf(method, tf);  // fill cache
4747  return tf;
4748}
4749
4750//------------------------------meet-------------------------------------------
4751// Compute the MEET of two types.  It returns a new Type object.
4752const Type *TypeFunc::xmeet( const Type *t ) const {
4753  // Perform a fast test for common case; meeting the same types together.
4754  if( this == t ) return this;  // Meeting same type-rep?
4755
4756  // Current "this->_base" is Func
4757  switch (t->base()) {          // switch on original type
4758
4759  case Bottom:                  // Ye Olde Default
4760    return t;
4761
4762  default:                      // All else is a mistake
4763    typerr(t);
4764
4765  case Top:
4766    break;
4767  }
4768  return this;                  // Return the double constant
4769}
4770
4771//------------------------------xdual------------------------------------------
4772// Dual: compute field-by-field dual
4773const Type *TypeFunc::xdual() const {
4774  return this;
4775}
4776
4777//------------------------------eq---------------------------------------------
4778// Structural equality check for Type representations
4779bool TypeFunc::eq( const Type *t ) const {
4780  const TypeFunc *a = (const TypeFunc*)t;
4781  return _domain == a->_domain &&
4782    _range == a->_range;
4783}
4784
4785//------------------------------hash-------------------------------------------
4786// Type-specific hashing function.
4787int TypeFunc::hash(void) const {
4788  return (intptr_t)_domain + (intptr_t)_range;
4789}
4790
4791//------------------------------dump2------------------------------------------
4792// Dump Function Type
4793#ifndef PRODUCT
4794void TypeFunc::dump2( Dict &d, uint depth, outputStream *st ) const {
4795  if( _range->_cnt <= Parms )
4796    st->print("void");
4797  else {
4798    uint i;
4799    for (i = Parms; i < _range->_cnt-1; i++) {
4800      _range->field_at(i)->dump2(d,depth,st);
4801      st->print("/");
4802    }
4803    _range->field_at(i)->dump2(d,depth,st);
4804  }
4805  st->print(" ");
4806  st->print("( ");
4807  if( !depth || d[this] ) {     // Check for recursive dump
4808    st->print("...)");
4809    return;
4810  }
4811  d.Insert((void*)this,(void*)this);    // Stop recursion
4812  if (Parms < _domain->_cnt)
4813    _domain->field_at(Parms)->dump2(d,depth-1,st);
4814  for (uint i = Parms+1; i < _domain->_cnt; i++) {
4815    st->print(", ");
4816    _domain->field_at(i)->dump2(d,depth-1,st);
4817  }
4818  st->print(" )");
4819}
4820#endif
4821
4822//------------------------------singleton--------------------------------------
4823// TRUE if Type is a singleton type, FALSE otherwise.   Singletons are simple
4824// constants (Ldi nodes).  Singletons are integer, float or double constants
4825// or a single symbol.
4826bool TypeFunc::singleton(void) const {
4827  return false;                 // Never a singleton
4828}
4829
4830bool TypeFunc::empty(void) const {
4831  return false;                 // Never empty
4832}
4833
4834
4835BasicType TypeFunc::return_type() const{
4836  if (range()->cnt() == TypeFunc::Parms) {
4837    return T_VOID;
4838  }
4839  return range()->field_at(TypeFunc::Parms)->basic_type();
4840}
4841