1 /*
2 * Copyright (c) 1999, 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/ciArray.hpp"
26 #include "ci/ciArrayKlass.hpp"
27 #include "ci/ciConstant.hpp"
28 #include "ci/ciKlass.hpp"
29 #include "ci/ciUtilities.inline.hpp"
30 #include "oops/flatArrayKlass.hpp"
31 #include "oops/layoutKind.hpp"
32 #include "oops/objArrayOop.inline.hpp"
33 #include "oops/oop.inline.hpp"
34 #include "oops/oopCast.inline.hpp"
35 #include "oops/typeArrayOop.inline.hpp"
36 #include "utilities/powerOfTwo.hpp"
37
38 // ciArray
39 //
40 // This class represents an arrayOop in the HotSpot virtual
41 // machine.
42 static BasicType fixup_element_type(BasicType bt) {
43 if (bt == T_FLAT_ELEMENT) return T_OBJECT;
44 if (is_reference_type(bt)) return T_OBJECT;
45 if (bt == T_BOOLEAN) return T_BYTE;
46 return bt;
47 }
48
49 ciConstant ciArray::element_value_impl(BasicType elembt,
50 arrayOop ary,
51 int index) {
52 if (ary == nullptr)
53 return ciConstant();
54 assert(ary->is_array(), "");
55 if (index < 0 || index >= ary->length())
56 return ciConstant();
57 ArrayKlass* ak = (ArrayKlass*) ary->klass();
58 BasicType abt = ak->element_type();
59 if (fixup_element_type(elembt) !=
60 fixup_element_type(abt))
61 return ciConstant();
62 switch (elembt) {
63 case T_ARRAY:
64 case T_OBJECT:
65 {
66 if (ary->is_refArray()) {
67 refArrayOop refary = oop_cast<refArrayOop>(ary);
68 oop elem = refary->obj_at(index);
69 return ciConstant(elembt, CURRENT_ENV->get_object(elem));
70 } else {
71 assert(ary->is_flatArray(), "");
72 flatArrayOop flatary = oop_cast<flatArrayOop>(ary);
73 assert(CompilerThread::current()->thread_state() == _thread_in_vm, "");
74 JavaThread* THREAD = CompilerThread::current();
75 oop elem = flatary->obj_at(index, THREAD);
76 if (HAS_PENDING_EXCEPTION) {
77 CLEAR_PENDING_EXCEPTION;
78 return ciConstant();
79 }
80 return ciConstant(elembt, CURRENT_ENV->get_object(elem));
81 }
82 }
83 default:
84 break;
85 }
86 assert(ary->is_typeArray(), "");
87 typeArrayOop tary = (typeArrayOop) ary;
88 jint value = 0;
89 switch (elembt) {
90 case T_LONG: return ciConstant(tary->long_at(index));
91 case T_FLOAT: return ciConstant(tary->float_at(index));
92 case T_DOUBLE: return ciConstant(tary->double_at(index));
93 default: return ciConstant();
94 case T_BYTE: value = tary->byte_at(index); break;
95 case T_BOOLEAN: value = tary->byte_at(index) & 1; break;
96 case T_SHORT: value = tary->short_at(index); break;
97 case T_CHAR: value = tary->char_at(index); break;
98 case T_INT: value = tary->int_at(index); break;
99 }
100 return ciConstant(elembt, value);
101 }
102
103 // ------------------------------------------------------------------
104 // ciArray::element_value
105 //
106 // Current value of an element.
107 // Returns T_ILLEGAL if there is no element at the given index.
108 ciConstant ciArray::element_value(int index) {
109 BasicType elembt = element_basic_type();
110 ciConstant value = check_constant_value_cache(index, elembt);
111 if (value.is_valid()) {
112 return value;
113 }
114 GUARDED_VM_ENTRY(
115 value = element_value_impl(elembt, get_arrayOop(), index);
116 )
117 add_to_constant_value_cache(index, value);
118 return value;
119 }
120
121 // ------------------------------------------------------------------
122 // ciArray::element_value_by_offset
123 //
124 // Current value of an element at the specified offset.
125 // Returns T_ILLEGAL if there is no element at the given offset.
126 ciConstant ciArray::element_value_by_offset(intptr_t element_offset) {
127 BasicType elembt = element_basic_type();
128 intptr_t shift = exact_log2(type2aelembytes(elembt));
129 intptr_t header = arrayOopDesc::base_offset_in_bytes(elembt);
130 intptr_t index = (element_offset - header) >> shift;
131 intptr_t offset = header + ((intptr_t)index << shift);
132 if (offset != element_offset || index != (jint)index || index < 0 || index >= length()) {
133 return ciConstant();
134 }
135 return element_value((jint) index);
136 }
137
138 bool ciArray::is_null_free() {
139 VM_ENTRY_MARK;
140 return get_oop()->is_null_free_array();
141 }
142
143 bool ciArray::is_atomic() {
144 VM_ENTRY_MARK;
145 arrayOop oop = get_arrayOop();
146 return !oop->is_flatArray() || LayoutKindHelper::is_atomic_flat(FlatArrayKlass::cast(oop->klass())->layout_kind());
147 }
148
149 // ------------------------------------------------------------------
150 // ciArray::print_impl
151 //
152 // Implementation of the print method.
153 void ciArray::print_impl(outputStream* st) {
154 st->print(" length=%d type=", length());
155 klass()->print(st);
156 }