src/share/vm/opto/mulnode.cpp

Fri, 11 Mar 2011 07:50:51 -0800

author
kvn
date
Fri, 11 Mar 2011 07:50:51 -0800
changeset 2636
83f08886981c
parent 2314
f95d63e2154a
child 3040
c7b60b601eb4
permissions
-rw-r--r--

7026631: field _klass is incorrectly set for dual type of TypeAryPtr::OOPS
Summary: add missing check this->dual() != TypeAryPtr::OOPS into TypeAryPtr::klass().
Reviewed-by: never

duke@435 1 /*
stefank@2314 2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "memory/allocation.inline.hpp"
stefank@2314 27 #include "opto/addnode.hpp"
stefank@2314 28 #include "opto/connode.hpp"
stefank@2314 29 #include "opto/memnode.hpp"
stefank@2314 30 #include "opto/mulnode.hpp"
stefank@2314 31 #include "opto/phaseX.hpp"
stefank@2314 32 #include "opto/subnode.hpp"
stefank@2314 33
duke@435 34 // Portions of code courtesy of Clifford Click
duke@435 35
duke@435 36
duke@435 37 //=============================================================================
duke@435 38 //------------------------------hash-------------------------------------------
duke@435 39 // Hash function over MulNodes. Needs to be commutative; i.e., I swap
duke@435 40 // (commute) inputs to MulNodes willy-nilly so the hash function must return
duke@435 41 // the same value in the presence of edge swapping.
duke@435 42 uint MulNode::hash() const {
duke@435 43 return (uintptr_t)in(1) + (uintptr_t)in(2) + Opcode();
duke@435 44 }
duke@435 45
duke@435 46 //------------------------------Identity---------------------------------------
duke@435 47 // Multiplying a one preserves the other argument
duke@435 48 Node *MulNode::Identity( PhaseTransform *phase ) {
duke@435 49 register const Type *one = mul_id(); // The multiplicative identity
duke@435 50 if( phase->type( in(1) )->higher_equal( one ) ) return in(2);
duke@435 51 if( phase->type( in(2) )->higher_equal( one ) ) return in(1);
duke@435 52
duke@435 53 return this;
duke@435 54 }
duke@435 55
duke@435 56 //------------------------------Ideal------------------------------------------
duke@435 57 // We also canonicalize the Node, moving constants to the right input,
duke@435 58 // and flatten expressions (so that 1+x+2 becomes x+3).
duke@435 59 Node *MulNode::Ideal(PhaseGVN *phase, bool can_reshape) {
duke@435 60 const Type *t1 = phase->type( in(1) );
duke@435 61 const Type *t2 = phase->type( in(2) );
duke@435 62 Node *progress = NULL; // Progress flag
duke@435 63 // We are OK if right is a constant, or right is a load and
duke@435 64 // left is a non-constant.
duke@435 65 if( !(t2->singleton() ||
duke@435 66 (in(2)->is_Load() && !(t1->singleton() || in(1)->is_Load())) ) ) {
duke@435 67 if( t1->singleton() || // Left input is a constant?
duke@435 68 // Otherwise, sort inputs (commutativity) to help value numbering.
duke@435 69 (in(1)->_idx > in(2)->_idx) ) {
duke@435 70 swap_edges(1, 2);
duke@435 71 const Type *t = t1;
duke@435 72 t1 = t2;
duke@435 73 t2 = t;
duke@435 74 progress = this; // Made progress
duke@435 75 }
duke@435 76 }
duke@435 77
duke@435 78 // If the right input is a constant, and the left input is a product of a
duke@435 79 // constant, flatten the expression tree.
duke@435 80 uint op = Opcode();
duke@435 81 if( t2->singleton() && // Right input is a constant?
duke@435 82 op != Op_MulF && // Float & double cannot reassociate
duke@435 83 op != Op_MulD ) {
duke@435 84 if( t2 == Type::TOP ) return NULL;
duke@435 85 Node *mul1 = in(1);
duke@435 86 #ifdef ASSERT
duke@435 87 // Check for dead loop
duke@435 88 int op1 = mul1->Opcode();
duke@435 89 if( phase->eqv( mul1, this ) || phase->eqv( in(2), this ) ||
duke@435 90 ( op1 == mul_opcode() || op1 == add_opcode() ) &&
duke@435 91 ( phase->eqv( mul1->in(1), this ) || phase->eqv( mul1->in(2), this ) ||
duke@435 92 phase->eqv( mul1->in(1), mul1 ) || phase->eqv( mul1->in(2), mul1 ) ) )
duke@435 93 assert(false, "dead loop in MulNode::Ideal");
duke@435 94 #endif
duke@435 95
duke@435 96 if( mul1->Opcode() == mul_opcode() ) { // Left input is a multiply?
duke@435 97 // Mul of a constant?
duke@435 98 const Type *t12 = phase->type( mul1->in(2) );
duke@435 99 if( t12->singleton() && t12 != Type::TOP) { // Left input is an add of a constant?
duke@435 100 // Compute new constant; check for overflow
duke@435 101 const Type *tcon01 = mul1->as_Mul()->mul_ring(t2,t12);
duke@435 102 if( tcon01->singleton() ) {
duke@435 103 // The Mul of the flattened expression
duke@435 104 set_req(1, mul1->in(1));
duke@435 105 set_req(2, phase->makecon( tcon01 ));
duke@435 106 t2 = tcon01;
duke@435 107 progress = this; // Made progress
duke@435 108 }
duke@435 109 }
duke@435 110 }
duke@435 111 // If the right input is a constant, and the left input is an add of a
duke@435 112 // constant, flatten the tree: (X+con1)*con0 ==> X*con0 + con1*con0
duke@435 113 const Node *add1 = in(1);
duke@435 114 if( add1->Opcode() == add_opcode() ) { // Left input is an add?
duke@435 115 // Add of a constant?
duke@435 116 const Type *t12 = phase->type( add1->in(2) );
duke@435 117 if( t12->singleton() && t12 != Type::TOP ) { // Left input is an add of a constant?
duke@435 118 assert( add1->in(1) != add1, "dead loop in MulNode::Ideal" );
duke@435 119 // Compute new constant; check for overflow
duke@435 120 const Type *tcon01 = mul_ring(t2,t12);
duke@435 121 if( tcon01->singleton() ) {
duke@435 122
duke@435 123 // Convert (X+con1)*con0 into X*con0
duke@435 124 Node *mul = clone(); // mul = ()*con0
duke@435 125 mul->set_req(1,add1->in(1)); // mul = X*con0
duke@435 126 mul = phase->transform(mul);
duke@435 127
duke@435 128 Node *add2 = add1->clone();
duke@435 129 add2->set_req(1, mul); // X*con0 + con0*con1
duke@435 130 add2->set_req(2, phase->makecon(tcon01) );
duke@435 131 progress = add2;
duke@435 132 }
duke@435 133 }
duke@435 134 } // End of is left input an add
duke@435 135 } // End of is right input a Mul
duke@435 136
duke@435 137 return progress;
duke@435 138 }
duke@435 139
duke@435 140 //------------------------------Value-----------------------------------------
duke@435 141 const Type *MulNode::Value( PhaseTransform *phase ) const {
duke@435 142 const Type *t1 = phase->type( in(1) );
duke@435 143 const Type *t2 = phase->type( in(2) );
duke@435 144 // Either input is TOP ==> the result is TOP
duke@435 145 if( t1 == Type::TOP ) return Type::TOP;
duke@435 146 if( t2 == Type::TOP ) return Type::TOP;
duke@435 147
duke@435 148 // Either input is ZERO ==> the result is ZERO.
duke@435 149 // Not valid for floats or doubles since +0.0 * -0.0 --> +0.0
duke@435 150 int op = Opcode();
duke@435 151 if( op == Op_MulI || op == Op_AndI || op == Op_MulL || op == Op_AndL ) {
duke@435 152 const Type *zero = add_id(); // The multiplicative zero
duke@435 153 if( t1->higher_equal( zero ) ) return zero;
duke@435 154 if( t2->higher_equal( zero ) ) return zero;
duke@435 155 }
duke@435 156
duke@435 157 // Either input is BOTTOM ==> the result is the local BOTTOM
duke@435 158 if( t1 == Type::BOTTOM || t2 == Type::BOTTOM )
duke@435 159 return bottom_type();
duke@435 160
rasbold@839 161 #if defined(IA32)
rasbold@839 162 // Can't trust native compilers to properly fold strict double
rasbold@839 163 // multiplication with round-to-zero on this platform.
rasbold@839 164 if (op == Op_MulD && phase->C->method()->is_strict()) {
rasbold@839 165 return TypeD::DOUBLE;
rasbold@839 166 }
rasbold@839 167 #endif
rasbold@839 168
duke@435 169 return mul_ring(t1,t2); // Local flavor of type multiplication
duke@435 170 }
duke@435 171
duke@435 172
duke@435 173 //=============================================================================
duke@435 174 //------------------------------Ideal------------------------------------------
duke@435 175 // Check for power-of-2 multiply, then try the regular MulNode::Ideal
duke@435 176 Node *MulINode::Ideal(PhaseGVN *phase, bool can_reshape) {
duke@435 177 // Swap constant to right
duke@435 178 jint con;
duke@435 179 if ((con = in(1)->find_int_con(0)) != 0) {
duke@435 180 swap_edges(1, 2);
duke@435 181 // Finish rest of method to use info in 'con'
duke@435 182 } else if ((con = in(2)->find_int_con(0)) == 0) {
duke@435 183 return MulNode::Ideal(phase, can_reshape);
duke@435 184 }
duke@435 185
duke@435 186 // Now we have a constant Node on the right and the constant in con
duke@435 187 if( con == 0 ) return NULL; // By zero is handled by Value call
duke@435 188 if( con == 1 ) return NULL; // By one is handled by Identity call
duke@435 189
duke@435 190 // Check for negative constant; if so negate the final result
duke@435 191 bool sign_flip = false;
duke@435 192 if( con < 0 ) {
duke@435 193 con = -con;
duke@435 194 sign_flip = true;
duke@435 195 }
duke@435 196
duke@435 197 // Get low bit; check for being the only bit
duke@435 198 Node *res = NULL;
duke@435 199 jint bit1 = con & -con; // Extract low bit
duke@435 200 if( bit1 == con ) { // Found a power of 2?
duke@435 201 res = new (phase->C, 3) LShiftINode( in(1), phase->intcon(log2_intptr(bit1)) );
duke@435 202 } else {
duke@435 203
duke@435 204 // Check for constant with 2 bits set
duke@435 205 jint bit2 = con-bit1;
duke@435 206 bit2 = bit2 & -bit2; // Extract 2nd bit
duke@435 207 if( bit2 + bit1 == con ) { // Found all bits in con?
duke@435 208 Node *n1 = phase->transform( new (phase->C, 3) LShiftINode( in(1), phase->intcon(log2_intptr(bit1)) ) );
duke@435 209 Node *n2 = phase->transform( new (phase->C, 3) LShiftINode( in(1), phase->intcon(log2_intptr(bit2)) ) );
duke@435 210 res = new (phase->C, 3) AddINode( n2, n1 );
duke@435 211
duke@435 212 } else if (is_power_of_2(con+1)) {
duke@435 213 // Sleezy: power-of-2 -1. Next time be generic.
duke@435 214 jint temp = (jint) (con + 1);
duke@435 215 Node *n1 = phase->transform( new (phase->C, 3) LShiftINode( in(1), phase->intcon(log2_intptr(temp)) ) );
duke@435 216 res = new (phase->C, 3) SubINode( n1, in(1) );
duke@435 217 } else {
duke@435 218 return MulNode::Ideal(phase, can_reshape);
duke@435 219 }
duke@435 220 }
duke@435 221
duke@435 222 if( sign_flip ) { // Need to negate result?
duke@435 223 res = phase->transform(res);// Transform, before making the zero con
duke@435 224 res = new (phase->C, 3) SubINode(phase->intcon(0),res);
duke@435 225 }
duke@435 226
duke@435 227 return res; // Return final result
duke@435 228 }
duke@435 229
duke@435 230 //------------------------------mul_ring---------------------------------------
duke@435 231 // Compute the product type of two integer ranges into this node.
duke@435 232 const Type *MulINode::mul_ring(const Type *t0, const Type *t1) const {
duke@435 233 const TypeInt *r0 = t0->is_int(); // Handy access
duke@435 234 const TypeInt *r1 = t1->is_int();
duke@435 235
duke@435 236 // Fetch endpoints of all ranges
duke@435 237 int32 lo0 = r0->_lo;
duke@435 238 double a = (double)lo0;
duke@435 239 int32 hi0 = r0->_hi;
duke@435 240 double b = (double)hi0;
duke@435 241 int32 lo1 = r1->_lo;
duke@435 242 double c = (double)lo1;
duke@435 243 int32 hi1 = r1->_hi;
duke@435 244 double d = (double)hi1;
duke@435 245
duke@435 246 // Compute all endpoints & check for overflow
duke@435 247 int32 A = lo0*lo1;
duke@435 248 if( (double)A != a*c ) return TypeInt::INT; // Overflow?
duke@435 249 int32 B = lo0*hi1;
duke@435 250 if( (double)B != a*d ) return TypeInt::INT; // Overflow?
duke@435 251 int32 C = hi0*lo1;
duke@435 252 if( (double)C != b*c ) return TypeInt::INT; // Overflow?
duke@435 253 int32 D = hi0*hi1;
duke@435 254 if( (double)D != b*d ) return TypeInt::INT; // Overflow?
duke@435 255
duke@435 256 if( A < B ) { lo0 = A; hi0 = B; } // Sort range endpoints
duke@435 257 else { lo0 = B; hi0 = A; }
duke@435 258 if( C < D ) {
duke@435 259 if( C < lo0 ) lo0 = C;
duke@435 260 if( D > hi0 ) hi0 = D;
duke@435 261 } else {
duke@435 262 if( D < lo0 ) lo0 = D;
duke@435 263 if( C > hi0 ) hi0 = C;
duke@435 264 }
duke@435 265 return TypeInt::make(lo0, hi0, MAX2(r0->_widen,r1->_widen));
duke@435 266 }
duke@435 267
duke@435 268
duke@435 269 //=============================================================================
duke@435 270 //------------------------------Ideal------------------------------------------
duke@435 271 // Check for power-of-2 multiply, then try the regular MulNode::Ideal
duke@435 272 Node *MulLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
duke@435 273 // Swap constant to right
duke@435 274 jlong con;
duke@435 275 if ((con = in(1)->find_long_con(0)) != 0) {
duke@435 276 swap_edges(1, 2);
duke@435 277 // Finish rest of method to use info in 'con'
duke@435 278 } else if ((con = in(2)->find_long_con(0)) == 0) {
duke@435 279 return MulNode::Ideal(phase, can_reshape);
duke@435 280 }
duke@435 281
duke@435 282 // Now we have a constant Node on the right and the constant in con
duke@435 283 if( con == CONST64(0) ) return NULL; // By zero is handled by Value call
duke@435 284 if( con == CONST64(1) ) return NULL; // By one is handled by Identity call
duke@435 285
duke@435 286 // Check for negative constant; if so negate the final result
duke@435 287 bool sign_flip = false;
duke@435 288 if( con < 0 ) {
duke@435 289 con = -con;
duke@435 290 sign_flip = true;
duke@435 291 }
duke@435 292
duke@435 293 // Get low bit; check for being the only bit
duke@435 294 Node *res = NULL;
duke@435 295 jlong bit1 = con & -con; // Extract low bit
duke@435 296 if( bit1 == con ) { // Found a power of 2?
duke@435 297 res = new (phase->C, 3) LShiftLNode( in(1), phase->intcon(log2_long(bit1)) );
duke@435 298 } else {
duke@435 299
duke@435 300 // Check for constant with 2 bits set
duke@435 301 jlong bit2 = con-bit1;
duke@435 302 bit2 = bit2 & -bit2; // Extract 2nd bit
duke@435 303 if( bit2 + bit1 == con ) { // Found all bits in con?
duke@435 304 Node *n1 = phase->transform( new (phase->C, 3) LShiftLNode( in(1), phase->intcon(log2_long(bit1)) ) );
duke@435 305 Node *n2 = phase->transform( new (phase->C, 3) LShiftLNode( in(1), phase->intcon(log2_long(bit2)) ) );
duke@435 306 res = new (phase->C, 3) AddLNode( n2, n1 );
duke@435 307
duke@435 308 } else if (is_power_of_2_long(con+1)) {
duke@435 309 // Sleezy: power-of-2 -1. Next time be generic.
duke@435 310 jlong temp = (jlong) (con + 1);
duke@435 311 Node *n1 = phase->transform( new (phase->C, 3) LShiftLNode( in(1), phase->intcon(log2_long(temp)) ) );
duke@435 312 res = new (phase->C, 3) SubLNode( n1, in(1) );
duke@435 313 } else {
duke@435 314 return MulNode::Ideal(phase, can_reshape);
duke@435 315 }
duke@435 316 }
duke@435 317
duke@435 318 if( sign_flip ) { // Need to negate result?
duke@435 319 res = phase->transform(res);// Transform, before making the zero con
duke@435 320 res = new (phase->C, 3) SubLNode(phase->longcon(0),res);
duke@435 321 }
duke@435 322
duke@435 323 return res; // Return final result
duke@435 324 }
duke@435 325
duke@435 326 //------------------------------mul_ring---------------------------------------
duke@435 327 // Compute the product type of two integer ranges into this node.
duke@435 328 const Type *MulLNode::mul_ring(const Type *t0, const Type *t1) const {
duke@435 329 const TypeLong *r0 = t0->is_long(); // Handy access
duke@435 330 const TypeLong *r1 = t1->is_long();
duke@435 331
duke@435 332 // Fetch endpoints of all ranges
duke@435 333 jlong lo0 = r0->_lo;
duke@435 334 double a = (double)lo0;
duke@435 335 jlong hi0 = r0->_hi;
duke@435 336 double b = (double)hi0;
duke@435 337 jlong lo1 = r1->_lo;
duke@435 338 double c = (double)lo1;
duke@435 339 jlong hi1 = r1->_hi;
duke@435 340 double d = (double)hi1;
duke@435 341
duke@435 342 // Compute all endpoints & check for overflow
duke@435 343 jlong A = lo0*lo1;
duke@435 344 if( (double)A != a*c ) return TypeLong::LONG; // Overflow?
duke@435 345 jlong B = lo0*hi1;
duke@435 346 if( (double)B != a*d ) return TypeLong::LONG; // Overflow?
duke@435 347 jlong C = hi0*lo1;
duke@435 348 if( (double)C != b*c ) return TypeLong::LONG; // Overflow?
duke@435 349 jlong D = hi0*hi1;
duke@435 350 if( (double)D != b*d ) return TypeLong::LONG; // Overflow?
duke@435 351
duke@435 352 if( A < B ) { lo0 = A; hi0 = B; } // Sort range endpoints
duke@435 353 else { lo0 = B; hi0 = A; }
duke@435 354 if( C < D ) {
duke@435 355 if( C < lo0 ) lo0 = C;
duke@435 356 if( D > hi0 ) hi0 = D;
duke@435 357 } else {
duke@435 358 if( D < lo0 ) lo0 = D;
duke@435 359 if( C > hi0 ) hi0 = C;
duke@435 360 }
duke@435 361 return TypeLong::make(lo0, hi0, MAX2(r0->_widen,r1->_widen));
duke@435 362 }
duke@435 363
duke@435 364 //=============================================================================
duke@435 365 //------------------------------mul_ring---------------------------------------
duke@435 366 // Compute the product type of two double ranges into this node.
duke@435 367 const Type *MulFNode::mul_ring(const Type *t0, const Type *t1) const {
duke@435 368 if( t0 == Type::FLOAT || t1 == Type::FLOAT ) return Type::FLOAT;
duke@435 369 return TypeF::make( t0->getf() * t1->getf() );
duke@435 370 }
duke@435 371
duke@435 372 //=============================================================================
duke@435 373 //------------------------------mul_ring---------------------------------------
duke@435 374 // Compute the product type of two double ranges into this node.
duke@435 375 const Type *MulDNode::mul_ring(const Type *t0, const Type *t1) const {
duke@435 376 if( t0 == Type::DOUBLE || t1 == Type::DOUBLE ) return Type::DOUBLE;
rasbold@839 377 // We must be multiplying 2 double constants.
duke@435 378 return TypeD::make( t0->getd() * t1->getd() );
duke@435 379 }
duke@435 380
duke@435 381 //=============================================================================
rasbold@580 382 //------------------------------Value------------------------------------------
rasbold@580 383 const Type *MulHiLNode::Value( PhaseTransform *phase ) const {
rasbold@580 384 // Either input is TOP ==> the result is TOP
rasbold@580 385 const Type *t1 = phase->type( in(1) );
rasbold@580 386 const Type *t2 = phase->type( in(2) );
rasbold@580 387 if( t1 == Type::TOP ) return Type::TOP;
rasbold@580 388 if( t2 == Type::TOP ) return Type::TOP;
rasbold@580 389
rasbold@580 390 // Either input is BOTTOM ==> the result is the local BOTTOM
rasbold@580 391 const Type *bot = bottom_type();
rasbold@580 392 if( (t1 == bot) || (t2 == bot) ||
rasbold@580 393 (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
rasbold@580 394 return bot;
rasbold@580 395
rasbold@580 396 // It is not worth trying to constant fold this stuff!
rasbold@580 397 return TypeLong::LONG;
rasbold@580 398 }
rasbold@580 399
rasbold@580 400 //=============================================================================
duke@435 401 //------------------------------mul_ring---------------------------------------
duke@435 402 // Supplied function returns the product of the inputs IN THE CURRENT RING.
duke@435 403 // For the logical operations the ring's MUL is really a logical AND function.
duke@435 404 // This also type-checks the inputs for sanity. Guaranteed never to
duke@435 405 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
duke@435 406 const Type *AndINode::mul_ring( const Type *t0, const Type *t1 ) const {
duke@435 407 const TypeInt *r0 = t0->is_int(); // Handy access
duke@435 408 const TypeInt *r1 = t1->is_int();
duke@435 409 int widen = MAX2(r0->_widen,r1->_widen);
duke@435 410
duke@435 411 // If either input is a constant, might be able to trim cases
duke@435 412 if( !r0->is_con() && !r1->is_con() )
duke@435 413 return TypeInt::INT; // No constants to be had
duke@435 414
duke@435 415 // Both constants? Return bits
duke@435 416 if( r0->is_con() && r1->is_con() )
duke@435 417 return TypeInt::make( r0->get_con() & r1->get_con() );
duke@435 418
duke@435 419 if( r0->is_con() && r0->get_con() > 0 )
duke@435 420 return TypeInt::make(0, r0->get_con(), widen);
duke@435 421
duke@435 422 if( r1->is_con() && r1->get_con() > 0 )
duke@435 423 return TypeInt::make(0, r1->get_con(), widen);
duke@435 424
duke@435 425 if( r0 == TypeInt::BOOL || r1 == TypeInt::BOOL ) {
duke@435 426 return TypeInt::BOOL;
duke@435 427 }
duke@435 428
duke@435 429 return TypeInt::INT; // No constants to be had
duke@435 430 }
duke@435 431
duke@435 432 //------------------------------Identity---------------------------------------
duke@435 433 // Masking off the high bits of an unsigned load is not required
duke@435 434 Node *AndINode::Identity( PhaseTransform *phase ) {
duke@435 435
duke@435 436 // x & x => x
duke@435 437 if (phase->eqv(in(1), in(2))) return in(1);
duke@435 438
twisti@1259 439 Node* in1 = in(1);
twisti@1259 440 uint op = in1->Opcode();
twisti@1259 441 const TypeInt* t2 = phase->type(in(2))->isa_int();
twisti@1259 442 if (t2 && t2->is_con()) {
duke@435 443 int con = t2->get_con();
duke@435 444 // Masking off high bits which are always zero is useless.
duke@435 445 const TypeInt* t1 = phase->type( in(1) )->isa_int();
duke@435 446 if (t1 != NULL && t1->_lo >= 0) {
twisti@1259 447 jint t1_support = right_n_bits(1 + log2_intptr(t1->_hi));
duke@435 448 if ((t1_support & con) == t1_support)
twisti@1259 449 return in1;
duke@435 450 }
duke@435 451 // Masking off the high bits of a unsigned-shift-right is not
duke@435 452 // needed either.
twisti@1259 453 if (op == Op_URShiftI) {
twisti@1259 454 const TypeInt* t12 = phase->type(in1->in(2))->isa_int();
twisti@1259 455 if (t12 && t12->is_con()) { // Shift is by a constant
twisti@994 456 int shift = t12->get_con();
twisti@994 457 shift &= BitsPerJavaInteger - 1; // semantics of Java shifts
twisti@994 458 int mask = max_juint >> shift;
twisti@1259 459 if ((mask & con) == mask) // If AND is useless, skip it
twisti@1259 460 return in1;
duke@435 461 }
duke@435 462 }
duke@435 463 }
duke@435 464 return MulNode::Identity(phase);
duke@435 465 }
duke@435 466
duke@435 467 //------------------------------Ideal------------------------------------------
duke@435 468 Node *AndINode::Ideal(PhaseGVN *phase, bool can_reshape) {
duke@435 469 // Special case constant AND mask
duke@435 470 const TypeInt *t2 = phase->type( in(2) )->isa_int();
duke@435 471 if( !t2 || !t2->is_con() ) return MulNode::Ideal(phase, can_reshape);
duke@435 472 const int mask = t2->get_con();
duke@435 473 Node *load = in(1);
duke@435 474 uint lop = load->Opcode();
duke@435 475
duke@435 476 // Masking bits off of a Character? Hi bits are already zero.
twisti@993 477 if( lop == Op_LoadUS &&
duke@435 478 (mask & 0xFFFF0000) ) // Can we make a smaller mask?
duke@435 479 return new (phase->C, 3) AndINode(load,phase->intcon(mask&0xFFFF));
duke@435 480
duke@435 481 // Masking bits off of a Short? Loading a Character does some masking
twisti@1259 482 if (lop == Op_LoadS && (mask & 0xFFFF0000) == 0 ) {
twisti@993 483 Node *ldus = new (phase->C, 3) LoadUSNode(load->in(MemNode::Control),
twisti@1259 484 load->in(MemNode::Memory),
twisti@1259 485 load->in(MemNode::Address),
twisti@1259 486 load->adr_type());
twisti@993 487 ldus = phase->transform(ldus);
twisti@1259 488 return new (phase->C, 3) AndINode(ldus, phase->intcon(mask & 0xFFFF));
duke@435 489 }
duke@435 490
twisti@1259 491 // Masking sign bits off of a Byte? Do an unsigned byte load plus
twisti@1259 492 // an and.
twisti@1059 493 if (lop == Op_LoadB && (mask & 0xFFFFFF00) == 0) {
twisti@1059 494 Node* ldub = new (phase->C, 3) LoadUBNode(load->in(MemNode::Control),
twisti@1059 495 load->in(MemNode::Memory),
twisti@1059 496 load->in(MemNode::Address),
twisti@1059 497 load->adr_type());
twisti@1059 498 ldub = phase->transform(ldub);
twisti@1059 499 return new (phase->C, 3) AndINode(ldub, phase->intcon(mask));
duke@435 500 }
duke@435 501
duke@435 502 // Masking off sign bits? Dont make them!
duke@435 503 if( lop == Op_RShiftI ) {
duke@435 504 const TypeInt *t12 = phase->type(load->in(2))->isa_int();
duke@435 505 if( t12 && t12->is_con() ) { // Shift is by a constant
duke@435 506 int shift = t12->get_con();
duke@435 507 shift &= BitsPerJavaInteger-1; // semantics of Java shifts
duke@435 508 const int sign_bits_mask = ~right_n_bits(BitsPerJavaInteger - shift);
duke@435 509 // If the AND'ing of the 2 masks has no bits, then only original shifted
duke@435 510 // bits survive. NO sign-extension bits survive the maskings.
duke@435 511 if( (sign_bits_mask & mask) == 0 ) {
duke@435 512 // Use zero-fill shift instead
duke@435 513 Node *zshift = phase->transform(new (phase->C, 3) URShiftINode(load->in(1),load->in(2)));
duke@435 514 return new (phase->C, 3) AndINode( zshift, in(2) );
duke@435 515 }
duke@435 516 }
duke@435 517 }
duke@435 518
duke@435 519 // Check for 'negate/and-1', a pattern emitted when someone asks for
duke@435 520 // 'mod 2'. Negate leaves the low order bit unchanged (think: complement
duke@435 521 // plus 1) and the mask is of the low order bit. Skip the negate.
duke@435 522 if( lop == Op_SubI && mask == 1 && load->in(1) &&
duke@435 523 phase->type(load->in(1)) == TypeInt::ZERO )
duke@435 524 return new (phase->C, 3) AndINode( load->in(2), in(2) );
duke@435 525
duke@435 526 return MulNode::Ideal(phase, can_reshape);
duke@435 527 }
duke@435 528
duke@435 529 //=============================================================================
duke@435 530 //------------------------------mul_ring---------------------------------------
duke@435 531 // Supplied function returns the product of the inputs IN THE CURRENT RING.
duke@435 532 // For the logical operations the ring's MUL is really a logical AND function.
duke@435 533 // This also type-checks the inputs for sanity. Guaranteed never to
duke@435 534 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
duke@435 535 const Type *AndLNode::mul_ring( const Type *t0, const Type *t1 ) const {
duke@435 536 const TypeLong *r0 = t0->is_long(); // Handy access
duke@435 537 const TypeLong *r1 = t1->is_long();
duke@435 538 int widen = MAX2(r0->_widen,r1->_widen);
duke@435 539
duke@435 540 // If either input is a constant, might be able to trim cases
duke@435 541 if( !r0->is_con() && !r1->is_con() )
duke@435 542 return TypeLong::LONG; // No constants to be had
duke@435 543
duke@435 544 // Both constants? Return bits
duke@435 545 if( r0->is_con() && r1->is_con() )
duke@435 546 return TypeLong::make( r0->get_con() & r1->get_con() );
duke@435 547
duke@435 548 if( r0->is_con() && r0->get_con() > 0 )
duke@435 549 return TypeLong::make(CONST64(0), r0->get_con(), widen);
duke@435 550
duke@435 551 if( r1->is_con() && r1->get_con() > 0 )
duke@435 552 return TypeLong::make(CONST64(0), r1->get_con(), widen);
duke@435 553
duke@435 554 return TypeLong::LONG; // No constants to be had
duke@435 555 }
duke@435 556
duke@435 557 //------------------------------Identity---------------------------------------
duke@435 558 // Masking off the high bits of an unsigned load is not required
duke@435 559 Node *AndLNode::Identity( PhaseTransform *phase ) {
duke@435 560
duke@435 561 // x & x => x
duke@435 562 if (phase->eqv(in(1), in(2))) return in(1);
duke@435 563
duke@435 564 Node *usr = in(1);
duke@435 565 const TypeLong *t2 = phase->type( in(2) )->isa_long();
duke@435 566 if( t2 && t2->is_con() ) {
duke@435 567 jlong con = t2->get_con();
duke@435 568 // Masking off high bits which are always zero is useless.
duke@435 569 const TypeLong* t1 = phase->type( in(1) )->isa_long();
duke@435 570 if (t1 != NULL && t1->_lo >= 0) {
duke@435 571 jlong t1_support = ((jlong)1 << (1 + log2_long(t1->_hi))) - 1;
duke@435 572 if ((t1_support & con) == t1_support)
duke@435 573 return usr;
duke@435 574 }
duke@435 575 uint lop = usr->Opcode();
duke@435 576 // Masking off the high bits of a unsigned-shift-right is not
duke@435 577 // needed either.
duke@435 578 if( lop == Op_URShiftL ) {
duke@435 579 const TypeInt *t12 = phase->type( usr->in(2) )->isa_int();
twisti@994 580 if( t12 && t12->is_con() ) { // Shift is by a constant
twisti@994 581 int shift = t12->get_con();
twisti@994 582 shift &= BitsPerJavaLong - 1; // semantics of Java shifts
twisti@994 583 jlong mask = max_julong >> shift;
duke@435 584 if( (mask&con) == mask ) // If AND is useless, skip it
duke@435 585 return usr;
duke@435 586 }
duke@435 587 }
duke@435 588 }
duke@435 589 return MulNode::Identity(phase);
duke@435 590 }
duke@435 591
duke@435 592 //------------------------------Ideal------------------------------------------
duke@435 593 Node *AndLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
duke@435 594 // Special case constant AND mask
duke@435 595 const TypeLong *t2 = phase->type( in(2) )->isa_long();
duke@435 596 if( !t2 || !t2->is_con() ) return MulNode::Ideal(phase, can_reshape);
duke@435 597 const jlong mask = t2->get_con();
duke@435 598
twisti@1059 599 Node* in1 = in(1);
twisti@1059 600 uint op = in1->Opcode();
twisti@1059 601
twisti@1259 602 // Masking sign bits off of an integer? Do an unsigned integer to
twisti@1259 603 // long load.
twisti@1259 604 // NOTE: This check must be *before* we try to convert the AndLNode
twisti@1259 605 // to an AndINode and commute it with ConvI2LNode because
twisti@1259 606 // 0xFFFFFFFFL masks the whole integer and we get a sign extension,
twisti@1259 607 // which is wrong.
twisti@1259 608 if (op == Op_ConvI2L && in1->in(1)->Opcode() == Op_LoadI && mask == CONST64(0x00000000FFFFFFFF)) {
twisti@1059 609 Node* load = in1->in(1);
twisti@1059 610 return new (phase->C, 3) LoadUI2LNode(load->in(MemNode::Control),
twisti@1059 611 load->in(MemNode::Memory),
twisti@1059 612 load->in(MemNode::Address),
twisti@1059 613 load->adr_type());
twisti@1059 614 }
duke@435 615
twisti@1259 616 // Are we masking a long that was converted from an int with a mask
twisti@1332 617 // that fits in 32-bits? Commute them and use an AndINode. Don't
twisti@1332 618 // convert masks which would cause a sign extension of the integer
twisti@1332 619 // value. This check includes UI2L masks (0x00000000FFFFFFFF) which
twisti@1332 620 // would be optimized away later in Identity.
twisti@1332 621 if (op == Op_ConvI2L && (mask & CONST64(0xFFFFFFFF80000000)) == 0) {
twisti@1332 622 Node* andi = new (phase->C, 3) AndINode(in1->in(1), phase->intcon(mask));
twisti@1332 623 andi = phase->transform(andi);
twisti@1332 624 return new (phase->C, 2) ConvI2LNode(andi);
twisti@1259 625 }
twisti@1259 626
duke@435 627 // Masking off sign bits? Dont make them!
twisti@1059 628 if (op == Op_RShiftL) {
twisti@1259 629 const TypeInt* t12 = phase->type(in1->in(2))->isa_int();
duke@435 630 if( t12 && t12->is_con() ) { // Shift is by a constant
duke@435 631 int shift = t12->get_con();
twisti@994 632 shift &= BitsPerJavaLong - 1; // semantics of Java shifts
twisti@994 633 const jlong sign_bits_mask = ~(((jlong)CONST64(1) << (jlong)(BitsPerJavaLong - shift)) -1);
duke@435 634 // If the AND'ing of the 2 masks has no bits, then only original shifted
duke@435 635 // bits survive. NO sign-extension bits survive the maskings.
duke@435 636 if( (sign_bits_mask & mask) == 0 ) {
duke@435 637 // Use zero-fill shift instead
twisti@1059 638 Node *zshift = phase->transform(new (phase->C, 3) URShiftLNode(in1->in(1), in1->in(2)));
twisti@1259 639 return new (phase->C, 3) AndLNode(zshift, in(2));
duke@435 640 }
duke@435 641 }
duke@435 642 }
duke@435 643
duke@435 644 return MulNode::Ideal(phase, can_reshape);
duke@435 645 }
duke@435 646
duke@435 647 //=============================================================================
duke@435 648 //------------------------------Identity---------------------------------------
duke@435 649 Node *LShiftINode::Identity( PhaseTransform *phase ) {
duke@435 650 const TypeInt *ti = phase->type( in(2) )->isa_int(); // shift count is an int
duke@435 651 return ( ti && ti->is_con() && ( ti->get_con() & ( BitsPerInt - 1 ) ) == 0 ) ? in(1) : this;
duke@435 652 }
duke@435 653
duke@435 654 //------------------------------Ideal------------------------------------------
duke@435 655 // If the right input is a constant, and the left input is an add of a
duke@435 656 // constant, flatten the tree: (X+con1)<<con0 ==> X<<con0 + con1<<con0
duke@435 657 Node *LShiftINode::Ideal(PhaseGVN *phase, bool can_reshape) {
duke@435 658 const Type *t = phase->type( in(2) );
duke@435 659 if( t == Type::TOP ) return NULL; // Right input is dead
duke@435 660 const TypeInt *t2 = t->isa_int();
duke@435 661 if( !t2 || !t2->is_con() ) return NULL; // Right input is a constant
duke@435 662 const int con = t2->get_con() & ( BitsPerInt - 1 ); // masked shift count
duke@435 663
duke@435 664 if ( con == 0 ) return NULL; // let Identity() handle 0 shift count
duke@435 665
duke@435 666 // Left input is an add of a constant?
duke@435 667 Node *add1 = in(1);
duke@435 668 int add1_op = add1->Opcode();
duke@435 669 if( add1_op == Op_AddI ) { // Left input is an add?
duke@435 670 assert( add1 != add1->in(1), "dead loop in LShiftINode::Ideal" );
duke@435 671 const TypeInt *t12 = phase->type(add1->in(2))->isa_int();
duke@435 672 if( t12 && t12->is_con() ){ // Left input is an add of a con?
duke@435 673 // Transform is legal, but check for profit. Avoid breaking 'i2s'
duke@435 674 // and 'i2b' patterns which typically fold into 'StoreC/StoreB'.
duke@435 675 if( con < 16 ) {
duke@435 676 // Compute X << con0
duke@435 677 Node *lsh = phase->transform( new (phase->C, 3) LShiftINode( add1->in(1), in(2) ) );
duke@435 678 // Compute X<<con0 + (con1<<con0)
duke@435 679 return new (phase->C, 3) AddINode( lsh, phase->intcon(t12->get_con() << con));
duke@435 680 }
duke@435 681 }
duke@435 682 }
duke@435 683
duke@435 684 // Check for "(x>>c0)<<c0" which just masks off low bits
duke@435 685 if( (add1_op == Op_RShiftI || add1_op == Op_URShiftI ) &&
duke@435 686 add1->in(2) == in(2) )
duke@435 687 // Convert to "(x & -(1<<c0))"
duke@435 688 return new (phase->C, 3) AndINode(add1->in(1),phase->intcon( -(1<<con)));
duke@435 689
duke@435 690 // Check for "((x>>c0) & Y)<<c0" which just masks off more low bits
duke@435 691 if( add1_op == Op_AndI ) {
duke@435 692 Node *add2 = add1->in(1);
duke@435 693 int add2_op = add2->Opcode();
duke@435 694 if( (add2_op == Op_RShiftI || add2_op == Op_URShiftI ) &&
duke@435 695 add2->in(2) == in(2) ) {
duke@435 696 // Convert to "(x & (Y<<c0))"
duke@435 697 Node *y_sh = phase->transform( new (phase->C, 3) LShiftINode( add1->in(2), in(2) ) );
duke@435 698 return new (phase->C, 3) AndINode( add2->in(1), y_sh );
duke@435 699 }
duke@435 700 }
duke@435 701
duke@435 702 // Check for ((x & ((1<<(32-c0))-1)) << c0) which ANDs off high bits
duke@435 703 // before shifting them away.
duke@435 704 const jint bits_mask = right_n_bits(BitsPerJavaInteger-con);
duke@435 705 if( add1_op == Op_AndI &&
duke@435 706 phase->type(add1->in(2)) == TypeInt::make( bits_mask ) )
duke@435 707 return new (phase->C, 3) LShiftINode( add1->in(1), in(2) );
duke@435 708
duke@435 709 return NULL;
duke@435 710 }
duke@435 711
duke@435 712 //------------------------------Value------------------------------------------
duke@435 713 // A LShiftINode shifts its input2 left by input1 amount.
duke@435 714 const Type *LShiftINode::Value( PhaseTransform *phase ) const {
duke@435 715 const Type *t1 = phase->type( in(1) );
duke@435 716 const Type *t2 = phase->type( in(2) );
duke@435 717 // Either input is TOP ==> the result is TOP
duke@435 718 if( t1 == Type::TOP ) return Type::TOP;
duke@435 719 if( t2 == Type::TOP ) return Type::TOP;
duke@435 720
duke@435 721 // Left input is ZERO ==> the result is ZERO.
duke@435 722 if( t1 == TypeInt::ZERO ) return TypeInt::ZERO;
duke@435 723 // Shift by zero does nothing
duke@435 724 if( t2 == TypeInt::ZERO ) return t1;
duke@435 725
duke@435 726 // Either input is BOTTOM ==> the result is BOTTOM
duke@435 727 if( (t1 == TypeInt::INT) || (t2 == TypeInt::INT) ||
duke@435 728 (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
duke@435 729 return TypeInt::INT;
duke@435 730
duke@435 731 const TypeInt *r1 = t1->is_int(); // Handy access
duke@435 732 const TypeInt *r2 = t2->is_int(); // Handy access
duke@435 733
duke@435 734 if (!r2->is_con())
duke@435 735 return TypeInt::INT;
duke@435 736
duke@435 737 uint shift = r2->get_con();
duke@435 738 shift &= BitsPerJavaInteger-1; // semantics of Java shifts
duke@435 739 // Shift by a multiple of 32 does nothing:
duke@435 740 if (shift == 0) return t1;
duke@435 741
duke@435 742 // If the shift is a constant, shift the bounds of the type,
duke@435 743 // unless this could lead to an overflow.
duke@435 744 if (!r1->is_con()) {
duke@435 745 jint lo = r1->_lo, hi = r1->_hi;
duke@435 746 if (((lo << shift) >> shift) == lo &&
duke@435 747 ((hi << shift) >> shift) == hi) {
duke@435 748 // No overflow. The range shifts up cleanly.
duke@435 749 return TypeInt::make((jint)lo << (jint)shift,
duke@435 750 (jint)hi << (jint)shift,
duke@435 751 MAX2(r1->_widen,r2->_widen));
duke@435 752 }
duke@435 753 return TypeInt::INT;
duke@435 754 }
duke@435 755
duke@435 756 return TypeInt::make( (jint)r1->get_con() << (jint)shift );
duke@435 757 }
duke@435 758
duke@435 759 //=============================================================================
duke@435 760 //------------------------------Identity---------------------------------------
duke@435 761 Node *LShiftLNode::Identity( PhaseTransform *phase ) {
duke@435 762 const TypeInt *ti = phase->type( in(2) )->isa_int(); // shift count is an int
duke@435 763 return ( ti && ti->is_con() && ( ti->get_con() & ( BitsPerLong - 1 ) ) == 0 ) ? in(1) : this;
duke@435 764 }
duke@435 765
duke@435 766 //------------------------------Ideal------------------------------------------
duke@435 767 // If the right input is a constant, and the left input is an add of a
duke@435 768 // constant, flatten the tree: (X+con1)<<con0 ==> X<<con0 + con1<<con0
duke@435 769 Node *LShiftLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
duke@435 770 const Type *t = phase->type( in(2) );
duke@435 771 if( t == Type::TOP ) return NULL; // Right input is dead
duke@435 772 const TypeInt *t2 = t->isa_int();
duke@435 773 if( !t2 || !t2->is_con() ) return NULL; // Right input is a constant
duke@435 774 const int con = t2->get_con() & ( BitsPerLong - 1 ); // masked shift count
duke@435 775
duke@435 776 if ( con == 0 ) return NULL; // let Identity() handle 0 shift count
duke@435 777
duke@435 778 // Left input is an add of a constant?
duke@435 779 Node *add1 = in(1);
duke@435 780 int add1_op = add1->Opcode();
duke@435 781 if( add1_op == Op_AddL ) { // Left input is an add?
duke@435 782 // Avoid dead data cycles from dead loops
duke@435 783 assert( add1 != add1->in(1), "dead loop in LShiftLNode::Ideal" );
duke@435 784 const TypeLong *t12 = phase->type(add1->in(2))->isa_long();
duke@435 785 if( t12 && t12->is_con() ){ // Left input is an add of a con?
duke@435 786 // Compute X << con0
duke@435 787 Node *lsh = phase->transform( new (phase->C, 3) LShiftLNode( add1->in(1), in(2) ) );
duke@435 788 // Compute X<<con0 + (con1<<con0)
duke@435 789 return new (phase->C, 3) AddLNode( lsh, phase->longcon(t12->get_con() << con));
duke@435 790 }
duke@435 791 }
duke@435 792
duke@435 793 // Check for "(x>>c0)<<c0" which just masks off low bits
duke@435 794 if( (add1_op == Op_RShiftL || add1_op == Op_URShiftL ) &&
duke@435 795 add1->in(2) == in(2) )
duke@435 796 // Convert to "(x & -(1<<c0))"
duke@435 797 return new (phase->C, 3) AndLNode(add1->in(1),phase->longcon( -(CONST64(1)<<con)));
duke@435 798
duke@435 799 // Check for "((x>>c0) & Y)<<c0" which just masks off more low bits
duke@435 800 if( add1_op == Op_AndL ) {
duke@435 801 Node *add2 = add1->in(1);
duke@435 802 int add2_op = add2->Opcode();
duke@435 803 if( (add2_op == Op_RShiftL || add2_op == Op_URShiftL ) &&
duke@435 804 add2->in(2) == in(2) ) {
duke@435 805 // Convert to "(x & (Y<<c0))"
duke@435 806 Node *y_sh = phase->transform( new (phase->C, 3) LShiftLNode( add1->in(2), in(2) ) );
duke@435 807 return new (phase->C, 3) AndLNode( add2->in(1), y_sh );
duke@435 808 }
duke@435 809 }
duke@435 810
duke@435 811 // Check for ((x & ((CONST64(1)<<(64-c0))-1)) << c0) which ANDs off high bits
duke@435 812 // before shifting them away.
twisti@994 813 const jlong bits_mask = ((jlong)CONST64(1) << (jlong)(BitsPerJavaLong - con)) - CONST64(1);
duke@435 814 if( add1_op == Op_AndL &&
duke@435 815 phase->type(add1->in(2)) == TypeLong::make( bits_mask ) )
duke@435 816 return new (phase->C, 3) LShiftLNode( add1->in(1), in(2) );
duke@435 817
duke@435 818 return NULL;
duke@435 819 }
duke@435 820
duke@435 821 //------------------------------Value------------------------------------------
duke@435 822 // A LShiftLNode shifts its input2 left by input1 amount.
duke@435 823 const Type *LShiftLNode::Value( PhaseTransform *phase ) const {
duke@435 824 const Type *t1 = phase->type( in(1) );
duke@435 825 const Type *t2 = phase->type( in(2) );
duke@435 826 // Either input is TOP ==> the result is TOP
duke@435 827 if( t1 == Type::TOP ) return Type::TOP;
duke@435 828 if( t2 == Type::TOP ) return Type::TOP;
duke@435 829
duke@435 830 // Left input is ZERO ==> the result is ZERO.
duke@435 831 if( t1 == TypeLong::ZERO ) return TypeLong::ZERO;
duke@435 832 // Shift by zero does nothing
duke@435 833 if( t2 == TypeInt::ZERO ) return t1;
duke@435 834
duke@435 835 // Either input is BOTTOM ==> the result is BOTTOM
duke@435 836 if( (t1 == TypeLong::LONG) || (t2 == TypeInt::INT) ||
duke@435 837 (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
duke@435 838 return TypeLong::LONG;
duke@435 839
duke@435 840 const TypeLong *r1 = t1->is_long(); // Handy access
duke@435 841 const TypeInt *r2 = t2->is_int(); // Handy access
duke@435 842
duke@435 843 if (!r2->is_con())
duke@435 844 return TypeLong::LONG;
duke@435 845
duke@435 846 uint shift = r2->get_con();
twisti@994 847 shift &= BitsPerJavaLong - 1; // semantics of Java shifts
duke@435 848 // Shift by a multiple of 64 does nothing:
duke@435 849 if (shift == 0) return t1;
duke@435 850
duke@435 851 // If the shift is a constant, shift the bounds of the type,
duke@435 852 // unless this could lead to an overflow.
duke@435 853 if (!r1->is_con()) {
duke@435 854 jlong lo = r1->_lo, hi = r1->_hi;
duke@435 855 if (((lo << shift) >> shift) == lo &&
duke@435 856 ((hi << shift) >> shift) == hi) {
duke@435 857 // No overflow. The range shifts up cleanly.
duke@435 858 return TypeLong::make((jlong)lo << (jint)shift,
duke@435 859 (jlong)hi << (jint)shift,
duke@435 860 MAX2(r1->_widen,r2->_widen));
duke@435 861 }
duke@435 862 return TypeLong::LONG;
duke@435 863 }
duke@435 864
duke@435 865 return TypeLong::make( (jlong)r1->get_con() << (jint)shift );
duke@435 866 }
duke@435 867
duke@435 868 //=============================================================================
duke@435 869 //------------------------------Identity---------------------------------------
duke@435 870 Node *RShiftINode::Identity( PhaseTransform *phase ) {
duke@435 871 const TypeInt *t2 = phase->type(in(2))->isa_int();
duke@435 872 if( !t2 ) return this;
duke@435 873 if ( t2->is_con() && ( t2->get_con() & ( BitsPerInt - 1 ) ) == 0 )
duke@435 874 return in(1);
duke@435 875
duke@435 876 // Check for useless sign-masking
duke@435 877 if( in(1)->Opcode() == Op_LShiftI &&
duke@435 878 in(1)->req() == 3 &&
duke@435 879 in(1)->in(2) == in(2) &&
duke@435 880 t2->is_con() ) {
duke@435 881 uint shift = t2->get_con();
duke@435 882 shift &= BitsPerJavaInteger-1; // semantics of Java shifts
duke@435 883 // Compute masks for which this shifting doesn't change
duke@435 884 int lo = (-1 << (BitsPerJavaInteger - shift-1)); // FFFF8000
duke@435 885 int hi = ~lo; // 00007FFF
duke@435 886 const TypeInt *t11 = phase->type(in(1)->in(1))->isa_int();
duke@435 887 if( !t11 ) return this;
duke@435 888 // Does actual value fit inside of mask?
duke@435 889 if( lo <= t11->_lo && t11->_hi <= hi )
duke@435 890 return in(1)->in(1); // Then shifting is a nop
duke@435 891 }
duke@435 892
duke@435 893 return this;
duke@435 894 }
duke@435 895
duke@435 896 //------------------------------Ideal------------------------------------------
duke@435 897 Node *RShiftINode::Ideal(PhaseGVN *phase, bool can_reshape) {
duke@435 898 // Inputs may be TOP if they are dead.
duke@435 899 const TypeInt *t1 = phase->type( in(1) )->isa_int();
duke@435 900 if( !t1 ) return NULL; // Left input is an integer
duke@435 901 const TypeInt *t2 = phase->type( in(2) )->isa_int();
duke@435 902 if( !t2 || !t2->is_con() ) return NULL; // Right input is a constant
duke@435 903 const TypeInt *t3; // type of in(1).in(2)
duke@435 904 int shift = t2->get_con();
duke@435 905 shift &= BitsPerJavaInteger-1; // semantics of Java shifts
duke@435 906
duke@435 907 if ( shift == 0 ) return NULL; // let Identity() handle 0 shift count
duke@435 908
duke@435 909 // Check for (x & 0xFF000000) >> 24, whose mask can be made smaller.
duke@435 910 // Such expressions arise normally from shift chains like (byte)(x >> 24).
duke@435 911 const Node *mask = in(1);
duke@435 912 if( mask->Opcode() == Op_AndI &&
duke@435 913 (t3 = phase->type(mask->in(2))->isa_int()) &&
duke@435 914 t3->is_con() ) {
duke@435 915 Node *x = mask->in(1);
duke@435 916 jint maskbits = t3->get_con();
duke@435 917 // Convert to "(x >> shift) & (mask >> shift)"
duke@435 918 Node *shr_nomask = phase->transform( new (phase->C, 3) RShiftINode(mask->in(1), in(2)) );
duke@435 919 return new (phase->C, 3) AndINode(shr_nomask, phase->intcon( maskbits >> shift));
duke@435 920 }
duke@435 921
duke@435 922 // Check for "(short[i] <<16)>>16" which simply sign-extends
duke@435 923 const Node *shl = in(1);
duke@435 924 if( shl->Opcode() != Op_LShiftI ) return NULL;
duke@435 925
duke@435 926 if( shift == 16 &&
duke@435 927 (t3 = phase->type(shl->in(2))->isa_int()) &&
duke@435 928 t3->is_con(16) ) {
duke@435 929 Node *ld = shl->in(1);
duke@435 930 if( ld->Opcode() == Op_LoadS ) {
duke@435 931 // Sign extension is just useless here. Return a RShiftI of zero instead
duke@435 932 // returning 'ld' directly. We cannot return an old Node directly as
duke@435 933 // that is the job of 'Identity' calls and Identity calls only work on
duke@435 934 // direct inputs ('ld' is an extra Node removed from 'this'). The
duke@435 935 // combined optimization requires Identity only return direct inputs.
duke@435 936 set_req(1, ld);
duke@435 937 set_req(2, phase->intcon(0));
duke@435 938 return this;
duke@435 939 }
twisti@993 940 else if( ld->Opcode() == Op_LoadUS )
duke@435 941 // Replace zero-extension-load with sign-extension-load
duke@435 942 return new (phase->C, 3) LoadSNode( ld->in(MemNode::Control),
duke@435 943 ld->in(MemNode::Memory),
duke@435 944 ld->in(MemNode::Address),
duke@435 945 ld->adr_type());
duke@435 946 }
duke@435 947
duke@435 948 // Check for "(byte[i] <<24)>>24" which simply sign-extends
duke@435 949 if( shift == 24 &&
duke@435 950 (t3 = phase->type(shl->in(2))->isa_int()) &&
duke@435 951 t3->is_con(24) ) {
duke@435 952 Node *ld = shl->in(1);
duke@435 953 if( ld->Opcode() == Op_LoadB ) {
duke@435 954 // Sign extension is just useless here
duke@435 955 set_req(1, ld);
duke@435 956 set_req(2, phase->intcon(0));
duke@435 957 return this;
duke@435 958 }
duke@435 959 }
duke@435 960
duke@435 961 return NULL;
duke@435 962 }
duke@435 963
duke@435 964 //------------------------------Value------------------------------------------
duke@435 965 // A RShiftINode shifts its input2 right by input1 amount.
duke@435 966 const Type *RShiftINode::Value( PhaseTransform *phase ) const {
duke@435 967 const Type *t1 = phase->type( in(1) );
duke@435 968 const Type *t2 = phase->type( in(2) );
duke@435 969 // Either input is TOP ==> the result is TOP
duke@435 970 if( t1 == Type::TOP ) return Type::TOP;
duke@435 971 if( t2 == Type::TOP ) return Type::TOP;
duke@435 972
duke@435 973 // Left input is ZERO ==> the result is ZERO.
duke@435 974 if( t1 == TypeInt::ZERO ) return TypeInt::ZERO;
duke@435 975 // Shift by zero does nothing
duke@435 976 if( t2 == TypeInt::ZERO ) return t1;
duke@435 977
duke@435 978 // Either input is BOTTOM ==> the result is BOTTOM
duke@435 979 if (t1 == Type::BOTTOM || t2 == Type::BOTTOM)
duke@435 980 return TypeInt::INT;
duke@435 981
duke@435 982 if (t2 == TypeInt::INT)
duke@435 983 return TypeInt::INT;
duke@435 984
duke@435 985 const TypeInt *r1 = t1->is_int(); // Handy access
duke@435 986 const TypeInt *r2 = t2->is_int(); // Handy access
duke@435 987
duke@435 988 // If the shift is a constant, just shift the bounds of the type.
duke@435 989 // For example, if the shift is 31, we just propagate sign bits.
duke@435 990 if (r2->is_con()) {
duke@435 991 uint shift = r2->get_con();
duke@435 992 shift &= BitsPerJavaInteger-1; // semantics of Java shifts
duke@435 993 // Shift by a multiple of 32 does nothing:
duke@435 994 if (shift == 0) return t1;
duke@435 995 // Calculate reasonably aggressive bounds for the result.
duke@435 996 // This is necessary if we are to correctly type things
duke@435 997 // like (x<<24>>24) == ((byte)x).
duke@435 998 jint lo = (jint)r1->_lo >> (jint)shift;
duke@435 999 jint hi = (jint)r1->_hi >> (jint)shift;
duke@435 1000 assert(lo <= hi, "must have valid bounds");
duke@435 1001 const TypeInt* ti = TypeInt::make(lo, hi, MAX2(r1->_widen,r2->_widen));
duke@435 1002 #ifdef ASSERT
duke@435 1003 // Make sure we get the sign-capture idiom correct.
duke@435 1004 if (shift == BitsPerJavaInteger-1) {
duke@435 1005 if (r1->_lo >= 0) assert(ti == TypeInt::ZERO, ">>31 of + is 0");
duke@435 1006 if (r1->_hi < 0) assert(ti == TypeInt::MINUS_1, ">>31 of - is -1");
duke@435 1007 }
duke@435 1008 #endif
duke@435 1009 return ti;
duke@435 1010 }
duke@435 1011
duke@435 1012 if( !r1->is_con() || !r2->is_con() )
duke@435 1013 return TypeInt::INT;
duke@435 1014
duke@435 1015 // Signed shift right
duke@435 1016 return TypeInt::make( r1->get_con() >> (r2->get_con()&31) );
duke@435 1017 }
duke@435 1018
duke@435 1019 //=============================================================================
duke@435 1020 //------------------------------Identity---------------------------------------
duke@435 1021 Node *RShiftLNode::Identity( PhaseTransform *phase ) {
duke@435 1022 const TypeInt *ti = phase->type( in(2) )->isa_int(); // shift count is an int
duke@435 1023 return ( ti && ti->is_con() && ( ti->get_con() & ( BitsPerLong - 1 ) ) == 0 ) ? in(1) : this;
duke@435 1024 }
duke@435 1025
duke@435 1026 //------------------------------Value------------------------------------------
duke@435 1027 // A RShiftLNode shifts its input2 right by input1 amount.
duke@435 1028 const Type *RShiftLNode::Value( PhaseTransform *phase ) const {
duke@435 1029 const Type *t1 = phase->type( in(1) );
duke@435 1030 const Type *t2 = phase->type( in(2) );
duke@435 1031 // Either input is TOP ==> the result is TOP
duke@435 1032 if( t1 == Type::TOP ) return Type::TOP;
duke@435 1033 if( t2 == Type::TOP ) return Type::TOP;
duke@435 1034
duke@435 1035 // Left input is ZERO ==> the result is ZERO.
duke@435 1036 if( t1 == TypeLong::ZERO ) return TypeLong::ZERO;
duke@435 1037 // Shift by zero does nothing
duke@435 1038 if( t2 == TypeInt::ZERO ) return t1;
duke@435 1039
duke@435 1040 // Either input is BOTTOM ==> the result is BOTTOM
duke@435 1041 if (t1 == Type::BOTTOM || t2 == Type::BOTTOM)
duke@435 1042 return TypeLong::LONG;
duke@435 1043
duke@435 1044 if (t2 == TypeInt::INT)
duke@435 1045 return TypeLong::LONG;
duke@435 1046
duke@435 1047 const TypeLong *r1 = t1->is_long(); // Handy access
duke@435 1048 const TypeInt *r2 = t2->is_int (); // Handy access
duke@435 1049
duke@435 1050 // If the shift is a constant, just shift the bounds of the type.
duke@435 1051 // For example, if the shift is 63, we just propagate sign bits.
duke@435 1052 if (r2->is_con()) {
duke@435 1053 uint shift = r2->get_con();
duke@435 1054 shift &= (2*BitsPerJavaInteger)-1; // semantics of Java shifts
duke@435 1055 // Shift by a multiple of 64 does nothing:
duke@435 1056 if (shift == 0) return t1;
duke@435 1057 // Calculate reasonably aggressive bounds for the result.
duke@435 1058 // This is necessary if we are to correctly type things
duke@435 1059 // like (x<<24>>24) == ((byte)x).
duke@435 1060 jlong lo = (jlong)r1->_lo >> (jlong)shift;
duke@435 1061 jlong hi = (jlong)r1->_hi >> (jlong)shift;
duke@435 1062 assert(lo <= hi, "must have valid bounds");
duke@435 1063 const TypeLong* tl = TypeLong::make(lo, hi, MAX2(r1->_widen,r2->_widen));
duke@435 1064 #ifdef ASSERT
duke@435 1065 // Make sure we get the sign-capture idiom correct.
duke@435 1066 if (shift == (2*BitsPerJavaInteger)-1) {
duke@435 1067 if (r1->_lo >= 0) assert(tl == TypeLong::ZERO, ">>63 of + is 0");
duke@435 1068 if (r1->_hi < 0) assert(tl == TypeLong::MINUS_1, ">>63 of - is -1");
duke@435 1069 }
duke@435 1070 #endif
duke@435 1071 return tl;
duke@435 1072 }
duke@435 1073
duke@435 1074 return TypeLong::LONG; // Give up
duke@435 1075 }
duke@435 1076
duke@435 1077 //=============================================================================
duke@435 1078 //------------------------------Identity---------------------------------------
duke@435 1079 Node *URShiftINode::Identity( PhaseTransform *phase ) {
duke@435 1080 const TypeInt *ti = phase->type( in(2) )->isa_int();
duke@435 1081 if ( ti && ti->is_con() && ( ti->get_con() & ( BitsPerInt - 1 ) ) == 0 ) return in(1);
duke@435 1082
duke@435 1083 // Check for "((x << LogBytesPerWord) + (wordSize-1)) >> LogBytesPerWord" which is just "x".
duke@435 1084 // Happens during new-array length computation.
duke@435 1085 // Safe if 'x' is in the range [0..(max_int>>LogBytesPerWord)]
duke@435 1086 Node *add = in(1);
duke@435 1087 if( add->Opcode() == Op_AddI ) {
duke@435 1088 const TypeInt *t2 = phase->type(add->in(2))->isa_int();
duke@435 1089 if( t2 && t2->is_con(wordSize - 1) &&
duke@435 1090 add->in(1)->Opcode() == Op_LShiftI ) {
duke@435 1091 // Check that shift_counts are LogBytesPerWord
duke@435 1092 Node *lshift_count = add->in(1)->in(2);
duke@435 1093 const TypeInt *t_lshift_count = phase->type(lshift_count)->isa_int();
duke@435 1094 if( t_lshift_count && t_lshift_count->is_con(LogBytesPerWord) &&
duke@435 1095 t_lshift_count == phase->type(in(2)) ) {
duke@435 1096 Node *x = add->in(1)->in(1);
duke@435 1097 const TypeInt *t_x = phase->type(x)->isa_int();
duke@435 1098 if( t_x != NULL && 0 <= t_x->_lo && t_x->_hi <= (max_jint>>LogBytesPerWord) ) {
duke@435 1099 return x;
duke@435 1100 }
duke@435 1101 }
duke@435 1102 }
duke@435 1103 }
duke@435 1104
duke@435 1105 return (phase->type(in(2))->higher_equal(TypeInt::ZERO)) ? in(1) : this;
duke@435 1106 }
duke@435 1107
duke@435 1108 //------------------------------Ideal------------------------------------------
duke@435 1109 Node *URShiftINode::Ideal(PhaseGVN *phase, bool can_reshape) {
duke@435 1110 const TypeInt *t2 = phase->type( in(2) )->isa_int();
duke@435 1111 if( !t2 || !t2->is_con() ) return NULL; // Right input is a constant
duke@435 1112 const int con = t2->get_con() & 31; // Shift count is always masked
duke@435 1113 if ( con == 0 ) return NULL; // let Identity() handle a 0 shift count
duke@435 1114 // We'll be wanting the right-shift amount as a mask of that many bits
duke@435 1115 const int mask = right_n_bits(BitsPerJavaInteger - con);
duke@435 1116
duke@435 1117 int in1_op = in(1)->Opcode();
duke@435 1118
duke@435 1119 // Check for ((x>>>a)>>>b) and replace with (x>>>(a+b)) when a+b < 32
duke@435 1120 if( in1_op == Op_URShiftI ) {
duke@435 1121 const TypeInt *t12 = phase->type( in(1)->in(2) )->isa_int();
duke@435 1122 if( t12 && t12->is_con() ) { // Right input is a constant
duke@435 1123 assert( in(1) != in(1)->in(1), "dead loop in URShiftINode::Ideal" );
duke@435 1124 const int con2 = t12->get_con() & 31; // Shift count is always masked
duke@435 1125 const int con3 = con+con2;
duke@435 1126 if( con3 < 32 ) // Only merge shifts if total is < 32
duke@435 1127 return new (phase->C, 3) URShiftINode( in(1)->in(1), phase->intcon(con3) );
duke@435 1128 }
duke@435 1129 }
duke@435 1130
duke@435 1131 // Check for ((x << z) + Y) >>> z. Replace with x + con>>>z
duke@435 1132 // The idiom for rounding to a power of 2 is "(Q+(2^z-1)) >>> z".
duke@435 1133 // If Q is "X << z" the rounding is useless. Look for patterns like
duke@435 1134 // ((X<<Z) + Y) >>> Z and replace with (X + Y>>>Z) & Z-mask.
duke@435 1135 Node *add = in(1);
duke@435 1136 if( in1_op == Op_AddI ) {
duke@435 1137 Node *lshl = add->in(1);
duke@435 1138 if( lshl->Opcode() == Op_LShiftI &&
duke@435 1139 phase->type(lshl->in(2)) == t2 ) {
duke@435 1140 Node *y_z = phase->transform( new (phase->C, 3) URShiftINode(add->in(2),in(2)) );
duke@435 1141 Node *sum = phase->transform( new (phase->C, 3) AddINode( lshl->in(1), y_z ) );
duke@435 1142 return new (phase->C, 3) AndINode( sum, phase->intcon(mask) );
duke@435 1143 }
duke@435 1144 }
duke@435 1145
duke@435 1146 // Check for (x & mask) >>> z. Replace with (x >>> z) & (mask >>> z)
duke@435 1147 // This shortens the mask. Also, if we are extracting a high byte and
duke@435 1148 // storing it to a buffer, the mask will be removed completely.
duke@435 1149 Node *andi = in(1);
duke@435 1150 if( in1_op == Op_AndI ) {
duke@435 1151 const TypeInt *t3 = phase->type( andi->in(2) )->isa_int();
duke@435 1152 if( t3 && t3->is_con() ) { // Right input is a constant
duke@435 1153 jint mask2 = t3->get_con();
duke@435 1154 mask2 >>= con; // *signed* shift downward (high-order zeroes do not help)
duke@435 1155 Node *newshr = phase->transform( new (phase->C, 3) URShiftINode(andi->in(1), in(2)) );
duke@435 1156 return new (phase->C, 3) AndINode(newshr, phase->intcon(mask2));
duke@435 1157 // The negative values are easier to materialize than positive ones.
duke@435 1158 // A typical case from address arithmetic is ((x & ~15) >> 4).
duke@435 1159 // It's better to change that to ((x >> 4) & ~0) versus
duke@435 1160 // ((x >> 4) & 0x0FFFFFFF). The difference is greatest in LP64.
duke@435 1161 }
duke@435 1162 }
duke@435 1163
duke@435 1164 // Check for "(X << z ) >>> z" which simply zero-extends
duke@435 1165 Node *shl = in(1);
duke@435 1166 if( in1_op == Op_LShiftI &&
duke@435 1167 phase->type(shl->in(2)) == t2 )
duke@435 1168 return new (phase->C, 3) AndINode( shl->in(1), phase->intcon(mask) );
duke@435 1169
duke@435 1170 return NULL;
duke@435 1171 }
duke@435 1172
duke@435 1173 //------------------------------Value------------------------------------------
duke@435 1174 // A URShiftINode shifts its input2 right by input1 amount.
duke@435 1175 const Type *URShiftINode::Value( PhaseTransform *phase ) const {
duke@435 1176 // (This is a near clone of RShiftINode::Value.)
duke@435 1177 const Type *t1 = phase->type( in(1) );
duke@435 1178 const Type *t2 = phase->type( in(2) );
duke@435 1179 // Either input is TOP ==> the result is TOP
duke@435 1180 if( t1 == Type::TOP ) return Type::TOP;
duke@435 1181 if( t2 == Type::TOP ) return Type::TOP;
duke@435 1182
duke@435 1183 // Left input is ZERO ==> the result is ZERO.
duke@435 1184 if( t1 == TypeInt::ZERO ) return TypeInt::ZERO;
duke@435 1185 // Shift by zero does nothing
duke@435 1186 if( t2 == TypeInt::ZERO ) return t1;
duke@435 1187
duke@435 1188 // Either input is BOTTOM ==> the result is BOTTOM
duke@435 1189 if (t1 == Type::BOTTOM || t2 == Type::BOTTOM)
duke@435 1190 return TypeInt::INT;
duke@435 1191
duke@435 1192 if (t2 == TypeInt::INT)
duke@435 1193 return TypeInt::INT;
duke@435 1194
duke@435 1195 const TypeInt *r1 = t1->is_int(); // Handy access
duke@435 1196 const TypeInt *r2 = t2->is_int(); // Handy access
duke@435 1197
duke@435 1198 if (r2->is_con()) {
duke@435 1199 uint shift = r2->get_con();
duke@435 1200 shift &= BitsPerJavaInteger-1; // semantics of Java shifts
duke@435 1201 // Shift by a multiple of 32 does nothing:
duke@435 1202 if (shift == 0) return t1;
duke@435 1203 // Calculate reasonably aggressive bounds for the result.
duke@435 1204 jint lo = (juint)r1->_lo >> (juint)shift;
duke@435 1205 jint hi = (juint)r1->_hi >> (juint)shift;
duke@435 1206 if (r1->_hi >= 0 && r1->_lo < 0) {
duke@435 1207 // If the type has both negative and positive values,
duke@435 1208 // there are two separate sub-domains to worry about:
duke@435 1209 // The positive half and the negative half.
duke@435 1210 jint neg_lo = lo;
duke@435 1211 jint neg_hi = (juint)-1 >> (juint)shift;
duke@435 1212 jint pos_lo = (juint) 0 >> (juint)shift;
duke@435 1213 jint pos_hi = hi;
duke@435 1214 lo = MIN2(neg_lo, pos_lo); // == 0
duke@435 1215 hi = MAX2(neg_hi, pos_hi); // == -1 >>> shift;
duke@435 1216 }
duke@435 1217 assert(lo <= hi, "must have valid bounds");
duke@435 1218 const TypeInt* ti = TypeInt::make(lo, hi, MAX2(r1->_widen,r2->_widen));
duke@435 1219 #ifdef ASSERT
duke@435 1220 // Make sure we get the sign-capture idiom correct.
duke@435 1221 if (shift == BitsPerJavaInteger-1) {
duke@435 1222 if (r1->_lo >= 0) assert(ti == TypeInt::ZERO, ">>>31 of + is 0");
duke@435 1223 if (r1->_hi < 0) assert(ti == TypeInt::ONE, ">>>31 of - is +1");
duke@435 1224 }
duke@435 1225 #endif
duke@435 1226 return ti;
duke@435 1227 }
duke@435 1228
duke@435 1229 //
duke@435 1230 // Do not support shifted oops in info for GC
duke@435 1231 //
duke@435 1232 // else if( t1->base() == Type::InstPtr ) {
duke@435 1233 //
duke@435 1234 // const TypeInstPtr *o = t1->is_instptr();
duke@435 1235 // if( t1->singleton() )
duke@435 1236 // return TypeInt::make( ((uint32)o->const_oop() + o->_offset) >> shift );
duke@435 1237 // }
duke@435 1238 // else if( t1->base() == Type::KlassPtr ) {
duke@435 1239 // const TypeKlassPtr *o = t1->is_klassptr();
duke@435 1240 // if( t1->singleton() )
duke@435 1241 // return TypeInt::make( ((uint32)o->const_oop() + o->_offset) >> shift );
duke@435 1242 // }
duke@435 1243
duke@435 1244 return TypeInt::INT;
duke@435 1245 }
duke@435 1246
duke@435 1247 //=============================================================================
duke@435 1248 //------------------------------Identity---------------------------------------
duke@435 1249 Node *URShiftLNode::Identity( PhaseTransform *phase ) {
duke@435 1250 const TypeInt *ti = phase->type( in(2) )->isa_int(); // shift count is an int
duke@435 1251 return ( ti && ti->is_con() && ( ti->get_con() & ( BitsPerLong - 1 ) ) == 0 ) ? in(1) : this;
duke@435 1252 }
duke@435 1253
duke@435 1254 //------------------------------Ideal------------------------------------------
duke@435 1255 Node *URShiftLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
duke@435 1256 const TypeInt *t2 = phase->type( in(2) )->isa_int();
duke@435 1257 if( !t2 || !t2->is_con() ) return NULL; // Right input is a constant
duke@435 1258 const int con = t2->get_con() & ( BitsPerLong - 1 ); // Shift count is always masked
duke@435 1259 if ( con == 0 ) return NULL; // let Identity() handle a 0 shift count
duke@435 1260 // note: mask computation below does not work for 0 shift count
duke@435 1261 // We'll be wanting the right-shift amount as a mask of that many bits
twisti@994 1262 const jlong mask = (((jlong)CONST64(1) << (jlong)(BitsPerJavaLong - con)) -1);
duke@435 1263
duke@435 1264 // Check for ((x << z) + Y) >>> z. Replace with x + con>>>z
duke@435 1265 // The idiom for rounding to a power of 2 is "(Q+(2^z-1)) >>> z".
duke@435 1266 // If Q is "X << z" the rounding is useless. Look for patterns like
duke@435 1267 // ((X<<Z) + Y) >>> Z and replace with (X + Y>>>Z) & Z-mask.
duke@435 1268 Node *add = in(1);
duke@435 1269 if( add->Opcode() == Op_AddL ) {
duke@435 1270 Node *lshl = add->in(1);
duke@435 1271 if( lshl->Opcode() == Op_LShiftL &&
duke@435 1272 phase->type(lshl->in(2)) == t2 ) {
duke@435 1273 Node *y_z = phase->transform( new (phase->C, 3) URShiftLNode(add->in(2),in(2)) );
duke@435 1274 Node *sum = phase->transform( new (phase->C, 3) AddLNode( lshl->in(1), y_z ) );
duke@435 1275 return new (phase->C, 3) AndLNode( sum, phase->longcon(mask) );
duke@435 1276 }
duke@435 1277 }
duke@435 1278
duke@435 1279 // Check for (x & mask) >>> z. Replace with (x >>> z) & (mask >>> z)
duke@435 1280 // This shortens the mask. Also, if we are extracting a high byte and
duke@435 1281 // storing it to a buffer, the mask will be removed completely.
duke@435 1282 Node *andi = in(1);
duke@435 1283 if( andi->Opcode() == Op_AndL ) {
duke@435 1284 const TypeLong *t3 = phase->type( andi->in(2) )->isa_long();
duke@435 1285 if( t3 && t3->is_con() ) { // Right input is a constant
duke@435 1286 jlong mask2 = t3->get_con();
duke@435 1287 mask2 >>= con; // *signed* shift downward (high-order zeroes do not help)
duke@435 1288 Node *newshr = phase->transform( new (phase->C, 3) URShiftLNode(andi->in(1), in(2)) );
duke@435 1289 return new (phase->C, 3) AndLNode(newshr, phase->longcon(mask2));
duke@435 1290 }
duke@435 1291 }
duke@435 1292
duke@435 1293 // Check for "(X << z ) >>> z" which simply zero-extends
duke@435 1294 Node *shl = in(1);
duke@435 1295 if( shl->Opcode() == Op_LShiftL &&
duke@435 1296 phase->type(shl->in(2)) == t2 )
duke@435 1297 return new (phase->C, 3) AndLNode( shl->in(1), phase->longcon(mask) );
duke@435 1298
duke@435 1299 return NULL;
duke@435 1300 }
duke@435 1301
duke@435 1302 //------------------------------Value------------------------------------------
duke@435 1303 // A URShiftINode shifts its input2 right by input1 amount.
duke@435 1304 const Type *URShiftLNode::Value( PhaseTransform *phase ) const {
duke@435 1305 // (This is a near clone of RShiftLNode::Value.)
duke@435 1306 const Type *t1 = phase->type( in(1) );
duke@435 1307 const Type *t2 = phase->type( in(2) );
duke@435 1308 // Either input is TOP ==> the result is TOP
duke@435 1309 if( t1 == Type::TOP ) return Type::TOP;
duke@435 1310 if( t2 == Type::TOP ) return Type::TOP;
duke@435 1311
duke@435 1312 // Left input is ZERO ==> the result is ZERO.
duke@435 1313 if( t1 == TypeLong::ZERO ) return TypeLong::ZERO;
duke@435 1314 // Shift by zero does nothing
duke@435 1315 if( t2 == TypeInt::ZERO ) return t1;
duke@435 1316
duke@435 1317 // Either input is BOTTOM ==> the result is BOTTOM
duke@435 1318 if (t1 == Type::BOTTOM || t2 == Type::BOTTOM)
duke@435 1319 return TypeLong::LONG;
duke@435 1320
duke@435 1321 if (t2 == TypeInt::INT)
duke@435 1322 return TypeLong::LONG;
duke@435 1323
duke@435 1324 const TypeLong *r1 = t1->is_long(); // Handy access
duke@435 1325 const TypeInt *r2 = t2->is_int (); // Handy access
duke@435 1326
duke@435 1327 if (r2->is_con()) {
duke@435 1328 uint shift = r2->get_con();
twisti@994 1329 shift &= BitsPerJavaLong - 1; // semantics of Java shifts
duke@435 1330 // Shift by a multiple of 64 does nothing:
duke@435 1331 if (shift == 0) return t1;
duke@435 1332 // Calculate reasonably aggressive bounds for the result.
duke@435 1333 jlong lo = (julong)r1->_lo >> (juint)shift;
duke@435 1334 jlong hi = (julong)r1->_hi >> (juint)shift;
duke@435 1335 if (r1->_hi >= 0 && r1->_lo < 0) {
duke@435 1336 // If the type has both negative and positive values,
duke@435 1337 // there are two separate sub-domains to worry about:
duke@435 1338 // The positive half and the negative half.
duke@435 1339 jlong neg_lo = lo;
duke@435 1340 jlong neg_hi = (julong)-1 >> (juint)shift;
duke@435 1341 jlong pos_lo = (julong) 0 >> (juint)shift;
duke@435 1342 jlong pos_hi = hi;
duke@435 1343 //lo = MIN2(neg_lo, pos_lo); // == 0
duke@435 1344 lo = neg_lo < pos_lo ? neg_lo : pos_lo;
duke@435 1345 //hi = MAX2(neg_hi, pos_hi); // == -1 >>> shift;
duke@435 1346 hi = neg_hi > pos_hi ? neg_hi : pos_hi;
duke@435 1347 }
duke@435 1348 assert(lo <= hi, "must have valid bounds");
duke@435 1349 const TypeLong* tl = TypeLong::make(lo, hi, MAX2(r1->_widen,r2->_widen));
duke@435 1350 #ifdef ASSERT
duke@435 1351 // Make sure we get the sign-capture idiom correct.
twisti@994 1352 if (shift == BitsPerJavaLong - 1) {
duke@435 1353 if (r1->_lo >= 0) assert(tl == TypeLong::ZERO, ">>>63 of + is 0");
duke@435 1354 if (r1->_hi < 0) assert(tl == TypeLong::ONE, ">>>63 of - is +1");
duke@435 1355 }
duke@435 1356 #endif
duke@435 1357 return tl;
duke@435 1358 }
duke@435 1359
duke@435 1360 return TypeLong::LONG; // Give up
duke@435 1361 }

mercurial