1 /* 2 * Copyright (c) 2020, 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 "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 if (subk->is_same_java_type_as(superk) && !sub_t->maybe_null()) { 47 // The super_t has no subclasses, and sub_t has the same type and is not null, 48 // hence the check should always evaluate to EQ. However, this is an impossible 49 // situation since super_t is also abstract, and hence sub_t cannot have the 50 // same type and be non-null. 51 // Still, if the non-static method of an abstract class without subclasses is 52 // force-compiled, the Param0 has the self/this pointer with NotNull. This 53 // method would now never be called, because of the leaf-type dependency. Hence, 54 // just for consistency with verification, we return EQ. 55 return TypeInt::CC_EQ; 56 } 57 // subk is either a supertype of superk, or null. In either case, superk is a subtype. 58 return TypeInt::CC_GT; 59 } 60 } 61 62 if (subk != nullptr) { 63 switch (Compile::current()->static_subtype_check(superk, subk, false)) { 64 case Compile::SSC_always_false: 65 return TypeInt::CC_GT; 66 case Compile::SSC_always_true: 67 return TypeInt::CC_EQ; 68 case Compile::SSC_easy_test: 69 case Compile::SSC_full_test: 70 break; 71 default: 72 ShouldNotReachHere(); 73 } 74 } 75 76 return bottom_type(); 77 } 78 79 Node *SubTypeCheckNode::Ideal(PhaseGVN* phase, bool can_reshape) { 80 Node* obj_or_subklass = in(ObjOrSubKlass); 81 Node* superklass = in(SuperKlass); 82 83 if (obj_or_subklass == nullptr || 84 superklass == nullptr) { 85 return nullptr; 86 } 87 88 const Type* sub_t = phase->type(obj_or_subklass); 89 const Type* super_t = phase->type(superklass); 90 91 if (!super_t->isa_klassptr() || 92 (!sub_t->isa_klassptr() && !sub_t->isa_oopptr())) { 93 return nullptr; 94 } 95 96 Node* addr = nullptr; 97 if (obj_or_subklass->is_DecodeNKlass()) { 98 if (obj_or_subklass->in(1) != nullptr && 99 obj_or_subklass->in(1)->Opcode() == Op_LoadNKlass) { 100 addr = obj_or_subklass->in(1)->in(MemNode::Address); 101 } 102 } else if (obj_or_subklass->Opcode() == Op_LoadKlass) { 103 addr = obj_or_subklass->in(MemNode::Address); 104 } 105 106 if (addr != nullptr) { 107 intptr_t con = 0; 108 Node* obj = AddPNode::Ideal_base_and_offset(addr, phase, con); 109 if (con == oopDesc::klass_offset_in_bytes() && obj != nullptr) { 110 assert(is_oop(phase, obj), "only for oop input"); 111 set_req_X(ObjOrSubKlass, obj, phase); 112 return this; 113 } 114 } 115 116 // AllocateNode might have more accurate klass input 117 Node* allocated_klass = AllocateNode::Ideal_klass(obj_or_subklass, phase); 118 if (allocated_klass != nullptr) { 119 assert(is_oop(phase, obj_or_subklass), "only for oop input"); 120 set_req_X(ObjOrSubKlass, allocated_klass, phase); 121 return this; 122 } 123 124 // Verify that optimizing the subtype check to a simple code pattern 125 // when possible would not constant fold better 126 assert(verify(phase), "missing Value() optimization"); 127 128 return nullptr; 129 } 130 131 #ifdef ASSERT 132 bool SubTypeCheckNode::is_oop(PhaseGVN* phase, Node* n) { 133 const Type* t = phase->type(n); 134 if (!t->isa_oopptr() && t != Type::TOP) { 135 n->dump(); 136 t->dump(); tty->cr(); 137 return false; 138 } 139 return true; 140 } 141 142 static Node* record_for_cleanup(Node* n, PhaseGVN* phase) { 143 if (phase->is_IterGVN()) { 144 phase->is_IterGVN()->_worklist.push(n); // record for cleanup 145 } 146 return n; 147 } 148 bool SubTypeCheckNode::verify_helper(PhaseGVN* phase, Node* subklass, const Type* cached_t) { 149 Node* cmp = phase->transform(new CmpPNode(subklass, in(SuperKlass))); 150 record_for_cleanup(cmp, phase); 151 152 const Type* cmp_t = phase->type(cmp); 153 const Type* t = Value(phase); 154 155 if (t == cmp_t || 156 t != cached_t || // previous observations don't hold anymore 157 (cmp_t != TypeInt::CC_GT && cmp_t != TypeInt::CC_EQ)) { 158 return true; 159 } else { 160 t->dump(); tty->cr(); 161 this->dump(2); tty->cr(); 162 cmp_t->dump(); tty->cr(); 163 subklass->dump(2); tty->cr(); 164 tty->print_cr("=============================="); 165 phase->C->root()->dump(9999); 166 return false; 167 } 168 } 169 170 // Verify that optimizing the subtype check to a simple code pattern when possible would not constant fold better. 171 bool SubTypeCheckNode::verify(PhaseGVN* phase) { 172 Compile* C = phase->C; 173 Node* obj_or_subklass = in(ObjOrSubKlass); 174 Node* superklass = in(SuperKlass); 175 176 const Type* sub_t = phase->type(obj_or_subklass); 177 const Type* super_t = phase->type(superklass); 178 179 const TypeKlassPtr* superk = super_t->isa_klassptr(); 180 const TypeKlassPtr* subk = sub_t->isa_klassptr() ? sub_t->is_klassptr() : sub_t->is_oopptr()->as_klass_type(); 181 182 if (super_t->singleton() && subk != nullptr) { 183 if (obj_or_subklass->bottom_type() == Type::TOP) { 184 // The bottom type of obj_or_subklass is TOP, despite its recorded type 185 // being an OOP or a klass pointer. This can happen for example in 186 // transient scenarios where obj_or_subklass is a projection of the TOP 187 // node. In such cases, skip verification to avoid violating the contract 188 // of LoadKlassNode::make(). This does not weaken the effect of verify(), 189 // as SubTypeCheck nodes with TOP obj_or_subklass inputs are dead anyway. 190 return true; 191 } 192 const Type* cached_t = Value(phase); // cache the type to validate consistency 193 switch (C->static_subtype_check(superk, subk)) { 194 case Compile::SSC_easy_test: { 195 return verify_helper(phase, load_klass(phase), cached_t); 196 } 197 case Compile::SSC_full_test: { 198 Node* p1 = phase->transform(new AddPNode(superklass, superklass, phase->MakeConX(in_bytes(Klass::super_check_offset_offset())))); 199 Node* chk_off = phase->transform(new LoadINode(nullptr, C->immutable_memory(), p1, phase->type(p1)->is_ptr(), TypeInt::INT, MemNode::unordered)); 200 record_for_cleanup(chk_off, phase); 201 202 int cacheoff_con = in_bytes(Klass::secondary_super_cache_offset()); 203 bool might_be_cache = (phase->find_int_con(chk_off, cacheoff_con) == cacheoff_con); 204 if (!might_be_cache) { 205 Node* subklass = load_klass(phase); 206 Node* chk_off_X = chk_off; 207 #ifdef _LP64 208 chk_off_X = phase->transform(new ConvI2LNode(chk_off_X)); 209 #endif 210 Node* p2 = phase->transform(new AddPNode(subklass, subklass, chk_off_X)); 211 Node* nkls = phase->transform(LoadKlassNode::make(*phase, nullptr, C->immutable_memory(), p2, phase->type(p2)->is_ptr(), TypeInstKlassPtr::OBJECT_OR_NULL)); 212 213 return verify_helper(phase, nkls, cached_t); 214 } 215 break; 216 } 217 case Compile::SSC_always_false: 218 case Compile::SSC_always_true: 219 default: { 220 break; // nothing to do 221 } 222 } 223 } 224 225 return true; 226 } 227 228 Node* SubTypeCheckNode::load_klass(PhaseGVN* phase) const { 229 Node* obj_or_subklass = in(ObjOrSubKlass); 230 const Type* sub_t = phase->type(obj_or_subklass); 231 Node* subklass = nullptr; 232 if (sub_t->isa_oopptr()) { 233 Node* adr = phase->transform(new AddPNode(obj_or_subklass, obj_or_subklass, phase->MakeConX(oopDesc::klass_offset_in_bytes()))); 234 subklass = phase->transform(LoadKlassNode::make(*phase, nullptr, phase->C->immutable_memory(), adr, TypeInstPtr::KLASS)); 235 record_for_cleanup(subklass, phase); 236 } else { 237 subklass = obj_or_subklass; 238 } 239 return subklass; 240 } 241 #endif 242 243 uint SubTypeCheckNode::size_of() const { 244 return sizeof(*this); 245 } 246 247 uint SubTypeCheckNode::hash() const { 248 return NO_HASH; 249 } 250 251 #ifndef PRODUCT 252 void SubTypeCheckNode::dump_spec(outputStream* st) const { 253 if (_method != nullptr) { 254 st->print(" profiled at: "); 255 _method->print_short_name(st); 256 st->print(":%d", _bci); 257 } 258 } 259 #endif