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