< prev index next >

src/hotspot/share/opto/castnode.cpp

Print this page

 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 }

195 bool ConstraintCastNode::higher_equal_types(PhaseGVN* phase, const Node* other) const {
196   const Type* t = phase->type(other);
197   if (!t->higher_equal_speculative(type())) {
198     return false;
199   }
200   if (_extra_types != nullptr) {
201     for (uint i = 0; i < _extra_types->cnt(); ++i) {
202       if (!t->higher_equal_speculative(_extra_types->field_at(i))) {
203         return false;
204       }
205     }
206   }
207   return true;
208 }
209 
210 Node* ConstraintCastNode::pin_node_under_control_impl() const {
211   assert(_dependency.is_floating(), "already pinned");
212   return make_cast_for_type(in(0), in(1), bottom_type(), _dependency.with_pinned_dependency(), _extra_types);
213 }
214 











































215 #ifndef PRODUCT
216 void ConstraintCastNode::dump_spec(outputStream *st) const {
217   TypeNode::dump_spec(st);
218   if (_extra_types != nullptr) {
219     st->print(" extra types: ");
220     _extra_types->dump_on(st);
221   }
222   st->print(" ");
223   _dependency.dump_on(st);
224 }
225 #endif
226 
227 CastIINode* CastIINode::make_with(Node* parent, const TypeInteger* type, const DependencyType& dependency) const {
228   return new CastIINode(in(0), parent, type, dependency, _range_check_dependency, _extra_types);
229 }
230 
231 CastLLNode* CastLLNode::make_with(Node* parent, const TypeInteger* type, const DependencyType& dependency) const {
232   return new CastLLNode(in(0), parent, type, dependency, _extra_types);
233 }
234 

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   if (in(1)->is_InlineType()) {
120     return ideal_cast_of_inline_type_node(phase);
121   }
122 
123   if (in(1) != nullptr && phase->type(in(1)) != Type::TOP) {
124     return TypeNode::Ideal(phase, can_reshape);
125   }
126 
127   return nullptr;
128 }
129 
130 uint ConstraintCastNode::hash() const {
131   return TypeNode::hash() + _dependency.hash() + (_extra_types != nullptr ? _extra_types->hash() : 0);
132 }
133 
134 bool ConstraintCastNode::cmp(const Node &n) const {
135   if (!TypeNode::cmp(n)) {
136     return false;
137   }
138   ConstraintCastNode& cast = (ConstraintCastNode&) n;
139   if (!cast._dependency.cmp(_dependency)) {
140     return false;
141   }
142   if (_extra_types == nullptr || cast._extra_types == nullptr) {
143     return _extra_types == cast._extra_types;
144   }
145   return _extra_types->eq(cast._extra_types);
146 }

205 bool ConstraintCastNode::higher_equal_types(PhaseGVN* phase, const Node* other) const {
206   const Type* t = phase->type(other);
207   if (!t->higher_equal_speculative(type())) {
208     return false;
209   }
210   if (_extra_types != nullptr) {
211     for (uint i = 0; i < _extra_types->cnt(); ++i) {
212       if (!t->higher_equal_speculative(_extra_types->field_at(i))) {
213         return false;
214       }
215     }
216   }
217   return true;
218 }
219 
220 Node* ConstraintCastNode::pin_node_under_control_impl() const {
221   assert(_dependency.is_floating(), "already pinned");
222   return make_cast_for_type(in(0), in(1), bottom_type(), _dependency.with_pinned_dependency(), _extra_types);
223 }
224 
225 Node* ConstraintCastNode::ideal_cast_of_inline_type_node(PhaseGVN* phase) {
226   InlineTypeNode* vt = in(1)->as_InlineType();
227   const Type* join = vt->type()->filter(type());
228   if (join == Type::TOP) {
229     // Do not push a dead Cast since its type can be unrelated
230     return nullptr;
231   }
232 
233   if (join == vt->type()->remove_speculative()) {
234     // Redundant cast, let Identity handle
235     return nullptr;
236   }
237 
238   if (join == TypePtr::NULL_PTR) {
239     // Will collapse to the constant null
240     return nullptr;
241   }
242 
243   // The only possible case left is that the cast is a cast to not-null
244   assert(join == vt->type()->filter(TypePtr::NOTNULL), "must be");
245   InlineTypeNode* res = vt->clone()->as_InlineType();
246   res->set_null_marker(*phase);
247 
248   // Push the cast to the oop input if possible
249   if (vt->is_allocated(phase)) {
250     Node* new_oop = clone();
251     new_oop->set_req(1, vt->get_oop());
252     res->set_oop(*phase, phase->transform(new_oop));
253   }
254 
255   // Push the cast to the null-free inputs of vt
256   for (uint i = 0; i < vt->field_count(); i++) {
257     if (vt->field(i)->is_null_free()) {
258       const ConstraintCastNode::DependencyType& dep = _dependency.is_floating() ? ConstraintCastNode::DependencyType::FloatingNarrowing
259                                                                                 : ConstraintCastNode::DependencyType::NonFloatingNarrowing;
260       Node* new_fv = new CastPPNode(in(0), vt->field_value(i), TypePtr::NOTNULL, dep);
261       res->set_field_value(i, phase->transform(new_fv));
262     }
263   }
264 
265   return res;
266 }
267 
268 #ifndef PRODUCT
269 void ConstraintCastNode::dump_spec(outputStream *st) const {
270   TypeNode::dump_spec(st);
271   if (_extra_types != nullptr) {
272     st->print(" extra types: ");
273     _extra_types->dump_on(st);
274   }
275   st->print(" ");
276   _dependency.dump_on(st);
277 }
278 #endif
279 
280 CastIINode* CastIINode::make_with(Node* parent, const TypeInteger* type, const DependencyType& dependency) const {
281   return new CastIINode(in(0), parent, type, dependency, _range_check_dependency, _extra_types);
282 }
283 
284 CastLLNode* CastLLNode::make_with(Node* parent, const TypeInteger* type, const DependencyType& dependency) const {
285   return new CastLLNode(in(0), parent, type, dependency, _extra_types);
286 }
287 

449       const TypeLong* t_in_l = t_in->is_long();
450       assert(t_in_l->contains(tl), "CastLL type should be narrower than or equal to the type of its input");
451       assert((tl != t_in_l) == t_in_l->strictly_contains(tl), "if type differs then this nodes's type must be narrower");
452       if (tl != t_in_l) {
453         const TypeInt* ti = TypeInt::make(checked_cast<jint>(tl->_lo), checked_cast<jint>(tl->_hi), tl->_widen);
454         Node* castii = phase->transform(new CastIINode(in(0), in1->in(1), ti));
455         Node* convi2l = in1->clone();
456         convi2l->set_req(1, castii);
457         return convi2l;
458       }
459     }
460   }
461   // If it's a cast created by PhaseIdealLoop::short_running_loop(), don't transform it until the counted loop is created
462   // in next loop opts pass
463   if (!can_reshape || !used_at_inner_loop_exit_test()) {
464     return optimize_integer_cast(phase, T_LONG);
465   }
466   return nullptr;
467 }
468 
469 //=============================================================================
470 //------------------------------Identity---------------------------------------
471 // If input is already higher or equal to cast type, then this is an identity.
472 Node* CheckCastPPNode::Identity(PhaseGVN* phase) {
473   if (in(1)->is_InlineType() && _type->isa_instptr() && phase->type(in(1))->inline_klass()->is_subtype_of(_type->is_instptr()->instance_klass())) {
474     return in(1);
475   }
476   return ConstraintCastNode::Identity(phase);
477 }
478 
479 // CastPPNodes are removed before matching, while alias classes are needed in global code motion.
480 // As a result, it is not valid for a CastPPNode to change the oop such that the derived pointers
481 // lie in different alias classes with and without the node. For example, a CastPPNode c may not
482 // cast an Object to a Bottom[], because later removal of c would affect the alias class of c's
483 // array length field (c + arrayOopDesc::length_offset_in_bytes()).
484 //
485 // This function verifies that a CastPPNode on an oop does not violate the aforementioned property.
486 //
487 // TODO 8382147: Currently, this verification only applies during the construction of a CastPPNode,
488 // we may want to apply the same verification during IGVN transformations, as well as final graph
489 // reshaping.
490 void CastPPNode::verify_type(const Type* in_type, const Type* out_type) {
491 #ifdef ASSERT
492   out_type = out_type->join(in_type);
493   if (in_type->empty() || out_type->empty()) {
494     return;
495   }
496   if (in_type == TypePtr::NULL_PTR || out_type == TypePtr::NULL_PTR) {
497     return;
498   }

512   assert(in_type->is_instptr()->instance_klass() == out_type->is_instptr()->instance_klass(), "must not cast to a different type");
513 #endif // ASSERT
514 }
515 
516 //------------------------------Value------------------------------------------
517 // Take 'join' of input and cast-up type, unless working with an Interface
518 const Type* CheckCastPPNode::Value(PhaseGVN* phase) const {
519   if( in(0) && phase->type(in(0)) == Type::TOP ) return Type::TOP;
520 
521   const Type *inn = phase->type(in(1));
522   if( inn == Type::TOP ) return Type::TOP;  // No information yet
523 
524   if (inn->isa_oopptr() && _type->isa_oopptr()) {
525     return ConstraintCastNode::Value(phase);
526   }
527 
528   const TypePtr *in_type = inn->isa_ptr();
529   const TypePtr *my_type = _type->isa_ptr();
530   const Type *result = _type;
531   if (in_type != nullptr && my_type != nullptr) {
532     // TODO 8302672
533     if (!StressReflectiveCode && my_type->isa_aryptr() && in_type->isa_aryptr()) {
534       // Propagate array properties (not flat/null-free)
535       // Don't do this when StressReflectiveCode is enabled because it might lead to
536       // a dying data path while the corresponding flat/null-free check is not folded.
537       my_type = my_type->is_aryptr()->update_properties(in_type->is_aryptr());
538       if (my_type == nullptr) {
539         return Type::TOP; // Inconsistent properties
540       }
541     }
542     TypePtr::PTR in_ptr = in_type->ptr();
543     if (in_ptr == TypePtr::Null) {
544       result = in_type;
545     } else if (in_ptr != TypePtr::Constant) {
546       result = my_type->cast_to_ptr_type(my_type->join_ptr(in_ptr));
547     }
548   }
549 
550   return result;
551 }
552 
553 Node* CheckCastPPNode::pin_node_under_control_impl() const {
554   assert(_dependency.is_floating(), "already pinned");
555   return new CheckCastPPNode(in(0), in(1), bottom_type(), _dependency.with_pinned_dependency(), _extra_types);
556 }
557 
558 //=============================================================================
559 //------------------------------Value------------------------------------------
560 const Type* CastX2PNode::Value(PhaseGVN* phase) const {
561   const Type* t = phase->type(in(1));
562   if (t == Type::TOP) return Type::TOP;
563   if (t->base() == Type_X && t->singleton()) {
564     uintptr_t bits = (uintptr_t) t->is_intptr_t()->get_con();
565     if (bits == 0)   return TypePtr::NULL_PTR;
566     return TypeRawPtr::make((address) bits);
< prev index next >