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