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