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