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