1 /* 2 * Copyright (c) 2020, 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 ciKlass* superk = super_t->is_klassptr()->klass(); 37 ciKlass* subk = sub_t->isa_klassptr() ? sub_t->is_klassptr()->klass() : sub_t->is_oopptr()->klass(); 38 39 bool xsubk = sub_t->isa_klassptr() ? sub_t->is_klassptr()->klass_is_exact() : sub_t->is_oopptr()->klass_is_exact(); 40 41 42 // Oop can't be a subtype of abstract type that has no subclass. 43 if (sub_t->isa_oopptr() && superk->is_instance_klass() && 44 !superk->is_interface() && superk->is_abstract() && 45 !superk->as_instance_klass()->has_subklass()) { 46 Compile::current()->dependencies()->assert_leaf_type(superk); 47 return TypeInt::CC_GT; 48 } 49 50 // Similar to logic in CmpPNode::sub() 51 52 // Interfaces can't be trusted unless the subclass is an exact 53 // interface (it can then only be a constant) or the subclass is an 54 // exact array of interfaces (a newly allocated array of interfaces 55 // for instance) 56 if (superk && subk && 57 superk->is_loaded() && !superk->is_interface() && 58 subk->is_loaded() && (!subk->is_interface() || xsubk) && 59 (!superk->is_obj_array_klass() || 60 !superk->as_obj_array_klass()->base_element_klass()->is_interface()) && 61 (!subk->is_obj_array_klass() || 62 !subk->as_obj_array_klass()->base_element_klass()->is_interface() || 63 xsubk)) { 64 bool unrelated_classes = false; 65 if (superk->equals(subk)) { 66 // skip 67 } else if (superk->is_subtype_of(subk)) { 68 // If the subclass is exact then the superclass is a subtype of 69 // the subclass. Given they're no equals, that subtype check can 70 // only fail. 71 unrelated_classes = xsubk; 72 } else if (subk->is_subtype_of(superk)) { 73 // skip 74 } else { 75 // Neither class subtypes the other: they are unrelated and this 76 // type check is known to fail. 77 unrelated_classes = true; 78 } 79 if (unrelated_classes) { 80 TypePtr::PTR jp = sub_t->is_ptr()->join_ptr(super_t->is_ptr()->_ptr); 81 if (jp != TypePtr::Null && jp != TypePtr::BotPTR) { 82 return TypeInt::CC_GT; 83 } 84 } 85 } 86 87 if (super_t->singleton()) { 88 if (subk != NULL) { 89 switch (Compile::current()->static_subtype_check(superk, subk)) { 90 case Compile::SSC_always_false: 91 return TypeInt::CC_GT; 92 case Compile::SSC_always_true: 93 return TypeInt::CC_EQ; 94 case Compile::SSC_easy_test: 95 case Compile::SSC_full_test: 96 break; 97 default: 98 ShouldNotReachHere(); 99 } 100 } 101 } 102 103 return bottom_type(); 104 } 105 106 Node *SubTypeCheckNode::Ideal(PhaseGVN* phase, bool can_reshape) { 107 Node* obj_or_subklass = in(ObjOrSubKlass); 108 Node* superklass = in(SuperKlass); 109 110 if (obj_or_subklass == NULL || 111 superklass == NULL) { 112 return NULL; 113 } 114 115 const Type* sub_t = phase->type(obj_or_subklass); 116 const Type* super_t = phase->type(superklass); 117 118 if (!super_t->isa_klassptr() || 119 (!sub_t->isa_klassptr() && !sub_t->isa_oopptr())) { 120 return NULL; 121 } 122 123 Node* addr = NULL; 124 if (obj_or_subklass->is_DecodeNKlass()) { 125 if (obj_or_subklass->in(1) != NULL && 126 obj_or_subklass->in(1)->Opcode() == Op_LoadNKlass) { 127 addr = obj_or_subklass->in(1)->in(MemNode::Address); 128 } 129 } else if (obj_or_subklass->Opcode() == Op_LoadKlass) { 130 addr = obj_or_subklass->in(MemNode::Address); 131 } 132 133 if (addr != NULL) { 134 intptr_t con = 0; 135 Node* obj = AddPNode::Ideal_base_and_offset(addr, phase, con); 136 if (con == oopDesc::klass_offset_in_bytes() && obj != NULL) { 137 assert(is_oop(phase, obj), "only for oop input"); 138 set_req_X(ObjOrSubKlass, obj, phase); 139 return this; 140 } 141 } 142 143 // AllocateNode might have more accurate klass input 144 Node* allocated_klass = AllocateNode::Ideal_klass(obj_or_subklass, phase); 145 if (allocated_klass != NULL) { 146 assert(is_oop(phase, obj_or_subklass), "only for oop input"); 147 set_req_X(ObjOrSubKlass, allocated_klass, phase); 148 return this; 149 } 150 151 // Verify that optimizing the subtype check to a simple code pattern 152 // when possible would not constant fold better 153 assert(verify(phase), "missing Value() optimization"); 154 155 return NULL; 156 } 157 158 #ifdef ASSERT 159 bool SubTypeCheckNode::is_oop(PhaseGVN* phase, Node* n) { 160 const Type* t = phase->type(n); 161 if (!t->isa_oopptr() && t != Type::TOP) { 162 n->dump(); 163 t->dump(); tty->cr(); 164 return false; 165 } 166 return true; 167 } 168 169 static Node* record_for_cleanup(Node* n, PhaseGVN* phase) { 170 if (phase->is_IterGVN()) { 171 phase->is_IterGVN()->_worklist.push(n); // record for cleanup 172 } 173 return n; 174 } 175 bool SubTypeCheckNode::verify_helper(PhaseGVN* phase, Node* subklass, const Type* cached_t) { 176 Node* cmp = phase->transform(new CmpPNode(subklass, in(SuperKlass))); 177 record_for_cleanup(cmp, phase); 178 179 const Type* cmp_t = phase->type(cmp); 180 const Type* t = Value(phase); 181 182 if (t == cmp_t || 183 t != cached_t || // previous observations don't hold anymore 184 (cmp_t != TypeInt::CC_GT && cmp_t != TypeInt::CC_EQ)) { 185 return true; 186 } else { 187 t->dump(); tty->cr(); 188 this->dump(2); tty->cr(); 189 cmp_t->dump(); tty->cr(); 190 subklass->dump(2); tty->cr(); 191 tty->print_cr("=============================="); 192 phase->C->root()->dump(9999); 193 return false; 194 } 195 } 196 197 // Verify that optimizing the subtype check to a simple code pattern when possible would not constant fold better. 198 bool SubTypeCheckNode::verify(PhaseGVN* phase) { 199 Compile* C = phase->C; 200 Node* obj_or_subklass = in(ObjOrSubKlass); 201 Node* superklass = in(SuperKlass); 202 203 const Type* sub_t = phase->type(obj_or_subklass); 204 const Type* super_t = phase->type(superklass); 205 206 ciKlass* subk = sub_t->isa_klassptr() ? sub_t->is_klassptr()->klass() : sub_t->is_oopptr()->klass(); // can be NULL for bottom[] 207 ciKlass* superk = super_t->is_klassptr()->klass(); 208 209 if (super_t->singleton() && subk != NULL) { 210 Node* subklass = NULL; 211 if (sub_t->isa_oopptr()) { 212 Node* adr = phase->transform(new AddPNode(obj_or_subklass, obj_or_subklass, phase->MakeConX(oopDesc::klass_offset_in_bytes()))); 213 subklass = phase->transform(LoadKlassNode::make(*phase, NULL, C->immutable_memory(), adr, TypeInstPtr::KLASS)); 214 record_for_cleanup(subklass, phase); 215 } else { 216 subklass = obj_or_subklass; 217 } 218 219 const Type* cached_t = Value(phase); // cache the type to validate consistency 220 switch (C->static_subtype_check(superk, subk)) { 221 case Compile::SSC_easy_test: { 222 return verify_helper(phase, subklass, cached_t); 223 } 224 case Compile::SSC_full_test: { 225 Node* p1 = phase->transform(new AddPNode(superklass, superklass, phase->MakeConX(in_bytes(Klass::super_check_offset_offset())))); 226 Node* chk_off = phase->transform(new LoadINode(NULL, C->immutable_memory(), p1, phase->type(p1)->is_ptr(), TypeInt::INT, MemNode::unordered)); 227 record_for_cleanup(chk_off, phase); 228 229 int cacheoff_con = in_bytes(Klass::secondary_super_cache_offset()); 230 bool might_be_cache = (phase->find_int_con(chk_off, cacheoff_con) == cacheoff_con); 231 if (!might_be_cache) { 232 Node* chk_off_X = chk_off; 233 #ifdef _LP64 234 chk_off_X = phase->transform(new ConvI2LNode(chk_off_X)); 235 #endif 236 Node* p2 = phase->transform(new AddPNode(subklass, subklass, chk_off_X)); 237 Node* nkls = phase->transform(LoadKlassNode::make(*phase, NULL, C->immutable_memory(), p2, phase->type(p2)->is_ptr(), TypeInstKlassPtr::OBJECT_OR_NULL)); 238 239 return verify_helper(phase, nkls, cached_t); 240 } 241 break; 242 } 243 case Compile::SSC_always_false: 244 case Compile::SSC_always_true: 245 default: { 246 break; // nothing to do 247 } 248 } 249 } 250 251 return true; 252 } 253 #endif