1 /*
  2  * Copyright (c) 2020, 2026, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 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* super_klass_type = super_t->isa_klassptr();
 37   assert(sub_t != Type::TOP && !TypePtr::NULL_PTR->higher_equal(sub_t), "should be not null");
 38   const TypeKlassPtr* sub_klass_type = 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() && super_klass_type->isa_instklassptr() && super_klass_type->klass_is_exact()) {
 42     ciKlass* superklass = super_klass_type->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 
 54   // Handle inline type arrays
 55   //
 56   // The super klass can be an exact non-array klass constant which is known to be not flat in array (e.g. Object)
 57   // while the sub klass could very well be flat in array:
 58   //
 59   //           MyValue       <:       Object:exact
 60   //        flat in array          not flat in array
 61   //
 62   // We therefore first cast the super klass to inexact (if the class is not final itself) and recompute the flat in
 63   // array property for the super klass (all done in cast_to_exactness()) in order to check whether the sub klass is
 64   // flat in array and the super klass is not flat in array. If that's the case, the classes must be unrelated.
 65   const TypeKlassPtr* super_klass_type_for_flat_in_array = super_klass_type;
 66   if (super_klass_type->isa_instklassptr()) {
 67     // Only relevant for TypeInstKlassPtr. TypeAryKlassPtr will always be not flat in array.
 68     super_klass_type_for_flat_in_array = super_klass_type->cast_to_exactness(false);
 69   }
 70 
 71   if ((sub_klass_type->is_flat_in_array() && super_klass_type_for_flat_in_array->is_not_flat_in_array()) ||
 72       (super_klass_type_for_flat_in_array->is_flat_in_array() && sub_klass_type->is_not_flat_in_array())) {
 73     // The subtype is flat in array and the supertype is not in flat array or vice versa. Cannot subtype and thus unrelated.
 74     unrelated_classes = true;
 75   } else if (sub_klass_type->is_not_flat() && super_klass_type->is_flat()) {
 76     // The subtype is a non-flat array and the supertype is a flat array. Must be unrelated.
 77     unrelated_classes = true;
 78   } else if (sub_klass_type->is_not_null_free() && super_klass_type->is_null_free()) {
 79     // The subtype is a nullable array and the supertype is null-free array. Must be unrelated.
 80     unrelated_classes = true;
 81   }
 82   if (unrelated_classes) {
 83     TypePtr::PTR jp = sub_t->is_ptr()->join_ptr(super_t->is_ptr()->_ptr);
 84     if (jp != TypePtr::Null && jp != TypePtr::BotPTR) {
 85       return TypeInt::CC_GT;
 86     }
 87   }
 88 
 89   switch (Compile::current()->static_subtype_check(super_klass_type, sub_klass_type, false)) {
 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   return bottom_type();
102 }
103 
104 Node *SubTypeCheckNode::Ideal(PhaseGVN* phase, bool can_reshape) {
105   Node* obj_or_subklass = in(ObjOrSubKlass);
106   Node* superklass = in(SuperKlass);
107 
108   if (obj_or_subklass == nullptr ||
109       superklass == nullptr) {
110     return nullptr;
111   }
112 
113   const Type* sub_t = phase->type(obj_or_subklass);
114   const Type* super_t = phase->type(superklass);
115 
116   if (!super_t->isa_klassptr() ||
117       (!sub_t->isa_klassptr() && !sub_t->isa_oopptr())) {
118     return nullptr;
119   }
120 
121   Node* addr = nullptr;
122   if (obj_or_subklass->is_DecodeNKlass()) {
123     if (obj_or_subklass->in(1) != nullptr &&
124         obj_or_subklass->in(1)->Opcode() == Op_LoadNKlass) {
125       addr = obj_or_subklass->in(1)->in(MemNode::Address);
126     }
127   } else if (obj_or_subklass->Opcode() == Op_LoadKlass) {
128     addr = obj_or_subklass->in(MemNode::Address);
129   }
130 
131   if (addr != nullptr) {
132     intptr_t con = 0;
133     Node* obj = AddPNode::Ideal_base_and_offset(addr, phase, con);
134     if (con == oopDesc::klass_offset_in_bytes() && obj != nullptr) {
135       assert(is_oop(phase, obj), "only for oop input");
136       set_req_X(ObjOrSubKlass, obj, phase);
137       return this;
138     }
139   }
140 
141   // AllocateNode might have more accurate klass input
142   Node* allocated_klass = AllocateNode::Ideal_klass(obj_or_subklass, phase);
143   if (allocated_klass != nullptr) {
144     assert(is_oop(phase, obj_or_subklass), "only for oop input");
145     set_req_X(ObjOrSubKlass, allocated_klass, phase);
146     return this;
147   }
148 
149   // Verify that optimizing the subtype check to a simple code pattern
150   // when possible would not constant fold better
151   assert(verify(phase), "missing Value() optimization");
152 
153   return nullptr;
154 }
155 
156 #ifdef ASSERT
157 bool SubTypeCheckNode::is_oop(PhaseGVN* phase, Node* n) {
158     const Type* t = phase->type(n);
159     if (!t->isa_oopptr() && t != Type::TOP) {
160       n->dump();
161       t->dump(); tty->cr();
162       return false;
163     }
164     return true;
165 }
166 
167 static Node* record_for_cleanup(Node* n, PhaseGVN* phase) {
168   if (phase->is_IterGVN()) {
169     phase->is_IterGVN()->_worklist.push(n); // record for cleanup
170   }
171   return n;
172 }
173 bool SubTypeCheckNode::verify_helper(PhaseGVN* phase, Node* subklass, const Type* cached_t) {
174   Node* cmp_orig = new CmpPNode(subklass, in(SuperKlass));
175   Node* cmp = phase->transform(cmp_orig);
176   record_for_cleanup(cmp, phase);
177 
178   const Type* cmp_t = phase->type(cmp);
179   const Type* t = Value(phase);
180 
181   if (t == cmp_t ||
182       t != cached_t || // previous observations don't hold anymore
183       (cmp_t != TypeInt::CC_GT && cmp_t != TypeInt::CC_EQ)) {
184     return true;
185   } else {
186     t->dump(); tty->cr();
187     this->dump(2); tty->cr();
188     tty->print_cr("VS.\n");
189     cmp_t->dump(); tty->cr();
190     cmp_orig->dump(2); tty->cr();
191     tty->print_cr("==============================\n");
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   const TypeKlassPtr* superk = super_t->isa_klassptr();
207   const TypeKlassPtr* subk = sub_t->isa_klassptr() ? sub_t->is_klassptr() : sub_t->is_oopptr()->as_klass_type();
208 
209   if (super_t->singleton() && subk != nullptr) {
210     if (obj_or_subklass->bottom_type() == Type::TOP) {
211       // The bottom type of obj_or_subklass is TOP, despite its recorded type
212       // being an OOP or a klass pointer. This can happen for example in
213       // transient scenarios where obj_or_subklass is a projection of the TOP
214       // node. In such cases, skip verification to avoid violating the contract
215       // of LoadKlassNode::make(). This does not weaken the effect of verify(),
216       // as SubTypeCheck nodes with TOP obj_or_subklass inputs are dead anyway.
217       return true;
218     }
219     const Type* cached_t = Value(phase); // cache the type to validate consistency
220     switch (C->static_subtype_check(superk, subk, false)) {
221       case Compile::SSC_easy_test: {
222         return verify_helper(phase, load_klass(phase), cached_t);
223       }
224       case Compile::SSC_full_test: {
225         Node* p1 = phase->transform(AddPNode::make_off_heap(superklass, phase->MakeConX(in_bytes(Klass::super_check_offset_offset()))));
226         Node* chk_off = phase->transform(new LoadINode(nullptr, 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* subklass = load_klass(phase);
233           Node* chk_off_X = chk_off;
234 #ifdef _LP64
235           chk_off_X = phase->transform(new ConvI2LNode(chk_off_X));
236 #endif
237           Node* p2 = phase->transform(AddPNode::make_off_heap(subklass, chk_off_X));
238           Node* nkls = phase->transform(LoadKlassNode::make(*phase, C->immutable_memory(), p2, phase->type(p2)->is_ptr(), TypeInstKlassPtr::OBJECT_OR_NULL));
239 
240           return verify_helper(phase, nkls, cached_t);
241         }
242         break;
243       }
244       case Compile::SSC_always_false:
245       case Compile::SSC_always_true:
246       default: {
247         break; // nothing to do
248       }
249     }
250   }
251 
252   return true;
253 }
254 
255 Node* SubTypeCheckNode::load_klass(PhaseGVN* phase) const {
256   Node* obj_or_subklass = in(ObjOrSubKlass);
257   const Type* sub_t = phase->type(obj_or_subklass);
258   Node* subklass = nullptr;
259   if (sub_t->isa_oopptr()) {
260     Node* adr = phase->transform(AddPNode::make_with_base(obj_or_subklass, phase->MakeConX(oopDesc::klass_offset_in_bytes())));
261     subklass  = phase->transform(LoadKlassNode::make(*phase, phase->C->immutable_memory(), adr, TypeInstPtr::KLASS));
262     record_for_cleanup(subklass, phase);
263   } else {
264     subklass = obj_or_subklass;
265   }
266   return subklass;
267 }
268 #endif
269 
270 uint SubTypeCheckNode::size_of() const {
271   return sizeof(*this);
272 }
273 
274 uint SubTypeCheckNode::hash() const {
275   return NO_HASH;
276 }
277 
278 #ifndef PRODUCT
279 void SubTypeCheckNode::dump_spec(outputStream* st) const {
280   if (_method != nullptr) {
281     st->print(" profiled at:");
282     _method->print_short_name(st);
283     st->print(":%d", _bci);
284   }
285 }
286 #endif