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 but be careful because in dead parts of the graph,
119 // an InlineTypeNode can be casted to some completely unrelated type.
120 InlineTypeNode* vt = in(1)->isa_InlineType();
121 if (vt != nullptr && vt->is_allocated(phase) && _type->isa_instptr() != nullptr &&
122 vt->type()->inline_klass()->is_subtype_of(_type->is_instptr()->instance_klass())) {
123 Node* cast = clone();
124 cast->set_req(1, vt->get_oop());
125 vt = vt->clone()->as_InlineType();
126 if (!_type->maybe_null()) {
127 vt->as_InlineType()->set_null_marker(*phase);
128 }
129 vt->set_oop(*phase, phase->transform(cast));
130 return vt;
131 }
132
133 if (in(1) != nullptr && phase->type(in(1)) != Type::TOP) {
134 return TypeNode::Ideal(phase, can_reshape);
135 }
136
137 return nullptr;
138 }
139
140 uint ConstraintCastNode::hash() const {
141 return TypeNode::hash() + _dependency.hash() + (_extra_types != nullptr ? _extra_types->hash() : 0);
142 }
143
144 bool ConstraintCastNode::cmp(const Node &n) const {
145 if (!TypeNode::cmp(n)) {
146 return false;
147 }
148 ConstraintCastNode& cast = (ConstraintCastNode&) n;
149 if (!cast._dependency.cmp(_dependency)) {
150 return false;
151 }
152 if (_extra_types == nullptr || cast._extra_types == nullptr) {
153 return _extra_types == cast._extra_types;
154 }
155 return _extra_types->eq(cast._extra_types);
156 }
416 const TypeLong* t_in_l = t_in->is_long();
417 assert(t_in_l->contains(tl), "CastLL type should be narrower than or equal to the type of its input");
418 assert((tl != t_in_l) == t_in_l->strictly_contains(tl), "if type differs then this nodes's type must be narrower");
419 if (tl != t_in_l) {
420 const TypeInt* ti = TypeInt::make(checked_cast<jint>(tl->_lo), checked_cast<jint>(tl->_hi), tl->_widen);
421 Node* castii = phase->transform(new CastIINode(in(0), in1->in(1), ti));
422 Node* convi2l = in1->clone();
423 convi2l->set_req(1, castii);
424 return convi2l;
425 }
426 }
427 }
428 // If it's a cast created by PhaseIdealLoop::short_running_loop(), don't transform it until the counted loop is created
429 // in next loop opts pass
430 if (!can_reshape || !used_at_inner_loop_exit_test()) {
431 return optimize_integer_cast(phase, T_LONG);
432 }
433 return nullptr;
434 }
435
436 //=============================================================================
437 //------------------------------Identity---------------------------------------
438 // If input is already higher or equal to cast type, then this is an identity.
439 Node* CheckCastPPNode::Identity(PhaseGVN* phase) {
440 if (in(1)->is_InlineType() && _type->isa_instptr() && phase->type(in(1))->inline_klass()->is_subtype_of(_type->is_instptr()->instance_klass())) {
441 return in(1);
442 }
443 return ConstraintCastNode::Identity(phase);
444 }
445
446 // CastPPNodes are removed before matching, while alias classes are needed in global code motion.
447 // As a result, it is not valid for a CastPPNode to change the oop such that the derived pointers
448 // lie in different alias classes with and without the node. For example, a CastPPNode c may not
449 // cast an Object to a Bottom[], because later removal of c would affect the alias class of c's
450 // array length field (c + arrayOopDesc::length_offset_in_bytes()).
451 //
452 // This function verifies that a CastPPNode on an oop does not violate the aforementioned property.
453 //
454 // TODO 8382147: Currently, this verification only applies during the construction of a CastPPNode,
455 // we may want to apply the same verification during IGVN transformations, as well as final graph
456 // reshaping.
457 void CastPPNode::verify_type(const Type* in_type, const Type* out_type) {
458 #ifdef ASSERT
459 out_type = out_type->join(in_type);
460 if (in_type->empty() || out_type->empty()) {
461 return;
462 }
463 if (in_type == TypePtr::NULL_PTR || out_type == TypePtr::NULL_PTR) {
464 return;
465 }
479 assert(in_type->is_instptr()->instance_klass() == out_type->is_instptr()->instance_klass(), "must not cast to a different type");
480 #endif // ASSERT
481 }
482
483 //------------------------------Value------------------------------------------
484 // Take 'join' of input and cast-up type, unless working with an Interface
485 const Type* CheckCastPPNode::Value(PhaseGVN* phase) const {
486 if( in(0) && phase->type(in(0)) == Type::TOP ) return Type::TOP;
487
488 const Type *inn = phase->type(in(1));
489 if( inn == Type::TOP ) return Type::TOP; // No information yet
490
491 if (inn->isa_oopptr() && _type->isa_oopptr()) {
492 return ConstraintCastNode::Value(phase);
493 }
494
495 const TypePtr *in_type = inn->isa_ptr();
496 const TypePtr *my_type = _type->isa_ptr();
497 const Type *result = _type;
498 if (in_type != nullptr && my_type != nullptr) {
499 // TODO 8302672
500 if (!StressReflectiveCode && my_type->isa_aryptr() && in_type->isa_aryptr()) {
501 // Propagate array properties (not flat/null-free)
502 // Don't do this when StressReflectiveCode is enabled because it might lead to
503 // a dying data path while the corresponding flat/null-free check is not folded.
504 my_type = my_type->is_aryptr()->update_properties(in_type->is_aryptr());
505 if (my_type == nullptr) {
506 return Type::TOP; // Inconsistent properties
507 }
508 }
509 TypePtr::PTR in_ptr = in_type->ptr();
510 if (in_ptr == TypePtr::Null) {
511 result = in_type;
512 } else if (in_ptr != TypePtr::Constant) {
513 result = my_type->cast_to_ptr_type(my_type->join_ptr(in_ptr));
514 }
515 }
516
517 return result;
518 }
519
520 Node* CheckCastPPNode::pin_node_under_control_impl() const {
521 assert(_dependency.is_floating(), "already pinned");
522 return new CheckCastPPNode(in(0), in(1), bottom_type(), _dependency.with_pinned_dependency(), _extra_types);
523 }
524
525 //=============================================================================
526 //------------------------------Value------------------------------------------
527 const Type* CastX2PNode::Value(PhaseGVN* phase) const {
528 const Type* t = phase->type(in(1));
529 if (t == Type::TOP) return Type::TOP;
530 if (t->base() == Type_X && t->singleton()) {
531 uintptr_t bits = (uintptr_t) t->is_intptr_t()->get_con();
532 if (bits == 0) return TypePtr::NULL_PTR;
533 return TypeRawPtr::make((address) bits);
|