annotate src/hotspot/share/opto/mulnode.cpp @ 53643:7d3cde494494

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