16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 *
24 */
25
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
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, false)) {
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(AddPNode::make_off_heap(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(AddPNode::make_off_heap(subklass, chk_off_X));
199 Node* nkls = phase->transform(LoadKlassNode::make(*phase, 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 }
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
|
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 *
24 */
25
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* super_klass_type = super_t->isa_klassptr();
37 assert(sub_t != Type::TOP && !TypePtr::NULL_PTR->higher_equal(sub_t), "should be not null");
38 const TypeKlassPtr* sub_klass_type = 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() && super_klass_type->isa_instklassptr() && super_klass_type->klass_is_exact()) {
42 ciKlass* superklass = super_klass_type->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
54 // Handle inline type arrays
55 //
56 // The super klass can be an exact non-array klass constant which is known to be not flat in array (e.g. Object)
57 // while the sub klass could very well be flat in array:
58 //
59 // MyValue <: Object:exact
60 // flat in array not flat in array
61 //
62 // We therefore first cast the super klass to inexact (if the class is not final itself) and recompute the flat in
63 // array property for the super klass (all done in cast_to_exactness()) in order to check whether the sub klass is
64 // flat in array and the super klass is not flat in array. If that's the case, the classes must be unrelated.
65 const TypeKlassPtr* super_klass_type_for_flat_in_array = super_klass_type;
66 if (super_klass_type->isa_instklassptr()) {
67 // Only relevant for TypeInstKlassPtr. TypeAryKlassPtr will always be not flat in array.
68 super_klass_type_for_flat_in_array = super_klass_type->cast_to_exactness(false);
69 }
70
71 if ((sub_klass_type->is_flat_in_array() && super_klass_type_for_flat_in_array->is_not_flat_in_array()) ||
72 (super_klass_type_for_flat_in_array->is_flat_in_array() && sub_klass_type->is_not_flat_in_array())) {
73 // The subtype is flat in array and the supertype is not in flat array or vice versa. Cannot subtype and thus unrelated.
74 unrelated_classes = true;
75 } else if (sub_klass_type->is_not_flat() && super_klass_type->is_flat()) {
76 // The subtype is a non-flat array and the supertype is a flat array. Must be unrelated.
77 unrelated_classes = true;
78 } else if (sub_klass_type->is_not_null_free() && super_klass_type->is_null_free()) {
79 // The subtype is a nullable array and the supertype is null-free array. Must be unrelated.
80 unrelated_classes = true;
81 }
82 if (unrelated_classes) {
83 TypePtr::PTR jp = sub_t->is_ptr()->join_ptr(super_t->is_ptr()->_ptr);
84 if (jp != TypePtr::Null && jp != TypePtr::BotPTR) {
85 return TypeInt::CC_GT;
86 }
87 }
88
89 switch (Compile::current()->static_subtype_check(super_klass_type, sub_klass_type, false)) {
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 return bottom_type();
102 }
103
104 Node *SubTypeCheckNode::Ideal(PhaseGVN* phase, bool can_reshape) {
105 Node* obj_or_subklass = in(ObjOrSubKlass);
106 Node* superklass = in(SuperKlass);
107
108 if (obj_or_subklass == nullptr ||
109 superklass == nullptr) {
110 return nullptr;
111 }
112
113 const Type* sub_t = phase->type(obj_or_subklass);
114 const Type* super_t = phase->type(superklass);
115
116 if (!super_t->isa_klassptr() ||
117 (!sub_t->isa_klassptr() && !sub_t->isa_oopptr())) {
118 return nullptr;
119 }
120
154 }
155
156 #ifdef ASSERT
157 bool SubTypeCheckNode::is_oop(PhaseGVN* phase, Node* n) {
158 const Type* t = phase->type(n);
159 if (!t->isa_oopptr() && t != Type::TOP) {
160 n->dump();
161 t->dump(); tty->cr();
162 return false;
163 }
164 return true;
165 }
166
167 static Node* record_for_cleanup(Node* n, PhaseGVN* phase) {
168 if (phase->is_IterGVN()) {
169 phase->is_IterGVN()->_worklist.push(n); // record for cleanup
170 }
171 return n;
172 }
173 bool SubTypeCheckNode::verify_helper(PhaseGVN* phase, Node* subklass, const Type* cached_t) {
174 Node* cmp_orig = new CmpPNode(subklass, in(SuperKlass));
175 Node* cmp = phase->transform(cmp_orig);
176 record_for_cleanup(cmp, phase);
177
178 const Type* cmp_t = phase->type(cmp);
179 const Type* t = Value(phase);
180
181 if (t == cmp_t ||
182 t != cached_t || // previous observations don't hold anymore
183 (cmp_t != TypeInt::CC_GT && cmp_t != TypeInt::CC_EQ)) {
184 return true;
185 } else {
186 t->dump(); tty->cr();
187 this->dump(2); tty->cr();
188 tty->print_cr("VS.\n");
189 cmp_t->dump(); tty->cr();
190 cmp_orig->dump(2); tty->cr();
191 tty->print_cr("==============================\n");
192 phase->C->root()->dump(9999);
193 return false;
194 }
195 }
196
197 // Verify that optimizing the subtype check to a simple code pattern when possible would not constant fold better.
198 bool SubTypeCheckNode::verify(PhaseGVN* phase) {
199 Compile* C = phase->C;
200 Node* obj_or_subklass = in(ObjOrSubKlass);
201 Node* superklass = in(SuperKlass);
202
203 const Type* sub_t = phase->type(obj_or_subklass);
204 const Type* super_t = phase->type(superklass);
205
206 const TypeKlassPtr* superk = super_t->isa_klassptr();
207 const TypeKlassPtr* subk = sub_t->isa_klassptr() ? sub_t->is_klassptr() : sub_t->is_oopptr()->as_klass_type();
208
209 if (super_t->singleton() && subk != nullptr) {
210 if (obj_or_subklass->bottom_type() == Type::TOP) {
211 // The bottom type of obj_or_subklass is TOP, despite its recorded type
212 // being an OOP or a klass pointer. This can happen for example in
213 // transient scenarios where obj_or_subklass is a projection of the TOP
214 // node. In such cases, skip verification to avoid violating the contract
215 // of LoadKlassNode::make(). This does not weaken the effect of verify(),
216 // as SubTypeCheck nodes with TOP obj_or_subklass inputs are dead anyway.
217 return true;
218 }
219 const Type* cached_t = Value(phase); // cache the type to validate consistency
220 switch (C->static_subtype_check(superk, subk, false)) {
221 case Compile::SSC_easy_test: {
222 return verify_helper(phase, load_klass(phase), cached_t);
223 }
224 case Compile::SSC_full_test: {
225 Node* p1 = phase->transform(AddPNode::make_off_heap(superklass, phase->MakeConX(in_bytes(Klass::super_check_offset_offset()))));
226 Node* chk_off = phase->transform(new LoadINode(nullptr, C->immutable_memory(), p1, phase->type(p1)->is_ptr(), TypeInt::INT, MemNode::unordered));
227 record_for_cleanup(chk_off, phase);
228
229 int cacheoff_con = in_bytes(Klass::secondary_super_cache_offset());
230 bool might_be_cache = phase->find_int_con(chk_off, cacheoff_con) == cacheoff_con;
231 if (!might_be_cache) {
232 Node* subklass = load_klass(phase);
233 Node* chk_off_X = chk_off;
234 #ifdef _LP64
235 chk_off_X = phase->transform(new ConvI2LNode(chk_off_X));
236 #endif
237 Node* p2 = phase->transform(AddPNode::make_off_heap(subklass, chk_off_X));
238 Node* nkls = phase->transform(LoadKlassNode::make(*phase, C->immutable_memory(), p2, phase->type(p2)->is_ptr(), TypeInstKlassPtr::OBJECT_OR_NULL));
239
240 return verify_helper(phase, nkls, cached_t);
241 }
242 break;
243 }
244 case Compile::SSC_always_false:
245 case Compile::SSC_always_true:
246 default: {
247 break; // nothing to do
248 }
249 }
250 }
266 return subklass;
267 }
268 #endif
269
270 uint SubTypeCheckNode::size_of() const {
271 return sizeof(*this);
272 }
273
274 uint SubTypeCheckNode::hash() const {
275 return NO_HASH;
276 }
277
278 #ifndef PRODUCT
279 void SubTypeCheckNode::dump_spec(outputStream* st) const {
280 if (_method != nullptr) {
281 st->print(" profiled at:");
282 _method->print_short_name(st);
283 st->print(":%d", _bci);
284 }
285 }
286 #endif
|