src/share/vm/opto/mulnode.cpp

Thu, 24 May 2018 19:26:50 +0800

author
aoqi
date
Thu, 24 May 2018 19:26:50 +0800
changeset 8862
fd13a567f179
parent 6876
710a3c8b516e
child 9637
eef07cd490d4
permissions
-rw-r--r--

#7046 C2 supports long branch
Contributed-by: fujie

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

mercurial