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