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 "opto/addnode.hpp"
26 #include "opto/callnode.hpp"
27 #include "opto/castnode.hpp"
28 #include "opto/cfgnode.hpp"
29 #include "opto/connode.hpp"
30 #include "opto/loopnode.hpp"
31 #include "opto/matcher.hpp"
32 #include "opto/phaseX.hpp"
33 #include "opto/subnode.hpp"
34 #include "opto/type.hpp"
35 #include "utilities/checkedCast.hpp"
36
37 const ConstraintCastNode::DependencyType ConstraintCastNode::DependencyType::FloatingNarrowing(true, true, "floating narrowing dependency"); // not pinned, narrows type
38 const ConstraintCastNode::DependencyType ConstraintCastNode::DependencyType::FloatingNonNarrowing(true, false, "floating non-narrowing dependency"); // not pinned, doesn't narrow type
39 const ConstraintCastNode::DependencyType ConstraintCastNode::DependencyType::NonFloatingNarrowing(false, true, "non-floating narrowing dependency"); // pinned, narrows type
40 const ConstraintCastNode::DependencyType ConstraintCastNode::DependencyType::NonFloatingNonNarrowing(false, false, "non-floating non-narrowing dependency"); // pinned, doesn't narrow type
41
42 //=============================================================================
43 // If input is already higher or equal to cast type, then this is an identity.
44 Node* ConstraintCastNode::Identity(PhaseGVN* phase) {
45 if (!_dependency.narrows_type()) {
46 // If this cast doesn't carry a type dependency (i.e. not used for type narrowing), we cannot optimize it.
47 return this;
48 }
49
50 // This cast node carries a type dependency. We can remove it if:
51 // - Its input has a narrower type
52 // - There's a dominating cast with same input but narrower type
90 if (rt->empty()) {
91 assert(ft == Type::TOP, "special case #2");
92 }
93 break;
94 }
95 case Op_CastPP:
96 if (in_type == TypePtr::NULL_PTR &&
97 _type->isa_ptr() && _type->is_ptr()->_ptr == TypePtr::NotNull) {
98 assert(ft == Type::TOP, "special case #3");
99 break;
100 }
101 }
102 #endif //ASSERT
103
104 return ft;
105 }
106
107 //------------------------------Ideal------------------------------------------
108 // Return a node which is more "ideal" than the current node. Strip out
109 // control copies
110 Node* ConstraintCastNode::Ideal(PhaseGVN* phase, bool can_reshape) {
111 if (in(0) != nullptr && remove_dead_region(phase, can_reshape)) {
112 return this;
113 }
114 if (in(1) != nullptr && phase->type(in(1)) != Type::TOP) {
115 return TypeNode::Ideal(phase, can_reshape);
116 }
117 return nullptr;
118 }
119
120 uint ConstraintCastNode::hash() const {
121 return TypeNode::hash() + _dependency.hash() + (_extra_types != nullptr ? _extra_types->hash() : 0);
122 }
123
124 bool ConstraintCastNode::cmp(const Node &n) const {
125 if (!TypeNode::cmp(n)) {
126 return false;
127 }
128 ConstraintCastNode& cast = (ConstraintCastNode&) n;
129 if (!cast._dependency.cmp(_dependency)) {
130 return false;
131 }
132 if (_extra_types == nullptr || cast._extra_types == nullptr) {
133 return _extra_types == cast._extra_types;
134 }
135 return _extra_types->eq(cast._extra_types);
136 }
396 const TypeLong* t_in_l = t_in->is_long();
397 assert(t_in_l->contains(tl), "CastLL type should be narrower than or equal to the type of its input");
398 assert((tl != t_in_l) == t_in_l->strictly_contains(tl), "if type differs then this nodes's type must be narrower");
399 if (tl != t_in_l) {
400 const TypeInt* ti = TypeInt::make(checked_cast<jint>(tl->_lo), checked_cast<jint>(tl->_hi), tl->_widen);
401 Node* castii = phase->transform(new CastIINode(in(0), in1->in(1), ti));
402 Node* convi2l = in1->clone();
403 convi2l->set_req(1, castii);
404 return convi2l;
405 }
406 }
407 }
408 // If it's a cast created by PhaseIdealLoop::short_running_loop(), don't transform it until the counted loop is created
409 // in next loop opts pass
410 if (!can_reshape || !used_at_inner_loop_exit_test()) {
411 return optimize_integer_cast(phase, T_LONG);
412 }
413 return nullptr;
414 }
415
416 // CastPPNodes are removed before matching, while alias classes are needed in global code motion.
417 // As a result, it is not valid for a CastPPNode to change the oop such that the derived pointers
418 // lie in different alias classes with and without the node. For example, a CastPPNode c may not
419 // cast an Object to a Bottom[], because later removal of c would affect the alias class of c's
420 // array length field (c + arrayOopDesc::length_offset_in_bytes()).
421 //
422 // This function verifies that a CastPPNode on an oop does not violate the aforementioned property.
423 //
424 // TODO 8382147: Currently, this verification only applies during the construction of a CastPPNode,
425 // we may want to apply the same verification during IGVN transformations, as well as final graph
426 // reshaping.
427 void CastPPNode::verify_type(const Type* in_type, const Type* out_type) {
428 #ifdef ASSERT
429 out_type = out_type->join(in_type);
430 if (in_type->empty() || out_type->empty()) {
431 return;
432 }
433 if (in_type == TypePtr::NULL_PTR || out_type == TypePtr::NULL_PTR) {
434 return;
435 }
449 assert(in_type->is_instptr()->instance_klass() == out_type->is_instptr()->instance_klass(), "must not cast to a different type");
450 #endif // ASSERT
451 }
452
453 //------------------------------Value------------------------------------------
454 // Take 'join' of input and cast-up type, unless working with an Interface
455 const Type* CheckCastPPNode::Value(PhaseGVN* phase) const {
456 if( in(0) && phase->type(in(0)) == Type::TOP ) return Type::TOP;
457
458 const Type *inn = phase->type(in(1));
459 if( inn == Type::TOP ) return Type::TOP; // No information yet
460
461 if (inn->isa_oopptr() && _type->isa_oopptr()) {
462 return ConstraintCastNode::Value(phase);
463 }
464
465 const TypePtr *in_type = inn->isa_ptr();
466 const TypePtr *my_type = _type->isa_ptr();
467 const Type *result = _type;
468 if (in_type != nullptr && my_type != nullptr) {
469 TypePtr::PTR in_ptr = in_type->ptr();
470 if (in_ptr == TypePtr::Null) {
471 result = in_type;
472 } else if (in_ptr != TypePtr::Constant) {
473 result = my_type->cast_to_ptr_type(my_type->join_ptr(in_ptr));
474 }
475 }
476
477 return result;
478 }
479
480 Node* CheckCastPPNode::pin_node_under_control_impl() const {
481 assert(_dependency.is_floating(), "already pinned");
482 return new CheckCastPPNode(in(0), in(1), bottom_type(), _dependency.with_pinned_dependency(), _extra_types);
483 }
484
485 //=============================================================================
486 //------------------------------Value------------------------------------------
487 const Type* CastX2PNode::Value(PhaseGVN* phase) const {
488 const Type* t = phase->type(in(1));
489 if (t == Type::TOP) return Type::TOP;
490 if (t->base() == Type_X && t->singleton()) {
491 uintptr_t bits = (uintptr_t) t->is_intptr_t()->get_con();
492 if (bits == 0) return TypePtr::NULL_PTR;
493 return TypeRawPtr::make((address) bits);
|
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 "opto/addnode.hpp"
26 #include "opto/callnode.hpp"
27 #include "opto/castnode.hpp"
28 #include "opto/cfgnode.hpp"
29 #include "opto/connode.hpp"
30 #include "opto/graphKit.hpp"
31 #include "opto/inlinetypenode.hpp"
32 #include "opto/loopnode.hpp"
33 #include "opto/matcher.hpp"
34 #include "opto/phaseX.hpp"
35 #include "opto/rootnode.hpp"
36 #include "opto/subnode.hpp"
37 #include "opto/type.hpp"
38 #include "utilities/checkedCast.hpp"
39
40 const ConstraintCastNode::DependencyType ConstraintCastNode::DependencyType::FloatingNarrowing(true, true, "floating narrowing dependency"); // not pinned, narrows type
41 const ConstraintCastNode::DependencyType ConstraintCastNode::DependencyType::FloatingNonNarrowing(true, false, "floating non-narrowing dependency"); // not pinned, doesn't narrow type
42 const ConstraintCastNode::DependencyType ConstraintCastNode::DependencyType::NonFloatingNarrowing(false, true, "non-floating narrowing dependency"); // pinned, narrows type
43 const ConstraintCastNode::DependencyType ConstraintCastNode::DependencyType::NonFloatingNonNarrowing(false, false, "non-floating non-narrowing dependency"); // pinned, doesn't narrow type
44
45 //=============================================================================
46 // If input is already higher or equal to cast type, then this is an identity.
47 Node* ConstraintCastNode::Identity(PhaseGVN* phase) {
48 if (!_dependency.narrows_type()) {
49 // If this cast doesn't carry a type dependency (i.e. not used for type narrowing), we cannot optimize it.
50 return this;
51 }
52
53 // This cast node carries a type dependency. We can remove it if:
54 // - Its input has a narrower type
55 // - There's a dominating cast with same input but narrower type
93 if (rt->empty()) {
94 assert(ft == Type::TOP, "special case #2");
95 }
96 break;
97 }
98 case Op_CastPP:
99 if (in_type == TypePtr::NULL_PTR &&
100 _type->isa_ptr() && _type->is_ptr()->_ptr == TypePtr::NotNull) {
101 assert(ft == Type::TOP, "special case #3");
102 break;
103 }
104 }
105 #endif //ASSERT
106
107 return ft;
108 }
109
110 //------------------------------Ideal------------------------------------------
111 // Return a node which is more "ideal" than the current node. Strip out
112 // control copies
113 Node *ConstraintCastNode::Ideal(PhaseGVN *phase, bool can_reshape) {
114 if (in(0) != nullptr && remove_dead_region(phase, can_reshape)) {
115 return this;
116 }
117
118 // Push cast through InlineTypeNode
119 InlineTypeNode* vt = in(1)->isa_InlineType();
120 if (vt != nullptr && phase->type(vt)->filter_speculative(_type) != Type::TOP) {
121 Node* cast = clone();
122 cast->set_req(1, vt->get_oop());
123 vt = vt->clone()->as_InlineType();
124 if (!_type->maybe_null()) {
125 vt->as_InlineType()->set_null_marker(*phase);
126 }
127 vt->set_oop(*phase, phase->transform(cast));
128 return vt;
129 }
130
131 if (in(1) != nullptr && phase->type(in(1)) != Type::TOP) {
132 return TypeNode::Ideal(phase, can_reshape);
133 }
134
135 return nullptr;
136 }
137
138 uint ConstraintCastNode::hash() const {
139 return TypeNode::hash() + _dependency.hash() + (_extra_types != nullptr ? _extra_types->hash() : 0);
140 }
141
142 bool ConstraintCastNode::cmp(const Node &n) const {
143 if (!TypeNode::cmp(n)) {
144 return false;
145 }
146 ConstraintCastNode& cast = (ConstraintCastNode&) n;
147 if (!cast._dependency.cmp(_dependency)) {
148 return false;
149 }
150 if (_extra_types == nullptr || cast._extra_types == nullptr) {
151 return _extra_types == cast._extra_types;
152 }
153 return _extra_types->eq(cast._extra_types);
154 }
414 const TypeLong* t_in_l = t_in->is_long();
415 assert(t_in_l->contains(tl), "CastLL type should be narrower than or equal to the type of its input");
416 assert((tl != t_in_l) == t_in_l->strictly_contains(tl), "if type differs then this nodes's type must be narrower");
417 if (tl != t_in_l) {
418 const TypeInt* ti = TypeInt::make(checked_cast<jint>(tl->_lo), checked_cast<jint>(tl->_hi), tl->_widen);
419 Node* castii = phase->transform(new CastIINode(in(0), in1->in(1), ti));
420 Node* convi2l = in1->clone();
421 convi2l->set_req(1, castii);
422 return convi2l;
423 }
424 }
425 }
426 // If it's a cast created by PhaseIdealLoop::short_running_loop(), don't transform it until the counted loop is created
427 // in next loop opts pass
428 if (!can_reshape || !used_at_inner_loop_exit_test()) {
429 return optimize_integer_cast(phase, T_LONG);
430 }
431 return nullptr;
432 }
433
434 //=============================================================================
435 //------------------------------Identity---------------------------------------
436 // If input is already higher or equal to cast type, then this is an identity.
437 Node* CheckCastPPNode::Identity(PhaseGVN* phase) {
438 if (in(1)->is_InlineType() && _type->isa_instptr() && phase->type(in(1))->inline_klass()->is_subtype_of(_type->is_instptr()->instance_klass())) {
439 return in(1);
440 }
441 return ConstraintCastNode::Identity(phase);
442 }
443
444 // CastPPNodes are removed before matching, while alias classes are needed in global code motion.
445 // As a result, it is not valid for a CastPPNode to change the oop such that the derived pointers
446 // lie in different alias classes with and without the node. For example, a CastPPNode c may not
447 // cast an Object to a Bottom[], because later removal of c would affect the alias class of c's
448 // array length field (c + arrayOopDesc::length_offset_in_bytes()).
449 //
450 // This function verifies that a CastPPNode on an oop does not violate the aforementioned property.
451 //
452 // TODO 8382147: Currently, this verification only applies during the construction of a CastPPNode,
453 // we may want to apply the same verification during IGVN transformations, as well as final graph
454 // reshaping.
455 void CastPPNode::verify_type(const Type* in_type, const Type* out_type) {
456 #ifdef ASSERT
457 out_type = out_type->join(in_type);
458 if (in_type->empty() || out_type->empty()) {
459 return;
460 }
461 if (in_type == TypePtr::NULL_PTR || out_type == TypePtr::NULL_PTR) {
462 return;
463 }
477 assert(in_type->is_instptr()->instance_klass() == out_type->is_instptr()->instance_klass(), "must not cast to a different type");
478 #endif // ASSERT
479 }
480
481 //------------------------------Value------------------------------------------
482 // Take 'join' of input and cast-up type, unless working with an Interface
483 const Type* CheckCastPPNode::Value(PhaseGVN* phase) const {
484 if( in(0) && phase->type(in(0)) == Type::TOP ) return Type::TOP;
485
486 const Type *inn = phase->type(in(1));
487 if( inn == Type::TOP ) return Type::TOP; // No information yet
488
489 if (inn->isa_oopptr() && _type->isa_oopptr()) {
490 return ConstraintCastNode::Value(phase);
491 }
492
493 const TypePtr *in_type = inn->isa_ptr();
494 const TypePtr *my_type = _type->isa_ptr();
495 const Type *result = _type;
496 if (in_type != nullptr && my_type != nullptr) {
497 // TODO 8302672
498 if (!StressReflectiveCode && my_type->isa_aryptr() && in_type->isa_aryptr()) {
499 // Propagate array properties (not flat/null-free)
500 // Don't do this when StressReflectiveCode is enabled because it might lead to
501 // a dying data path while the corresponding flat/null-free check is not folded.
502 my_type = my_type->is_aryptr()->update_properties(in_type->is_aryptr());
503 if (my_type == nullptr) {
504 return Type::TOP; // Inconsistent properties
505 }
506 }
507 TypePtr::PTR in_ptr = in_type->ptr();
508 if (in_ptr == TypePtr::Null) {
509 result = in_type;
510 } else if (in_ptr != TypePtr::Constant) {
511 result = my_type->cast_to_ptr_type(my_type->join_ptr(in_ptr));
512 }
513 }
514
515 return result;
516 }
517
518 Node* CheckCastPPNode::pin_node_under_control_impl() const {
519 assert(_dependency.is_floating(), "already pinned");
520 return new CheckCastPPNode(in(0), in(1), bottom_type(), _dependency.with_pinned_dependency(), _extra_types);
521 }
522
523 //=============================================================================
524 //------------------------------Value------------------------------------------
525 const Type* CastX2PNode::Value(PhaseGVN* phase) const {
526 const Type* t = phase->type(in(1));
527 if (t == Type::TOP) return Type::TOP;
528 if (t->base() == Type_X && t->singleton()) {
529 uintptr_t bits = (uintptr_t) t->is_intptr_t()->get_con();
530 if (bits == 0) return TypePtr::NULL_PTR;
531 return TypeRawPtr::make((address) bits);
|