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