1 /* 2 * Copyright (c) 2020, 2024, 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 "opto/addnode.hpp" 27 #include "opto/callnode.hpp" 28 #include "opto/connode.hpp" 29 #include "opto/convertnode.hpp" 30 #include "opto/phaseX.hpp" 31 #include "opto/rootnode.hpp" 32 #include "opto/subnode.hpp" 33 #include "opto/subtypenode.hpp" 34 35 const Type* SubTypeCheckNode::sub(const Type* sub_t, const Type* super_t) const { 36 const TypeKlassPtr* superk = super_t->isa_klassptr(); 37 assert(sub_t != Type::TOP && !TypePtr::NULL_PTR->higher_equal(sub_t), "should be not null"); 38 const TypeKlassPtr* subk = sub_t->isa_klassptr() ? sub_t->is_klassptr() : sub_t->is_oopptr()->as_klass_type(); 39 40 // Oop can't be a subtype of abstract type that has no subclass. 41 if (sub_t->isa_oopptr() && superk->isa_instklassptr() && superk->klass_is_exact()) { 42 ciKlass* superklass = superk->exact_klass(); 43 if (!superklass->is_interface() && superklass->is_abstract() && 44 !superklass->as_instance_klass()->has_subklass()) { 45 Compile::current()->dependencies()->assert_leaf_type(superklass); 46 return TypeInt::CC_GT; 47 } 48 } 49 50 // FIXME: shouldn't this be encoded in helper methods of the type system (maybe_java_subtype_of() etc.?) 51 // Similar to logic in CmpPNode::sub() 52 bool unrelated_classes = false; 53 // Handle inline type arrays 54 if (subk->flat_in_array() && superk->not_flat_in_array()) { 55 // The subtype is in flat arrays and the supertype is not in flat arrays. Must be unrelated. 56 unrelated_classes = true; 57 } else if (subk->is_not_flat() && superk->is_flat()) { 58 // The subtype is a non-flat array and the supertype is a flat array. Must be unrelated. 59 unrelated_classes = true; 60 } else if (subk->is_not_null_free() && superk->is_null_free()) { 61 // The subtype is a nullable array and the supertype is null-free array. Must be unrelated. 62 unrelated_classes = true; 63 } 64 if (unrelated_classes) { 65 TypePtr::PTR jp = sub_t->is_ptr()->join_ptr(super_t->is_ptr()->_ptr); 66 if (jp != TypePtr::Null && jp != TypePtr::BotPTR) { 67 return TypeInt::CC_GT; 68 } 69 } 70 71 if (subk != nullptr) { 72 switch (Compile::current()->static_subtype_check(superk, subk, false)) { 73 case Compile::SSC_always_false: 74 return TypeInt::CC_GT; 75 case Compile::SSC_always_true: 76 return TypeInt::CC_EQ; 77 case Compile::SSC_easy_test: 78 case Compile::SSC_full_test: 79 break; 80 default: 81 ShouldNotReachHere(); 82 } 83 } 84 85 return bottom_type(); 86 } 87 88 Node *SubTypeCheckNode::Ideal(PhaseGVN* phase, bool can_reshape) { 89 Node* obj_or_subklass = in(ObjOrSubKlass); 90 Node* superklass = in(SuperKlass); 91 92 if (obj_or_subklass == nullptr || 93 superklass == nullptr) { 94 return nullptr; 95 } 96 97 const Type* sub_t = phase->type(obj_or_subklass); 98 const Type* super_t = phase->type(superklass); 99 100 if (!super_t->isa_klassptr() || 101 (!sub_t->isa_klassptr() && !sub_t->isa_oopptr())) { 102 return nullptr; 103 } 104 105 Node* addr = nullptr; 106 if (obj_or_subklass->is_DecodeNKlass()) { 107 if (obj_or_subklass->in(1) != nullptr && 108 obj_or_subklass->in(1)->Opcode() == Op_LoadNKlass) { 109 addr = obj_or_subklass->in(1)->in(MemNode::Address); 110 } 111 } else if (obj_or_subklass->Opcode() == Op_LoadKlass) { 112 addr = obj_or_subklass->in(MemNode::Address); 113 } 114 115 if (addr != nullptr) { 116 intptr_t con = 0; 117 Node* obj = AddPNode::Ideal_base_and_offset(addr, phase, con); 118 if (con == oopDesc::klass_offset_in_bytes() && obj != nullptr) { 119 assert(is_oop(phase, obj), "only for oop input"); 120 set_req_X(ObjOrSubKlass, obj, phase); 121 return this; 122 } 123 } 124 125 // AllocateNode might have more accurate klass input 126 Node* allocated_klass = AllocateNode::Ideal_klass(obj_or_subklass, phase); 127 if (allocated_klass != nullptr) { 128 assert(is_oop(phase, obj_or_subklass), "only for oop input"); 129 set_req_X(ObjOrSubKlass, allocated_klass, phase); 130 return this; 131 } 132 133 // Verify that optimizing the subtype check to a simple code pattern 134 // when possible would not constant fold better 135 assert(verify(phase), "missing Value() optimization"); 136 137 return nullptr; 138 } 139 140 #ifdef ASSERT 141 bool SubTypeCheckNode::is_oop(PhaseGVN* phase, Node* n) { 142 const Type* t = phase->type(n); 143 if (!t->isa_oopptr() && t != Type::TOP) { 144 n->dump(); 145 t->dump(); tty->cr(); 146 return false; 147 } 148 return true; 149 } 150 151 static Node* record_for_cleanup(Node* n, PhaseGVN* phase) { 152 if (phase->is_IterGVN()) { 153 phase->is_IterGVN()->_worklist.push(n); // record for cleanup 154 } 155 return n; 156 } 157 bool SubTypeCheckNode::verify_helper(PhaseGVN* phase, Node* subklass, const Type* cached_t) { 158 Node* cmp = phase->transform(new CmpPNode(subklass, in(SuperKlass))); 159 record_for_cleanup(cmp, phase); 160 161 const Type* cmp_t = phase->type(cmp); 162 const Type* t = Value(phase); 163 164 if (t == cmp_t || 165 t != cached_t || // previous observations don't hold anymore 166 (cmp_t != TypeInt::CC_GT && cmp_t != TypeInt::CC_EQ)) { 167 return true; 168 } else { 169 t->dump(); tty->cr(); 170 this->dump(2); tty->cr(); 171 cmp_t->dump(); tty->cr(); 172 subklass->dump(2); tty->cr(); 173 tty->print_cr("=============================="); 174 phase->C->root()->dump(9999); 175 return false; 176 } 177 } 178 179 // Verify that optimizing the subtype check to a simple code pattern when possible would not constant fold better. 180 bool SubTypeCheckNode::verify(PhaseGVN* phase) { 181 Compile* C = phase->C; 182 Node* obj_or_subklass = in(ObjOrSubKlass); 183 Node* superklass = in(SuperKlass); 184 185 const Type* sub_t = phase->type(obj_or_subklass); 186 const Type* super_t = phase->type(superklass); 187 188 const TypeKlassPtr* superk = super_t->isa_klassptr(); 189 const TypeKlassPtr* subk = sub_t->isa_klassptr() ? sub_t->is_klassptr() : sub_t->is_oopptr()->as_klass_type(); 190 191 if (super_t->singleton() && subk != nullptr) { 192 if (obj_or_subklass->bottom_type() == Type::TOP) { 193 // The bottom type of obj_or_subklass is TOP, despite its recorded type 194 // being an OOP or a klass pointer. This can happen for example in 195 // transient scenarios where obj_or_subklass is a projection of the TOP 196 // node. In such cases, skip verification to avoid violating the contract 197 // of LoadKlassNode::make(). This does not weaken the effect of verify(), 198 // as SubTypeCheck nodes with TOP obj_or_subklass inputs are dead anyway. 199 return true; 200 } 201 const Type* cached_t = Value(phase); // cache the type to validate consistency 202 switch (C->static_subtype_check(superk, subk)) { 203 case Compile::SSC_easy_test: { 204 return verify_helper(phase, load_klass(phase), cached_t); 205 } 206 case Compile::SSC_full_test: { 207 Node* p1 = phase->transform(new AddPNode(superklass, superklass, phase->MakeConX(in_bytes(Klass::super_check_offset_offset())))); 208 Node* chk_off = phase->transform(new LoadINode(nullptr, C->immutable_memory(), p1, phase->type(p1)->is_ptr(), TypeInt::INT, MemNode::unordered)); 209 record_for_cleanup(chk_off, phase); 210 211 int cacheoff_con = in_bytes(Klass::secondary_super_cache_offset()); 212 bool might_be_cache = (phase->find_int_con(chk_off, cacheoff_con) == cacheoff_con); 213 if (!might_be_cache) { 214 Node* subklass = load_klass(phase); 215 Node* chk_off_X = chk_off; 216 #ifdef _LP64 217 chk_off_X = phase->transform(new ConvI2LNode(chk_off_X)); 218 #endif 219 Node* p2 = phase->transform(new AddPNode(subklass, subklass, chk_off_X)); 220 Node* nkls = phase->transform(LoadKlassNode::make(*phase, nullptr, C->immutable_memory(), p2, phase->type(p2)->is_ptr(), TypeInstKlassPtr::OBJECT_OR_NULL)); 221 222 return verify_helper(phase, nkls, cached_t); 223 } 224 break; 225 } 226 case Compile::SSC_always_false: 227 case Compile::SSC_always_true: 228 default: { 229 break; // nothing to do 230 } 231 } 232 } 233 234 return true; 235 } 236 237 Node* SubTypeCheckNode::load_klass(PhaseGVN* phase) const { 238 Node* obj_or_subklass = in(ObjOrSubKlass); 239 const Type* sub_t = phase->type(obj_or_subklass); 240 Node* subklass = nullptr; 241 if (sub_t->isa_oopptr()) { 242 Node* adr = phase->transform(new AddPNode(obj_or_subklass, obj_or_subklass, phase->MakeConX(oopDesc::klass_offset_in_bytes()))); 243 subklass = phase->transform(LoadKlassNode::make(*phase, nullptr, phase->C->immutable_memory(), adr, TypeInstPtr::KLASS)); 244 record_for_cleanup(subklass, phase); 245 } else { 246 subklass = obj_or_subklass; 247 } 248 return subklass; 249 } 250 #endif 251 252 uint SubTypeCheckNode::size_of() const { 253 return sizeof(*this); 254 } 255 256 uint SubTypeCheckNode::hash() const { 257 return NO_HASH; 258 } 259 260 #ifndef PRODUCT 261 void SubTypeCheckNode::dump_spec(outputStream* st) const { 262 if (_method != nullptr) { 263 st->print(" profiled at:"); 264 _method->print_short_name(st); 265 st->print(":%d", _bci); 266 } 267 } 268 #endif