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