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