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 //------------------------------Value------------------------------------------
417 // Take 'join' of input and cast-up type, unless working with an Interface
418 const Type* CheckCastPPNode::Value(PhaseGVN* phase) const {
419 if( in(0) && phase->type(in(0)) == Type::TOP ) return Type::TOP;
420
421 const Type *inn = phase->type(in(1));
422 if( inn == Type::TOP ) return Type::TOP; // No information yet
423
424 if (inn->isa_oopptr() && _type->isa_oopptr()) {
425 return ConstraintCastNode::Value(phase);
426 }
427
428 const TypePtr *in_type = inn->isa_ptr();
429 const TypePtr *my_type = _type->isa_ptr();
430 const Type *result = _type;
431 if (in_type != nullptr && my_type != nullptr) {
432 TypePtr::PTR in_ptr = in_type->ptr();
433 if (in_ptr == TypePtr::Null) {
434 result = in_type;
435 } else if (in_ptr != TypePtr::Constant) {
436 result = my_type->cast_to_ptr_type(my_type->join_ptr(in_ptr));
437 }
438 }
439
440 return result;
441 }
442
443 //=============================================================================
444 //------------------------------Value------------------------------------------
445 const Type* CastX2PNode::Value(PhaseGVN* phase) const {
446 const Type* t = phase->type(in(1));
447 if (t == Type::TOP) return Type::TOP;
448 if (t->base() == Type_X && t->singleton()) {
449 uintptr_t bits = (uintptr_t) t->is_intptr_t()->get_con();
450 if (bits == 0) return TypePtr::NULL_PTR;
451 return TypeRawPtr::make((address) bits);
452 }
453 return CastX2PNode::bottom_type();
454 }
455
456 //------------------------------Idealize---------------------------------------
|
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 //------------------------------Value------------------------------------------
445 // Take 'join' of input and cast-up type, unless working with an Interface
446 const Type* CheckCastPPNode::Value(PhaseGVN* phase) const {
447 if( in(0) && phase->type(in(0)) == Type::TOP ) return Type::TOP;
448
449 const Type *inn = phase->type(in(1));
450 if( inn == Type::TOP ) return Type::TOP; // No information yet
451
452 if (inn->isa_oopptr() && _type->isa_oopptr()) {
453 return ConstraintCastNode::Value(phase);
454 }
455
456 const TypePtr *in_type = inn->isa_ptr();
457 const TypePtr *my_type = _type->isa_ptr();
458 const Type *result = _type;
459 if (in_type != nullptr && my_type != nullptr) {
460 // TODO 8302672
461 if (!StressReflectiveCode && my_type->isa_aryptr() && in_type->isa_aryptr()) {
462 // Propagate array properties (not flat/null-free)
463 // Don't do this when StressReflectiveCode is enabled because it might lead to
464 // a dying data path while the corresponding flat/null-free check is not folded.
465 my_type = my_type->is_aryptr()->update_properties(in_type->is_aryptr());
466 if (my_type == nullptr) {
467 return Type::TOP; // Inconsistent properties
468 }
469 }
470 TypePtr::PTR in_ptr = in_type->ptr();
471 if (in_ptr == TypePtr::Null) {
472 result = in_type;
473 } else if (in_ptr != TypePtr::Constant) {
474 result = my_type->cast_to_ptr_type(my_type->join_ptr(in_ptr));
475 }
476 }
477
478 return result;
479 }
480
481 //=============================================================================
482 //------------------------------Value------------------------------------------
483 const Type* CastX2PNode::Value(PhaseGVN* phase) const {
484 const Type* t = phase->type(in(1));
485 if (t == Type::TOP) return Type::TOP;
486 if (t->base() == Type_X && t->singleton()) {
487 uintptr_t bits = (uintptr_t) t->is_intptr_t()->get_con();
488 if (bits == 0) return TypePtr::NULL_PTR;
489 return TypeRawPtr::make((address) bits);
490 }
491 return CastX2PNode::bottom_type();
492 }
493
494 //------------------------------Idealize---------------------------------------
|