Mon, 28 Apr 2008 08:08:12 -0700
Merge
1 /*
2 * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
25 // Portions of code courtesy of Clifford Click
27 // Optimization - Graph Style
29 #include "incls/_precompiled.incl"
30 #include "incls/_type.cpp.incl"
32 // Dictionary of types shared among compilations.
33 Dict* Type::_shared_type_dict = NULL;
35 // Array which maps compiler types to Basic Types
36 const BasicType Type::_basic_type[Type::lastype] = {
37 T_ILLEGAL, // Bad
38 T_ILLEGAL, // Control
39 T_VOID, // Top
40 T_INT, // Int
41 T_LONG, // Long
42 T_VOID, // Half
43 T_NARROWOOP, // NarrowOop
45 T_ILLEGAL, // Tuple
46 T_ARRAY, // Array
48 T_ADDRESS, // AnyPtr // shows up in factory methods for NULL_PTR
49 T_ADDRESS, // RawPtr
50 T_OBJECT, // OopPtr
51 T_OBJECT, // InstPtr
52 T_OBJECT, // AryPtr
53 T_OBJECT, // KlassPtr
55 T_OBJECT, // Function
56 T_ILLEGAL, // Abio
57 T_ADDRESS, // Return_Address
58 T_ILLEGAL, // Memory
59 T_FLOAT, // FloatTop
60 T_FLOAT, // FloatCon
61 T_FLOAT, // FloatBot
62 T_DOUBLE, // DoubleTop
63 T_DOUBLE, // DoubleCon
64 T_DOUBLE, // DoubleBot
65 T_ILLEGAL, // Bottom
66 };
68 // Map ideal registers (machine types) to ideal types
69 const Type *Type::mreg2type[_last_machine_leaf];
71 // Map basic types to canonical Type* pointers.
72 const Type* Type:: _const_basic_type[T_CONFLICT+1];
74 // Map basic types to constant-zero Types.
75 const Type* Type:: _zero_type[T_CONFLICT+1];
77 // Map basic types to array-body alias types.
78 const TypeAryPtr* TypeAryPtr::_array_body_type[T_CONFLICT+1];
80 //=============================================================================
81 // Convenience common pre-built types.
82 const Type *Type::ABIO; // State-of-machine only
83 const Type *Type::BOTTOM; // All values
84 const Type *Type::CONTROL; // Control only
85 const Type *Type::DOUBLE; // All doubles
86 const Type *Type::FLOAT; // All floats
87 const Type *Type::HALF; // Placeholder half of doublewide type
88 const Type *Type::MEMORY; // Abstract store only
89 const Type *Type::RETURN_ADDRESS;
90 const Type *Type::TOP; // No values in set
92 //------------------------------get_const_type---------------------------
93 const Type* Type::get_const_type(ciType* type) {
94 if (type == NULL) {
95 return NULL;
96 } else if (type->is_primitive_type()) {
97 return get_const_basic_type(type->basic_type());
98 } else {
99 return TypeOopPtr::make_from_klass(type->as_klass());
100 }
101 }
103 //---------------------------array_element_basic_type---------------------------------
104 // Mapping to the array element's basic type.
105 BasicType Type::array_element_basic_type() const {
106 BasicType bt = basic_type();
107 if (bt == T_INT) {
108 if (this == TypeInt::INT) return T_INT;
109 if (this == TypeInt::CHAR) return T_CHAR;
110 if (this == TypeInt::BYTE) return T_BYTE;
111 if (this == TypeInt::BOOL) return T_BOOLEAN;
112 if (this == TypeInt::SHORT) return T_SHORT;
113 return T_VOID;
114 }
115 return bt;
116 }
118 //---------------------------get_typeflow_type---------------------------------
119 // Import a type produced by ciTypeFlow.
120 const Type* Type::get_typeflow_type(ciType* type) {
121 switch (type->basic_type()) {
123 case ciTypeFlow::StateVector::T_BOTTOM:
124 assert(type == ciTypeFlow::StateVector::bottom_type(), "");
125 return Type::BOTTOM;
127 case ciTypeFlow::StateVector::T_TOP:
128 assert(type == ciTypeFlow::StateVector::top_type(), "");
129 return Type::TOP;
131 case ciTypeFlow::StateVector::T_NULL:
132 assert(type == ciTypeFlow::StateVector::null_type(), "");
133 return TypePtr::NULL_PTR;
135 case ciTypeFlow::StateVector::T_LONG2:
136 // The ciTypeFlow pass pushes a long, then the half.
137 // We do the same.
138 assert(type == ciTypeFlow::StateVector::long2_type(), "");
139 return TypeInt::TOP;
141 case ciTypeFlow::StateVector::T_DOUBLE2:
142 // The ciTypeFlow pass pushes double, then the half.
143 // Our convention is the same.
144 assert(type == ciTypeFlow::StateVector::double2_type(), "");
145 return Type::TOP;
147 case T_ADDRESS:
148 assert(type->is_return_address(), "");
149 return TypeRawPtr::make((address)(intptr_t)type->as_return_address()->bci());
151 default:
152 // make sure we did not mix up the cases:
153 assert(type != ciTypeFlow::StateVector::bottom_type(), "");
154 assert(type != ciTypeFlow::StateVector::top_type(), "");
155 assert(type != ciTypeFlow::StateVector::null_type(), "");
156 assert(type != ciTypeFlow::StateVector::long2_type(), "");
157 assert(type != ciTypeFlow::StateVector::double2_type(), "");
158 assert(!type->is_return_address(), "");
160 return Type::get_const_type(type);
161 }
162 }
165 //------------------------------make-------------------------------------------
166 // Create a simple Type, with default empty symbol sets. Then hashcons it
167 // and look for an existing copy in the type dictionary.
168 const Type *Type::make( enum TYPES t ) {
169 return (new Type(t))->hashcons();
170 }
172 //------------------------------cmp--------------------------------------------
173 int Type::cmp( const Type *const t1, const Type *const t2 ) {
174 if( t1->_base != t2->_base )
175 return 1; // Missed badly
176 assert(t1 != t2 || t1->eq(t2), "eq must be reflexive");
177 return !t1->eq(t2); // Return ZERO if equal
178 }
180 //------------------------------hash-------------------------------------------
181 int Type::uhash( const Type *const t ) {
182 return t->hash();
183 }
185 //--------------------------Initialize_shared----------------------------------
186 void Type::Initialize_shared(Compile* current) {
187 // This method does not need to be locked because the first system
188 // compilations (stub compilations) occur serially. If they are
189 // changed to proceed in parallel, then this section will need
190 // locking.
192 Arena* save = current->type_arena();
193 Arena* shared_type_arena = new Arena();
195 current->set_type_arena(shared_type_arena);
196 _shared_type_dict =
197 new (shared_type_arena) Dict( (CmpKey)Type::cmp, (Hash)Type::uhash,
198 shared_type_arena, 128 );
199 current->set_type_dict(_shared_type_dict);
201 // Make shared pre-built types.
202 CONTROL = make(Control); // Control only
203 TOP = make(Top); // No values in set
204 MEMORY = make(Memory); // Abstract store only
205 ABIO = make(Abio); // State-of-machine only
206 RETURN_ADDRESS=make(Return_Address);
207 FLOAT = make(FloatBot); // All floats
208 DOUBLE = make(DoubleBot); // All doubles
209 BOTTOM = make(Bottom); // Everything
210 HALF = make(Half); // Placeholder half of doublewide type
212 TypeF::ZERO = TypeF::make(0.0); // Float 0 (positive zero)
213 TypeF::ONE = TypeF::make(1.0); // Float 1
215 TypeD::ZERO = TypeD::make(0.0); // Double 0 (positive zero)
216 TypeD::ONE = TypeD::make(1.0); // Double 1
218 TypeInt::MINUS_1 = TypeInt::make(-1); // -1
219 TypeInt::ZERO = TypeInt::make( 0); // 0
220 TypeInt::ONE = TypeInt::make( 1); // 1
221 TypeInt::BOOL = TypeInt::make(0,1, WidenMin); // 0 or 1, FALSE or TRUE.
222 TypeInt::CC = TypeInt::make(-1, 1, WidenMin); // -1, 0 or 1, condition codes
223 TypeInt::CC_LT = TypeInt::make(-1,-1, WidenMin); // == TypeInt::MINUS_1
224 TypeInt::CC_GT = TypeInt::make( 1, 1, WidenMin); // == TypeInt::ONE
225 TypeInt::CC_EQ = TypeInt::make( 0, 0, WidenMin); // == TypeInt::ZERO
226 TypeInt::CC_LE = TypeInt::make(-1, 0, WidenMin);
227 TypeInt::CC_GE = TypeInt::make( 0, 1, WidenMin); // == TypeInt::BOOL
228 TypeInt::BYTE = TypeInt::make(-128,127, WidenMin); // Bytes
229 TypeInt::CHAR = TypeInt::make(0,65535, WidenMin); // Java chars
230 TypeInt::SHORT = TypeInt::make(-32768,32767, WidenMin); // Java shorts
231 TypeInt::POS = TypeInt::make(0,max_jint, WidenMin); // Non-neg values
232 TypeInt::POS1 = TypeInt::make(1,max_jint, WidenMin); // Positive values
233 TypeInt::INT = TypeInt::make(min_jint,max_jint, WidenMax); // 32-bit integers
234 TypeInt::SYMINT = TypeInt::make(-max_jint,max_jint,WidenMin); // symmetric range
235 // CmpL is overloaded both as the bytecode computation returning
236 // a trinary (-1,0,+1) integer result AND as an efficient long
237 // compare returning optimizer ideal-type flags.
238 assert( TypeInt::CC_LT == TypeInt::MINUS_1, "types must match for CmpL to work" );
239 assert( TypeInt::CC_GT == TypeInt::ONE, "types must match for CmpL to work" );
240 assert( TypeInt::CC_EQ == TypeInt::ZERO, "types must match for CmpL to work" );
241 assert( TypeInt::CC_GE == TypeInt::BOOL, "types must match for CmpL to work" );
243 TypeLong::MINUS_1 = TypeLong::make(-1); // -1
244 TypeLong::ZERO = TypeLong::make( 0); // 0
245 TypeLong::ONE = TypeLong::make( 1); // 1
246 TypeLong::POS = TypeLong::make(0,max_jlong, WidenMin); // Non-neg values
247 TypeLong::LONG = TypeLong::make(min_jlong,max_jlong,WidenMax); // 64-bit integers
248 TypeLong::INT = TypeLong::make((jlong)min_jint,(jlong)max_jint,WidenMin);
249 TypeLong::UINT = TypeLong::make(0,(jlong)max_juint,WidenMin);
251 const Type **fboth =(const Type**)shared_type_arena->Amalloc_4(2*sizeof(Type*));
252 fboth[0] = Type::CONTROL;
253 fboth[1] = Type::CONTROL;
254 TypeTuple::IFBOTH = TypeTuple::make( 2, fboth );
256 const Type **ffalse =(const Type**)shared_type_arena->Amalloc_4(2*sizeof(Type*));
257 ffalse[0] = Type::CONTROL;
258 ffalse[1] = Type::TOP;
259 TypeTuple::IFFALSE = TypeTuple::make( 2, ffalse );
261 const Type **fneither =(const Type**)shared_type_arena->Amalloc_4(2*sizeof(Type*));
262 fneither[0] = Type::TOP;
263 fneither[1] = Type::TOP;
264 TypeTuple::IFNEITHER = TypeTuple::make( 2, fneither );
266 const Type **ftrue =(const Type**)shared_type_arena->Amalloc_4(2*sizeof(Type*));
267 ftrue[0] = Type::TOP;
268 ftrue[1] = Type::CONTROL;
269 TypeTuple::IFTRUE = TypeTuple::make( 2, ftrue );
271 const Type **floop =(const Type**)shared_type_arena->Amalloc_4(2*sizeof(Type*));
272 floop[0] = Type::CONTROL;
273 floop[1] = TypeInt::INT;
274 TypeTuple::LOOPBODY = TypeTuple::make( 2, floop );
276 TypePtr::NULL_PTR= TypePtr::make( AnyPtr, TypePtr::Null, 0 );
277 TypePtr::NOTNULL = TypePtr::make( AnyPtr, TypePtr::NotNull, OffsetBot );
278 TypePtr::BOTTOM = TypePtr::make( AnyPtr, TypePtr::BotPTR, OffsetBot );
280 TypeRawPtr::BOTTOM = TypeRawPtr::make( TypePtr::BotPTR );
281 TypeRawPtr::NOTNULL= TypeRawPtr::make( TypePtr::NotNull );
283 const Type **fmembar = TypeTuple::fields(0);
284 TypeTuple::MEMBAR = TypeTuple::make(TypeFunc::Parms+0, fmembar);
286 const Type **fsc = (const Type**)shared_type_arena->Amalloc_4(2*sizeof(Type*));
287 fsc[0] = TypeInt::CC;
288 fsc[1] = Type::MEMORY;
289 TypeTuple::STORECONDITIONAL = TypeTuple::make(2, fsc);
291 TypeInstPtr::NOTNULL = TypeInstPtr::make(TypePtr::NotNull, current->env()->Object_klass());
292 TypeInstPtr::BOTTOM = TypeInstPtr::make(TypePtr::BotPTR, current->env()->Object_klass());
293 TypeInstPtr::MIRROR = TypeInstPtr::make(TypePtr::NotNull, current->env()->Class_klass());
294 TypeInstPtr::MARK = TypeInstPtr::make(TypePtr::BotPTR, current->env()->Object_klass(),
295 false, 0, oopDesc::mark_offset_in_bytes());
296 TypeInstPtr::KLASS = TypeInstPtr::make(TypePtr::BotPTR, current->env()->Object_klass(),
297 false, 0, oopDesc::klass_offset_in_bytes());
298 TypeOopPtr::BOTTOM = TypeOopPtr::make(TypePtr::BotPTR, OffsetBot);
300 TypeNarrowOop::NULL_PTR = TypeNarrowOop::make( TypePtr::NULL_PTR );
301 TypeNarrowOop::BOTTOM = TypeNarrowOop::make( TypeInstPtr::BOTTOM );
303 mreg2type[Op_Node] = Type::BOTTOM;
304 mreg2type[Op_Set ] = 0;
305 mreg2type[Op_RegN] = TypeNarrowOop::BOTTOM;
306 mreg2type[Op_RegI] = TypeInt::INT;
307 mreg2type[Op_RegP] = TypePtr::BOTTOM;
308 mreg2type[Op_RegF] = Type::FLOAT;
309 mreg2type[Op_RegD] = Type::DOUBLE;
310 mreg2type[Op_RegL] = TypeLong::LONG;
311 mreg2type[Op_RegFlags] = TypeInt::CC;
313 TypeAryPtr::RANGE = TypeAryPtr::make( TypePtr::BotPTR, TypeAry::make(Type::BOTTOM,TypeInt::POS), current->env()->Object_klass(), false, arrayOopDesc::length_offset_in_bytes());
314 // There is no shared klass for Object[]. See note in TypeAryPtr::klass().
315 TypeAryPtr::OOPS = TypeAryPtr::make(TypePtr::BotPTR, TypeAry::make(TypeInstPtr::BOTTOM,TypeInt::POS), NULL /*ciArrayKlass::make(o)*/, false, Type::OffsetBot);
316 TypeAryPtr::BYTES = TypeAryPtr::make(TypePtr::BotPTR, TypeAry::make(TypeInt::BYTE ,TypeInt::POS), ciTypeArrayKlass::make(T_BYTE), true, Type::OffsetBot);
317 TypeAryPtr::SHORTS = TypeAryPtr::make(TypePtr::BotPTR, TypeAry::make(TypeInt::SHORT ,TypeInt::POS), ciTypeArrayKlass::make(T_SHORT), true, Type::OffsetBot);
318 TypeAryPtr::CHARS = TypeAryPtr::make(TypePtr::BotPTR, TypeAry::make(TypeInt::CHAR ,TypeInt::POS), ciTypeArrayKlass::make(T_CHAR), true, Type::OffsetBot);
319 TypeAryPtr::INTS = TypeAryPtr::make(TypePtr::BotPTR, TypeAry::make(TypeInt::INT ,TypeInt::POS), ciTypeArrayKlass::make(T_INT), true, Type::OffsetBot);
320 TypeAryPtr::LONGS = TypeAryPtr::make(TypePtr::BotPTR, TypeAry::make(TypeLong::LONG ,TypeInt::POS), ciTypeArrayKlass::make(T_LONG), true, Type::OffsetBot);
321 TypeAryPtr::FLOATS = TypeAryPtr::make(TypePtr::BotPTR, TypeAry::make(Type::FLOAT ,TypeInt::POS), ciTypeArrayKlass::make(T_FLOAT), true, Type::OffsetBot);
322 TypeAryPtr::DOUBLES = TypeAryPtr::make(TypePtr::BotPTR, TypeAry::make(Type::DOUBLE ,TypeInt::POS), ciTypeArrayKlass::make(T_DOUBLE), true, Type::OffsetBot);
324 TypeAryPtr::_array_body_type[T_NARROWOOP] = NULL; // what should this be?
325 TypeAryPtr::_array_body_type[T_OBJECT] = TypeAryPtr::OOPS;
326 TypeAryPtr::_array_body_type[T_ARRAY] = TypeAryPtr::OOPS; // arrays are stored in oop arrays
327 TypeAryPtr::_array_body_type[T_BYTE] = TypeAryPtr::BYTES;
328 TypeAryPtr::_array_body_type[T_BOOLEAN] = TypeAryPtr::BYTES; // boolean[] is a byte array
329 TypeAryPtr::_array_body_type[T_SHORT] = TypeAryPtr::SHORTS;
330 TypeAryPtr::_array_body_type[T_CHAR] = TypeAryPtr::CHARS;
331 TypeAryPtr::_array_body_type[T_INT] = TypeAryPtr::INTS;
332 TypeAryPtr::_array_body_type[T_LONG] = TypeAryPtr::LONGS;
333 TypeAryPtr::_array_body_type[T_FLOAT] = TypeAryPtr::FLOATS;
334 TypeAryPtr::_array_body_type[T_DOUBLE] = TypeAryPtr::DOUBLES;
336 TypeKlassPtr::OBJECT = TypeKlassPtr::make( TypePtr::NotNull, current->env()->Object_klass(), 0 );
337 TypeKlassPtr::OBJECT_OR_NULL = TypeKlassPtr::make( TypePtr::BotPTR, current->env()->Object_klass(), 0 );
339 const Type **fi2c = TypeTuple::fields(2);
340 fi2c[TypeFunc::Parms+0] = TypeInstPtr::BOTTOM; // methodOop
341 fi2c[TypeFunc::Parms+1] = TypeRawPtr::BOTTOM; // argument pointer
342 TypeTuple::START_I2C = TypeTuple::make(TypeFunc::Parms+2, fi2c);
344 const Type **intpair = TypeTuple::fields(2);
345 intpair[0] = TypeInt::INT;
346 intpair[1] = TypeInt::INT;
347 TypeTuple::INT_PAIR = TypeTuple::make(2, intpair);
349 const Type **longpair = TypeTuple::fields(2);
350 longpair[0] = TypeLong::LONG;
351 longpair[1] = TypeLong::LONG;
352 TypeTuple::LONG_PAIR = TypeTuple::make(2, longpair);
354 _const_basic_type[T_NARROWOOP] = TypeNarrowOop::BOTTOM;
355 _const_basic_type[T_BOOLEAN] = TypeInt::BOOL;
356 _const_basic_type[T_CHAR] = TypeInt::CHAR;
357 _const_basic_type[T_BYTE] = TypeInt::BYTE;
358 _const_basic_type[T_SHORT] = TypeInt::SHORT;
359 _const_basic_type[T_INT] = TypeInt::INT;
360 _const_basic_type[T_LONG] = TypeLong::LONG;
361 _const_basic_type[T_FLOAT] = Type::FLOAT;
362 _const_basic_type[T_DOUBLE] = Type::DOUBLE;
363 _const_basic_type[T_OBJECT] = TypeInstPtr::BOTTOM;
364 _const_basic_type[T_ARRAY] = TypeInstPtr::BOTTOM; // there is no separate bottom for arrays
365 _const_basic_type[T_VOID] = TypePtr::NULL_PTR; // reflection represents void this way
366 _const_basic_type[T_ADDRESS] = TypeRawPtr::BOTTOM; // both interpreter return addresses & random raw ptrs
367 _const_basic_type[T_CONFLICT]= Type::BOTTOM; // why not?
369 _zero_type[T_NARROWOOP] = TypeNarrowOop::NULL_PTR;
370 _zero_type[T_BOOLEAN] = TypeInt::ZERO; // false == 0
371 _zero_type[T_CHAR] = TypeInt::ZERO; // '\0' == 0
372 _zero_type[T_BYTE] = TypeInt::ZERO; // 0x00 == 0
373 _zero_type[T_SHORT] = TypeInt::ZERO; // 0x0000 == 0
374 _zero_type[T_INT] = TypeInt::ZERO;
375 _zero_type[T_LONG] = TypeLong::ZERO;
376 _zero_type[T_FLOAT] = TypeF::ZERO;
377 _zero_type[T_DOUBLE] = TypeD::ZERO;
378 _zero_type[T_OBJECT] = TypePtr::NULL_PTR;
379 _zero_type[T_ARRAY] = TypePtr::NULL_PTR; // null array is null oop
380 _zero_type[T_ADDRESS] = TypePtr::NULL_PTR; // raw pointers use the same null
381 _zero_type[T_VOID] = Type::TOP; // the only void value is no value at all
383 // get_zero_type() should not happen for T_CONFLICT
384 _zero_type[T_CONFLICT]= NULL;
386 // Restore working type arena.
387 current->set_type_arena(save);
388 current->set_type_dict(NULL);
389 }
391 //------------------------------Initialize-------------------------------------
392 void Type::Initialize(Compile* current) {
393 assert(current->type_arena() != NULL, "must have created type arena");
395 if (_shared_type_dict == NULL) {
396 Initialize_shared(current);
397 }
399 Arena* type_arena = current->type_arena();
401 // Create the hash-cons'ing dictionary with top-level storage allocation
402 Dict *tdic = new (type_arena) Dict( (CmpKey)Type::cmp,(Hash)Type::uhash, type_arena, 128 );
403 current->set_type_dict(tdic);
405 // Transfer the shared types.
406 DictI i(_shared_type_dict);
407 for( ; i.test(); ++i ) {
408 Type* t = (Type*)i._value;
409 tdic->Insert(t,t); // New Type, insert into Type table
410 }
412 #ifdef ASSERT
413 verify_lastype();
414 #endif
415 }
417 //------------------------------hashcons---------------------------------------
418 // Do the hash-cons trick. If the Type already exists in the type table,
419 // delete the current Type and return the existing Type. Otherwise stick the
420 // current Type in the Type table.
421 const Type *Type::hashcons(void) {
422 debug_only(base()); // Check the assertion in Type::base().
423 // Look up the Type in the Type dictionary
424 Dict *tdic = type_dict();
425 Type* old = (Type*)(tdic->Insert(this, this, false));
426 if( old ) { // Pre-existing Type?
427 if( old != this ) // Yes, this guy is not the pre-existing?
428 delete this; // Yes, Nuke this guy
429 assert( old->_dual, "" );
430 return old; // Return pre-existing
431 }
433 // Every type has a dual (to make my lattice symmetric).
434 // Since we just discovered a new Type, compute its dual right now.
435 assert( !_dual, "" ); // No dual yet
436 _dual = xdual(); // Compute the dual
437 if( cmp(this,_dual)==0 ) { // Handle self-symmetric
438 _dual = this;
439 return this;
440 }
441 assert( !_dual->_dual, "" ); // No reverse dual yet
442 assert( !(*tdic)[_dual], "" ); // Dual not in type system either
443 // New Type, insert into Type table
444 tdic->Insert((void*)_dual,(void*)_dual);
445 ((Type*)_dual)->_dual = this; // Finish up being symmetric
446 #ifdef ASSERT
447 Type *dual_dual = (Type*)_dual->xdual();
448 assert( eq(dual_dual), "xdual(xdual()) should be identity" );
449 delete dual_dual;
450 #endif
451 return this; // Return new Type
452 }
454 //------------------------------eq---------------------------------------------
455 // Structural equality check for Type representations
456 bool Type::eq( const Type * ) const {
457 return true; // Nothing else can go wrong
458 }
460 //------------------------------hash-------------------------------------------
461 // Type-specific hashing function.
462 int Type::hash(void) const {
463 return _base;
464 }
466 //------------------------------is_finite--------------------------------------
467 // Has a finite value
468 bool Type::is_finite() const {
469 return false;
470 }
472 //------------------------------is_nan-----------------------------------------
473 // Is not a number (NaN)
474 bool Type::is_nan() const {
475 return false;
476 }
478 //------------------------------meet-------------------------------------------
479 // Compute the MEET of two types. NOT virtual. It enforces that meet is
480 // commutative and the lattice is symmetric.
481 const Type *Type::meet( const Type *t ) const {
482 if (isa_narrowoop() && t->isa_narrowoop()) {
483 const Type* result = is_narrowoop()->make_oopptr()->meet(t->is_narrowoop()->make_oopptr());
484 if (result->isa_oopptr()) {
485 return result->isa_oopptr()->make_narrowoop();
486 } else if (result == TypePtr::NULL_PTR) {
487 return TypeNarrowOop::NULL_PTR;
488 } else {
489 return result;
490 }
491 }
493 const Type *mt = xmeet(t);
494 if (isa_narrowoop() || t->isa_narrowoop()) return mt;
495 #ifdef ASSERT
496 assert( mt == t->xmeet(this), "meet not commutative" );
497 const Type* dual_join = mt->_dual;
498 const Type *t2t = dual_join->xmeet(t->_dual);
499 const Type *t2this = dual_join->xmeet( _dual);
501 // Interface meet Oop is Not Symmetric:
502 // Interface:AnyNull meet Oop:AnyNull == Interface:AnyNull
503 // Interface:NotNull meet Oop:NotNull == java/lang/Object:NotNull
504 const TypeInstPtr* this_inst = this->isa_instptr();
505 const TypeInstPtr* t_inst = t->isa_instptr();
506 bool interface_vs_oop = false;
507 if( this_inst && this_inst->is_loaded() && t_inst && t_inst->is_loaded() ) {
508 bool this_interface = this_inst->klass()->is_interface();
509 bool t_interface = t_inst->klass()->is_interface();
510 interface_vs_oop = this_interface ^ t_interface;
511 }
512 const Type *tdual = t->_dual;
513 const Type *thisdual = _dual;
514 // strip out instances
515 if (t2t->isa_oopptr() != NULL) {
516 t2t = t2t->isa_oopptr()->cast_to_instance(TypeOopPtr::UNKNOWN_INSTANCE);
517 }
518 if (t2this->isa_oopptr() != NULL) {
519 t2this = t2this->isa_oopptr()->cast_to_instance(TypeOopPtr::UNKNOWN_INSTANCE);
520 }
521 if (tdual->isa_oopptr() != NULL) {
522 tdual = tdual->isa_oopptr()->cast_to_instance(TypeOopPtr::UNKNOWN_INSTANCE);
523 }
524 if (thisdual->isa_oopptr() != NULL) {
525 thisdual = thisdual->isa_oopptr()->cast_to_instance(TypeOopPtr::UNKNOWN_INSTANCE);
526 }
528 if( !interface_vs_oop && (t2t != tdual || t2this != thisdual) ) {
529 tty->print_cr("=== Meet Not Symmetric ===");
530 tty->print("t = "); t->dump(); tty->cr();
531 tty->print("this= "); dump(); tty->cr();
532 tty->print("mt=(t meet this)= "); mt->dump(); tty->cr();
534 tty->print("t_dual= "); t->_dual->dump(); tty->cr();
535 tty->print("this_dual= "); _dual->dump(); tty->cr();
536 tty->print("mt_dual= "); mt->_dual->dump(); tty->cr();
538 tty->print("mt_dual meet t_dual= "); t2t ->dump(); tty->cr();
539 tty->print("mt_dual meet this_dual= "); t2this ->dump(); tty->cr();
541 fatal("meet not symmetric" );
542 }
543 #endif
544 return mt;
545 }
547 //------------------------------xmeet------------------------------------------
548 // Compute the MEET of two types. It returns a new Type object.
549 const Type *Type::xmeet( const Type *t ) const {
550 // Perform a fast test for common case; meeting the same types together.
551 if( this == t ) return this; // Meeting same type-rep?
553 // Meeting TOP with anything?
554 if( _base == Top ) return t;
556 // Meeting BOTTOM with anything?
557 if( _base == Bottom ) return BOTTOM;
559 // Current "this->_base" is one of: Bad, Multi, Control, Top,
560 // Abio, Abstore, Floatxxx, Doublexxx, Bottom, lastype.
561 switch (t->base()) { // Switch on original type
563 // Cut in half the number of cases I must handle. Only need cases for when
564 // the given enum "t->type" is less than or equal to the local enum "type".
565 case FloatCon:
566 case DoubleCon:
567 case Int:
568 case Long:
569 return t->xmeet(this);
571 case OopPtr:
572 return t->xmeet(this);
574 case InstPtr:
575 return t->xmeet(this);
577 case KlassPtr:
578 return t->xmeet(this);
580 case AryPtr:
581 return t->xmeet(this);
583 case NarrowOop:
584 return t->xmeet(this);
586 case Bad: // Type check
587 default: // Bogus type not in lattice
588 typerr(t);
589 return Type::BOTTOM;
591 case Bottom: // Ye Olde Default
592 return t;
594 case FloatTop:
595 if( _base == FloatTop ) return this;
596 case FloatBot: // Float
597 if( _base == FloatBot || _base == FloatTop ) return FLOAT;
598 if( _base == DoubleTop || _base == DoubleBot ) return Type::BOTTOM;
599 typerr(t);
600 return Type::BOTTOM;
602 case DoubleTop:
603 if( _base == DoubleTop ) return this;
604 case DoubleBot: // Double
605 if( _base == DoubleBot || _base == DoubleTop ) return DOUBLE;
606 if( _base == FloatTop || _base == FloatBot ) return Type::BOTTOM;
607 typerr(t);
608 return Type::BOTTOM;
610 // These next few cases must match exactly or it is a compile-time error.
611 case Control: // Control of code
612 case Abio: // State of world outside of program
613 case Memory:
614 if( _base == t->_base ) return this;
615 typerr(t);
616 return Type::BOTTOM;
618 case Top: // Top of the lattice
619 return this;
620 }
622 // The type is unchanged
623 return this;
624 }
626 //-----------------------------filter------------------------------------------
627 const Type *Type::filter( const Type *kills ) const {
628 const Type* ft = join(kills);
629 if (ft->empty())
630 return Type::TOP; // Canonical empty value
631 return ft;
632 }
634 //------------------------------xdual------------------------------------------
635 // Compute dual right now.
636 const Type::TYPES Type::dual_type[Type::lastype] = {
637 Bad, // Bad
638 Control, // Control
639 Bottom, // Top
640 Bad, // Int - handled in v-call
641 Bad, // Long - handled in v-call
642 Half, // Half
643 Bad, // NarrowOop - handled in v-call
645 Bad, // Tuple - handled in v-call
646 Bad, // Array - handled in v-call
648 Bad, // AnyPtr - handled in v-call
649 Bad, // RawPtr - handled in v-call
650 Bad, // OopPtr - handled in v-call
651 Bad, // InstPtr - handled in v-call
652 Bad, // AryPtr - handled in v-call
653 Bad, // KlassPtr - handled in v-call
655 Bad, // Function - handled in v-call
656 Abio, // Abio
657 Return_Address,// Return_Address
658 Memory, // Memory
659 FloatBot, // FloatTop
660 FloatCon, // FloatCon
661 FloatTop, // FloatBot
662 DoubleBot, // DoubleTop
663 DoubleCon, // DoubleCon
664 DoubleTop, // DoubleBot
665 Top // Bottom
666 };
668 const Type *Type::xdual() const {
669 // Note: the base() accessor asserts the sanity of _base.
670 assert(dual_type[base()] != Bad, "implement with v-call");
671 return new Type(dual_type[_base]);
672 }
674 //------------------------------has_memory-------------------------------------
675 bool Type::has_memory() const {
676 Type::TYPES tx = base();
677 if (tx == Memory) return true;
678 if (tx == Tuple) {
679 const TypeTuple *t = is_tuple();
680 for (uint i=0; i < t->cnt(); i++) {
681 tx = t->field_at(i)->base();
682 if (tx == Memory) return true;
683 }
684 }
685 return false;
686 }
688 #ifndef PRODUCT
689 //------------------------------dump2------------------------------------------
690 void Type::dump2( Dict &d, uint depth, outputStream *st ) const {
691 st->print(msg[_base]);
692 }
694 //------------------------------dump-------------------------------------------
695 void Type::dump_on(outputStream *st) const {
696 ResourceMark rm;
697 Dict d(cmpkey,hashkey); // Stop recursive type dumping
698 dump2(d,1, st);
699 if (isa_ptr() && is_ptr()->is_narrow()) {
700 st->print(" [narrow]");
701 }
702 }
704 //------------------------------data-------------------------------------------
705 const char * const Type::msg[Type::lastype] = {
706 "bad","control","top","int:","long:","half", "narrowoop:",
707 "tuple:", "aryptr",
708 "anyptr:", "rawptr:", "java:", "inst:", "ary:", "klass:",
709 "func", "abIO", "return_address", "memory",
710 "float_top", "ftcon:", "float",
711 "double_top", "dblcon:", "double",
712 "bottom"
713 };
714 #endif
716 //------------------------------singleton--------------------------------------
717 // TRUE if Type is a singleton type, FALSE otherwise. Singletons are simple
718 // constants (Ldi nodes). Singletons are integer, float or double constants.
719 bool Type::singleton(void) const {
720 return _base == Top || _base == Half;
721 }
723 //------------------------------empty------------------------------------------
724 // TRUE if Type is a type with no values, FALSE otherwise.
725 bool Type::empty(void) const {
726 switch (_base) {
727 case DoubleTop:
728 case FloatTop:
729 case Top:
730 return true;
732 case Half:
733 case Abio:
734 case Return_Address:
735 case Memory:
736 case Bottom:
737 case FloatBot:
738 case DoubleBot:
739 return false; // never a singleton, therefore never empty
740 }
742 ShouldNotReachHere();
743 return false;
744 }
746 //------------------------------dump_stats-------------------------------------
747 // Dump collected statistics to stderr
748 #ifndef PRODUCT
749 void Type::dump_stats() {
750 tty->print("Types made: %d\n", type_dict()->Size());
751 }
752 #endif
754 //------------------------------typerr-----------------------------------------
755 void Type::typerr( const Type *t ) const {
756 #ifndef PRODUCT
757 tty->print("\nError mixing types: ");
758 dump();
759 tty->print(" and ");
760 t->dump();
761 tty->print("\n");
762 #endif
763 ShouldNotReachHere();
764 }
766 //------------------------------isa_oop_ptr------------------------------------
767 // Return true if type is an oop pointer type. False for raw pointers.
768 static char isa_oop_ptr_tbl[Type::lastype] = {
769 0,0,0,0,0,0,0/*narrowoop*/,0/*tuple*/, 0/*ary*/,
770 0/*anyptr*/,0/*rawptr*/,1/*OopPtr*/,1/*InstPtr*/,1/*AryPtr*/,1/*KlassPtr*/,
771 0/*func*/,0,0/*return_address*/,0,
772 /*floats*/0,0,0, /*doubles*/0,0,0,
773 0
774 };
775 bool Type::isa_oop_ptr() const {
776 return isa_oop_ptr_tbl[_base] != 0;
777 }
779 //------------------------------dump_stats-------------------------------------
780 // // Check that arrays match type enum
781 #ifndef PRODUCT
782 void Type::verify_lastype() {
783 // Check that arrays match enumeration
784 assert( Type::dual_type [Type::lastype - 1] == Type::Top, "did not update array");
785 assert( strcmp(Type::msg [Type::lastype - 1],"bottom") == 0, "did not update array");
786 // assert( PhiNode::tbl [Type::lastype - 1] == NULL, "did not update array");
787 assert( Matcher::base2reg[Type::lastype - 1] == 0, "did not update array");
788 assert( isa_oop_ptr_tbl [Type::lastype - 1] == (char)0, "did not update array");
789 }
790 #endif
792 //=============================================================================
793 // Convenience common pre-built types.
794 const TypeF *TypeF::ZERO; // Floating point zero
795 const TypeF *TypeF::ONE; // Floating point one
797 //------------------------------make-------------------------------------------
798 // Create a float constant
799 const TypeF *TypeF::make(float f) {
800 return (TypeF*)(new TypeF(f))->hashcons();
801 }
803 //------------------------------meet-------------------------------------------
804 // Compute the MEET of two types. It returns a new Type object.
805 const Type *TypeF::xmeet( const Type *t ) const {
806 // Perform a fast test for common case; meeting the same types together.
807 if( this == t ) return this; // Meeting same type-rep?
809 // Current "this->_base" is FloatCon
810 switch (t->base()) { // Switch on original type
811 case AnyPtr: // Mixing with oops happens when javac
812 case RawPtr: // reuses local variables
813 case OopPtr:
814 case InstPtr:
815 case KlassPtr:
816 case AryPtr:
817 case Int:
818 case Long:
819 case DoubleTop:
820 case DoubleCon:
821 case DoubleBot:
822 case Bottom: // Ye Olde Default
823 return Type::BOTTOM;
825 case FloatBot:
826 return t;
828 default: // All else is a mistake
829 typerr(t);
831 case FloatCon: // Float-constant vs Float-constant?
832 if( jint_cast(_f) != jint_cast(t->getf()) ) // unequal constants?
833 // must compare bitwise as positive zero, negative zero and NaN have
834 // all the same representation in C++
835 return FLOAT; // Return generic float
836 // Equal constants
837 case Top:
838 case FloatTop:
839 break; // Return the float constant
840 }
841 return this; // Return the float constant
842 }
844 //------------------------------xdual------------------------------------------
845 // Dual: symmetric
846 const Type *TypeF::xdual() const {
847 return this;
848 }
850 //------------------------------eq---------------------------------------------
851 // Structural equality check for Type representations
852 bool TypeF::eq( const Type *t ) const {
853 if( g_isnan(_f) ||
854 g_isnan(t->getf()) ) {
855 // One or both are NANs. If both are NANs return true, else false.
856 return (g_isnan(_f) && g_isnan(t->getf()));
857 }
858 if (_f == t->getf()) {
859 // (NaN is impossible at this point, since it is not equal even to itself)
860 if (_f == 0.0) {
861 // difference between positive and negative zero
862 if (jint_cast(_f) != jint_cast(t->getf())) return false;
863 }
864 return true;
865 }
866 return false;
867 }
869 //------------------------------hash-------------------------------------------
870 // Type-specific hashing function.
871 int TypeF::hash(void) const {
872 return *(int*)(&_f);
873 }
875 //------------------------------is_finite--------------------------------------
876 // Has a finite value
877 bool TypeF::is_finite() const {
878 return g_isfinite(getf()) != 0;
879 }
881 //------------------------------is_nan-----------------------------------------
882 // Is not a number (NaN)
883 bool TypeF::is_nan() const {
884 return g_isnan(getf()) != 0;
885 }
887 //------------------------------dump2------------------------------------------
888 // Dump float constant Type
889 #ifndef PRODUCT
890 void TypeF::dump2( Dict &d, uint depth, outputStream *st ) const {
891 Type::dump2(d,depth, st);
892 st->print("%f", _f);
893 }
894 #endif
896 //------------------------------singleton--------------------------------------
897 // TRUE if Type is a singleton type, FALSE otherwise. Singletons are simple
898 // constants (Ldi nodes). Singletons are integer, float or double constants
899 // or a single symbol.
900 bool TypeF::singleton(void) const {
901 return true; // Always a singleton
902 }
904 bool TypeF::empty(void) const {
905 return false; // always exactly a singleton
906 }
908 //=============================================================================
909 // Convenience common pre-built types.
910 const TypeD *TypeD::ZERO; // Floating point zero
911 const TypeD *TypeD::ONE; // Floating point one
913 //------------------------------make-------------------------------------------
914 const TypeD *TypeD::make(double d) {
915 return (TypeD*)(new TypeD(d))->hashcons();
916 }
918 //------------------------------meet-------------------------------------------
919 // Compute the MEET of two types. It returns a new Type object.
920 const Type *TypeD::xmeet( const Type *t ) const {
921 // Perform a fast test for common case; meeting the same types together.
922 if( this == t ) return this; // Meeting same type-rep?
924 // Current "this->_base" is DoubleCon
925 switch (t->base()) { // Switch on original type
926 case AnyPtr: // Mixing with oops happens when javac
927 case RawPtr: // reuses local variables
928 case OopPtr:
929 case InstPtr:
930 case KlassPtr:
931 case AryPtr:
932 case Int:
933 case Long:
934 case FloatTop:
935 case FloatCon:
936 case FloatBot:
937 case Bottom: // Ye Olde Default
938 return Type::BOTTOM;
940 case DoubleBot:
941 return t;
943 default: // All else is a mistake
944 typerr(t);
946 case DoubleCon: // Double-constant vs Double-constant?
947 if( jlong_cast(_d) != jlong_cast(t->getd()) ) // unequal constants? (see comment in TypeF::xmeet)
948 return DOUBLE; // Return generic double
949 case Top:
950 case DoubleTop:
951 break;
952 }
953 return this; // Return the double constant
954 }
956 //------------------------------xdual------------------------------------------
957 // Dual: symmetric
958 const Type *TypeD::xdual() const {
959 return this;
960 }
962 //------------------------------eq---------------------------------------------
963 // Structural equality check for Type representations
964 bool TypeD::eq( const Type *t ) const {
965 if( g_isnan(_d) ||
966 g_isnan(t->getd()) ) {
967 // One or both are NANs. If both are NANs return true, else false.
968 return (g_isnan(_d) && g_isnan(t->getd()));
969 }
970 if (_d == t->getd()) {
971 // (NaN is impossible at this point, since it is not equal even to itself)
972 if (_d == 0.0) {
973 // difference between positive and negative zero
974 if (jlong_cast(_d) != jlong_cast(t->getd())) return false;
975 }
976 return true;
977 }
978 return false;
979 }
981 //------------------------------hash-------------------------------------------
982 // Type-specific hashing function.
983 int TypeD::hash(void) const {
984 return *(int*)(&_d);
985 }
987 //------------------------------is_finite--------------------------------------
988 // Has a finite value
989 bool TypeD::is_finite() const {
990 return g_isfinite(getd()) != 0;
991 }
993 //------------------------------is_nan-----------------------------------------
994 // Is not a number (NaN)
995 bool TypeD::is_nan() const {
996 return g_isnan(getd()) != 0;
997 }
999 //------------------------------dump2------------------------------------------
1000 // Dump double constant Type
1001 #ifndef PRODUCT
1002 void TypeD::dump2( Dict &d, uint depth, outputStream *st ) const {
1003 Type::dump2(d,depth,st);
1004 st->print("%f", _d);
1005 }
1006 #endif
1008 //------------------------------singleton--------------------------------------
1009 // TRUE if Type is a singleton type, FALSE otherwise. Singletons are simple
1010 // constants (Ldi nodes). Singletons are integer, float or double constants
1011 // or a single symbol.
1012 bool TypeD::singleton(void) const {
1013 return true; // Always a singleton
1014 }
1016 bool TypeD::empty(void) const {
1017 return false; // always exactly a singleton
1018 }
1020 //=============================================================================
1021 // Convience common pre-built types.
1022 const TypeInt *TypeInt::MINUS_1;// -1
1023 const TypeInt *TypeInt::ZERO; // 0
1024 const TypeInt *TypeInt::ONE; // 1
1025 const TypeInt *TypeInt::BOOL; // 0 or 1, FALSE or TRUE.
1026 const TypeInt *TypeInt::CC; // -1,0 or 1, condition codes
1027 const TypeInt *TypeInt::CC_LT; // [-1] == MINUS_1
1028 const TypeInt *TypeInt::CC_GT; // [1] == ONE
1029 const TypeInt *TypeInt::CC_EQ; // [0] == ZERO
1030 const TypeInt *TypeInt::CC_LE; // [-1,0]
1031 const TypeInt *TypeInt::CC_GE; // [0,1] == BOOL (!)
1032 const TypeInt *TypeInt::BYTE; // Bytes, -128 to 127
1033 const TypeInt *TypeInt::CHAR; // Java chars, 0-65535
1034 const TypeInt *TypeInt::SHORT; // Java shorts, -32768-32767
1035 const TypeInt *TypeInt::POS; // Positive 32-bit integers or zero
1036 const TypeInt *TypeInt::POS1; // Positive 32-bit integers
1037 const TypeInt *TypeInt::INT; // 32-bit integers
1038 const TypeInt *TypeInt::SYMINT; // symmetric range [-max_jint..max_jint]
1040 //------------------------------TypeInt----------------------------------------
1041 TypeInt::TypeInt( jint lo, jint hi, int w ) : Type(Int), _lo(lo), _hi(hi), _widen(w) {
1042 }
1044 //------------------------------make-------------------------------------------
1045 const TypeInt *TypeInt::make( jint lo ) {
1046 return (TypeInt*)(new TypeInt(lo,lo,WidenMin))->hashcons();
1047 }
1049 #define SMALLINT ((juint)3) // a value too insignificant to consider widening
1051 const TypeInt *TypeInt::make( jint lo, jint hi, int w ) {
1052 // Certain normalizations keep us sane when comparing types.
1053 // The 'SMALLINT' covers constants and also CC and its relatives.
1054 assert(CC == NULL || (juint)(CC->_hi - CC->_lo) <= SMALLINT, "CC is truly small");
1055 if (lo <= hi) {
1056 if ((juint)(hi - lo) <= SMALLINT) w = Type::WidenMin;
1057 if ((juint)(hi - lo) >= max_juint) w = Type::WidenMax; // plain int
1058 }
1059 return (TypeInt*)(new TypeInt(lo,hi,w))->hashcons();
1060 }
1062 //------------------------------meet-------------------------------------------
1063 // Compute the MEET of two types. It returns a new Type representation object
1064 // with reference count equal to the number of Types pointing at it.
1065 // Caller should wrap a Types around it.
1066 const Type *TypeInt::xmeet( const Type *t ) const {
1067 // Perform a fast test for common case; meeting the same types together.
1068 if( this == t ) return this; // Meeting same type?
1070 // Currently "this->_base" is a TypeInt
1071 switch (t->base()) { // Switch on original type
1072 case AnyPtr: // Mixing with oops happens when javac
1073 case RawPtr: // reuses local variables
1074 case OopPtr:
1075 case InstPtr:
1076 case KlassPtr:
1077 case AryPtr:
1078 case Long:
1079 case FloatTop:
1080 case FloatCon:
1081 case FloatBot:
1082 case DoubleTop:
1083 case DoubleCon:
1084 case DoubleBot:
1085 case NarrowOop:
1086 case Bottom: // Ye Olde Default
1087 return Type::BOTTOM;
1088 default: // All else is a mistake
1089 typerr(t);
1090 case Top: // No change
1091 return this;
1092 case Int: // Int vs Int?
1093 break;
1094 }
1096 // Expand covered set
1097 const TypeInt *r = t->is_int();
1098 // (Avoid TypeInt::make, to avoid the argument normalizations it enforces.)
1099 return (new TypeInt( MIN2(_lo,r->_lo), MAX2(_hi,r->_hi), MAX2(_widen,r->_widen) ))->hashcons();
1100 }
1102 //------------------------------xdual------------------------------------------
1103 // Dual: reverse hi & lo; flip widen
1104 const Type *TypeInt::xdual() const {
1105 return new TypeInt(_hi,_lo,WidenMax-_widen);
1106 }
1108 //------------------------------widen------------------------------------------
1109 // Only happens for optimistic top-down optimizations.
1110 const Type *TypeInt::widen( const Type *old ) const {
1111 // Coming from TOP or such; no widening
1112 if( old->base() != Int ) return this;
1113 const TypeInt *ot = old->is_int();
1115 // If new guy is equal to old guy, no widening
1116 if( _lo == ot->_lo && _hi == ot->_hi )
1117 return old;
1119 // If new guy contains old, then we widened
1120 if( _lo <= ot->_lo && _hi >= ot->_hi ) {
1121 // New contains old
1122 // If new guy is already wider than old, no widening
1123 if( _widen > ot->_widen ) return this;
1124 // If old guy was a constant, do not bother
1125 if (ot->_lo == ot->_hi) return this;
1126 // Now widen new guy.
1127 // Check for widening too far
1128 if (_widen == WidenMax) {
1129 if (min_jint < _lo && _hi < max_jint) {
1130 // If neither endpoint is extremal yet, push out the endpoint
1131 // which is closer to its respective limit.
1132 if (_lo >= 0 || // easy common case
1133 (juint)(_lo - min_jint) >= (juint)(max_jint - _hi)) {
1134 // Try to widen to an unsigned range type of 31 bits:
1135 return make(_lo, max_jint, WidenMax);
1136 } else {
1137 return make(min_jint, _hi, WidenMax);
1138 }
1139 }
1140 return TypeInt::INT;
1141 }
1142 // Returned widened new guy
1143 return make(_lo,_hi,_widen+1);
1144 }
1146 // If old guy contains new, then we probably widened too far & dropped to
1147 // bottom. Return the wider fellow.
1148 if ( ot->_lo <= _lo && ot->_hi >= _hi )
1149 return old;
1151 //fatal("Integer value range is not subset");
1152 //return this;
1153 return TypeInt::INT;
1154 }
1156 //------------------------------narrow---------------------------------------
1157 // Only happens for pessimistic optimizations.
1158 const Type *TypeInt::narrow( const Type *old ) const {
1159 if (_lo >= _hi) return this; // already narrow enough
1160 if (old == NULL) return this;
1161 const TypeInt* ot = old->isa_int();
1162 if (ot == NULL) return this;
1163 jint olo = ot->_lo;
1164 jint ohi = ot->_hi;
1166 // If new guy is equal to old guy, no narrowing
1167 if (_lo == olo && _hi == ohi) return old;
1169 // If old guy was maximum range, allow the narrowing
1170 if (olo == min_jint && ohi == max_jint) return this;
1172 if (_lo < olo || _hi > ohi)
1173 return this; // doesn't narrow; pretty wierd
1175 // The new type narrows the old type, so look for a "death march".
1176 // See comments on PhaseTransform::saturate.
1177 juint nrange = _hi - _lo;
1178 juint orange = ohi - olo;
1179 if (nrange < max_juint - 1 && nrange > (orange >> 1) + (SMALLINT*2)) {
1180 // Use the new type only if the range shrinks a lot.
1181 // We do not want the optimizer computing 2^31 point by point.
1182 return old;
1183 }
1185 return this;
1186 }
1188 //-----------------------------filter------------------------------------------
1189 const Type *TypeInt::filter( const Type *kills ) const {
1190 const TypeInt* ft = join(kills)->isa_int();
1191 if (ft == NULL || ft->_lo > ft->_hi)
1192 return Type::TOP; // Canonical empty value
1193 if (ft->_widen < this->_widen) {
1194 // Do not allow the value of kill->_widen to affect the outcome.
1195 // The widen bits must be allowed to run freely through the graph.
1196 ft = TypeInt::make(ft->_lo, ft->_hi, this->_widen);
1197 }
1198 return ft;
1199 }
1201 //------------------------------eq---------------------------------------------
1202 // Structural equality check for Type representations
1203 bool TypeInt::eq( const Type *t ) const {
1204 const TypeInt *r = t->is_int(); // Handy access
1205 return r->_lo == _lo && r->_hi == _hi && r->_widen == _widen;
1206 }
1208 //------------------------------hash-------------------------------------------
1209 // Type-specific hashing function.
1210 int TypeInt::hash(void) const {
1211 return _lo+_hi+_widen+(int)Type::Int;
1212 }
1214 //------------------------------is_finite--------------------------------------
1215 // Has a finite value
1216 bool TypeInt::is_finite() const {
1217 return true;
1218 }
1220 //------------------------------dump2------------------------------------------
1221 // Dump TypeInt
1222 #ifndef PRODUCT
1223 static const char* intname(char* buf, jint n) {
1224 if (n == min_jint)
1225 return "min";
1226 else if (n < min_jint + 10000)
1227 sprintf(buf, "min+" INT32_FORMAT, n - min_jint);
1228 else if (n == max_jint)
1229 return "max";
1230 else if (n > max_jint - 10000)
1231 sprintf(buf, "max-" INT32_FORMAT, max_jint - n);
1232 else
1233 sprintf(buf, INT32_FORMAT, n);
1234 return buf;
1235 }
1237 void TypeInt::dump2( Dict &d, uint depth, outputStream *st ) const {
1238 char buf[40], buf2[40];
1239 if (_lo == min_jint && _hi == max_jint)
1240 st->print("int");
1241 else if (is_con())
1242 st->print("int:%s", intname(buf, get_con()));
1243 else if (_lo == BOOL->_lo && _hi == BOOL->_hi)
1244 st->print("bool");
1245 else if (_lo == BYTE->_lo && _hi == BYTE->_hi)
1246 st->print("byte");
1247 else if (_lo == CHAR->_lo && _hi == CHAR->_hi)
1248 st->print("char");
1249 else if (_lo == SHORT->_lo && _hi == SHORT->_hi)
1250 st->print("short");
1251 else if (_hi == max_jint)
1252 st->print("int:>=%s", intname(buf, _lo));
1253 else if (_lo == min_jint)
1254 st->print("int:<=%s", intname(buf, _hi));
1255 else
1256 st->print("int:%s..%s", intname(buf, _lo), intname(buf2, _hi));
1258 if (_widen != 0 && this != TypeInt::INT)
1259 st->print(":%.*s", _widen, "wwww");
1260 }
1261 #endif
1263 //------------------------------singleton--------------------------------------
1264 // TRUE if Type is a singleton type, FALSE otherwise. Singletons are simple
1265 // constants.
1266 bool TypeInt::singleton(void) const {
1267 return _lo >= _hi;
1268 }
1270 bool TypeInt::empty(void) const {
1271 return _lo > _hi;
1272 }
1274 //=============================================================================
1275 // Convenience common pre-built types.
1276 const TypeLong *TypeLong::MINUS_1;// -1
1277 const TypeLong *TypeLong::ZERO; // 0
1278 const TypeLong *TypeLong::ONE; // 1
1279 const TypeLong *TypeLong::POS; // >=0
1280 const TypeLong *TypeLong::LONG; // 64-bit integers
1281 const TypeLong *TypeLong::INT; // 32-bit subrange
1282 const TypeLong *TypeLong::UINT; // 32-bit unsigned subrange
1284 //------------------------------TypeLong---------------------------------------
1285 TypeLong::TypeLong( jlong lo, jlong hi, int w ) : Type(Long), _lo(lo), _hi(hi), _widen(w) {
1286 }
1288 //------------------------------make-------------------------------------------
1289 const TypeLong *TypeLong::make( jlong lo ) {
1290 return (TypeLong*)(new TypeLong(lo,lo,WidenMin))->hashcons();
1291 }
1293 const TypeLong *TypeLong::make( jlong lo, jlong hi, int w ) {
1294 // Certain normalizations keep us sane when comparing types.
1295 // The '1' covers constants.
1296 if (lo <= hi) {
1297 if ((julong)(hi - lo) <= SMALLINT) w = Type::WidenMin;
1298 if ((julong)(hi - lo) >= max_julong) w = Type::WidenMax; // plain long
1299 }
1300 return (TypeLong*)(new TypeLong(lo,hi,w))->hashcons();
1301 }
1304 //------------------------------meet-------------------------------------------
1305 // Compute the MEET of two types. It returns a new Type representation object
1306 // with reference count equal to the number of Types pointing at it.
1307 // Caller should wrap a Types around it.
1308 const Type *TypeLong::xmeet( const Type *t ) const {
1309 // Perform a fast test for common case; meeting the same types together.
1310 if( this == t ) return this; // Meeting same type?
1312 // Currently "this->_base" is a TypeLong
1313 switch (t->base()) { // Switch on original type
1314 case AnyPtr: // Mixing with oops happens when javac
1315 case RawPtr: // reuses local variables
1316 case OopPtr:
1317 case InstPtr:
1318 case KlassPtr:
1319 case AryPtr:
1320 case Int:
1321 case FloatTop:
1322 case FloatCon:
1323 case FloatBot:
1324 case DoubleTop:
1325 case DoubleCon:
1326 case DoubleBot:
1327 case Bottom: // Ye Olde Default
1328 return Type::BOTTOM;
1329 default: // All else is a mistake
1330 typerr(t);
1331 case Top: // No change
1332 return this;
1333 case Long: // Long vs Long?
1334 break;
1335 }
1337 // Expand covered set
1338 const TypeLong *r = t->is_long(); // Turn into a TypeLong
1339 // (Avoid TypeLong::make, to avoid the argument normalizations it enforces.)
1340 return (new TypeLong( MIN2(_lo,r->_lo), MAX2(_hi,r->_hi), MAX2(_widen,r->_widen) ))->hashcons();
1341 }
1343 //------------------------------xdual------------------------------------------
1344 // Dual: reverse hi & lo; flip widen
1345 const Type *TypeLong::xdual() const {
1346 return new TypeLong(_hi,_lo,WidenMax-_widen);
1347 }
1349 //------------------------------widen------------------------------------------
1350 // Only happens for optimistic top-down optimizations.
1351 const Type *TypeLong::widen( const Type *old ) const {
1352 // Coming from TOP or such; no widening
1353 if( old->base() != Long ) return this;
1354 const TypeLong *ot = old->is_long();
1356 // If new guy is equal to old guy, no widening
1357 if( _lo == ot->_lo && _hi == ot->_hi )
1358 return old;
1360 // If new guy contains old, then we widened
1361 if( _lo <= ot->_lo && _hi >= ot->_hi ) {
1362 // New contains old
1363 // If new guy is already wider than old, no widening
1364 if( _widen > ot->_widen ) return this;
1365 // If old guy was a constant, do not bother
1366 if (ot->_lo == ot->_hi) return this;
1367 // Now widen new guy.
1368 // Check for widening too far
1369 if (_widen == WidenMax) {
1370 if (min_jlong < _lo && _hi < max_jlong) {
1371 // If neither endpoint is extremal yet, push out the endpoint
1372 // which is closer to its respective limit.
1373 if (_lo >= 0 || // easy common case
1374 (julong)(_lo - min_jlong) >= (julong)(max_jlong - _hi)) {
1375 // Try to widen to an unsigned range type of 32/63 bits:
1376 if (_hi < max_juint)
1377 return make(_lo, max_juint, WidenMax);
1378 else
1379 return make(_lo, max_jlong, WidenMax);
1380 } else {
1381 return make(min_jlong, _hi, WidenMax);
1382 }
1383 }
1384 return TypeLong::LONG;
1385 }
1386 // Returned widened new guy
1387 return make(_lo,_hi,_widen+1);
1388 }
1390 // If old guy contains new, then we probably widened too far & dropped to
1391 // bottom. Return the wider fellow.
1392 if ( ot->_lo <= _lo && ot->_hi >= _hi )
1393 return old;
1395 // fatal("Long value range is not subset");
1396 // return this;
1397 return TypeLong::LONG;
1398 }
1400 //------------------------------narrow----------------------------------------
1401 // Only happens for pessimistic optimizations.
1402 const Type *TypeLong::narrow( const Type *old ) const {
1403 if (_lo >= _hi) return this; // already narrow enough
1404 if (old == NULL) return this;
1405 const TypeLong* ot = old->isa_long();
1406 if (ot == NULL) return this;
1407 jlong olo = ot->_lo;
1408 jlong ohi = ot->_hi;
1410 // If new guy is equal to old guy, no narrowing
1411 if (_lo == olo && _hi == ohi) return old;
1413 // If old guy was maximum range, allow the narrowing
1414 if (olo == min_jlong && ohi == max_jlong) return this;
1416 if (_lo < olo || _hi > ohi)
1417 return this; // doesn't narrow; pretty wierd
1419 // The new type narrows the old type, so look for a "death march".
1420 // See comments on PhaseTransform::saturate.
1421 julong nrange = _hi - _lo;
1422 julong orange = ohi - olo;
1423 if (nrange < max_julong - 1 && nrange > (orange >> 1) + (SMALLINT*2)) {
1424 // Use the new type only if the range shrinks a lot.
1425 // We do not want the optimizer computing 2^31 point by point.
1426 return old;
1427 }
1429 return this;
1430 }
1432 //-----------------------------filter------------------------------------------
1433 const Type *TypeLong::filter( const Type *kills ) const {
1434 const TypeLong* ft = join(kills)->isa_long();
1435 if (ft == NULL || ft->_lo > ft->_hi)
1436 return Type::TOP; // Canonical empty value
1437 if (ft->_widen < this->_widen) {
1438 // Do not allow the value of kill->_widen to affect the outcome.
1439 // The widen bits must be allowed to run freely through the graph.
1440 ft = TypeLong::make(ft->_lo, ft->_hi, this->_widen);
1441 }
1442 return ft;
1443 }
1445 //------------------------------eq---------------------------------------------
1446 // Structural equality check for Type representations
1447 bool TypeLong::eq( const Type *t ) const {
1448 const TypeLong *r = t->is_long(); // Handy access
1449 return r->_lo == _lo && r->_hi == _hi && r->_widen == _widen;
1450 }
1452 //------------------------------hash-------------------------------------------
1453 // Type-specific hashing function.
1454 int TypeLong::hash(void) const {
1455 return (int)(_lo+_hi+_widen+(int)Type::Long);
1456 }
1458 //------------------------------is_finite--------------------------------------
1459 // Has a finite value
1460 bool TypeLong::is_finite() const {
1461 return true;
1462 }
1464 //------------------------------dump2------------------------------------------
1465 // Dump TypeLong
1466 #ifndef PRODUCT
1467 static const char* longnamenear(jlong x, const char* xname, char* buf, jlong n) {
1468 if (n > x) {
1469 if (n >= x + 10000) return NULL;
1470 sprintf(buf, "%s+" INT64_FORMAT, xname, n - x);
1471 } else if (n < x) {
1472 if (n <= x - 10000) return NULL;
1473 sprintf(buf, "%s-" INT64_FORMAT, xname, x - n);
1474 } else {
1475 return xname;
1476 }
1477 return buf;
1478 }
1480 static const char* longname(char* buf, jlong n) {
1481 const char* str;
1482 if (n == min_jlong)
1483 return "min";
1484 else if (n < min_jlong + 10000)
1485 sprintf(buf, "min+" INT64_FORMAT, n - min_jlong);
1486 else if (n == max_jlong)
1487 return "max";
1488 else if (n > max_jlong - 10000)
1489 sprintf(buf, "max-" INT64_FORMAT, max_jlong - n);
1490 else if ((str = longnamenear(max_juint, "maxuint", buf, n)) != NULL)
1491 return str;
1492 else if ((str = longnamenear(max_jint, "maxint", buf, n)) != NULL)
1493 return str;
1494 else if ((str = longnamenear(min_jint, "minint", buf, n)) != NULL)
1495 return str;
1496 else
1497 sprintf(buf, INT64_FORMAT, n);
1498 return buf;
1499 }
1501 void TypeLong::dump2( Dict &d, uint depth, outputStream *st ) const {
1502 char buf[80], buf2[80];
1503 if (_lo == min_jlong && _hi == max_jlong)
1504 st->print("long");
1505 else if (is_con())
1506 st->print("long:%s", longname(buf, get_con()));
1507 else if (_hi == max_jlong)
1508 st->print("long:>=%s", longname(buf, _lo));
1509 else if (_lo == min_jlong)
1510 st->print("long:<=%s", longname(buf, _hi));
1511 else
1512 st->print("long:%s..%s", longname(buf, _lo), longname(buf2, _hi));
1514 if (_widen != 0 && this != TypeLong::LONG)
1515 st->print(":%.*s", _widen, "wwww");
1516 }
1517 #endif
1519 //------------------------------singleton--------------------------------------
1520 // TRUE if Type is a singleton type, FALSE otherwise. Singletons are simple
1521 // constants
1522 bool TypeLong::singleton(void) const {
1523 return _lo >= _hi;
1524 }
1526 bool TypeLong::empty(void) const {
1527 return _lo > _hi;
1528 }
1530 //=============================================================================
1531 // Convenience common pre-built types.
1532 const TypeTuple *TypeTuple::IFBOTH; // Return both arms of IF as reachable
1533 const TypeTuple *TypeTuple::IFFALSE;
1534 const TypeTuple *TypeTuple::IFTRUE;
1535 const TypeTuple *TypeTuple::IFNEITHER;
1536 const TypeTuple *TypeTuple::LOOPBODY;
1537 const TypeTuple *TypeTuple::MEMBAR;
1538 const TypeTuple *TypeTuple::STORECONDITIONAL;
1539 const TypeTuple *TypeTuple::START_I2C;
1540 const TypeTuple *TypeTuple::INT_PAIR;
1541 const TypeTuple *TypeTuple::LONG_PAIR;
1544 //------------------------------make-------------------------------------------
1545 // Make a TypeTuple from the range of a method signature
1546 const TypeTuple *TypeTuple::make_range(ciSignature* sig) {
1547 ciType* return_type = sig->return_type();
1548 uint total_fields = TypeFunc::Parms + return_type->size();
1549 const Type **field_array = fields(total_fields);
1550 switch (return_type->basic_type()) {
1551 case T_LONG:
1552 field_array[TypeFunc::Parms] = TypeLong::LONG;
1553 field_array[TypeFunc::Parms+1] = Type::HALF;
1554 break;
1555 case T_DOUBLE:
1556 field_array[TypeFunc::Parms] = Type::DOUBLE;
1557 field_array[TypeFunc::Parms+1] = Type::HALF;
1558 break;
1559 case T_OBJECT:
1560 case T_ARRAY:
1561 case T_BOOLEAN:
1562 case T_CHAR:
1563 case T_FLOAT:
1564 case T_BYTE:
1565 case T_SHORT:
1566 case T_INT:
1567 field_array[TypeFunc::Parms] = get_const_type(return_type);
1568 break;
1569 case T_VOID:
1570 break;
1571 default:
1572 ShouldNotReachHere();
1573 }
1574 return (TypeTuple*)(new TypeTuple(total_fields,field_array))->hashcons();
1575 }
1577 // Make a TypeTuple from the domain of a method signature
1578 const TypeTuple *TypeTuple::make_domain(ciInstanceKlass* recv, ciSignature* sig) {
1579 uint total_fields = TypeFunc::Parms + sig->size();
1581 uint pos = TypeFunc::Parms;
1582 const Type **field_array;
1583 if (recv != NULL) {
1584 total_fields++;
1585 field_array = fields(total_fields);
1586 // Use get_const_type here because it respects UseUniqueSubclasses:
1587 field_array[pos++] = get_const_type(recv)->join(TypePtr::NOTNULL);
1588 } else {
1589 field_array = fields(total_fields);
1590 }
1592 int i = 0;
1593 while (pos < total_fields) {
1594 ciType* type = sig->type_at(i);
1596 switch (type->basic_type()) {
1597 case T_LONG:
1598 field_array[pos++] = TypeLong::LONG;
1599 field_array[pos++] = Type::HALF;
1600 break;
1601 case T_DOUBLE:
1602 field_array[pos++] = Type::DOUBLE;
1603 field_array[pos++] = Type::HALF;
1604 break;
1605 case T_OBJECT:
1606 case T_ARRAY:
1607 case T_BOOLEAN:
1608 case T_CHAR:
1609 case T_FLOAT:
1610 case T_BYTE:
1611 case T_SHORT:
1612 case T_INT:
1613 field_array[pos++] = get_const_type(type);
1614 break;
1615 default:
1616 ShouldNotReachHere();
1617 }
1618 i++;
1619 }
1620 return (TypeTuple*)(new TypeTuple(total_fields,field_array))->hashcons();
1621 }
1623 const TypeTuple *TypeTuple::make( uint cnt, const Type **fields ) {
1624 return (TypeTuple*)(new TypeTuple(cnt,fields))->hashcons();
1625 }
1627 //------------------------------fields-----------------------------------------
1628 // Subroutine call type with space allocated for argument types
1629 const Type **TypeTuple::fields( uint arg_cnt ) {
1630 const Type **flds = (const Type **)(Compile::current()->type_arena()->Amalloc_4((TypeFunc::Parms+arg_cnt)*sizeof(Type*) ));
1631 flds[TypeFunc::Control ] = Type::CONTROL;
1632 flds[TypeFunc::I_O ] = Type::ABIO;
1633 flds[TypeFunc::Memory ] = Type::MEMORY;
1634 flds[TypeFunc::FramePtr ] = TypeRawPtr::BOTTOM;
1635 flds[TypeFunc::ReturnAdr] = Type::RETURN_ADDRESS;
1637 return flds;
1638 }
1640 //------------------------------meet-------------------------------------------
1641 // Compute the MEET of two types. It returns a new Type object.
1642 const Type *TypeTuple::xmeet( const Type *t ) const {
1643 // Perform a fast test for common case; meeting the same types together.
1644 if( this == t ) return this; // Meeting same type-rep?
1646 // Current "this->_base" is Tuple
1647 switch (t->base()) { // switch on original type
1649 case Bottom: // Ye Olde Default
1650 return t;
1652 default: // All else is a mistake
1653 typerr(t);
1655 case Tuple: { // Meeting 2 signatures?
1656 const TypeTuple *x = t->is_tuple();
1657 assert( _cnt == x->_cnt, "" );
1658 const Type **fields = (const Type **)(Compile::current()->type_arena()->Amalloc_4( _cnt*sizeof(Type*) ));
1659 for( uint i=0; i<_cnt; i++ )
1660 fields[i] = field_at(i)->xmeet( x->field_at(i) );
1661 return TypeTuple::make(_cnt,fields);
1662 }
1663 case Top:
1664 break;
1665 }
1666 return this; // Return the double constant
1667 }
1669 //------------------------------xdual------------------------------------------
1670 // Dual: compute field-by-field dual
1671 const Type *TypeTuple::xdual() const {
1672 const Type **fields = (const Type **)(Compile::current()->type_arena()->Amalloc_4( _cnt*sizeof(Type*) ));
1673 for( uint i=0; i<_cnt; i++ )
1674 fields[i] = _fields[i]->dual();
1675 return new TypeTuple(_cnt,fields);
1676 }
1678 //------------------------------eq---------------------------------------------
1679 // Structural equality check for Type representations
1680 bool TypeTuple::eq( const Type *t ) const {
1681 const TypeTuple *s = (const TypeTuple *)t;
1682 if (_cnt != s->_cnt) return false; // Unequal field counts
1683 for (uint i = 0; i < _cnt; i++)
1684 if (field_at(i) != s->field_at(i)) // POINTER COMPARE! NO RECURSION!
1685 return false; // Missed
1686 return true;
1687 }
1689 //------------------------------hash-------------------------------------------
1690 // Type-specific hashing function.
1691 int TypeTuple::hash(void) const {
1692 intptr_t sum = _cnt;
1693 for( uint i=0; i<_cnt; i++ )
1694 sum += (intptr_t)_fields[i]; // Hash on pointers directly
1695 return sum;
1696 }
1698 //------------------------------dump2------------------------------------------
1699 // Dump signature Type
1700 #ifndef PRODUCT
1701 void TypeTuple::dump2( Dict &d, uint depth, outputStream *st ) const {
1702 st->print("{");
1703 if( !depth || d[this] ) { // Check for recursive print
1704 st->print("...}");
1705 return;
1706 }
1707 d.Insert((void*)this, (void*)this); // Stop recursion
1708 if( _cnt ) {
1709 uint i;
1710 for( i=0; i<_cnt-1; i++ ) {
1711 st->print("%d:", i);
1712 _fields[i]->dump2(d, depth-1, st);
1713 st->print(", ");
1714 }
1715 st->print("%d:", i);
1716 _fields[i]->dump2(d, depth-1, st);
1717 }
1718 st->print("}");
1719 }
1720 #endif
1722 //------------------------------singleton--------------------------------------
1723 // TRUE if Type is a singleton type, FALSE otherwise. Singletons are simple
1724 // constants (Ldi nodes). Singletons are integer, float or double constants
1725 // or a single symbol.
1726 bool TypeTuple::singleton(void) const {
1727 return false; // Never a singleton
1728 }
1730 bool TypeTuple::empty(void) const {
1731 for( uint i=0; i<_cnt; i++ ) {
1732 if (_fields[i]->empty()) return true;
1733 }
1734 return false;
1735 }
1737 //=============================================================================
1738 // Convenience common pre-built types.
1740 inline const TypeInt* normalize_array_size(const TypeInt* size) {
1741 // Certain normalizations keep us sane when comparing types.
1742 // We do not want arrayOop variables to differ only by the wideness
1743 // of their index types. Pick minimum wideness, since that is the
1744 // forced wideness of small ranges anyway.
1745 if (size->_widen != Type::WidenMin)
1746 return TypeInt::make(size->_lo, size->_hi, Type::WidenMin);
1747 else
1748 return size;
1749 }
1751 //------------------------------make-------------------------------------------
1752 const TypeAry *TypeAry::make( const Type *elem, const TypeInt *size) {
1753 if (UseCompressedOops && elem->isa_oopptr()) {
1754 elem = elem->is_oopptr()->make_narrowoop();
1755 }
1756 size = normalize_array_size(size);
1757 return (TypeAry*)(new TypeAry(elem,size))->hashcons();
1758 }
1760 //------------------------------meet-------------------------------------------
1761 // Compute the MEET of two types. It returns a new Type object.
1762 const Type *TypeAry::xmeet( const Type *t ) const {
1763 // Perform a fast test for common case; meeting the same types together.
1764 if( this == t ) return this; // Meeting same type-rep?
1766 // Current "this->_base" is Ary
1767 switch (t->base()) { // switch on original type
1769 case Bottom: // Ye Olde Default
1770 return t;
1772 default: // All else is a mistake
1773 typerr(t);
1775 case Array: { // Meeting 2 arrays?
1776 const TypeAry *a = t->is_ary();
1777 return TypeAry::make(_elem->meet(a->_elem),
1778 _size->xmeet(a->_size)->is_int());
1779 }
1780 case Top:
1781 break;
1782 }
1783 return this; // Return the double constant
1784 }
1786 //------------------------------xdual------------------------------------------
1787 // Dual: compute field-by-field dual
1788 const Type *TypeAry::xdual() const {
1789 const TypeInt* size_dual = _size->dual()->is_int();
1790 size_dual = normalize_array_size(size_dual);
1791 return new TypeAry( _elem->dual(), size_dual);
1792 }
1794 //------------------------------eq---------------------------------------------
1795 // Structural equality check for Type representations
1796 bool TypeAry::eq( const Type *t ) const {
1797 const TypeAry *a = (const TypeAry*)t;
1798 return _elem == a->_elem &&
1799 _size == a->_size;
1800 }
1802 //------------------------------hash-------------------------------------------
1803 // Type-specific hashing function.
1804 int TypeAry::hash(void) const {
1805 return (intptr_t)_elem + (intptr_t)_size;
1806 }
1808 //------------------------------dump2------------------------------------------
1809 #ifndef PRODUCT
1810 void TypeAry::dump2( Dict &d, uint depth, outputStream *st ) const {
1811 _elem->dump2(d, depth, st);
1812 st->print("[");
1813 _size->dump2(d, depth, st);
1814 st->print("]");
1815 }
1816 #endif
1818 //------------------------------singleton--------------------------------------
1819 // TRUE if Type is a singleton type, FALSE otherwise. Singletons are simple
1820 // constants (Ldi nodes). Singletons are integer, float or double constants
1821 // or a single symbol.
1822 bool TypeAry::singleton(void) const {
1823 return false; // Never a singleton
1824 }
1826 bool TypeAry::empty(void) const {
1827 return _elem->empty() || _size->empty();
1828 }
1830 //--------------------------ary_must_be_exact----------------------------------
1831 bool TypeAry::ary_must_be_exact() const {
1832 if (!UseExactTypes) return false;
1833 // This logic looks at the element type of an array, and returns true
1834 // if the element type is either a primitive or a final instance class.
1835 // In such cases, an array built on this ary must have no subclasses.
1836 if (_elem == BOTTOM) return false; // general array not exact
1837 if (_elem == TOP ) return false; // inverted general array not exact
1838 const TypeOopPtr* toop = NULL;
1839 if (UseCompressedOops) {
1840 const TypeNarrowOop* noop = _elem->isa_narrowoop();
1841 if (noop) toop = noop->make_oopptr()->isa_oopptr();
1842 } else {
1843 toop = _elem->isa_oopptr();
1844 }
1845 if (!toop) return true; // a primitive type, like int
1846 ciKlass* tklass = toop->klass();
1847 if (tklass == NULL) return false; // unloaded class
1848 if (!tklass->is_loaded()) return false; // unloaded class
1849 const TypeInstPtr* tinst;
1850 if (_elem->isa_narrowoop())
1851 tinst = _elem->is_narrowoop()->make_oopptr()->isa_instptr();
1852 else
1853 tinst = _elem->isa_instptr();
1854 if (tinst) return tklass->as_instance_klass()->is_final();
1855 const TypeAryPtr* tap;
1856 if (_elem->isa_narrowoop())
1857 tap = _elem->is_narrowoop()->make_oopptr()->isa_aryptr();
1858 else
1859 tap = _elem->isa_aryptr();
1860 if (tap) return tap->ary()->ary_must_be_exact();
1861 return false;
1862 }
1864 //=============================================================================
1865 // Convenience common pre-built types.
1866 const TypePtr *TypePtr::NULL_PTR;
1867 const TypePtr *TypePtr::NOTNULL;
1868 const TypePtr *TypePtr::BOTTOM;
1870 //------------------------------meet-------------------------------------------
1871 // Meet over the PTR enum
1872 const TypePtr::PTR TypePtr::ptr_meet[TypePtr::lastPTR][TypePtr::lastPTR] = {
1873 // TopPTR, AnyNull, Constant, Null, NotNull, BotPTR,
1874 { /* Top */ TopPTR, AnyNull, Constant, Null, NotNull, BotPTR,},
1875 { /* AnyNull */ AnyNull, AnyNull, Constant, BotPTR, NotNull, BotPTR,},
1876 { /* Constant*/ Constant, Constant, Constant, BotPTR, NotNull, BotPTR,},
1877 { /* Null */ Null, BotPTR, BotPTR, Null, BotPTR, BotPTR,},
1878 { /* NotNull */ NotNull, NotNull, NotNull, BotPTR, NotNull, BotPTR,},
1879 { /* BotPTR */ BotPTR, BotPTR, BotPTR, BotPTR, BotPTR, BotPTR,}
1880 };
1882 //------------------------------make-------------------------------------------
1883 const TypePtr *TypePtr::make( TYPES t, enum PTR ptr, int offset ) {
1884 return (TypePtr*)(new TypePtr(t,ptr,offset))->hashcons();
1885 }
1887 //------------------------------cast_to_ptr_type-------------------------------
1888 const Type *TypePtr::cast_to_ptr_type(PTR ptr) const {
1889 assert(_base == AnyPtr, "subclass must override cast_to_ptr_type");
1890 if( ptr == _ptr ) return this;
1891 return make(_base, ptr, _offset);
1892 }
1894 //------------------------------get_con----------------------------------------
1895 intptr_t TypePtr::get_con() const {
1896 assert( _ptr == Null, "" );
1897 return _offset;
1898 }
1900 //------------------------------meet-------------------------------------------
1901 // Compute the MEET of two types. It returns a new Type object.
1902 const Type *TypePtr::xmeet( const Type *t ) const {
1903 // Perform a fast test for common case; meeting the same types together.
1904 if( this == t ) return this; // Meeting same type-rep?
1906 // Current "this->_base" is AnyPtr
1907 switch (t->base()) { // switch on original type
1908 case Int: // Mixing ints & oops happens when javac
1909 case Long: // reuses local variables
1910 case FloatTop:
1911 case FloatCon:
1912 case FloatBot:
1913 case DoubleTop:
1914 case DoubleCon:
1915 case DoubleBot:
1916 case NarrowOop:
1917 case Bottom: // Ye Olde Default
1918 return Type::BOTTOM;
1919 case Top:
1920 return this;
1922 case AnyPtr: { // Meeting to AnyPtrs
1923 const TypePtr *tp = t->is_ptr();
1924 return make( AnyPtr, meet_ptr(tp->ptr()), meet_offset(tp->offset()) );
1925 }
1926 case RawPtr: // For these, flip the call around to cut down
1927 case OopPtr:
1928 case InstPtr: // on the cases I have to handle.
1929 case KlassPtr:
1930 case AryPtr:
1931 return t->xmeet(this); // Call in reverse direction
1932 default: // All else is a mistake
1933 typerr(t);
1935 }
1936 return this;
1937 }
1939 //------------------------------meet_offset------------------------------------
1940 int TypePtr::meet_offset( int offset ) const {
1941 // Either is 'TOP' offset? Return the other offset!
1942 if( _offset == OffsetTop ) return offset;
1943 if( offset == OffsetTop ) return _offset;
1944 // If either is different, return 'BOTTOM' offset
1945 if( _offset != offset ) return OffsetBot;
1946 return _offset;
1947 }
1949 //------------------------------dual_offset------------------------------------
1950 int TypePtr::dual_offset( ) const {
1951 if( _offset == OffsetTop ) return OffsetBot;// Map 'TOP' into 'BOTTOM'
1952 if( _offset == OffsetBot ) return OffsetTop;// Map 'BOTTOM' into 'TOP'
1953 return _offset; // Map everything else into self
1954 }
1956 //------------------------------xdual------------------------------------------
1957 // Dual: compute field-by-field dual
1958 const TypePtr::PTR TypePtr::ptr_dual[TypePtr::lastPTR] = {
1959 BotPTR, NotNull, Constant, Null, AnyNull, TopPTR
1960 };
1961 const Type *TypePtr::xdual() const {
1962 return new TypePtr( AnyPtr, dual_ptr(), dual_offset() );
1963 }
1965 //------------------------------add_offset-------------------------------------
1966 const TypePtr *TypePtr::add_offset( int offset ) const {
1967 if( offset == 0 ) return this; // No change
1968 if( _offset == OffsetBot ) return this;
1969 if( offset == OffsetBot ) offset = OffsetBot;
1970 else if( _offset == OffsetTop || offset == OffsetTop ) offset = OffsetTop;
1971 else offset += _offset;
1972 return make( AnyPtr, _ptr, offset );
1973 }
1975 //------------------------------eq---------------------------------------------
1976 // Structural equality check for Type representations
1977 bool TypePtr::eq( const Type *t ) const {
1978 const TypePtr *a = (const TypePtr*)t;
1979 return _ptr == a->ptr() && _offset == a->offset();
1980 }
1982 //------------------------------hash-------------------------------------------
1983 // Type-specific hashing function.
1984 int TypePtr::hash(void) const {
1985 return _ptr + _offset;
1986 }
1988 //------------------------------dump2------------------------------------------
1989 const char *const TypePtr::ptr_msg[TypePtr::lastPTR] = {
1990 "TopPTR","AnyNull","Constant","NULL","NotNull","BotPTR"
1991 };
1993 #ifndef PRODUCT
1994 void TypePtr::dump2( Dict &d, uint depth, outputStream *st ) const {
1995 if( _ptr == Null ) st->print("NULL");
1996 else st->print("%s *", ptr_msg[_ptr]);
1997 if( _offset == OffsetTop ) st->print("+top");
1998 else if( _offset == OffsetBot ) st->print("+bot");
1999 else if( _offset ) st->print("+%d", _offset);
2000 }
2001 #endif
2003 //------------------------------singleton--------------------------------------
2004 // TRUE if Type is a singleton type, FALSE otherwise. Singletons are simple
2005 // constants
2006 bool TypePtr::singleton(void) const {
2007 // TopPTR, Null, AnyNull, Constant are all singletons
2008 return (_offset != OffsetBot) && !below_centerline(_ptr);
2009 }
2011 bool TypePtr::empty(void) const {
2012 return (_offset == OffsetTop) || above_centerline(_ptr);
2013 }
2015 //=============================================================================
2016 // Convenience common pre-built types.
2017 const TypeRawPtr *TypeRawPtr::BOTTOM;
2018 const TypeRawPtr *TypeRawPtr::NOTNULL;
2020 //------------------------------make-------------------------------------------
2021 const TypeRawPtr *TypeRawPtr::make( enum PTR ptr ) {
2022 assert( ptr != Constant, "what is the constant?" );
2023 assert( ptr != Null, "Use TypePtr for NULL" );
2024 return (TypeRawPtr*)(new TypeRawPtr(ptr,0))->hashcons();
2025 }
2027 const TypeRawPtr *TypeRawPtr::make( address bits ) {
2028 assert( bits, "Use TypePtr for NULL" );
2029 return (TypeRawPtr*)(new TypeRawPtr(Constant,bits))->hashcons();
2030 }
2032 //------------------------------cast_to_ptr_type-------------------------------
2033 const Type *TypeRawPtr::cast_to_ptr_type(PTR ptr) const {
2034 assert( ptr != Constant, "what is the constant?" );
2035 assert( ptr != Null, "Use TypePtr for NULL" );
2036 assert( _bits==0, "Why cast a constant address?");
2037 if( ptr == _ptr ) return this;
2038 return make(ptr);
2039 }
2041 //------------------------------get_con----------------------------------------
2042 intptr_t TypeRawPtr::get_con() const {
2043 assert( _ptr == Null || _ptr == Constant, "" );
2044 return (intptr_t)_bits;
2045 }
2047 //------------------------------meet-------------------------------------------
2048 // Compute the MEET of two types. It returns a new Type object.
2049 const Type *TypeRawPtr::xmeet( const Type *t ) const {
2050 // Perform a fast test for common case; meeting the same types together.
2051 if( this == t ) return this; // Meeting same type-rep?
2053 // Current "this->_base" is RawPtr
2054 switch( t->base() ) { // switch on original type
2055 case Bottom: // Ye Olde Default
2056 return t;
2057 case Top:
2058 return this;
2059 case AnyPtr: // Meeting to AnyPtrs
2060 break;
2061 case RawPtr: { // might be top, bot, any/not or constant
2062 enum PTR tptr = t->is_ptr()->ptr();
2063 enum PTR ptr = meet_ptr( tptr );
2064 if( ptr == Constant ) { // Cannot be equal constants, so...
2065 if( tptr == Constant && _ptr != Constant) return t;
2066 if( _ptr == Constant && tptr != Constant) return this;
2067 ptr = NotNull; // Fall down in lattice
2068 }
2069 return make( ptr );
2070 }
2072 case OopPtr:
2073 case InstPtr:
2074 case KlassPtr:
2075 case AryPtr:
2076 return TypePtr::BOTTOM; // Oop meet raw is not well defined
2077 default: // All else is a mistake
2078 typerr(t);
2079 }
2081 // Found an AnyPtr type vs self-RawPtr type
2082 const TypePtr *tp = t->is_ptr();
2083 switch (tp->ptr()) {
2084 case TypePtr::TopPTR: return this;
2085 case TypePtr::BotPTR: return t;
2086 case TypePtr::Null:
2087 if( _ptr == TypePtr::TopPTR ) return t;
2088 return TypeRawPtr::BOTTOM;
2089 case TypePtr::NotNull: return TypePtr::make( AnyPtr, meet_ptr(TypePtr::NotNull), tp->meet_offset(0) );
2090 case TypePtr::AnyNull:
2091 if( _ptr == TypePtr::Constant) return this;
2092 return make( meet_ptr(TypePtr::AnyNull) );
2093 default: ShouldNotReachHere();
2094 }
2095 return this;
2096 }
2098 //------------------------------xdual------------------------------------------
2099 // Dual: compute field-by-field dual
2100 const Type *TypeRawPtr::xdual() const {
2101 return new TypeRawPtr( dual_ptr(), _bits );
2102 }
2104 //------------------------------add_offset-------------------------------------
2105 const TypePtr *TypeRawPtr::add_offset( int offset ) const {
2106 if( offset == OffsetTop ) return BOTTOM; // Undefined offset-> undefined pointer
2107 if( offset == OffsetBot ) return BOTTOM; // Unknown offset-> unknown pointer
2108 if( offset == 0 ) return this; // No change
2109 switch (_ptr) {
2110 case TypePtr::TopPTR:
2111 case TypePtr::BotPTR:
2112 case TypePtr::NotNull:
2113 return this;
2114 case TypePtr::Null:
2115 case TypePtr::Constant:
2116 return make( _bits+offset );
2117 default: ShouldNotReachHere();
2118 }
2119 return NULL; // Lint noise
2120 }
2122 //------------------------------eq---------------------------------------------
2123 // Structural equality check for Type representations
2124 bool TypeRawPtr::eq( const Type *t ) const {
2125 const TypeRawPtr *a = (const TypeRawPtr*)t;
2126 return _bits == a->_bits && TypePtr::eq(t);
2127 }
2129 //------------------------------hash-------------------------------------------
2130 // Type-specific hashing function.
2131 int TypeRawPtr::hash(void) const {
2132 return (intptr_t)_bits + TypePtr::hash();
2133 }
2135 //------------------------------dump2------------------------------------------
2136 #ifndef PRODUCT
2137 void TypeRawPtr::dump2( Dict &d, uint depth, outputStream *st ) const {
2138 if( _ptr == Constant )
2139 st->print(INTPTR_FORMAT, _bits);
2140 else
2141 st->print("rawptr:%s", ptr_msg[_ptr]);
2142 }
2143 #endif
2145 //=============================================================================
2146 // Convenience common pre-built type.
2147 const TypeOopPtr *TypeOopPtr::BOTTOM;
2149 //------------------------------make-------------------------------------------
2150 const TypeOopPtr *TypeOopPtr::make(PTR ptr,
2151 int offset) {
2152 assert(ptr != Constant, "no constant generic pointers");
2153 ciKlass* k = ciKlassKlass::make();
2154 bool xk = false;
2155 ciObject* o = NULL;
2156 return (TypeOopPtr*)(new TypeOopPtr(OopPtr, ptr, k, xk, o, offset, UNKNOWN_INSTANCE))->hashcons();
2157 }
2160 //------------------------------cast_to_ptr_type-------------------------------
2161 const Type *TypeOopPtr::cast_to_ptr_type(PTR ptr) const {
2162 assert(_base == OopPtr, "subclass must override cast_to_ptr_type");
2163 if( ptr == _ptr ) return this;
2164 return make(ptr, _offset);
2165 }
2167 //-----------------------------cast_to_instance-------------------------------
2168 const TypeOopPtr *TypeOopPtr::cast_to_instance(int instance_id) const {
2169 // There are no instances of a general oop.
2170 // Return self unchanged.
2171 return this;
2172 }
2174 //-----------------------------cast_to_exactness-------------------------------
2175 const Type *TypeOopPtr::cast_to_exactness(bool klass_is_exact) const {
2176 // There is no such thing as an exact general oop.
2177 // Return self unchanged.
2178 return this;
2179 }
2182 //------------------------------as_klass_type----------------------------------
2183 // Return the klass type corresponding to this instance or array type.
2184 // It is the type that is loaded from an object of this type.
2185 const TypeKlassPtr* TypeOopPtr::as_klass_type() const {
2186 ciKlass* k = klass();
2187 bool xk = klass_is_exact();
2188 if (k == NULL || !k->is_java_klass())
2189 return TypeKlassPtr::OBJECT;
2190 else
2191 return TypeKlassPtr::make(xk? Constant: NotNull, k, 0);
2192 }
2195 //------------------------------meet-------------------------------------------
2196 // Compute the MEET of two types. It returns a new Type object.
2197 const Type *TypeOopPtr::xmeet( const Type *t ) const {
2198 // Perform a fast test for common case; meeting the same types together.
2199 if( this == t ) return this; // Meeting same type-rep?
2201 // Current "this->_base" is OopPtr
2202 switch (t->base()) { // switch on original type
2204 case Int: // Mixing ints & oops happens when javac
2205 case Long: // reuses local variables
2206 case FloatTop:
2207 case FloatCon:
2208 case FloatBot:
2209 case DoubleTop:
2210 case DoubleCon:
2211 case DoubleBot:
2212 case Bottom: // Ye Olde Default
2213 return Type::BOTTOM;
2214 case Top:
2215 return this;
2217 default: // All else is a mistake
2218 typerr(t);
2220 case RawPtr:
2221 return TypePtr::BOTTOM; // Oop meet raw is not well defined
2223 case AnyPtr: {
2224 // Found an AnyPtr type vs self-OopPtr type
2225 const TypePtr *tp = t->is_ptr();
2226 int offset = meet_offset(tp->offset());
2227 PTR ptr = meet_ptr(tp->ptr());
2228 switch (tp->ptr()) {
2229 case Null:
2230 if (ptr == Null) return TypePtr::make(AnyPtr, ptr, offset);
2231 // else fall through:
2232 case TopPTR:
2233 case AnyNull:
2234 return make(ptr, offset);
2235 case BotPTR:
2236 case NotNull:
2237 return TypePtr::make(AnyPtr, ptr, offset);
2238 default: typerr(t);
2239 }
2240 }
2242 case OopPtr: { // Meeting to other OopPtrs
2243 const TypeOopPtr *tp = t->is_oopptr();
2244 return make( meet_ptr(tp->ptr()), meet_offset(tp->offset()) );
2245 }
2247 case InstPtr: // For these, flip the call around to cut down
2248 case KlassPtr: // on the cases I have to handle.
2249 case AryPtr:
2250 return t->xmeet(this); // Call in reverse direction
2252 } // End of switch
2253 return this; // Return the double constant
2254 }
2257 //------------------------------xdual------------------------------------------
2258 // Dual of a pure heap pointer. No relevant klass or oop information.
2259 const Type *TypeOopPtr::xdual() const {
2260 assert(klass() == ciKlassKlass::make(), "no klasses here");
2261 assert(const_oop() == NULL, "no constants here");
2262 return new TypeOopPtr(_base, dual_ptr(), klass(), klass_is_exact(), const_oop(), dual_offset(), dual_instance() );
2263 }
2265 //--------------------------make_from_klass_common-----------------------------
2266 // Computes the element-type given a klass.
2267 const TypeOopPtr* TypeOopPtr::make_from_klass_common(ciKlass *klass, bool klass_change, bool try_for_exact) {
2268 assert(klass->is_java_klass(), "must be java language klass");
2269 if (klass->is_instance_klass()) {
2270 Compile* C = Compile::current();
2271 Dependencies* deps = C->dependencies();
2272 assert((deps != NULL) == (C->method() != NULL && C->method()->code_size() > 0), "sanity");
2273 // Element is an instance
2274 bool klass_is_exact = false;
2275 if (klass->is_loaded()) {
2276 // Try to set klass_is_exact.
2277 ciInstanceKlass* ik = klass->as_instance_klass();
2278 klass_is_exact = ik->is_final();
2279 if (!klass_is_exact && klass_change
2280 && deps != NULL && UseUniqueSubclasses) {
2281 ciInstanceKlass* sub = ik->unique_concrete_subklass();
2282 if (sub != NULL) {
2283 deps->assert_abstract_with_unique_concrete_subtype(ik, sub);
2284 klass = ik = sub;
2285 klass_is_exact = sub->is_final();
2286 }
2287 }
2288 if (!klass_is_exact && try_for_exact
2289 && deps != NULL && UseExactTypes) {
2290 if (!ik->is_interface() && !ik->has_subklass()) {
2291 // Add a dependence; if concrete subclass added we need to recompile
2292 deps->assert_leaf_type(ik);
2293 klass_is_exact = true;
2294 }
2295 }
2296 }
2297 return TypeInstPtr::make(TypePtr::BotPTR, klass, klass_is_exact, NULL, 0);
2298 } else if (klass->is_obj_array_klass()) {
2299 // Element is an object array. Recursively call ourself.
2300 const TypeOopPtr *etype = TypeOopPtr::make_from_klass_common(klass->as_obj_array_klass()->element_klass(), false, try_for_exact);
2301 bool xk = etype->klass_is_exact();
2302 const TypeAry* arr0 = TypeAry::make(etype, TypeInt::POS);
2303 // We used to pass NotNull in here, asserting that the sub-arrays
2304 // are all not-null. This is not true in generally, as code can
2305 // slam NULLs down in the subarrays.
2306 const TypeAryPtr* arr = TypeAryPtr::make(TypePtr::BotPTR, arr0, klass, xk, 0);
2307 return arr;
2308 } else if (klass->is_type_array_klass()) {
2309 // Element is an typeArray
2310 const Type* etype = get_const_basic_type(klass->as_type_array_klass()->element_type());
2311 const TypeAry* arr0 = TypeAry::make(etype, TypeInt::POS);
2312 // We used to pass NotNull in here, asserting that the array pointer
2313 // is not-null. That was not true in general.
2314 const TypeAryPtr* arr = TypeAryPtr::make(TypePtr::BotPTR, arr0, klass, true, 0);
2315 return arr;
2316 } else {
2317 ShouldNotReachHere();
2318 return NULL;
2319 }
2320 }
2322 //------------------------------make_from_constant-----------------------------
2323 // Make a java pointer from an oop constant
2324 const TypeOopPtr* TypeOopPtr::make_from_constant(ciObject* o) {
2325 if (o->is_method_data() || o->is_method()) {
2326 // Treat much like a typeArray of bytes, like below, but fake the type...
2327 assert(o->has_encoding(), "must be a perm space object");
2328 const Type* etype = (Type*)get_const_basic_type(T_BYTE);
2329 const TypeAry* arr0 = TypeAry::make(etype, TypeInt::POS);
2330 ciKlass *klass = ciTypeArrayKlass::make((BasicType) T_BYTE);
2331 assert(o->has_encoding(), "method data oops should be tenured");
2332 const TypeAryPtr* arr = TypeAryPtr::make(TypePtr::Constant, o, arr0, klass, true, 0);
2333 return arr;
2334 } else {
2335 assert(o->is_java_object(), "must be java language object");
2336 assert(!o->is_null_object(), "null object not yet handled here.");
2337 ciKlass *klass = o->klass();
2338 if (klass->is_instance_klass()) {
2339 // Element is an instance
2340 if (!o->has_encoding()) { // not a perm-space constant
2341 // %%% remove this restriction by rewriting non-perm ConPNodes in a later phase
2342 return TypeInstPtr::make(TypePtr::NotNull, klass, true, NULL, 0);
2343 }
2344 return TypeInstPtr::make(o);
2345 } else if (klass->is_obj_array_klass()) {
2346 // Element is an object array. Recursively call ourself.
2347 const Type *etype =
2348 TypeOopPtr::make_from_klass_raw(klass->as_obj_array_klass()->element_klass());
2349 const TypeAry* arr0 = TypeAry::make(etype, TypeInt::make(o->as_array()->length()));
2350 // We used to pass NotNull in here, asserting that the sub-arrays
2351 // are all not-null. This is not true in generally, as code can
2352 // slam NULLs down in the subarrays.
2353 if (!o->has_encoding()) { // not a perm-space constant
2354 // %%% remove this restriction by rewriting non-perm ConPNodes in a later phase
2355 return TypeAryPtr::make(TypePtr::NotNull, arr0, klass, true, 0);
2356 }
2357 const TypeAryPtr* arr = TypeAryPtr::make(TypePtr::Constant, o, arr0, klass, true, 0);
2358 return arr;
2359 } else if (klass->is_type_array_klass()) {
2360 // Element is an typeArray
2361 const Type* etype =
2362 (Type*)get_const_basic_type(klass->as_type_array_klass()->element_type());
2363 const TypeAry* arr0 = TypeAry::make(etype, TypeInt::make(o->as_array()->length()));
2364 // We used to pass NotNull in here, asserting that the array pointer
2365 // is not-null. That was not true in general.
2366 if (!o->has_encoding()) { // not a perm-space constant
2367 // %%% remove this restriction by rewriting non-perm ConPNodes in a later phase
2368 return TypeAryPtr::make(TypePtr::NotNull, arr0, klass, true, 0);
2369 }
2370 const TypeAryPtr* arr = TypeAryPtr::make(TypePtr::Constant, o, arr0, klass, true, 0);
2371 return arr;
2372 }
2373 }
2375 ShouldNotReachHere();
2376 return NULL;
2377 }
2379 //------------------------------get_con----------------------------------------
2380 intptr_t TypeOopPtr::get_con() const {
2381 assert( _ptr == Null || _ptr == Constant, "" );
2382 assert( _offset >= 0, "" );
2384 if (_offset != 0) {
2385 // After being ported to the compiler interface, the compiler no longer
2386 // directly manipulates the addresses of oops. Rather, it only has a pointer
2387 // to a handle at compile time. This handle is embedded in the generated
2388 // code and dereferenced at the time the nmethod is made. Until that time,
2389 // it is not reasonable to do arithmetic with the addresses of oops (we don't
2390 // have access to the addresses!). This does not seem to currently happen,
2391 // but this assertion here is to help prevent its occurrance.
2392 tty->print_cr("Found oop constant with non-zero offset");
2393 ShouldNotReachHere();
2394 }
2396 return (intptr_t)const_oop()->encoding();
2397 }
2400 //-----------------------------filter------------------------------------------
2401 // Do not allow interface-vs.-noninterface joins to collapse to top.
2402 const Type *TypeOopPtr::filter( const Type *kills ) const {
2404 const Type* ft = join(kills);
2405 const TypeInstPtr* ftip = ft->isa_instptr();
2406 const TypeInstPtr* ktip = kills->isa_instptr();
2408 if (ft->empty()) {
2409 // Check for evil case of 'this' being a class and 'kills' expecting an
2410 // interface. This can happen because the bytecodes do not contain
2411 // enough type info to distinguish a Java-level interface variable
2412 // from a Java-level object variable. If we meet 2 classes which
2413 // both implement interface I, but their meet is at 'j/l/O' which
2414 // doesn't implement I, we have no way to tell if the result should
2415 // be 'I' or 'j/l/O'. Thus we'll pick 'j/l/O'. If this then flows
2416 // into a Phi which "knows" it's an Interface type we'll have to
2417 // uplift the type.
2418 if (!empty() && ktip != NULL && ktip->is_loaded() && ktip->klass()->is_interface())
2419 return kills; // Uplift to interface
2421 return Type::TOP; // Canonical empty value
2422 }
2424 // If we have an interface-typed Phi or cast and we narrow to a class type,
2425 // the join should report back the class. However, if we have a J/L/Object
2426 // class-typed Phi and an interface flows in, it's possible that the meet &
2427 // join report an interface back out. This isn't possible but happens
2428 // because the type system doesn't interact well with interfaces.
2429 if (ftip != NULL && ktip != NULL &&
2430 ftip->is_loaded() && ftip->klass()->is_interface() &&
2431 ktip->is_loaded() && !ktip->klass()->is_interface()) {
2432 // Happens in a CTW of rt.jar, 320-341, no extra flags
2433 return ktip->cast_to_ptr_type(ftip->ptr());
2434 }
2436 return ft;
2437 }
2439 //------------------------------eq---------------------------------------------
2440 // Structural equality check for Type representations
2441 bool TypeOopPtr::eq( const Type *t ) const {
2442 const TypeOopPtr *a = (const TypeOopPtr*)t;
2443 if (_klass_is_exact != a->_klass_is_exact ||
2444 _instance_id != a->_instance_id) return false;
2445 ciObject* one = const_oop();
2446 ciObject* two = a->const_oop();
2447 if (one == NULL || two == NULL) {
2448 return (one == two) && TypePtr::eq(t);
2449 } else {
2450 return one->equals(two) && TypePtr::eq(t);
2451 }
2452 }
2454 //------------------------------hash-------------------------------------------
2455 // Type-specific hashing function.
2456 int TypeOopPtr::hash(void) const {
2457 return
2458 (const_oop() ? const_oop()->hash() : 0) +
2459 _klass_is_exact +
2460 _instance_id +
2461 TypePtr::hash();
2462 }
2464 //------------------------------dump2------------------------------------------
2465 #ifndef PRODUCT
2466 void TypeOopPtr::dump2( Dict &d, uint depth, outputStream *st ) const {
2467 st->print("oopptr:%s", ptr_msg[_ptr]);
2468 if( _klass_is_exact ) st->print(":exact");
2469 if( const_oop() ) st->print(INTPTR_FORMAT, const_oop());
2470 switch( _offset ) {
2471 case OffsetTop: st->print("+top"); break;
2472 case OffsetBot: st->print("+any"); break;
2473 case 0: break;
2474 default: st->print("+%d",_offset); break;
2475 }
2476 if (_instance_id != UNKNOWN_INSTANCE)
2477 st->print(",iid=%d",_instance_id);
2478 }
2479 #endif
2481 //------------------------------singleton--------------------------------------
2482 // TRUE if Type is a singleton type, FALSE otherwise. Singletons are simple
2483 // constants
2484 bool TypeOopPtr::singleton(void) const {
2485 // detune optimizer to not generate constant oop + constant offset as a constant!
2486 // TopPTR, Null, AnyNull, Constant are all singletons
2487 return (_offset == 0) && !below_centerline(_ptr);
2488 }
2490 //------------------------------xadd_offset------------------------------------
2491 int TypeOopPtr::xadd_offset( int offset ) const {
2492 // Adding to 'TOP' offset? Return 'TOP'!
2493 if( _offset == OffsetTop || offset == OffsetTop ) return OffsetTop;
2494 // Adding to 'BOTTOM' offset? Return 'BOTTOM'!
2495 if( _offset == OffsetBot || offset == OffsetBot ) return OffsetBot;
2497 // assert( _offset >= 0 && _offset+offset >= 0, "" );
2498 // It is possible to construct a negative offset during PhaseCCP
2500 return _offset+offset; // Sum valid offsets
2501 }
2503 //------------------------------add_offset-------------------------------------
2504 const TypePtr *TypeOopPtr::add_offset( int offset ) const {
2505 return make( _ptr, xadd_offset(offset) );
2506 }
2508 const TypeNarrowOop* TypeOopPtr::make_narrowoop() const {
2509 return TypeNarrowOop::make(this);
2510 }
2512 int TypeOopPtr::meet_instance(int iid) const {
2513 if (iid == 0) {
2514 return (_instance_id < 0) ? _instance_id : UNKNOWN_INSTANCE;
2515 } else if (_instance_id == UNKNOWN_INSTANCE) {
2516 return (iid < 0) ? iid : UNKNOWN_INSTANCE;
2517 } else {
2518 return (_instance_id == iid) ? iid : UNKNOWN_INSTANCE;
2519 }
2520 }
2522 //=============================================================================
2523 // Convenience common pre-built types.
2524 const TypeInstPtr *TypeInstPtr::NOTNULL;
2525 const TypeInstPtr *TypeInstPtr::BOTTOM;
2526 const TypeInstPtr *TypeInstPtr::MIRROR;
2527 const TypeInstPtr *TypeInstPtr::MARK;
2528 const TypeInstPtr *TypeInstPtr::KLASS;
2530 //------------------------------TypeInstPtr-------------------------------------
2531 TypeInstPtr::TypeInstPtr(PTR ptr, ciKlass* k, bool xk, ciObject* o, int off, int instance_id)
2532 : TypeOopPtr(InstPtr, ptr, k, xk, o, off, instance_id), _name(k->name()) {
2533 assert(k != NULL &&
2534 (k->is_loaded() || o == NULL),
2535 "cannot have constants with non-loaded klass");
2536 };
2538 //------------------------------make-------------------------------------------
2539 const TypeInstPtr *TypeInstPtr::make(PTR ptr,
2540 ciKlass* k,
2541 bool xk,
2542 ciObject* o,
2543 int offset,
2544 int instance_id) {
2545 assert( !k->is_loaded() || k->is_instance_klass() ||
2546 k->is_method_klass(), "Must be for instance or method");
2547 // Either const_oop() is NULL or else ptr is Constant
2548 assert( (!o && ptr != Constant) || (o && ptr == Constant),
2549 "constant pointers must have a value supplied" );
2550 // Ptr is never Null
2551 assert( ptr != Null, "NULL pointers are not typed" );
2553 if (instance_id != UNKNOWN_INSTANCE)
2554 xk = true; // instances are always exactly typed
2555 if (!UseExactTypes) xk = false;
2556 if (ptr == Constant) {
2557 // Note: This case includes meta-object constants, such as methods.
2558 xk = true;
2559 } else if (k->is_loaded()) {
2560 ciInstanceKlass* ik = k->as_instance_klass();
2561 if (!xk && ik->is_final()) xk = true; // no inexact final klass
2562 if (xk && ik->is_interface()) xk = false; // no exact interface
2563 }
2565 // Now hash this baby
2566 TypeInstPtr *result =
2567 (TypeInstPtr*)(new TypeInstPtr(ptr, k, xk, o ,offset, instance_id))->hashcons();
2569 return result;
2570 }
2573 //------------------------------cast_to_ptr_type-------------------------------
2574 const Type *TypeInstPtr::cast_to_ptr_type(PTR ptr) const {
2575 if( ptr == _ptr ) return this;
2576 // Reconstruct _sig info here since not a problem with later lazy
2577 // construction, _sig will show up on demand.
2578 return make(ptr, klass(), klass_is_exact(), const_oop(), _offset);
2579 }
2582 //-----------------------------cast_to_exactness-------------------------------
2583 const Type *TypeInstPtr::cast_to_exactness(bool klass_is_exact) const {
2584 if( klass_is_exact == _klass_is_exact ) return this;
2585 if (!UseExactTypes) return this;
2586 if (!_klass->is_loaded()) return this;
2587 ciInstanceKlass* ik = _klass->as_instance_klass();
2588 if( (ik->is_final() || _const_oop) ) return this; // cannot clear xk
2589 if( ik->is_interface() ) return this; // cannot set xk
2590 return make(ptr(), klass(), klass_is_exact, const_oop(), _offset, _instance_id);
2591 }
2593 //-----------------------------cast_to_instance-------------------------------
2594 const TypeOopPtr *TypeInstPtr::cast_to_instance(int instance_id) const {
2595 if( instance_id == _instance_id) return this;
2596 bool exact = (instance_id == UNKNOWN_INSTANCE) ? _klass_is_exact : true;
2598 return make(ptr(), klass(), exact, const_oop(), _offset, instance_id);
2599 }
2601 //------------------------------xmeet_unloaded---------------------------------
2602 // Compute the MEET of two InstPtrs when at least one is unloaded.
2603 // Assume classes are different since called after check for same name/class-loader
2604 const TypeInstPtr *TypeInstPtr::xmeet_unloaded(const TypeInstPtr *tinst) const {
2605 int off = meet_offset(tinst->offset());
2606 PTR ptr = meet_ptr(tinst->ptr());
2608 const TypeInstPtr *loaded = is_loaded() ? this : tinst;
2609 const TypeInstPtr *unloaded = is_loaded() ? tinst : this;
2610 if( loaded->klass()->equals(ciEnv::current()->Object_klass()) ) {
2611 //
2612 // Meet unloaded class with java/lang/Object
2613 //
2614 // Meet
2615 // | Unloaded Class
2616 // Object | TOP | AnyNull | Constant | NotNull | BOTTOM |
2617 // ===================================================================
2618 // TOP | ..........................Unloaded......................|
2619 // AnyNull | U-AN |................Unloaded......................|
2620 // Constant | ... O-NN .................................. | O-BOT |
2621 // NotNull | ... O-NN .................................. | O-BOT |
2622 // BOTTOM | ........................Object-BOTTOM ..................|
2623 //
2624 assert(loaded->ptr() != TypePtr::Null, "insanity check");
2625 //
2626 if( loaded->ptr() == TypePtr::TopPTR ) { return unloaded; }
2627 else if (loaded->ptr() == TypePtr::AnyNull) { return TypeInstPtr::make( ptr, unloaded->klass() ); }
2628 else if (loaded->ptr() == TypePtr::BotPTR ) { return TypeInstPtr::BOTTOM; }
2629 else if (loaded->ptr() == TypePtr::Constant || loaded->ptr() == TypePtr::NotNull) {
2630 if (unloaded->ptr() == TypePtr::BotPTR ) { return TypeInstPtr::BOTTOM; }
2631 else { return TypeInstPtr::NOTNULL; }
2632 }
2633 else if( unloaded->ptr() == TypePtr::TopPTR ) { return unloaded; }
2635 return unloaded->cast_to_ptr_type(TypePtr::AnyNull)->is_instptr();
2636 }
2638 // Both are unloaded, not the same class, not Object
2639 // Or meet unloaded with a different loaded class, not java/lang/Object
2640 if( ptr != TypePtr::BotPTR ) {
2641 return TypeInstPtr::NOTNULL;
2642 }
2643 return TypeInstPtr::BOTTOM;
2644 }
2647 //------------------------------meet-------------------------------------------
2648 // Compute the MEET of two types. It returns a new Type object.
2649 const Type *TypeInstPtr::xmeet( const Type *t ) const {
2650 // Perform a fast test for common case; meeting the same types together.
2651 if( this == t ) return this; // Meeting same type-rep?
2653 // Current "this->_base" is Pointer
2654 switch (t->base()) { // switch on original type
2656 case Int: // Mixing ints & oops happens when javac
2657 case Long: // reuses local variables
2658 case FloatTop:
2659 case FloatCon:
2660 case FloatBot:
2661 case DoubleTop:
2662 case DoubleCon:
2663 case DoubleBot:
2664 case NarrowOop:
2665 case Bottom: // Ye Olde Default
2666 return Type::BOTTOM;
2667 case Top:
2668 return this;
2670 default: // All else is a mistake
2671 typerr(t);
2673 case RawPtr: return TypePtr::BOTTOM;
2675 case AryPtr: { // All arrays inherit from Object class
2676 const TypeAryPtr *tp = t->is_aryptr();
2677 int offset = meet_offset(tp->offset());
2678 PTR ptr = meet_ptr(tp->ptr());
2679 int iid = meet_instance(tp->instance_id());
2680 switch (ptr) {
2681 case TopPTR:
2682 case AnyNull: // Fall 'down' to dual of object klass
2683 if (klass()->equals(ciEnv::current()->Object_klass())) {
2684 return TypeAryPtr::make(ptr, tp->ary(), tp->klass(), tp->klass_is_exact(), offset, iid);
2685 } else {
2686 // cannot subclass, so the meet has to fall badly below the centerline
2687 ptr = NotNull;
2688 return TypeInstPtr::make( ptr, ciEnv::current()->Object_klass(), false, NULL, offset, iid);
2689 }
2690 case Constant:
2691 case NotNull:
2692 case BotPTR: // Fall down to object klass
2693 // LCA is object_klass, but if we subclass from the top we can do better
2694 if( above_centerline(_ptr) ) { // if( _ptr == TopPTR || _ptr == AnyNull )
2695 // If 'this' (InstPtr) is above the centerline and it is Object class
2696 // then we can subclass in the Java class heirarchy.
2697 if (klass()->equals(ciEnv::current()->Object_klass())) {
2698 // that is, tp's array type is a subtype of my klass
2699 return TypeAryPtr::make(ptr, tp->ary(), tp->klass(), tp->klass_is_exact(), offset, iid);
2700 }
2701 }
2702 // The other case cannot happen, since I cannot be a subtype of an array.
2703 // The meet falls down to Object class below centerline.
2704 if( ptr == Constant )
2705 ptr = NotNull;
2706 return make( ptr, ciEnv::current()->Object_klass(), false, NULL, offset, iid );
2707 default: typerr(t);
2708 }
2709 }
2711 case OopPtr: { // Meeting to OopPtrs
2712 // Found a OopPtr type vs self-InstPtr type
2713 const TypePtr *tp = t->is_oopptr();
2714 int offset = meet_offset(tp->offset());
2715 PTR ptr = meet_ptr(tp->ptr());
2716 switch (tp->ptr()) {
2717 case TopPTR:
2718 case AnyNull:
2719 return make(ptr, klass(), klass_is_exact(),
2720 (ptr == Constant ? const_oop() : NULL), offset);
2721 case NotNull:
2722 case BotPTR:
2723 return TypeOopPtr::make(ptr, offset);
2724 default: typerr(t);
2725 }
2726 }
2728 case AnyPtr: { // Meeting to AnyPtrs
2729 // Found an AnyPtr type vs self-InstPtr type
2730 const TypePtr *tp = t->is_ptr();
2731 int offset = meet_offset(tp->offset());
2732 PTR ptr = meet_ptr(tp->ptr());
2733 switch (tp->ptr()) {
2734 case Null:
2735 if( ptr == Null ) return TypePtr::make( AnyPtr, ptr, offset );
2736 case TopPTR:
2737 case AnyNull:
2738 return make( ptr, klass(), klass_is_exact(),
2739 (ptr == Constant ? const_oop() : NULL), offset );
2740 case NotNull:
2741 case BotPTR:
2742 return TypePtr::make( AnyPtr, ptr, offset );
2743 default: typerr(t);
2744 }
2745 }
2747 /*
2748 A-top }
2749 / | \ } Tops
2750 B-top A-any C-top }
2751 | / | \ | } Any-nulls
2752 B-any | C-any }
2753 | | |
2754 B-con A-con C-con } constants; not comparable across classes
2755 | | |
2756 B-not | C-not }
2757 | \ | / | } not-nulls
2758 B-bot A-not C-bot }
2759 \ | / } Bottoms
2760 A-bot }
2761 */
2763 case InstPtr: { // Meeting 2 Oops?
2764 // Found an InstPtr sub-type vs self-InstPtr type
2765 const TypeInstPtr *tinst = t->is_instptr();
2766 int off = meet_offset( tinst->offset() );
2767 PTR ptr = meet_ptr( tinst->ptr() );
2768 int instance_id = meet_instance(tinst->instance_id());
2770 // Check for easy case; klasses are equal (and perhaps not loaded!)
2771 // If we have constants, then we created oops so classes are loaded
2772 // and we can handle the constants further down. This case handles
2773 // both-not-loaded or both-loaded classes
2774 if (ptr != Constant && klass()->equals(tinst->klass()) && klass_is_exact() == tinst->klass_is_exact()) {
2775 return make( ptr, klass(), klass_is_exact(), NULL, off, instance_id );
2776 }
2778 // Classes require inspection in the Java klass hierarchy. Must be loaded.
2779 ciKlass* tinst_klass = tinst->klass();
2780 ciKlass* this_klass = this->klass();
2781 bool tinst_xk = tinst->klass_is_exact();
2782 bool this_xk = this->klass_is_exact();
2783 if (!tinst_klass->is_loaded() || !this_klass->is_loaded() ) {
2784 // One of these classes has not been loaded
2785 const TypeInstPtr *unloaded_meet = xmeet_unloaded(tinst);
2786 #ifndef PRODUCT
2787 if( PrintOpto && Verbose ) {
2788 tty->print("meet of unloaded classes resulted in: "); unloaded_meet->dump(); tty->cr();
2789 tty->print(" this == "); this->dump(); tty->cr();
2790 tty->print(" tinst == "); tinst->dump(); tty->cr();
2791 }
2792 #endif
2793 return unloaded_meet;
2794 }
2796 // Handle mixing oops and interfaces first.
2797 if( this_klass->is_interface() && !tinst_klass->is_interface() ) {
2798 ciKlass *tmp = tinst_klass; // Swap interface around
2799 tinst_klass = this_klass;
2800 this_klass = tmp;
2801 bool tmp2 = tinst_xk;
2802 tinst_xk = this_xk;
2803 this_xk = tmp2;
2804 }
2805 if (tinst_klass->is_interface() &&
2806 !(this_klass->is_interface() ||
2807 // Treat java/lang/Object as an honorary interface,
2808 // because we need a bottom for the interface hierarchy.
2809 this_klass == ciEnv::current()->Object_klass())) {
2810 // Oop meets interface!
2812 // See if the oop subtypes (implements) interface.
2813 ciKlass *k;
2814 bool xk;
2815 if( this_klass->is_subtype_of( tinst_klass ) ) {
2816 // Oop indeed subtypes. Now keep oop or interface depending
2817 // on whether we are both above the centerline or either is
2818 // below the centerline. If we are on the centerline
2819 // (e.g., Constant vs. AnyNull interface), use the constant.
2820 k = below_centerline(ptr) ? tinst_klass : this_klass;
2821 // If we are keeping this_klass, keep its exactness too.
2822 xk = below_centerline(ptr) ? tinst_xk : this_xk;
2823 } else { // Does not implement, fall to Object
2824 // Oop does not implement interface, so mixing falls to Object
2825 // just like the verifier does (if both are above the
2826 // centerline fall to interface)
2827 k = above_centerline(ptr) ? tinst_klass : ciEnv::current()->Object_klass();
2828 xk = above_centerline(ptr) ? tinst_xk : false;
2829 // Watch out for Constant vs. AnyNull interface.
2830 if (ptr == Constant) ptr = NotNull; // forget it was a constant
2831 }
2832 ciObject* o = NULL; // the Constant value, if any
2833 if (ptr == Constant) {
2834 // Find out which constant.
2835 o = (this_klass == klass()) ? const_oop() : tinst->const_oop();
2836 }
2837 return make( ptr, k, xk, o, off );
2838 }
2840 // Either oop vs oop or interface vs interface or interface vs Object
2842 // !!! Here's how the symmetry requirement breaks down into invariants:
2843 // If we split one up & one down AND they subtype, take the down man.
2844 // If we split one up & one down AND they do NOT subtype, "fall hard".
2845 // If both are up and they subtype, take the subtype class.
2846 // If both are up and they do NOT subtype, "fall hard".
2847 // If both are down and they subtype, take the supertype class.
2848 // If both are down and they do NOT subtype, "fall hard".
2849 // Constants treated as down.
2851 // Now, reorder the above list; observe that both-down+subtype is also
2852 // "fall hard"; "fall hard" becomes the default case:
2853 // If we split one up & one down AND they subtype, take the down man.
2854 // If both are up and they subtype, take the subtype class.
2856 // If both are down and they subtype, "fall hard".
2857 // If both are down and they do NOT subtype, "fall hard".
2858 // If both are up and they do NOT subtype, "fall hard".
2859 // If we split one up & one down AND they do NOT subtype, "fall hard".
2861 // If a proper subtype is exact, and we return it, we return it exactly.
2862 // If a proper supertype is exact, there can be no subtyping relationship!
2863 // If both types are equal to the subtype, exactness is and-ed below the
2864 // centerline and or-ed above it. (N.B. Constants are always exact.)
2866 // Check for subtyping:
2867 ciKlass *subtype = NULL;
2868 bool subtype_exact = false;
2869 if( tinst_klass->equals(this_klass) ) {
2870 subtype = this_klass;
2871 subtype_exact = below_centerline(ptr) ? (this_xk & tinst_xk) : (this_xk | tinst_xk);
2872 } else if( !tinst_xk && this_klass->is_subtype_of( tinst_klass ) ) {
2873 subtype = this_klass; // Pick subtyping class
2874 subtype_exact = this_xk;
2875 } else if( !this_xk && tinst_klass->is_subtype_of( this_klass ) ) {
2876 subtype = tinst_klass; // Pick subtyping class
2877 subtype_exact = tinst_xk;
2878 }
2880 if( subtype ) {
2881 if( above_centerline(ptr) ) { // both are up?
2882 this_klass = tinst_klass = subtype;
2883 this_xk = tinst_xk = subtype_exact;
2884 } else if( above_centerline(this ->_ptr) && !above_centerline(tinst->_ptr) ) {
2885 this_klass = tinst_klass; // tinst is down; keep down man
2886 this_xk = tinst_xk;
2887 } else if( above_centerline(tinst->_ptr) && !above_centerline(this ->_ptr) ) {
2888 tinst_klass = this_klass; // this is down; keep down man
2889 tinst_xk = this_xk;
2890 } else {
2891 this_xk = subtype_exact; // either they are equal, or we'll do an LCA
2892 }
2893 }
2895 // Check for classes now being equal
2896 if (tinst_klass->equals(this_klass)) {
2897 // If the klasses are equal, the constants may still differ. Fall to
2898 // NotNull if they do (neither constant is NULL; that is a special case
2899 // handled elsewhere).
2900 ciObject* o = NULL; // Assume not constant when done
2901 ciObject* this_oop = const_oop();
2902 ciObject* tinst_oop = tinst->const_oop();
2903 if( ptr == Constant ) {
2904 if (this_oop != NULL && tinst_oop != NULL &&
2905 this_oop->equals(tinst_oop) )
2906 o = this_oop;
2907 else if (above_centerline(this ->_ptr))
2908 o = tinst_oop;
2909 else if (above_centerline(tinst ->_ptr))
2910 o = this_oop;
2911 else
2912 ptr = NotNull;
2913 }
2914 return make( ptr, this_klass, this_xk, o, off, instance_id );
2915 } // Else classes are not equal
2917 // Since klasses are different, we require a LCA in the Java
2918 // class hierarchy - which means we have to fall to at least NotNull.
2919 if( ptr == TopPTR || ptr == AnyNull || ptr == Constant )
2920 ptr = NotNull;
2922 // Now we find the LCA of Java classes
2923 ciKlass* k = this_klass->least_common_ancestor(tinst_klass);
2924 return make( ptr, k, false, NULL, off );
2925 } // End of case InstPtr
2927 case KlassPtr:
2928 return TypeInstPtr::BOTTOM;
2930 } // End of switch
2931 return this; // Return the double constant
2932 }
2935 //------------------------java_mirror_type--------------------------------------
2936 ciType* TypeInstPtr::java_mirror_type() const {
2937 // must be a singleton type
2938 if( const_oop() == NULL ) return NULL;
2940 // must be of type java.lang.Class
2941 if( klass() != ciEnv::current()->Class_klass() ) return NULL;
2943 return const_oop()->as_instance()->java_mirror_type();
2944 }
2947 //------------------------------xdual------------------------------------------
2948 // Dual: do NOT dual on klasses. This means I do NOT understand the Java
2949 // inheritence mechanism.
2950 const Type *TypeInstPtr::xdual() const {
2951 return new TypeInstPtr( dual_ptr(), klass(), klass_is_exact(), const_oop(), dual_offset(), dual_instance() );
2952 }
2954 //------------------------------eq---------------------------------------------
2955 // Structural equality check for Type representations
2956 bool TypeInstPtr::eq( const Type *t ) const {
2957 const TypeInstPtr *p = t->is_instptr();
2958 return
2959 klass()->equals(p->klass()) &&
2960 TypeOopPtr::eq(p); // Check sub-type stuff
2961 }
2963 //------------------------------hash-------------------------------------------
2964 // Type-specific hashing function.
2965 int TypeInstPtr::hash(void) const {
2966 int hash = klass()->hash() + TypeOopPtr::hash();
2967 return hash;
2968 }
2970 //------------------------------dump2------------------------------------------
2971 // Dump oop Type
2972 #ifndef PRODUCT
2973 void TypeInstPtr::dump2( Dict &d, uint depth, outputStream *st ) const {
2974 // Print the name of the klass.
2975 klass()->print_name_on(st);
2977 switch( _ptr ) {
2978 case Constant:
2979 // TO DO: Make CI print the hex address of the underlying oop.
2980 if (WizardMode || Verbose) {
2981 const_oop()->print_oop(st);
2982 }
2983 case BotPTR:
2984 if (!WizardMode && !Verbose) {
2985 if( _klass_is_exact ) st->print(":exact");
2986 break;
2987 }
2988 case TopPTR:
2989 case AnyNull:
2990 case NotNull:
2991 st->print(":%s", ptr_msg[_ptr]);
2992 if( _klass_is_exact ) st->print(":exact");
2993 break;
2994 }
2996 if( _offset ) { // Dump offset, if any
2997 if( _offset == OffsetBot ) st->print("+any");
2998 else if( _offset == OffsetTop ) st->print("+unknown");
2999 else st->print("+%d", _offset);
3000 }
3002 st->print(" *");
3003 if (_instance_id != UNKNOWN_INSTANCE)
3004 st->print(",iid=%d",_instance_id);
3005 }
3006 #endif
3008 //------------------------------add_offset-------------------------------------
3009 const TypePtr *TypeInstPtr::add_offset( int offset ) const {
3010 return make( _ptr, klass(), klass_is_exact(), const_oop(), xadd_offset(offset), _instance_id );
3011 }
3013 //=============================================================================
3014 // Convenience common pre-built types.
3015 const TypeAryPtr *TypeAryPtr::RANGE;
3016 const TypeAryPtr *TypeAryPtr::OOPS;
3017 const TypeAryPtr *TypeAryPtr::BYTES;
3018 const TypeAryPtr *TypeAryPtr::SHORTS;
3019 const TypeAryPtr *TypeAryPtr::CHARS;
3020 const TypeAryPtr *TypeAryPtr::INTS;
3021 const TypeAryPtr *TypeAryPtr::LONGS;
3022 const TypeAryPtr *TypeAryPtr::FLOATS;
3023 const TypeAryPtr *TypeAryPtr::DOUBLES;
3025 //------------------------------make-------------------------------------------
3026 const TypeAryPtr *TypeAryPtr::make( PTR ptr, const TypeAry *ary, ciKlass* k, bool xk, int offset, int instance_id ) {
3027 assert(!(k == NULL && ary->_elem->isa_int()),
3028 "integral arrays must be pre-equipped with a class");
3029 if (!xk) xk = ary->ary_must_be_exact();
3030 if (instance_id != UNKNOWN_INSTANCE)
3031 xk = true; // instances are always exactly typed
3032 if (!UseExactTypes) xk = (ptr == Constant);
3033 return (TypeAryPtr*)(new TypeAryPtr(ptr, NULL, ary, k, xk, offset, instance_id))->hashcons();
3034 }
3036 //------------------------------make-------------------------------------------
3037 const TypeAryPtr *TypeAryPtr::make( PTR ptr, ciObject* o, const TypeAry *ary, ciKlass* k, bool xk, int offset, int instance_id ) {
3038 assert(!(k == NULL && ary->_elem->isa_int()),
3039 "integral arrays must be pre-equipped with a class");
3040 assert( (ptr==Constant && o) || (ptr!=Constant && !o), "" );
3041 if (!xk) xk = (o != NULL) || ary->ary_must_be_exact();
3042 if (instance_id != UNKNOWN_INSTANCE)
3043 xk = true; // instances are always exactly typed
3044 if (!UseExactTypes) xk = (ptr == Constant);
3045 return (TypeAryPtr*)(new TypeAryPtr(ptr, o, ary, k, xk, offset, instance_id))->hashcons();
3046 }
3048 //------------------------------cast_to_ptr_type-------------------------------
3049 const Type *TypeAryPtr::cast_to_ptr_type(PTR ptr) const {
3050 if( ptr == _ptr ) return this;
3051 return make(ptr, const_oop(), _ary, klass(), klass_is_exact(), _offset);
3052 }
3055 //-----------------------------cast_to_exactness-------------------------------
3056 const Type *TypeAryPtr::cast_to_exactness(bool klass_is_exact) const {
3057 if( klass_is_exact == _klass_is_exact ) return this;
3058 if (!UseExactTypes) return this;
3059 if (_ary->ary_must_be_exact()) return this; // cannot clear xk
3060 return make(ptr(), const_oop(), _ary, klass(), klass_is_exact, _offset, _instance_id);
3061 }
3063 //-----------------------------cast_to_instance-------------------------------
3064 const TypeOopPtr *TypeAryPtr::cast_to_instance(int instance_id) const {
3065 if( instance_id == _instance_id) return this;
3066 bool exact = (instance_id == UNKNOWN_INSTANCE) ? _klass_is_exact : true;
3067 return make(ptr(), const_oop(), _ary, klass(), exact, _offset, instance_id);
3068 }
3070 //-----------------------------narrow_size_type-------------------------------
3071 // Local cache for arrayOopDesc::max_array_length(etype),
3072 // which is kind of slow (and cached elsewhere by other users).
3073 static jint max_array_length_cache[T_CONFLICT+1];
3074 static jint max_array_length(BasicType etype) {
3075 jint& cache = max_array_length_cache[etype];
3076 jint res = cache;
3077 if (res == 0) {
3078 switch (etype) {
3079 case T_NARROWOOP:
3080 etype = T_OBJECT;
3081 break;
3082 case T_CONFLICT:
3083 case T_ILLEGAL:
3084 case T_VOID:
3085 etype = T_BYTE; // will produce conservatively high value
3086 }
3087 cache = res = arrayOopDesc::max_array_length(etype);
3088 }
3089 return res;
3090 }
3092 // Narrow the given size type to the index range for the given array base type.
3093 // Return NULL if the resulting int type becomes empty.
3094 const TypeInt* TypeAryPtr::narrow_size_type(const TypeInt* size, BasicType elem) {
3095 jint hi = size->_hi;
3096 jint lo = size->_lo;
3097 jint min_lo = 0;
3098 jint max_hi = max_array_length(elem);
3099 //if (index_not_size) --max_hi; // type of a valid array index, FTR
3100 bool chg = false;
3101 if (lo < min_lo) { lo = min_lo; chg = true; }
3102 if (hi > max_hi) { hi = max_hi; chg = true; }
3103 if (lo > hi)
3104 return NULL;
3105 if (!chg)
3106 return size;
3107 return TypeInt::make(lo, hi, Type::WidenMin);
3108 }
3110 //-------------------------------cast_to_size----------------------------------
3111 const TypeAryPtr* TypeAryPtr::cast_to_size(const TypeInt* new_size) const {
3112 assert(new_size != NULL, "");
3113 new_size = narrow_size_type(new_size, elem()->basic_type());
3114 if (new_size == NULL) // Negative length arrays will produce weird
3115 new_size = TypeInt::ZERO; // intermediate dead fast-path goo
3116 if (new_size == size()) return this;
3117 const TypeAry* new_ary = TypeAry::make(elem(), new_size);
3118 return make(ptr(), const_oop(), new_ary, klass(), klass_is_exact(), _offset);
3119 }
3122 //------------------------------eq---------------------------------------------
3123 // Structural equality check for Type representations
3124 bool TypeAryPtr::eq( const Type *t ) const {
3125 const TypeAryPtr *p = t->is_aryptr();
3126 return
3127 _ary == p->_ary && // Check array
3128 TypeOopPtr::eq(p); // Check sub-parts
3129 }
3131 //------------------------------hash-------------------------------------------
3132 // Type-specific hashing function.
3133 int TypeAryPtr::hash(void) const {
3134 return (intptr_t)_ary + TypeOopPtr::hash();
3135 }
3137 //------------------------------meet-------------------------------------------
3138 // Compute the MEET of two types. It returns a new Type object.
3139 const Type *TypeAryPtr::xmeet( const Type *t ) const {
3140 // Perform a fast test for common case; meeting the same types together.
3141 if( this == t ) return this; // Meeting same type-rep?
3142 // Current "this->_base" is Pointer
3143 switch (t->base()) { // switch on original type
3145 // Mixing ints & oops happens when javac reuses local variables
3146 case Int:
3147 case Long:
3148 case FloatTop:
3149 case FloatCon:
3150 case FloatBot:
3151 case DoubleTop:
3152 case DoubleCon:
3153 case DoubleBot:
3154 case NarrowOop:
3155 case Bottom: // Ye Olde Default
3156 return Type::BOTTOM;
3157 case Top:
3158 return this;
3160 default: // All else is a mistake
3161 typerr(t);
3163 case OopPtr: { // Meeting to OopPtrs
3164 // Found a OopPtr type vs self-AryPtr type
3165 const TypePtr *tp = t->is_oopptr();
3166 int offset = meet_offset(tp->offset());
3167 PTR ptr = meet_ptr(tp->ptr());
3168 switch (tp->ptr()) {
3169 case TopPTR:
3170 case AnyNull:
3171 return make(ptr, (ptr == Constant ? const_oop() : NULL), _ary, _klass, _klass_is_exact, offset);
3172 case BotPTR:
3173 case NotNull:
3174 return TypeOopPtr::make(ptr, offset);
3175 default: ShouldNotReachHere();
3176 }
3177 }
3179 case AnyPtr: { // Meeting two AnyPtrs
3180 // Found an AnyPtr type vs self-AryPtr type
3181 const TypePtr *tp = t->is_ptr();
3182 int offset = meet_offset(tp->offset());
3183 PTR ptr = meet_ptr(tp->ptr());
3184 switch (tp->ptr()) {
3185 case TopPTR:
3186 return this;
3187 case BotPTR:
3188 case NotNull:
3189 return TypePtr::make(AnyPtr, ptr, offset);
3190 case Null:
3191 if( ptr == Null ) return TypePtr::make(AnyPtr, ptr, offset);
3192 case AnyNull:
3193 return make( ptr, (ptr == Constant ? const_oop() : NULL), _ary, _klass, _klass_is_exact, offset );
3194 default: ShouldNotReachHere();
3195 }
3196 }
3198 case RawPtr: return TypePtr::BOTTOM;
3200 case AryPtr: { // Meeting 2 references?
3201 const TypeAryPtr *tap = t->is_aryptr();
3202 int off = meet_offset(tap->offset());
3203 const TypeAry *tary = _ary->meet(tap->_ary)->is_ary();
3204 PTR ptr = meet_ptr(tap->ptr());
3205 int iid = meet_instance(tap->instance_id());
3206 ciKlass* lazy_klass = NULL;
3207 if (tary->_elem->isa_int()) {
3208 // Integral array element types have irrelevant lattice relations.
3209 // It is the klass that determines array layout, not the element type.
3210 if (_klass == NULL)
3211 lazy_klass = tap->_klass;
3212 else if (tap->_klass == NULL || tap->_klass == _klass) {
3213 lazy_klass = _klass;
3214 } else {
3215 // Something like byte[int+] meets char[int+].
3216 // This must fall to bottom, not (int[-128..65535])[int+].
3217 tary = TypeAry::make(Type::BOTTOM, tary->_size);
3218 }
3219 }
3220 bool xk;
3221 switch (tap->ptr()) {
3222 case AnyNull:
3223 case TopPTR:
3224 // Compute new klass on demand, do not use tap->_klass
3225 xk = (tap->_klass_is_exact | this->_klass_is_exact);
3226 return make( ptr, const_oop(), tary, lazy_klass, xk, off, iid );
3227 case Constant: {
3228 ciObject* o = const_oop();
3229 if( _ptr == Constant ) {
3230 if( tap->const_oop() != NULL && !o->equals(tap->const_oop()) ) {
3231 ptr = NotNull;
3232 o = NULL;
3233 }
3234 } else if( above_centerline(_ptr) ) {
3235 o = tap->const_oop();
3236 }
3237 xk = true;
3238 return TypeAryPtr::make( ptr, o, tary, tap->_klass, xk, off, iid );
3239 }
3240 case NotNull:
3241 case BotPTR:
3242 // Compute new klass on demand, do not use tap->_klass
3243 if (above_centerline(this->_ptr))
3244 xk = tap->_klass_is_exact;
3245 else if (above_centerline(tap->_ptr))
3246 xk = this->_klass_is_exact;
3247 else xk = (tap->_klass_is_exact & this->_klass_is_exact) &&
3248 (klass() == tap->klass()); // Only precise for identical arrays
3249 return TypeAryPtr::make( ptr, NULL, tary, lazy_klass, xk, off, iid );
3250 default: ShouldNotReachHere();
3251 }
3252 }
3254 // All arrays inherit from Object class
3255 case InstPtr: {
3256 const TypeInstPtr *tp = t->is_instptr();
3257 int offset = meet_offset(tp->offset());
3258 PTR ptr = meet_ptr(tp->ptr());
3259 int iid = meet_instance(tp->instance_id());
3260 switch (ptr) {
3261 case TopPTR:
3262 case AnyNull: // Fall 'down' to dual of object klass
3263 if( tp->klass()->equals(ciEnv::current()->Object_klass()) ) {
3264 return TypeAryPtr::make( ptr, _ary, _klass, _klass_is_exact, offset, iid );
3265 } else {
3266 // cannot subclass, so the meet has to fall badly below the centerline
3267 ptr = NotNull;
3268 return TypeInstPtr::make( ptr, ciEnv::current()->Object_klass(), false, NULL,offset, iid);
3269 }
3270 case Constant:
3271 case NotNull:
3272 case BotPTR: // Fall down to object klass
3273 // LCA is object_klass, but if we subclass from the top we can do better
3274 if (above_centerline(tp->ptr())) {
3275 // If 'tp' is above the centerline and it is Object class
3276 // then we can subclass in the Java class heirarchy.
3277 if( tp->klass()->equals(ciEnv::current()->Object_klass()) ) {
3278 // that is, my array type is a subtype of 'tp' klass
3279 return make( ptr, _ary, _klass, _klass_is_exact, offset, iid );
3280 }
3281 }
3282 // The other case cannot happen, since t cannot be a subtype of an array.
3283 // The meet falls down to Object class below centerline.
3284 if( ptr == Constant )
3285 ptr = NotNull;
3286 return TypeInstPtr::make( ptr, ciEnv::current()->Object_klass(), false, NULL,offset, iid);
3287 default: typerr(t);
3288 }
3289 }
3291 case KlassPtr:
3292 return TypeInstPtr::BOTTOM;
3294 }
3295 return this; // Lint noise
3296 }
3298 //------------------------------xdual------------------------------------------
3299 // Dual: compute field-by-field dual
3300 const Type *TypeAryPtr::xdual() const {
3301 return new TypeAryPtr( dual_ptr(), _const_oop, _ary->dual()->is_ary(),_klass, _klass_is_exact, dual_offset(), dual_instance() );
3302 }
3304 //------------------------------dump2------------------------------------------
3305 #ifndef PRODUCT
3306 void TypeAryPtr::dump2( Dict &d, uint depth, outputStream *st ) const {
3307 _ary->dump2(d,depth,st);
3308 switch( _ptr ) {
3309 case Constant:
3310 const_oop()->print(st);
3311 break;
3312 case BotPTR:
3313 if (!WizardMode && !Verbose) {
3314 if( _klass_is_exact ) st->print(":exact");
3315 break;
3316 }
3317 case TopPTR:
3318 case AnyNull:
3319 case NotNull:
3320 st->print(":%s", ptr_msg[_ptr]);
3321 if( _klass_is_exact ) st->print(":exact");
3322 break;
3323 }
3325 if( _offset != 0 ) {
3326 int header_size = objArrayOopDesc::header_size() * wordSize;
3327 if( _offset == OffsetTop ) st->print("+undefined");
3328 else if( _offset == OffsetBot ) st->print("+any");
3329 else if( _offset < header_size ) st->print("+%d", _offset);
3330 else {
3331 BasicType basic_elem_type = elem()->basic_type();
3332 int array_base = arrayOopDesc::base_offset_in_bytes(basic_elem_type);
3333 int elem_size = type2aelembytes(basic_elem_type);
3334 st->print("[%d]", (_offset - array_base)/elem_size);
3335 }
3336 }
3337 st->print(" *");
3338 if (_instance_id != UNKNOWN_INSTANCE)
3339 st->print(",iid=%d",_instance_id);
3340 }
3341 #endif
3343 bool TypeAryPtr::empty(void) const {
3344 if (_ary->empty()) return true;
3345 return TypeOopPtr::empty();
3346 }
3348 //------------------------------add_offset-------------------------------------
3349 const TypePtr *TypeAryPtr::add_offset( int offset ) const {
3350 return make( _ptr, _const_oop, _ary, _klass, _klass_is_exact, xadd_offset(offset), _instance_id );
3351 }
3354 //=============================================================================
3355 const TypeNarrowOop *TypeNarrowOop::BOTTOM;
3356 const TypeNarrowOop *TypeNarrowOop::NULL_PTR;
3359 const TypeNarrowOop* TypeNarrowOop::make(const TypePtr* type) {
3360 return (const TypeNarrowOop*)(new TypeNarrowOop(type))->hashcons();
3361 }
3363 //------------------------------hash-------------------------------------------
3364 // Type-specific hashing function.
3365 int TypeNarrowOop::hash(void) const {
3366 return _ooptype->hash() + 7;
3367 }
3370 bool TypeNarrowOop::eq( const Type *t ) const {
3371 const TypeNarrowOop* tc = t->isa_narrowoop();
3372 if (tc != NULL) {
3373 if (_ooptype->base() != tc->_ooptype->base()) {
3374 return false;
3375 }
3376 return tc->_ooptype->eq(_ooptype);
3377 }
3378 return false;
3379 }
3381 bool TypeNarrowOop::singleton(void) const { // TRUE if type is a singleton
3382 return _ooptype->singleton();
3383 }
3385 bool TypeNarrowOop::empty(void) const {
3386 return _ooptype->empty();
3387 }
3389 //------------------------------meet-------------------------------------------
3390 // Compute the MEET of two types. It returns a new Type object.
3391 const Type *TypeNarrowOop::xmeet( const Type *t ) const {
3392 // Perform a fast test for common case; meeting the same types together.
3393 if( this == t ) return this; // Meeting same type-rep?
3396 // Current "this->_base" is OopPtr
3397 switch (t->base()) { // switch on original type
3399 case Int: // Mixing ints & oops happens when javac
3400 case Long: // reuses local variables
3401 case FloatTop:
3402 case FloatCon:
3403 case FloatBot:
3404 case DoubleTop:
3405 case DoubleCon:
3406 case DoubleBot:
3407 case Bottom: // Ye Olde Default
3408 return Type::BOTTOM;
3409 case Top:
3410 return this;
3412 case NarrowOop: {
3413 const Type* result = _ooptype->xmeet(t->is_narrowoop()->make_oopptr());
3414 if (result->isa_ptr()) {
3415 return TypeNarrowOop::make(result->is_ptr());
3416 }
3417 return result;
3418 }
3420 default: // All else is a mistake
3421 typerr(t);
3423 case RawPtr:
3424 case AnyPtr:
3425 case OopPtr:
3426 case InstPtr:
3427 case KlassPtr:
3428 case AryPtr:
3429 typerr(t);
3430 return Type::BOTTOM;
3432 } // End of switch
3433 }
3435 const Type *TypeNarrowOop::xdual() const { // Compute dual right now.
3436 const TypePtr* odual = _ooptype->dual()->is_ptr();
3437 return new TypeNarrowOop(odual);
3438 }
3440 const Type *TypeNarrowOop::filter( const Type *kills ) const {
3441 if (kills->isa_narrowoop()) {
3442 const Type* ft =_ooptype->filter(kills->is_narrowoop()->_ooptype);
3443 if (ft->empty())
3444 return Type::TOP; // Canonical empty value
3445 if (ft->isa_ptr()) {
3446 return make(ft->isa_ptr());
3447 }
3448 return ft;
3449 } else if (kills->isa_ptr()) {
3450 const Type* ft = _ooptype->join(kills);
3451 if (ft->empty())
3452 return Type::TOP; // Canonical empty value
3453 return ft;
3454 } else {
3455 return Type::TOP;
3456 }
3457 }
3460 intptr_t TypeNarrowOop::get_con() const {
3461 return _ooptype->get_con();
3462 }
3464 #ifndef PRODUCT
3465 void TypeNarrowOop::dump2( Dict & d, uint depth, outputStream *st ) const {
3466 tty->print("narrowoop: ");
3467 _ooptype->dump2(d, depth, st);
3468 }
3469 #endif
3472 //=============================================================================
3473 // Convenience common pre-built types.
3475 // Not-null object klass or below
3476 const TypeKlassPtr *TypeKlassPtr::OBJECT;
3477 const TypeKlassPtr *TypeKlassPtr::OBJECT_OR_NULL;
3479 //------------------------------TypeKlasPtr------------------------------------
3480 TypeKlassPtr::TypeKlassPtr( PTR ptr, ciKlass* klass, int offset )
3481 : TypeOopPtr(KlassPtr, ptr, klass, (ptr==Constant), (ptr==Constant ? klass : NULL), offset, 0) {
3482 }
3484 //------------------------------make-------------------------------------------
3485 // ptr to klass 'k', if Constant, or possibly to a sub-klass if not a Constant
3486 const TypeKlassPtr *TypeKlassPtr::make( PTR ptr, ciKlass* k, int offset ) {
3487 assert( k != NULL, "Expect a non-NULL klass");
3488 assert(k->is_instance_klass() || k->is_array_klass() ||
3489 k->is_method_klass(), "Incorrect type of klass oop");
3490 TypeKlassPtr *r =
3491 (TypeKlassPtr*)(new TypeKlassPtr(ptr, k, offset))->hashcons();
3493 return r;
3494 }
3496 //------------------------------eq---------------------------------------------
3497 // Structural equality check for Type representations
3498 bool TypeKlassPtr::eq( const Type *t ) const {
3499 const TypeKlassPtr *p = t->is_klassptr();
3500 return
3501 klass()->equals(p->klass()) &&
3502 TypeOopPtr::eq(p);
3503 }
3505 //------------------------------hash-------------------------------------------
3506 // Type-specific hashing function.
3507 int TypeKlassPtr::hash(void) const {
3508 return klass()->hash() + TypeOopPtr::hash();
3509 }
3512 //------------------------------klass------------------------------------------
3513 // Return the defining klass for this class
3514 ciKlass* TypeAryPtr::klass() const {
3515 if( _klass ) return _klass; // Return cached value, if possible
3517 // Oops, need to compute _klass and cache it
3518 ciKlass* k_ary = NULL;
3519 const TypeInstPtr *tinst;
3520 const TypeAryPtr *tary;
3521 const Type* el = elem();
3522 if (el->isa_narrowoop()) {
3523 el = el->is_narrowoop()->make_oopptr();
3524 }
3526 // Get element klass
3527 if ((tinst = el->isa_instptr()) != NULL) {
3528 // Compute array klass from element klass
3529 k_ary = ciObjArrayKlass::make(tinst->klass());
3530 } else if ((tary = el->isa_aryptr()) != NULL) {
3531 // Compute array klass from element klass
3532 ciKlass* k_elem = tary->klass();
3533 // If element type is something like bottom[], k_elem will be null.
3534 if (k_elem != NULL)
3535 k_ary = ciObjArrayKlass::make(k_elem);
3536 } else if ((el->base() == Type::Top) ||
3537 (el->base() == Type::Bottom)) {
3538 // element type of Bottom occurs from meet of basic type
3539 // and object; Top occurs when doing join on Bottom.
3540 // Leave k_ary at NULL.
3541 } else {
3542 // Cannot compute array klass directly from basic type,
3543 // since subtypes of TypeInt all have basic type T_INT.
3544 assert(!el->isa_int(),
3545 "integral arrays must be pre-equipped with a class");
3546 // Compute array klass directly from basic type
3547 k_ary = ciTypeArrayKlass::make(el->basic_type());
3548 }
3550 if( this != TypeAryPtr::OOPS )
3551 // The _klass field acts as a cache of the underlying
3552 // ciKlass for this array type. In order to set the field,
3553 // we need to cast away const-ness.
3554 //
3555 // IMPORTANT NOTE: we *never* set the _klass field for the
3556 // type TypeAryPtr::OOPS. This Type is shared between all
3557 // active compilations. However, the ciKlass which represents
3558 // this Type is *not* shared between compilations, so caching
3559 // this value would result in fetching a dangling pointer.
3560 //
3561 // Recomputing the underlying ciKlass for each request is
3562 // a bit less efficient than caching, but calls to
3563 // TypeAryPtr::OOPS->klass() are not common enough to matter.
3564 ((TypeAryPtr*)this)->_klass = k_ary;
3565 return k_ary;
3566 }
3569 //------------------------------add_offset-------------------------------------
3570 // Access internals of klass object
3571 const TypePtr *TypeKlassPtr::add_offset( int offset ) const {
3572 return make( _ptr, klass(), xadd_offset(offset) );
3573 }
3575 //------------------------------cast_to_ptr_type-------------------------------
3576 const Type *TypeKlassPtr::cast_to_ptr_type(PTR ptr) const {
3577 assert(_base == OopPtr, "subclass must override cast_to_ptr_type");
3578 if( ptr == _ptr ) return this;
3579 return make(ptr, _klass, _offset);
3580 }
3583 //-----------------------------cast_to_exactness-------------------------------
3584 const Type *TypeKlassPtr::cast_to_exactness(bool klass_is_exact) const {
3585 if( klass_is_exact == _klass_is_exact ) return this;
3586 if (!UseExactTypes) return this;
3587 return make(klass_is_exact ? Constant : NotNull, _klass, _offset);
3588 }
3591 //-----------------------------as_instance_type--------------------------------
3592 // Corresponding type for an instance of the given class.
3593 // It will be NotNull, and exact if and only if the klass type is exact.
3594 const TypeOopPtr* TypeKlassPtr::as_instance_type() const {
3595 ciKlass* k = klass();
3596 bool xk = klass_is_exact();
3597 //return TypeInstPtr::make(TypePtr::NotNull, k, xk, NULL, 0);
3598 const TypeOopPtr* toop = TypeOopPtr::make_from_klass_raw(k);
3599 toop = toop->cast_to_ptr_type(TypePtr::NotNull)->is_oopptr();
3600 return toop->cast_to_exactness(xk)->is_oopptr();
3601 }
3604 //------------------------------xmeet------------------------------------------
3605 // Compute the MEET of two types, return a new Type object.
3606 const Type *TypeKlassPtr::xmeet( const Type *t ) const {
3607 // Perform a fast test for common case; meeting the same types together.
3608 if( this == t ) return this; // Meeting same type-rep?
3610 // Current "this->_base" is Pointer
3611 switch (t->base()) { // switch on original type
3613 case Int: // Mixing ints & oops happens when javac
3614 case Long: // reuses local variables
3615 case FloatTop:
3616 case FloatCon:
3617 case FloatBot:
3618 case DoubleTop:
3619 case DoubleCon:
3620 case DoubleBot:
3621 case Bottom: // Ye Olde Default
3622 return Type::BOTTOM;
3623 case Top:
3624 return this;
3626 default: // All else is a mistake
3627 typerr(t);
3629 case RawPtr: return TypePtr::BOTTOM;
3631 case OopPtr: { // Meeting to OopPtrs
3632 // Found a OopPtr type vs self-KlassPtr type
3633 const TypePtr *tp = t->is_oopptr();
3634 int offset = meet_offset(tp->offset());
3635 PTR ptr = meet_ptr(tp->ptr());
3636 switch (tp->ptr()) {
3637 case TopPTR:
3638 case AnyNull:
3639 return make(ptr, klass(), offset);
3640 case BotPTR:
3641 case NotNull:
3642 return TypePtr::make(AnyPtr, ptr, offset);
3643 default: typerr(t);
3644 }
3645 }
3647 case AnyPtr: { // Meeting to AnyPtrs
3648 // Found an AnyPtr type vs self-KlassPtr type
3649 const TypePtr *tp = t->is_ptr();
3650 int offset = meet_offset(tp->offset());
3651 PTR ptr = meet_ptr(tp->ptr());
3652 switch (tp->ptr()) {
3653 case TopPTR:
3654 return this;
3655 case Null:
3656 if( ptr == Null ) return TypePtr::make( AnyPtr, ptr, offset );
3657 case AnyNull:
3658 return make( ptr, klass(), offset );
3659 case BotPTR:
3660 case NotNull:
3661 return TypePtr::make(AnyPtr, ptr, offset);
3662 default: typerr(t);
3663 }
3664 }
3666 case AryPtr: // Meet with AryPtr
3667 case InstPtr: // Meet with InstPtr
3668 return TypeInstPtr::BOTTOM;
3670 //
3671 // A-top }
3672 // / | \ } Tops
3673 // B-top A-any C-top }
3674 // | / | \ | } Any-nulls
3675 // B-any | C-any }
3676 // | | |
3677 // B-con A-con C-con } constants; not comparable across classes
3678 // | | |
3679 // B-not | C-not }
3680 // | \ | / | } not-nulls
3681 // B-bot A-not C-bot }
3682 // \ | / } Bottoms
3683 // A-bot }
3684 //
3686 case KlassPtr: { // Meet two KlassPtr types
3687 const TypeKlassPtr *tkls = t->is_klassptr();
3688 int off = meet_offset(tkls->offset());
3689 PTR ptr = meet_ptr(tkls->ptr());
3691 // Check for easy case; klasses are equal (and perhaps not loaded!)
3692 // If we have constants, then we created oops so classes are loaded
3693 // and we can handle the constants further down. This case handles
3694 // not-loaded classes
3695 if( ptr != Constant && tkls->klass()->equals(klass()) ) {
3696 return make( ptr, klass(), off );
3697 }
3699 // Classes require inspection in the Java klass hierarchy. Must be loaded.
3700 ciKlass* tkls_klass = tkls->klass();
3701 ciKlass* this_klass = this->klass();
3702 assert( tkls_klass->is_loaded(), "This class should have been loaded.");
3703 assert( this_klass->is_loaded(), "This class should have been loaded.");
3705 // If 'this' type is above the centerline and is a superclass of the
3706 // other, we can treat 'this' as having the same type as the other.
3707 if ((above_centerline(this->ptr())) &&
3708 tkls_klass->is_subtype_of(this_klass)) {
3709 this_klass = tkls_klass;
3710 }
3711 // If 'tinst' type is above the centerline and is a superclass of the
3712 // other, we can treat 'tinst' as having the same type as the other.
3713 if ((above_centerline(tkls->ptr())) &&
3714 this_klass->is_subtype_of(tkls_klass)) {
3715 tkls_klass = this_klass;
3716 }
3718 // Check for classes now being equal
3719 if (tkls_klass->equals(this_klass)) {
3720 // If the klasses are equal, the constants may still differ. Fall to
3721 // NotNull if they do (neither constant is NULL; that is a special case
3722 // handled elsewhere).
3723 ciObject* o = NULL; // Assume not constant when done
3724 ciObject* this_oop = const_oop();
3725 ciObject* tkls_oop = tkls->const_oop();
3726 if( ptr == Constant ) {
3727 if (this_oop != NULL && tkls_oop != NULL &&
3728 this_oop->equals(tkls_oop) )
3729 o = this_oop;
3730 else if (above_centerline(this->ptr()))
3731 o = tkls_oop;
3732 else if (above_centerline(tkls->ptr()))
3733 o = this_oop;
3734 else
3735 ptr = NotNull;
3736 }
3737 return make( ptr, this_klass, off );
3738 } // Else classes are not equal
3740 // Since klasses are different, we require the LCA in the Java
3741 // class hierarchy - which means we have to fall to at least NotNull.
3742 if( ptr == TopPTR || ptr == AnyNull || ptr == Constant )
3743 ptr = NotNull;
3744 // Now we find the LCA of Java classes
3745 ciKlass* k = this_klass->least_common_ancestor(tkls_klass);
3746 return make( ptr, k, off );
3747 } // End of case KlassPtr
3749 } // End of switch
3750 return this; // Return the double constant
3751 }
3753 //------------------------------xdual------------------------------------------
3754 // Dual: compute field-by-field dual
3755 const Type *TypeKlassPtr::xdual() const {
3756 return new TypeKlassPtr( dual_ptr(), klass(), dual_offset() );
3757 }
3759 //------------------------------dump2------------------------------------------
3760 // Dump Klass Type
3761 #ifndef PRODUCT
3762 void TypeKlassPtr::dump2( Dict & d, uint depth, outputStream *st ) const {
3763 switch( _ptr ) {
3764 case Constant:
3765 st->print("precise ");
3766 case NotNull:
3767 {
3768 const char *name = klass()->name()->as_utf8();
3769 if( name ) {
3770 st->print("klass %s: " INTPTR_FORMAT, name, klass());
3771 } else {
3772 ShouldNotReachHere();
3773 }
3774 }
3775 case BotPTR:
3776 if( !WizardMode && !Verbose && !_klass_is_exact ) break;
3777 case TopPTR:
3778 case AnyNull:
3779 st->print(":%s", ptr_msg[_ptr]);
3780 if( _klass_is_exact ) st->print(":exact");
3781 break;
3782 }
3784 if( _offset ) { // Dump offset, if any
3785 if( _offset == OffsetBot ) { st->print("+any"); }
3786 else if( _offset == OffsetTop ) { st->print("+unknown"); }
3787 else { st->print("+%d", _offset); }
3788 }
3790 st->print(" *");
3791 }
3792 #endif
3796 //=============================================================================
3797 // Convenience common pre-built types.
3799 //------------------------------make-------------------------------------------
3800 const TypeFunc *TypeFunc::make( const TypeTuple *domain, const TypeTuple *range ) {
3801 return (TypeFunc*)(new TypeFunc(domain,range))->hashcons();
3802 }
3804 //------------------------------make-------------------------------------------
3805 const TypeFunc *TypeFunc::make(ciMethod* method) {
3806 Compile* C = Compile::current();
3807 const TypeFunc* tf = C->last_tf(method); // check cache
3808 if (tf != NULL) return tf; // The hit rate here is almost 50%.
3809 const TypeTuple *domain;
3810 if (method->flags().is_static()) {
3811 domain = TypeTuple::make_domain(NULL, method->signature());
3812 } else {
3813 domain = TypeTuple::make_domain(method->holder(), method->signature());
3814 }
3815 const TypeTuple *range = TypeTuple::make_range(method->signature());
3816 tf = TypeFunc::make(domain, range);
3817 C->set_last_tf(method, tf); // fill cache
3818 return tf;
3819 }
3821 //------------------------------meet-------------------------------------------
3822 // Compute the MEET of two types. It returns a new Type object.
3823 const Type *TypeFunc::xmeet( const Type *t ) const {
3824 // Perform a fast test for common case; meeting the same types together.
3825 if( this == t ) return this; // Meeting same type-rep?
3827 // Current "this->_base" is Func
3828 switch (t->base()) { // switch on original type
3830 case Bottom: // Ye Olde Default
3831 return t;
3833 default: // All else is a mistake
3834 typerr(t);
3836 case Top:
3837 break;
3838 }
3839 return this; // Return the double constant
3840 }
3842 //------------------------------xdual------------------------------------------
3843 // Dual: compute field-by-field dual
3844 const Type *TypeFunc::xdual() const {
3845 return this;
3846 }
3848 //------------------------------eq---------------------------------------------
3849 // Structural equality check for Type representations
3850 bool TypeFunc::eq( const Type *t ) const {
3851 const TypeFunc *a = (const TypeFunc*)t;
3852 return _domain == a->_domain &&
3853 _range == a->_range;
3854 }
3856 //------------------------------hash-------------------------------------------
3857 // Type-specific hashing function.
3858 int TypeFunc::hash(void) const {
3859 return (intptr_t)_domain + (intptr_t)_range;
3860 }
3862 //------------------------------dump2------------------------------------------
3863 // Dump Function Type
3864 #ifndef PRODUCT
3865 void TypeFunc::dump2( Dict &d, uint depth, outputStream *st ) const {
3866 if( _range->_cnt <= Parms )
3867 st->print("void");
3868 else {
3869 uint i;
3870 for (i = Parms; i < _range->_cnt-1; i++) {
3871 _range->field_at(i)->dump2(d,depth,st);
3872 st->print("/");
3873 }
3874 _range->field_at(i)->dump2(d,depth,st);
3875 }
3876 st->print(" ");
3877 st->print("( ");
3878 if( !depth || d[this] ) { // Check for recursive dump
3879 st->print("...)");
3880 return;
3881 }
3882 d.Insert((void*)this,(void*)this); // Stop recursion
3883 if (Parms < _domain->_cnt)
3884 _domain->field_at(Parms)->dump2(d,depth-1,st);
3885 for (uint i = Parms+1; i < _domain->_cnt; i++) {
3886 st->print(", ");
3887 _domain->field_at(i)->dump2(d,depth-1,st);
3888 }
3889 st->print(" )");
3890 }
3892 //------------------------------print_flattened--------------------------------
3893 // Print a 'flattened' signature
3894 static const char * const flat_type_msg[Type::lastype] = {
3895 "bad","control","top","int","long","_", "narrowoop",
3896 "tuple:", "array:",
3897 "ptr", "rawptr", "ptr", "ptr", "ptr", "ptr",
3898 "func", "abIO", "return_address", "mem",
3899 "float_top", "ftcon:", "flt",
3900 "double_top", "dblcon:", "dbl",
3901 "bottom"
3902 };
3904 void TypeFunc::print_flattened() const {
3905 if( _range->_cnt <= Parms )
3906 tty->print("void");
3907 else {
3908 uint i;
3909 for (i = Parms; i < _range->_cnt-1; i++)
3910 tty->print("%s/",flat_type_msg[_range->field_at(i)->base()]);
3911 tty->print("%s",flat_type_msg[_range->field_at(i)->base()]);
3912 }
3913 tty->print(" ( ");
3914 if (Parms < _domain->_cnt)
3915 tty->print("%s",flat_type_msg[_domain->field_at(Parms)->base()]);
3916 for (uint i = Parms+1; i < _domain->_cnt; i++)
3917 tty->print(", %s",flat_type_msg[_domain->field_at(i)->base()]);
3918 tty->print(" )");
3919 }
3920 #endif
3922 //------------------------------singleton--------------------------------------
3923 // TRUE if Type is a singleton type, FALSE otherwise. Singletons are simple
3924 // constants (Ldi nodes). Singletons are integer, float or double constants
3925 // or a single symbol.
3926 bool TypeFunc::singleton(void) const {
3927 return false; // Never a singleton
3928 }
3930 bool TypeFunc::empty(void) const {
3931 return false; // Never empty
3932 }
3935 BasicType TypeFunc::return_type() const{
3936 if (range()->cnt() == TypeFunc::Parms) {
3937 return T_VOID;
3938 }
3939 return range()->field_at(TypeFunc::Parms)->basic_type();
3940 }