1 /*
  2  * Copyright (c) 2020, 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   ciKlass* superk = super_t->is_klassptr()->klass();
 37   ciKlass* subk   = sub_t->isa_klassptr() ? sub_t->is_klassptr()->klass() : sub_t->is_oopptr()->klass();
 38 
 39   bool xsubk = sub_t->isa_klassptr() ? sub_t->is_klassptr()->klass_is_exact() : sub_t->is_oopptr()->klass_is_exact();
 40 
 41 
 42   // Oop can't be a subtype of abstract type that has no subclass.
 43   if (sub_t->isa_oopptr() && superk->is_instance_klass() &&
 44       !superk->is_interface() && superk->is_abstract() &&
 45       !superk->as_instance_klass()->has_subklass()) {
 46     Compile::current()->dependencies()->assert_leaf_type(superk);
 47     return TypeInt::CC_GT;
 48   }
 49 
 50   // Similar to logic in CmpPNode::sub()
 51 
 52   // Interfaces can't be trusted unless the subclass is an exact
 53   // interface (it can then only be a constant) or the subclass is an
 54   // exact array of interfaces (a newly allocated array of interfaces
 55   // for instance)
 56   if (superk && subk &&
 57       superk->is_loaded() && !superk->is_interface() &&
 58       subk->is_loaded() && (!subk->is_interface() || xsubk) &&
 59       (!superk->is_obj_array_klass() ||
 60        !superk->as_obj_array_klass()->base_element_klass()->is_interface()) &&
 61       (!subk->is_obj_array_klass() ||
 62        !subk->as_obj_array_klass()->base_element_klass()->is_interface() ||
 63        xsubk)) {
 64     bool unrelated_classes = false;
 65     if (superk->equals(subk)) {
 66       // skip
 67     } else if (superk->is_subtype_of(subk)) {
 68       // If the subclass is exact then the superclass is a subtype of
 69       // the subclass. Given they're no equals, that subtype check can
 70       // only fail.
 71       unrelated_classes = xsubk;
 72     } else if (subk->is_subtype_of(superk)) {
 73       // skip
 74     } else {
 75       // Neither class subtypes the other: they are unrelated and this
 76       // type check is known to fail.
 77       unrelated_classes = true;
 78     }
 79     if (!unrelated_classes) {
 80       // Handle inline type arrays
 81       if (sub_t->isa_aryptr() && sub_t->is_aryptr()->is_not_flat() && superk->is_flat_array_klass()) {
 82         // Subtype is not a flat array but supertype is. Must be unrelated.
 83         unrelated_classes = true;
 84       } else if (sub_t->isa_aryptr() && sub_t->is_aryptr()->is_not_null_free() &&
 85                  superk->is_array_klass() && superk->as_array_klass()->is_elem_null_free()) {
 86         // Subtype is not a null-free array but supertype is. Must be unrelated.
 87         unrelated_classes = true;
 88       } else if (sub_t->is_ptr()->flatten_array() && (!superk->can_be_inline_klass() || (superk->is_inlinetype() && !superk->flatten_array()))) {
 89         // Subtype is flattened in arrays but supertype is not. Must be unrelated.
 90         unrelated_classes = true;
 91       }
 92     }
 93     if (unrelated_classes) {
 94       TypePtr::PTR jp = sub_t->is_ptr()->join_ptr(super_t->is_ptr()->_ptr);
 95       if (jp != TypePtr::Null && jp != TypePtr::BotPTR) {
 96         return TypeInt::CC_GT;
 97       }
 98     }
 99   }
100 
101   if (super_t->singleton()) {
102     if (subk != NULL) {
103       switch (Compile::current()->static_subtype_check(superk, subk)) {
104       case Compile::SSC_always_false:
105         return TypeInt::CC_GT;
106       case Compile::SSC_always_true:
107         return TypeInt::CC_EQ;
108       case Compile::SSC_easy_test:
109       case Compile::SSC_full_test:
110         break;
111       default:
112         ShouldNotReachHere();
113       }
114     }
115   }
116 
117   return bottom_type();
118 }
119 
120 Node *SubTypeCheckNode::Ideal(PhaseGVN* phase, bool can_reshape) {
121   Node* obj_or_subklass = in(ObjOrSubKlass);
122   Node* superklass = in(SuperKlass);
123 
124   if (obj_or_subklass == NULL ||
125       superklass == NULL) {
126     return NULL;
127   }
128 
129   const Type* sub_t = phase->type(obj_or_subklass);
130   const Type* super_t = phase->type(superklass);
131 
132   if (!super_t->isa_klassptr() ||
133       (!sub_t->isa_klassptr() && !sub_t->isa_oopptr())) {
134     return NULL;
135   }
136 
137   Node* addr = NULL;
138   if (obj_or_subklass->is_DecodeNKlass()) {
139     if (obj_or_subklass->in(1) != NULL &&
140         obj_or_subklass->in(1)->Opcode() == Op_LoadNKlass) {
141       addr = obj_or_subklass->in(1)->in(MemNode::Address);
142     }
143   } else if (obj_or_subklass->Opcode() == Op_LoadKlass) {
144     addr = obj_or_subklass->in(MemNode::Address);
145   }
146 
147   if (addr != NULL) {
148     intptr_t con = 0;
149     Node* obj = AddPNode::Ideal_base_and_offset(addr, phase, con);
150     if (con == oopDesc::klass_offset_in_bytes() && obj != NULL) {
151       assert(is_oop(phase, obj), "only for oop input");
152       set_req(ObjOrSubKlass, obj);
153       return this;
154     }
155   }
156 
157   // AllocateNode might have more accurate klass input
158   Node* allocated_klass = AllocateNode::Ideal_klass(obj_or_subklass, phase);
159   if (allocated_klass != NULL) {
160     assert(is_oop(phase, obj_or_subklass), "only for oop input");
161     set_req(ObjOrSubKlass, allocated_klass);
162     return this;
163   }
164 
165   // Verify that optimizing the subtype check to a simple code pattern
166   // when possible would not constant fold better
167   assert(verify(phase), "missing Value() optimization");
168 
169   return NULL;
170 }
171 
172 #ifdef ASSERT
173 bool SubTypeCheckNode::is_oop(PhaseGVN* phase, Node* n) {
174     const Type* t = phase->type(n);
175     if (!t->isa_oopptr() && t != Type::TOP) {
176       n->dump();
177       t->dump(); tty->cr();
178       return false;
179     }
180     return true;
181 }
182 
183 static Node* record_for_cleanup(Node* n, PhaseGVN* phase) {
184   if (phase->is_IterGVN()) {
185     phase->is_IterGVN()->_worklist.push(n); // record for cleanup
186   }
187   return n;
188 }
189 bool SubTypeCheckNode::verify_helper(PhaseGVN* phase, Node* subklass, const Type* cached_t) {
190   Node* cmp = phase->transform(new CmpPNode(subklass, in(SuperKlass)));
191   record_for_cleanup(cmp, phase);
192 
193   const Type* cmp_t = phase->type(cmp);
194   const Type* t = Value(phase);
195 
196   if (t == cmp_t ||
197       t != cached_t || // previous observations don't hold anymore
198       (cmp_t != TypeInt::CC_GT && cmp_t != TypeInt::CC_EQ)) {
199     return true;
200   } else {
201     t->dump(); tty->cr();
202     this->dump(2); tty->cr();
203     cmp_t->dump(); tty->cr();
204     subklass->dump(2); tty->cr();
205     tty->print_cr("==============================");
206     phase->C->root()->dump(9999);
207     return false;
208   }
209 }
210 
211 // Verify that optimizing the subtype check to a simple code pattern when possible would not constant fold better.
212 bool SubTypeCheckNode::verify(PhaseGVN* phase) {
213   Compile* C = phase->C;
214   Node* obj_or_subklass = in(ObjOrSubKlass);
215   Node* superklass = in(SuperKlass);
216 
217   const Type* sub_t = phase->type(obj_or_subklass);
218   const Type* super_t = phase->type(superklass);
219 
220   ciKlass* subk = sub_t->isa_klassptr() ? sub_t->is_klassptr()->klass() : sub_t->is_oopptr()->klass(); // can be NULL for bottom[]
221   ciKlass* superk = super_t->is_klassptr()->klass();
222 
223   if (super_t->singleton() && subk != NULL) {
224     Node* subklass = NULL;
225     if (sub_t->isa_oopptr()) {
226       Node* adr = phase->transform(new AddPNode(obj_or_subklass, obj_or_subklass, phase->MakeConX(oopDesc::klass_offset_in_bytes())));
227       subklass  = phase->transform(LoadKlassNode::make(*phase, NULL, C->immutable_memory(), adr, TypeInstPtr::KLASS));
228       record_for_cleanup(subklass, phase);
229     } else {
230       subklass = obj_or_subklass;
231     }
232 
233     const Type* cached_t = Value(phase); // cache the type to validate consistency
234     switch (C->static_subtype_check(superk, subk)) {
235       case Compile::SSC_easy_test: {
236         return verify_helper(phase, subklass, cached_t);
237       }
238       case Compile::SSC_full_test: {
239         Node* p1 = phase->transform(new AddPNode(superklass, superklass, phase->MakeConX(in_bytes(Klass::super_check_offset_offset()))));
240         Node* chk_off = phase->transform(new LoadINode(NULL, C->immutable_memory(), p1, phase->type(p1)->is_ptr(), TypeInt::INT, MemNode::unordered));
241         record_for_cleanup(chk_off, phase);
242 
243         int cacheoff_con = in_bytes(Klass::secondary_super_cache_offset());
244         bool might_be_cache = (phase->find_int_con(chk_off, cacheoff_con) == cacheoff_con);
245         if (!might_be_cache) {
246           Node* chk_off_X = chk_off;
247 #ifdef _LP64
248           chk_off_X = phase->transform(new ConvI2LNode(chk_off_X));
249 #endif
250           Node* p2 = phase->transform(new AddPNode(subklass, subklass, chk_off_X));
251           Node* nkls = phase->transform(LoadKlassNode::make(*phase, NULL, C->immutable_memory(), p2, phase->type(p2)->is_ptr(), TypeInstKlassPtr::OBJECT_OR_NULL));
252 
253           return verify_helper(phase, nkls, cached_t);
254         }
255         break;
256       }
257       case Compile::SSC_always_false:
258       case Compile::SSC_always_true:
259       default: {
260         break; // nothing to do
261       }
262     }
263   }
264 
265   return true;
266 }
267 #endif