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 
sub(const Type * sub_t,const Type * super_t) const35 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       TypePtr::PTR jp = sub_t->is_ptr()->join_ptr(super_t->is_ptr()->_ptr);
81       if (jp != TypePtr::Null && jp != TypePtr::BotPTR) {
82         return TypeInt::CC_GT;
83       }
84     }
85   }
86 
87   if (super_t->singleton()) {
88     if (subk != NULL) {
89       switch (Compile::current()->static_subtype_check(superk, subk)) {
90       case Compile::SSC_always_false:
91         return TypeInt::CC_GT;
92       case Compile::SSC_always_true:
93         return TypeInt::CC_EQ;
94       case Compile::SSC_easy_test:
95       case Compile::SSC_full_test:
96         break;
97       default:
98         ShouldNotReachHere();
99       }
100     }
101   }
102 
103   return bottom_type();
104 }
105 
Ideal(PhaseGVN * phase,bool can_reshape)106 Node *SubTypeCheckNode::Ideal(PhaseGVN *phase, bool can_reshape) {
107   Node* obj_or_subklass = in(ObjOrSubKlass);
108   Node* superklass = in(SuperKlass);
109 
110   if (obj_or_subklass == NULL ||
111       superklass == NULL) {
112     return NULL;
113   }
114 
115   const Type* sub_t = phase->type(obj_or_subklass);
116   const Type* super_t = phase->type(superklass);
117 
118   if (!super_t->isa_klassptr() ||
119       (!sub_t->isa_klassptr() && !sub_t->isa_oopptr())) {
120     return NULL;
121   }
122 
123   Node* addr = NULL;
124   if (obj_or_subklass->is_DecodeNKlass()) {
125     if (obj_or_subklass->in(1) != NULL &&
126         obj_or_subklass->in(1)->Opcode() == Op_LoadNKlass) {
127       addr = obj_or_subklass->in(1)->in(MemNode::Address);
128     }
129   } else if (obj_or_subklass->Opcode() == Op_LoadKlass) {
130     addr = obj_or_subklass->in(MemNode::Address);
131   }
132 
133   if (addr != NULL) {
134     intptr_t con = 0;
135     Node* obj = AddPNode::Ideal_base_and_offset(addr, phase, con);
136     if (con == oopDesc::klass_offset_in_bytes() && obj != NULL) {
137 #ifdef ASSERT
138       const Type* obj_t = phase->type(obj);
139       if (!obj_t->isa_oopptr() && obj_t != Type::TOP) {
140         obj->dump();
141         obj_t->dump(); tty->cr();
142         fatal("only for oop input");
143       }
144 #endif
145       set_req(ObjOrSubKlass, obj);
146       return this;
147     }
148   }
149 
150   // AllocateNode might have more accurate klass input
151   Node* allocated_klass = AllocateNode::Ideal_klass(obj_or_subklass, phase);
152   if (allocated_klass != NULL) {
153 #ifdef ASSERT
154       const Type* obj_or_subklass_t = phase->type(obj_or_subklass);
155       if (!obj_or_subklass_t->isa_oopptr() && obj_or_subklass_t != Type::TOP) {
156         obj_or_subklass->dump();
157         obj_or_subklass_t->dump(); tty->cr();
158         fatal("only for oop input");
159       }
160 #endif
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 #ifdef ASSERT
168   ciKlass* superk = super_t->is_klassptr()->klass();
169   ciKlass* subk   = sub_t->isa_klassptr() ? sub_t->is_klassptr()->klass() : sub_t->is_oopptr()->klass();
170 
171   if (super_t->singleton() && subk != NULL && phase->C->static_subtype_check(superk, subk) == Compile::SSC_easy_test) {
172     Node* subklass = NULL;
173     if (sub_t->isa_oopptr()) {
174       Node* adr = phase->transform(new AddPNode(obj_or_subklass, obj_or_subklass, phase->MakeConX(oopDesc::klass_offset_in_bytes())));
175       subklass = phase->transform(LoadKlassNode::make(*phase, NULL, phase->C->immutable_memory(), adr, TypeInstPtr::KLASS));
176     } else {
177       subklass = obj_or_subklass;
178     }
179     Node* res = new CmpPNode(subklass, superklass);
180     Node* cmp = phase->transform(res);
181     const Type* t = phase->type(cmp);
182     if (!((Value(phase) == t) || (t != TypeInt::CC_GT && t != TypeInt::CC_EQ))) {
183       Value(phase)->dump(); tty->cr();
184       t->dump(); tty->cr();
185       obj_or_subklass->dump();
186       subklass->dump();
187       superklass->dump();
188       cmp->dump();
189       tty->print_cr("==============================");
190       phase->C->root()->dump(9999);
191       fatal("missing Value() optimization");
192     }
193     if (phase->is_IterGVN()) {
194       phase->is_IterGVN()->_worklist.push(res);
195     }
196     return NULL;
197   }
198 
199   if (super_t->singleton() && subk != NULL && phase->C->static_subtype_check(superk, subk) == Compile::SSC_full_test) {
200     Node* subklass = NULL;
201     if (sub_t->isa_oopptr()) {
202       Node* adr = phase->transform(new AddPNode(obj_or_subklass, obj_or_subklass, phase->MakeConX(oopDesc::klass_offset_in_bytes())));
203       subklass = phase->transform(LoadKlassNode::make(*phase, NULL, phase->C->immutable_memory(), adr, TypeInstPtr::KLASS));
204     } else {
205       subklass = obj_or_subklass;
206     }
207 
208     Node *p1 = phase->transform(new AddPNode(superklass, superklass, phase->MakeConX(in_bytes(Klass::super_check_offset_offset()))));
209     Node* m = phase->C->immutable_memory();
210     Node *chk_off = phase->transform(new LoadINode(NULL, m, p1, phase->type(p1)->is_ptr(), TypeInt::INT, MemNode::unordered));
211     int cacheoff_con = in_bytes(Klass::secondary_super_cache_offset());
212     bool might_be_cache = (phase->find_int_con(chk_off, cacheoff_con) == cacheoff_con);
213 
214     if (might_be_cache) {
215       return NULL;
216     }
217 
218     Node *chk_off_X = chk_off;
219 #ifdef _LP64
220     chk_off_X = phase->transform(new ConvI2LNode(chk_off_X));
221 #endif
222     Node *p2 = phase->transform(new AddPNode(subklass,subklass,chk_off_X));
223     Node *kmem = phase->C->immutable_memory();
224     Node *nkls = phase->transform(LoadKlassNode::make(*phase, NULL, kmem, p2, phase->type(p2)->is_ptr(), TypeKlassPtr::OBJECT_OR_NULL));
225 
226     Node* res = new CmpPNode(superklass, nkls);
227     Node* cmp = phase->transform(res);
228     const Type* t = phase->type(cmp);
229     if (!((Value(phase) == t) || (t != TypeInt::CC_GT && t != TypeInt::CC_EQ))) {
230       Value(phase)->dump(); tty->cr();
231       t->dump(); tty->cr();
232       obj_or_subklass->dump();
233       subklass->dump();
234       superklass->dump();
235       nkls->dump();
236       cmp->dump();
237       tty->print_cr("==============================");
238       phase->C->root()->dump(9999);
239       fatal("missing Value() optimization");
240     }
241     if (phase->is_IterGVN()) {
242       phase->is_IterGVN()->_worklist.push(res);
243     }
244     return NULL;
245   }
246 #endif
247 
248   return NULL;
249 }
250