1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 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 "precompiled.hpp" 26 #include "opto/addnode.hpp" 27 #include "opto/castnode.hpp" 28 #include "opto/connode.hpp" 29 #include "opto/matcher.hpp" 30 #include "opto/phaseX.hpp" 31 #include "opto/subnode.hpp" 32 #include "opto/type.hpp" 33 34 //============================================================================= 35 // If input is already higher or equal to cast type, then this is an identity. 36 Node *ConstraintCastNode::Identity( PhaseTransform *phase ) { 37 return phase->type(in(1))->higher_equal_speculative(_type) ? in(1) : this; 38 } 39 40 //------------------------------Value------------------------------------------ 41 // Take 'join' of input and cast-up type 42 const Type *ConstraintCastNode::Value( PhaseTransform *phase ) const { 43 if( in(0) && phase->type(in(0)) == Type::TOP ) return Type::TOP; 44 const Type* ft = phase->type(in(1))->filter_speculative(_type); 45 46 #ifdef ASSERT 47 // Previous versions of this function had some special case logic, 48 // which is no longer necessary. Make sure of the required effects. 49 switch (Opcode()) { 50 case Op_CastII: 51 { 52 const Type* t1 = phase->type(in(1)); 53 if( t1 == Type::TOP ) assert(ft == Type::TOP, "special case #1"); 54 const Type* rt = t1->join_speculative(_type); 55 if (rt->empty()) assert(ft == Type::TOP, "special case #2"); 56 break; 57 } 58 case Op_CastPP: 59 if (phase->type(in(1)) == TypePtr::NULL_PTR && 60 _type->isa_ptr() && _type->is_ptr()->_ptr == TypePtr::NotNull) 61 assert(ft == Type::TOP, "special case #3"); 62 break; 63 } 64 #endif //ASSERT 65 66 return ft; 67 } 68 69 //------------------------------Ideal------------------------------------------ 70 // Return a node which is more "ideal" than the current node. Strip out 71 // control copies 72 Node *ConstraintCastNode::Ideal(PhaseGVN *phase, bool can_reshape){ 73 return (in(0) && remove_dead_region(phase, can_reshape)) ? this : NULL; 74 } 75 76 //------------------------------Ideal_DU_postCCP------------------------------- 77 // Throw away cast after constant propagation 78 Node *ConstraintCastNode::Ideal_DU_postCCP( PhaseCCP *ccp ) { 79 const Type *t = ccp->type(in(1)); 80 ccp->hash_delete(this); 81 set_type(t); // Turn into ID function 82 ccp->hash_insert(this); 83 return this; 84 } 85 86 uint CastIINode::size_of() const { 87 return sizeof(*this); 88 } 89 90 uint CastIINode::cmp(const Node &n) const { 91 return TypeNode::cmp(n) && ((CastIINode&)n)._carry_dependency == _carry_dependency; 92 } 93 94 Node *CastIINode::Identity(PhaseTransform *phase) { 95 if (_carry_dependency) { 96 return this; 97 } 98 return ConstraintCastNode::Identity(phase); 99 } 100 101 const Type *CastIINode::Value(PhaseTransform *phase) const { 102 const Type *res = ConstraintCastNode::Value(phase); 103 104 // Try to improve the type of the CastII if we recognize a CmpI/If 105 // pattern. 106 if (_carry_dependency) { 107 if (in(0) != NULL && (in(0)->is_IfFalse() || in(0)->is_IfTrue())) { 108 Node* proj = in(0); 109 if (proj->in(0)->in(1)->is_Bool()) { 110 Node* b = proj->in(0)->in(1); 111 if (b->in(1)->Opcode() == Op_CmpI) { 112 Node* cmp = b->in(1); 113 if (cmp->in(1) == in(1) && phase->type(cmp->in(2))->isa_int()) { 114 const TypeInt* in2_t = phase->type(cmp->in(2))->is_int(); 115 const Type* t = TypeInt::INT; 116 BoolTest test = b->as_Bool()->_test; 117 if (proj->is_IfFalse()) { 118 test = test.negate(); 119 } 120 BoolTest::mask m = test._test; 121 jlong lo_long = min_jint; 122 jlong hi_long = max_jint; 123 if (m == BoolTest::le || m == BoolTest::lt) { 124 hi_long = in2_t->_hi; 125 if (m == BoolTest::lt) { 126 hi_long -= 1; 127 } 128 } else if (m == BoolTest::ge || m == BoolTest::gt) { 129 lo_long = in2_t->_lo; 130 if (m == BoolTest::gt) { 131 lo_long += 1; 132 } 133 } else if (m == BoolTest::eq) { 134 lo_long = in2_t->_lo; 135 hi_long = in2_t->_hi; 136 } else if (m == BoolTest::ne) { 137 // can't do any better 138 } else { 139 stringStream ss; 140 test.dump_on(&ss); 141 fatal(err_msg_res("unexpected comparison %s", ss.as_string())); 142 } 143 int lo_int = (int)lo_long; 144 int hi_int = (int)hi_long; 145 146 if (lo_long != (jlong)lo_int) { 147 lo_int = min_jint; 148 } 149 if (hi_long != (jlong)hi_int) { 150 hi_int = max_jint; 151 } 152 153 t = TypeInt::make(lo_int, hi_int, Type::WidenMax); 154 155 res = res->filter_speculative(t); 156 157 return res; 158 } 159 } 160 } 161 } 162 } 163 return res; 164 } 165 166 Node *CastIINode::Ideal_DU_postCCP(PhaseCCP *ccp) { 167 if (_carry_dependency) { 168 return NULL; 169 } 170 return ConstraintCastNode::Ideal_DU_postCCP(ccp); 171 } 172 173 #ifndef PRODUCT 174 void CastIINode::dump_spec(outputStream *st) const { 175 TypeNode::dump_spec(st); 176 if (_carry_dependency) { 177 st->print(" carry dependency"); 178 } 179 } 180 #endif 181 182 //============================================================================= 183 184 //------------------------------Ideal_DU_postCCP------------------------------- 185 // If not converting int->oop, throw away cast after constant propagation 186 Node *CastPPNode::Ideal_DU_postCCP( PhaseCCP *ccp ) { 187 const Type *t = ccp->type(in(1)); 188 if (!t->isa_oop_ptr() || ((in(1)->is_DecodeN()) && Matcher::gen_narrow_oop_implicit_null_checks())) { 189 return NULL; // do not transform raw pointers or narrow oops 190 } 191 return ConstraintCastNode::Ideal_DU_postCCP(ccp); 192 } 193 194 195 196 //============================================================================= 197 //------------------------------Identity--------------------------------------- 198 // If input is already higher or equal to cast type, then this is an identity. 199 Node *CheckCastPPNode::Identity( PhaseTransform *phase ) { 200 // Toned down to rescue meeting at a Phi 3 different oops all implementing 201 // the same interface. CompileTheWorld starting at 502, kd12rc1.zip. 202 return (phase->type(in(1)) == phase->type(this)) ? in(1) : this; 203 } 204 205 //------------------------------Value------------------------------------------ 206 // Take 'join' of input and cast-up type, unless working with an Interface 207 const Type *CheckCastPPNode::Value( PhaseTransform *phase ) const { 208 if( in(0) && phase->type(in(0)) == Type::TOP ) return Type::TOP; 209 210 const Type *inn = phase->type(in(1)); 211 if( inn == Type::TOP ) return Type::TOP; // No information yet 212 213 const TypePtr *in_type = inn->isa_ptr(); 214 const TypePtr *my_type = _type->isa_ptr(); 215 const Type *result = _type; 216 if( in_type != NULL && my_type != NULL ) { 217 TypePtr::PTR in_ptr = in_type->ptr(); 218 if( in_ptr == TypePtr::Null ) { 219 result = in_type; 220 } else if( in_ptr == TypePtr::Constant ) { 221 // Casting a constant oop to an interface? 222 // (i.e., a String to a Comparable?) 223 // Then return the interface. 224 const TypeOopPtr *jptr = my_type->isa_oopptr(); 225 assert( jptr, "" ); 226 result = (jptr->klass()->is_interface() || !in_type->higher_equal(_type)) 227 ? my_type->cast_to_ptr_type( TypePtr::NotNull ) 228 : in_type; 229 } else { 230 result = my_type->cast_to_ptr_type( my_type->join_ptr(in_ptr) ); 231 } 232 } 233 234 // This is the code from TypePtr::xmeet() that prevents us from 235 // having 2 ways to represent the same type. We have to replicate it 236 // here because we don't go through meet/join. 237 if (result->remove_speculative() == result->speculative()) { 238 result = result->remove_speculative(); 239 } 240 241 // Same as above: because we don't go through meet/join, remove the 242 // speculative type if we know we won't use it. 243 return result->cleanup_speculative(); 244 245 // JOIN NOT DONE HERE BECAUSE OF INTERFACE ISSUES. 246 // FIX THIS (DO THE JOIN) WHEN UNION TYPES APPEAR! 247 248 // 249 // Remove this code after overnight run indicates no performance 250 // loss from not performing JOIN at CheckCastPPNode 251 // 252 // const TypeInstPtr *in_oop = in->isa_instptr(); 253 // const TypeInstPtr *my_oop = _type->isa_instptr(); 254 // // If either input is an 'interface', return destination type 255 // assert (in_oop == NULL || in_oop->klass() != NULL, ""); 256 // assert (my_oop == NULL || my_oop->klass() != NULL, ""); 257 // if( (in_oop && in_oop->klass()->is_interface()) 258 // ||(my_oop && my_oop->klass()->is_interface()) ) { 259 // TypePtr::PTR in_ptr = in->isa_ptr() ? in->is_ptr()->_ptr : TypePtr::BotPTR; 260 // // Preserve cast away nullness for interfaces 261 // if( in_ptr == TypePtr::NotNull && my_oop && my_oop->_ptr == TypePtr::BotPTR ) { 262 // return my_oop->cast_to_ptr_type(TypePtr::NotNull); 263 // } 264 // return _type; 265 // } 266 // 267 // // Neither the input nor the destination type is an interface, 268 // 269 // // history: JOIN used to cause weird corner case bugs 270 // // return (in == TypeOopPtr::NULL_PTR) ? in : _type; 271 // // JOIN picks up NotNull in common instance-of/check-cast idioms, both oops. 272 // // JOIN does not preserve NotNull in other cases, e.g. RawPtr vs InstPtr 273 // const Type *join = in->join(_type); 274 // // Check if join preserved NotNull'ness for pointers 275 // if( join->isa_ptr() && _type->isa_ptr() ) { 276 // TypePtr::PTR join_ptr = join->is_ptr()->_ptr; 277 // TypePtr::PTR type_ptr = _type->is_ptr()->_ptr; 278 // // If there isn't any NotNull'ness to preserve 279 // // OR if join preserved NotNull'ness then return it 280 // if( type_ptr == TypePtr::BotPTR || type_ptr == TypePtr::Null || 281 // join_ptr == TypePtr::NotNull || join_ptr == TypePtr::Constant ) { 282 // return join; 283 // } 284 // // ELSE return same old type as before 285 // return _type; 286 // } 287 // // Not joining two pointers 288 // return join; 289 } 290 291 //------------------------------Ideal------------------------------------------ 292 // Return a node which is more "ideal" than the current node. Strip out 293 // control copies 294 Node *CheckCastPPNode::Ideal(PhaseGVN *phase, bool can_reshape){ 295 return (in(0) && remove_dead_region(phase, can_reshape)) ? this : NULL; 296 } 297 298 //============================================================================= 299 //------------------------------Value------------------------------------------ 300 const Type *CastX2PNode::Value( PhaseTransform *phase ) const { 301 const Type* t = phase->type(in(1)); 302 if (t == Type::TOP) return Type::TOP; 303 if (t->base() == Type_X && t->singleton()) { 304 uintptr_t bits = (uintptr_t) t->is_intptr_t()->get_con(); 305 if (bits == 0) return TypePtr::NULL_PTR; 306 return TypeRawPtr::make((address) bits); 307 } 308 return CastX2PNode::bottom_type(); 309 } 310 311 //------------------------------Idealize--------------------------------------- 312 static inline bool fits_in_int(const Type* t, bool but_not_min_int = false) { 313 if (t == Type::TOP) return false; 314 const TypeX* tl = t->is_intptr_t(); 315 jint lo = min_jint; 316 jint hi = max_jint; 317 if (but_not_min_int) ++lo; // caller wants to negate the value w/o overflow 318 return (tl->_lo >= lo) && (tl->_hi <= hi); 319 } 320 321 static inline Node* addP_of_X2P(PhaseGVN *phase, 322 Node* base, 323 Node* dispX, 324 bool negate = false) { 325 if (negate) { 326 dispX = new SubXNode(phase->MakeConX(0), phase->transform(dispX)); 327 } 328 return new AddPNode(phase->C->top(), 329 phase->transform(new CastX2PNode(base)), 330 phase->transform(dispX)); 331 } 332 333 Node *CastX2PNode::Ideal(PhaseGVN *phase, bool can_reshape) { 334 // convert CastX2P(AddX(x, y)) to AddP(CastX2P(x), y) if y fits in an int 335 int op = in(1)->Opcode(); 336 Node* x; 337 Node* y; 338 switch (op) { 339 case Op_SubX: 340 x = in(1)->in(1); 341 // Avoid ideal transformations ping-pong between this and AddP for raw pointers. 342 if (phase->find_intptr_t_con(x, -1) == 0) 343 break; 344 y = in(1)->in(2); 345 if (fits_in_int(phase->type(y), true)) { 346 return addP_of_X2P(phase, x, y, true); 347 } 348 break; 349 case Op_AddX: 350 x = in(1)->in(1); 351 y = in(1)->in(2); 352 if (fits_in_int(phase->type(y))) { 353 return addP_of_X2P(phase, x, y); 354 } 355 if (fits_in_int(phase->type(x))) { 356 return addP_of_X2P(phase, y, x); 357 } 358 break; 359 } 360 return NULL; 361 } 362 363 //------------------------------Identity--------------------------------------- 364 Node *CastX2PNode::Identity( PhaseTransform *phase ) { 365 if (in(1)->Opcode() == Op_CastP2X) return in(1)->in(1); 366 return this; 367 } 368 369 //============================================================================= 370 //------------------------------Value------------------------------------------ 371 const Type *CastP2XNode::Value( PhaseTransform *phase ) const { 372 const Type* t = phase->type(in(1)); 373 if (t == Type::TOP) return Type::TOP; 374 if (t->base() == Type::RawPtr && t->singleton()) { 375 uintptr_t bits = (uintptr_t) t->is_rawptr()->get_con(); 376 return TypeX::make(bits); 377 } 378 return CastP2XNode::bottom_type(); 379 } 380 381 Node *CastP2XNode::Ideal(PhaseGVN *phase, bool can_reshape) { 382 return (in(0) && remove_dead_region(phase, can_reshape)) ? this : NULL; 383 } 384 385 //------------------------------Identity--------------------------------------- 386 Node *CastP2XNode::Identity( PhaseTransform *phase ) { 387 if (in(1)->Opcode() == Op_CastX2P) return in(1)->in(1); 388 return this; 389 }