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