1 /*
2 * Copyright (c) 1998, 2025, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "ci/ciSymbols.hpp"
26 #include "compiler/compileLog.hpp"
27 #include "oops/objArrayKlass.hpp"
28 #include "opto/addnode.hpp"
29 #include "opto/memnode.hpp"
30 #include "opto/mulnode.hpp"
31 #include "opto/parse.hpp"
32 #include "opto/rootnode.hpp"
33 #include "opto/runtime.hpp"
34 #include "runtime/runtimeUpcalls.hpp"
35 #include "runtime/sharedRuntime.hpp"
36
37 void GraphKit::install_on_method_entry_runtime_upcalls(ciMethod* method) {
38 MethodDetails method_details(method);
39 RuntimeUpcallInfo* upcall = RuntimeUpcalls::get_first_upcall(RuntimeUpcallType::onMethodEntry, method_details);
40 while (upcall != nullptr) {
41 // Get base of thread-local storage area
42 Node* thread = _gvn.transform( new ThreadLocalNode() );
43 kill_dead_locals();
44
45 // For some reason, this call reads only raw memory.
46 const TypeFunc *call_type = OptoRuntime::runtime_up_call_Type();
47 const TypePtr* raw_adr_type = TypeRawPtr::BOTTOM;
48 make_runtime_call(RC_LEAF | RC_NARROW_MEM,
49 call_type, upcall->upcall_address(),
50 upcall->upcall_name(), raw_adr_type,
51 thread);
52
53 upcall = RuntimeUpcalls::get_next_upcall(RuntimeUpcallType::onMethodEntry, method_details, upcall);
54 }
55 }
56
57 //------------------------------make_dtrace_method_entry_exit ----------------
58 // Dtrace -- record entry or exit of a method if compiled with dtrace support
59 void GraphKit::make_dtrace_method_entry_exit(ciMethod* method, bool is_entry) {
60 const TypeFunc *call_type = OptoRuntime::dtrace_method_entry_exit_Type();
61 address call_address = is_entry ? CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_entry) :
62 CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit);
63 const char *call_name = is_entry ? "dtrace_method_entry" : "dtrace_method_exit";
64
65 // Get base of thread-local storage area
66 Node* thread = _gvn.transform( new ThreadLocalNode() );
67
68 // Get method
69 const TypePtr* method_type = TypeMetadataPtr::make(method);
70 Node *method_node = _gvn.transform(ConNode::make(method_type));
71
72 kill_dead_locals();
73
74 // For some reason, this call reads only raw memory.
75 const TypePtr* raw_adr_type = TypeRawPtr::BOTTOM;
76 make_runtime_call(RC_LEAF | RC_NARROW_MEM,
77 call_type, call_address,
78 call_name, raw_adr_type,
79 thread, method_node);
80 }
81
82
83 //=============================================================================
84 //------------------------------do_checkcast-----------------------------------
85 void Parse::do_checkcast() {
86 bool will_link;
87 ciKlass* klass = iter().get_klass(will_link);
88
89 Node *obj = peek();
90
91 // Throw uncommon trap if class is not loaded or the value we are casting
92 // _from_ is not loaded, and value is not null. If the value _is_ null,
93 // then the checkcast does nothing.
94 const TypeOopPtr *tp = _gvn.type(obj)->isa_oopptr();
95 if (!will_link || (tp && !tp->is_loaded())) {
96 if (C->log() != nullptr) {
97 if (!will_link) {
98 C->log()->elem("assert_null reason='checkcast' klass='%d'",
99 C->log()->identify(klass));
100 }
101 if (tp && !tp->is_loaded()) {
102 // %%% Cannot happen?
103 ciKlass* klass = tp->unloaded_klass();
104 C->log()->elem("assert_null reason='checkcast source' klass='%d'",
105 C->log()->identify(klass));
106 }
107 }
108 null_assert(obj);
109 assert( stopped() || _gvn.type(peek())->higher_equal(TypePtr::NULL_PTR), "what's left behind is null" );
110 return;
111 }
112
113 Node* res = gen_checkcast(obj, makecon(TypeKlassPtr::make(klass, Type::trust_interfaces)));
114 if (stopped()) {
115 return;
116 }
117
118 // Pop from stack AFTER gen_checkcast because it can uncommon trap and
119 // the debug info has to be correct.
120 pop();
121 push(res);
122 }
123
124
125 //------------------------------do_instanceof----------------------------------
126 void Parse::do_instanceof() {
127 if (stopped()) return;
128 // We would like to return false if class is not loaded, emitting a
129 // dependency, but Java requires instanceof to load its operand.
130
131 // Throw uncommon trap if class is not loaded
132 bool will_link;
133 ciKlass* klass = iter().get_klass(will_link);
134
135 if (!will_link) {
136 if (C->log() != nullptr) {
137 C->log()->elem("assert_null reason='instanceof' klass='%d'",
138 C->log()->identify(klass));
139 }
140 null_assert(peek());
141 assert( stopped() || _gvn.type(peek())->higher_equal(TypePtr::NULL_PTR), "what's left behind is null" );
142 if (!stopped()) {
143 // The object is now known to be null.
144 // Shortcut the effect of gen_instanceof and return "false" directly.
145 pop(); // pop the null
146 push(_gvn.intcon(0)); // push false answer
147 }
148 return;
149 }
150
151 // Push the bool result back on stack
152 Node* res = gen_instanceof(peek(), makecon(TypeKlassPtr::make(klass, Type::trust_interfaces)), true);
153
154 // Pop from stack AFTER gen_instanceof because it can uncommon trap.
155 pop();
156 push(res);
157 }
158
159 //------------------------------array_store_check------------------------------
160 // pull array from stack and check that the store is valid
161 void Parse::array_store_check() {
162
163 // Shorthand access to array store elements without popping them.
164 Node *obj = peek(0);
165 Node *idx = peek(1);
166 Node *ary = peek(2);
167
168 if (_gvn.type(obj) == TypePtr::NULL_PTR) {
169 // There's never a type check on null values.
170 // This cutout lets us avoid the uncommon_trap(Reason_array_check)
171 // below, which turns into a performance liability if the
172 // gen_checkcast folds up completely.
173 return;
174 }
175
176 // Extract the array klass type
177 int klass_offset = oopDesc::klass_offset_in_bytes();
178 Node* p = basic_plus_adr( ary, ary, klass_offset );
179 // p's type is array-of-OOPS plus klass_offset
180 Node* array_klass = _gvn.transform(LoadKlassNode::make(_gvn, immutable_memory(), p, TypeInstPtr::KLASS));
181 // Get the array klass
182 const TypeKlassPtr *tak = _gvn.type(array_klass)->is_klassptr();
183
184 // The type of array_klass is usually INexact array-of-oop. Heroically
185 // cast array_klass to EXACT array and uncommon-trap if the cast fails.
186 // Make constant out of the inexact array klass, but use it only if the cast
187 // succeeds.
188 if (MonomorphicArrayCheck &&
189 !too_many_traps(Deoptimization::Reason_array_check) &&
190 !tak->klass_is_exact() &&
191 tak->isa_aryklassptr()) {
192 // Regarding the fourth condition in the if-statement from above:
193 //
194 // If the compiler has determined that the type of array 'ary' (represented
195 // by 'array_klass') is java/lang/Object, the compiler must not assume that
196 // the array 'ary' is monomorphic.
197 //
198 // If 'ary' were of type java/lang/Object, this arraystore would have to fail,
199 // because it is not possible to perform a arraystore into an object that is not
200 // a "proper" array.
201 //
202 // Therefore, let's obtain at runtime the type of 'ary' and check if we can still
203 // successfully perform the store.
204 //
205 // The implementation reasons for the condition are the following:
206 //
207 // java/lang/Object is the superclass of all arrays, but it is represented by the VM
208 // as an InstanceKlass. The checks generated by gen_checkcast() (see below) expect
209 // 'array_klass' to be ObjArrayKlass, which can result in invalid memory accesses.
210 //
211 // See issue JDK-8057622 for details.
212
213 // Make a constant out of the exact array klass
214 const TypeAryKlassPtr* extak = tak->cast_to_exactness(true)->is_aryklassptr();
215 if (extak->exact_klass(true) != nullptr) {
216 Node* con = makecon(extak);
217 Node* cmp = _gvn.transform(new CmpPNode(array_klass, con));
218 Node* bol = _gvn.transform(new BoolNode(cmp, BoolTest::eq));
219 Node* ctrl= control();
220 { BuildCutout unless(this, bol, PROB_MAX);
221 uncommon_trap(Deoptimization::Reason_array_check,
222 Deoptimization::Action_maybe_recompile,
223 extak->exact_klass());
224 }
225 if (stopped()) { // MUST uncommon-trap?
226 set_control(ctrl); // Then Don't Do It, just fall into the normal checking
227 } else { // Cast array klass to exactness:
228 // Use the exact constant value we know it is.
229 replace_in_map(array_klass, con);
230 CompileLog* log = C->log();
231 if (log != nullptr) {
232 log->elem("cast_up reason='monomorphic_array' from='%d' to='(exact)'",
233 log->identify(extak->exact_klass()));
234 }
235 array_klass = con; // Use cast value moving forward
236 }
237 }
238 }
239
240 // Come here for polymorphic array klasses
241
242 // Extract the array element class
243 int element_klass_offset = in_bytes(ObjArrayKlass::element_klass_offset());
244 Node* p2 = basic_plus_adr(array_klass, array_klass, element_klass_offset);
245 Node* a_e_klass = _gvn.transform(LoadKlassNode::make(_gvn, immutable_memory(), p2, tak));
246 assert(array_klass->is_Con() == a_e_klass->is_Con() || StressReflectiveCode, "a constant array type must come with a constant element type");
247
248 // Check (the hard way) and throw if not a subklass.
249 // Result is ignored, we just need the CFG effects.
250 gen_checkcast(obj, a_e_klass);
251 }
252
253
254 //------------------------------do_new-----------------------------------------
255 void Parse::do_new() {
256 kill_dead_locals();
257
258 bool will_link;
259 ciInstanceKlass* klass = iter().get_klass(will_link)->as_instance_klass();
260 assert(will_link, "_new: typeflow responsibility");
261
262 // Should throw an InstantiationError?
263 if (klass->is_abstract() || klass->is_interface() ||
264 klass->name() == ciSymbols::java_lang_Class() ||
265 iter().is_unresolved_klass()) {
266 uncommon_trap(Deoptimization::Reason_unhandled,
267 Deoptimization::Action_none,
268 klass);
269 return;
270 }
271
272 if (C->needs_clinit_barrier(klass, method())) {
273 clinit_barrier(klass, method());
274 if (stopped()) return;
275 }
276
277 Node* kls = makecon(TypeKlassPtr::make(klass));
278 Node* obj = new_instance(kls);
279
280 // Push resultant oop onto stack
281 push(obj);
282
283 // Keep track of whether opportunities exist for StringBuilder
284 // optimizations.
285 if (OptimizeStringConcat &&
286 (klass == C->env()->StringBuilder_klass() ||
287 klass == C->env()->StringBuffer_klass())) {
288 C->set_has_stringbuilder(true);
289 }
290
291 // Keep track of boxed values for EliminateAutoBox optimizations.
292 if (C->eliminate_boxing() && klass->is_box_klass()) {
293 C->set_has_boxed_value(true);
294 }
295 }
296
297 #ifndef PRODUCT
298 //------------------------------dump_map_adr_mem-------------------------------
299 // Debug dump of the mapping from address types to MergeMemNode indices.
300 void Parse::dump_map_adr_mem() const {
301 tty->print_cr("--- Mapping from address types to memory Nodes ---");
302 MergeMemNode *mem = map() == nullptr ? nullptr : (map()->memory()->is_MergeMem() ?
303 map()->memory()->as_MergeMem() : nullptr);
304 for (uint i = 0; i < (uint)C->num_alias_types(); i++) {
305 C->alias_type(i)->print_on(tty);
306 tty->print("\t");
307 // Node mapping, if any
308 if (mem && i < mem->req() && mem->in(i) && mem->in(i) != mem->empty_memory()) {
309 mem->in(i)->dump();
310 } else {
311 tty->cr();
312 }
313 }
314 }
315
316 #endif