duke@435: /* mikael@4153: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "memory/allocation.inline.hpp" stefank@2314: #include "opto/addnode.hpp" stefank@2314: #include "opto/connode.hpp" stefank@2314: #include "opto/memnode.hpp" stefank@2314: #include "opto/mulnode.hpp" stefank@2314: #include "opto/phaseX.hpp" stefank@2314: #include "opto/subnode.hpp" stefank@2314: duke@435: // Portions of code courtesy of Clifford Click duke@435: duke@435: duke@435: //============================================================================= duke@435: //------------------------------hash------------------------------------------- duke@435: // Hash function over MulNodes. Needs to be commutative; i.e., I swap duke@435: // (commute) inputs to MulNodes willy-nilly so the hash function must return duke@435: // the same value in the presence of edge swapping. duke@435: uint MulNode::hash() const { duke@435: return (uintptr_t)in(1) + (uintptr_t)in(2) + Opcode(); duke@435: } duke@435: duke@435: //------------------------------Identity--------------------------------------- duke@435: // Multiplying a one preserves the other argument duke@435: Node *MulNode::Identity( PhaseTransform *phase ) { duke@435: register const Type *one = mul_id(); // The multiplicative identity duke@435: if( phase->type( in(1) )->higher_equal( one ) ) return in(2); duke@435: if( phase->type( in(2) )->higher_equal( one ) ) return in(1); duke@435: duke@435: return this; duke@435: } duke@435: duke@435: //------------------------------Ideal------------------------------------------ duke@435: // We also canonicalize the Node, moving constants to the right input, duke@435: // and flatten expressions (so that 1+x+2 becomes x+3). duke@435: Node *MulNode::Ideal(PhaseGVN *phase, bool can_reshape) { duke@435: const Type *t1 = phase->type( in(1) ); duke@435: const Type *t2 = phase->type( in(2) ); duke@435: Node *progress = NULL; // Progress flag duke@435: // We are OK if right is a constant, or right is a load and duke@435: // left is a non-constant. duke@435: if( !(t2->singleton() || duke@435: (in(2)->is_Load() && !(t1->singleton() || in(1)->is_Load())) ) ) { duke@435: if( t1->singleton() || // Left input is a constant? duke@435: // Otherwise, sort inputs (commutativity) to help value numbering. duke@435: (in(1)->_idx > in(2)->_idx) ) { duke@435: swap_edges(1, 2); duke@435: const Type *t = t1; duke@435: t1 = t2; duke@435: t2 = t; duke@435: progress = this; // Made progress duke@435: } duke@435: } duke@435: duke@435: // If the right input is a constant, and the left input is a product of a duke@435: // constant, flatten the expression tree. duke@435: uint op = Opcode(); duke@435: if( t2->singleton() && // Right input is a constant? duke@435: op != Op_MulF && // Float & double cannot reassociate duke@435: op != Op_MulD ) { duke@435: if( t2 == Type::TOP ) return NULL; duke@435: Node *mul1 = in(1); duke@435: #ifdef ASSERT duke@435: // Check for dead loop duke@435: int op1 = mul1->Opcode(); duke@435: if( phase->eqv( mul1, this ) || phase->eqv( in(2), this ) || duke@435: ( op1 == mul_opcode() || op1 == add_opcode() ) && duke@435: ( phase->eqv( mul1->in(1), this ) || phase->eqv( mul1->in(2), this ) || duke@435: phase->eqv( mul1->in(1), mul1 ) || phase->eqv( mul1->in(2), mul1 ) ) ) duke@435: assert(false, "dead loop in MulNode::Ideal"); duke@435: #endif duke@435: duke@435: if( mul1->Opcode() == mul_opcode() ) { // Left input is a multiply? duke@435: // Mul of a constant? duke@435: const Type *t12 = phase->type( mul1->in(2) ); duke@435: if( t12->singleton() && t12 != Type::TOP) { // Left input is an add of a constant? duke@435: // Compute new constant; check for overflow kvn@3040: const Type *tcon01 = ((MulNode*)mul1)->mul_ring(t2,t12); duke@435: if( tcon01->singleton() ) { duke@435: // The Mul of the flattened expression duke@435: set_req(1, mul1->in(1)); duke@435: set_req(2, phase->makecon( tcon01 )); duke@435: t2 = tcon01; duke@435: progress = this; // Made progress duke@435: } duke@435: } duke@435: } duke@435: // If the right input is a constant, and the left input is an add of a duke@435: // constant, flatten the tree: (X+con1)*con0 ==> X*con0 + con1*con0 duke@435: const Node *add1 = in(1); duke@435: if( add1->Opcode() == add_opcode() ) { // Left input is an add? duke@435: // Add of a constant? duke@435: const Type *t12 = phase->type( add1->in(2) ); duke@435: if( t12->singleton() && t12 != Type::TOP ) { // Left input is an add of a constant? duke@435: assert( add1->in(1) != add1, "dead loop in MulNode::Ideal" ); duke@435: // Compute new constant; check for overflow duke@435: const Type *tcon01 = mul_ring(t2,t12); duke@435: if( tcon01->singleton() ) { duke@435: duke@435: // Convert (X+con1)*con0 into X*con0 duke@435: Node *mul = clone(); // mul = ()*con0 duke@435: mul->set_req(1,add1->in(1)); // mul = X*con0 duke@435: mul = phase->transform(mul); duke@435: duke@435: Node *add2 = add1->clone(); duke@435: add2->set_req(1, mul); // X*con0 + con0*con1 duke@435: add2->set_req(2, phase->makecon(tcon01) ); duke@435: progress = add2; duke@435: } duke@435: } duke@435: } // End of is left input an add duke@435: } // End of is right input a Mul duke@435: duke@435: return progress; duke@435: } duke@435: duke@435: //------------------------------Value----------------------------------------- duke@435: const Type *MulNode::Value( PhaseTransform *phase ) const { duke@435: const Type *t1 = phase->type( in(1) ); duke@435: const Type *t2 = phase->type( in(2) ); duke@435: // Either input is TOP ==> the result is TOP duke@435: if( t1 == Type::TOP ) return Type::TOP; duke@435: if( t2 == Type::TOP ) return Type::TOP; duke@435: duke@435: // Either input is ZERO ==> the result is ZERO. duke@435: // Not valid for floats or doubles since +0.0 * -0.0 --> +0.0 duke@435: int op = Opcode(); duke@435: if( op == Op_MulI || op == Op_AndI || op == Op_MulL || op == Op_AndL ) { duke@435: const Type *zero = add_id(); // The multiplicative zero duke@435: if( t1->higher_equal( zero ) ) return zero; duke@435: if( t2->higher_equal( zero ) ) return zero; duke@435: } duke@435: duke@435: // Either input is BOTTOM ==> the result is the local BOTTOM duke@435: if( t1 == Type::BOTTOM || t2 == Type::BOTTOM ) duke@435: return bottom_type(); duke@435: rasbold@839: #if defined(IA32) rasbold@839: // Can't trust native compilers to properly fold strict double rasbold@839: // multiplication with round-to-zero on this platform. rasbold@839: if (op == Op_MulD && phase->C->method()->is_strict()) { rasbold@839: return TypeD::DOUBLE; rasbold@839: } rasbold@839: #endif rasbold@839: duke@435: return mul_ring(t1,t2); // Local flavor of type multiplication duke@435: } duke@435: duke@435: duke@435: //============================================================================= duke@435: //------------------------------Ideal------------------------------------------ duke@435: // Check for power-of-2 multiply, then try the regular MulNode::Ideal duke@435: Node *MulINode::Ideal(PhaseGVN *phase, bool can_reshape) { duke@435: // Swap constant to right duke@435: jint con; duke@435: if ((con = in(1)->find_int_con(0)) != 0) { duke@435: swap_edges(1, 2); duke@435: // Finish rest of method to use info in 'con' duke@435: } else if ((con = in(2)->find_int_con(0)) == 0) { duke@435: return MulNode::Ideal(phase, can_reshape); duke@435: } duke@435: duke@435: // Now we have a constant Node on the right and the constant in con duke@435: if( con == 0 ) return NULL; // By zero is handled by Value call duke@435: if( con == 1 ) return NULL; // By one is handled by Identity call duke@435: duke@435: // Check for negative constant; if so negate the final result duke@435: bool sign_flip = false; duke@435: if( con < 0 ) { duke@435: con = -con; duke@435: sign_flip = true; duke@435: } duke@435: duke@435: // Get low bit; check for being the only bit duke@435: Node *res = NULL; duke@435: jint bit1 = con & -con; // Extract low bit duke@435: if( bit1 == con ) { // Found a power of 2? kvn@4115: res = new (phase->C) LShiftINode( in(1), phase->intcon(log2_intptr(bit1)) ); duke@435: } else { duke@435: duke@435: // Check for constant with 2 bits set duke@435: jint bit2 = con-bit1; duke@435: bit2 = bit2 & -bit2; // Extract 2nd bit duke@435: if( bit2 + bit1 == con ) { // Found all bits in con? kvn@4115: Node *n1 = phase->transform( new (phase->C) LShiftINode( in(1), phase->intcon(log2_intptr(bit1)) ) ); kvn@4115: Node *n2 = phase->transform( new (phase->C) LShiftINode( in(1), phase->intcon(log2_intptr(bit2)) ) ); kvn@4115: res = new (phase->C) AddINode( n2, n1 ); duke@435: duke@435: } else if (is_power_of_2(con+1)) { duke@435: // Sleezy: power-of-2 -1. Next time be generic. duke@435: jint temp = (jint) (con + 1); kvn@4115: Node *n1 = phase->transform( new (phase->C) LShiftINode( in(1), phase->intcon(log2_intptr(temp)) ) ); kvn@4115: res = new (phase->C) SubINode( n1, in(1) ); duke@435: } else { duke@435: return MulNode::Ideal(phase, can_reshape); duke@435: } duke@435: } duke@435: duke@435: if( sign_flip ) { // Need to negate result? duke@435: res = phase->transform(res);// Transform, before making the zero con kvn@4115: res = new (phase->C) SubINode(phase->intcon(0),res); duke@435: } duke@435: duke@435: return res; // Return final result duke@435: } duke@435: duke@435: //------------------------------mul_ring--------------------------------------- duke@435: // Compute the product type of two integer ranges into this node. duke@435: const Type *MulINode::mul_ring(const Type *t0, const Type *t1) const { duke@435: const TypeInt *r0 = t0->is_int(); // Handy access duke@435: const TypeInt *r1 = t1->is_int(); duke@435: duke@435: // Fetch endpoints of all ranges duke@435: int32 lo0 = r0->_lo; duke@435: double a = (double)lo0; duke@435: int32 hi0 = r0->_hi; duke@435: double b = (double)hi0; duke@435: int32 lo1 = r1->_lo; duke@435: double c = (double)lo1; duke@435: int32 hi1 = r1->_hi; duke@435: double d = (double)hi1; duke@435: duke@435: // Compute all endpoints & check for overflow duke@435: int32 A = lo0*lo1; duke@435: if( (double)A != a*c ) return TypeInt::INT; // Overflow? duke@435: int32 B = lo0*hi1; duke@435: if( (double)B != a*d ) return TypeInt::INT; // Overflow? duke@435: int32 C = hi0*lo1; duke@435: if( (double)C != b*c ) return TypeInt::INT; // Overflow? duke@435: int32 D = hi0*hi1; duke@435: if( (double)D != b*d ) return TypeInt::INT; // Overflow? duke@435: duke@435: if( A < B ) { lo0 = A; hi0 = B; } // Sort range endpoints duke@435: else { lo0 = B; hi0 = A; } duke@435: if( C < D ) { duke@435: if( C < lo0 ) lo0 = C; duke@435: if( D > hi0 ) hi0 = D; duke@435: } else { duke@435: if( D < lo0 ) lo0 = D; duke@435: if( C > hi0 ) hi0 = C; duke@435: } duke@435: return TypeInt::make(lo0, hi0, MAX2(r0->_widen,r1->_widen)); duke@435: } duke@435: duke@435: duke@435: //============================================================================= duke@435: //------------------------------Ideal------------------------------------------ duke@435: // Check for power-of-2 multiply, then try the regular MulNode::Ideal duke@435: Node *MulLNode::Ideal(PhaseGVN *phase, bool can_reshape) { duke@435: // Swap constant to right duke@435: jlong con; duke@435: if ((con = in(1)->find_long_con(0)) != 0) { duke@435: swap_edges(1, 2); duke@435: // Finish rest of method to use info in 'con' duke@435: } else if ((con = in(2)->find_long_con(0)) == 0) { duke@435: return MulNode::Ideal(phase, can_reshape); duke@435: } duke@435: duke@435: // Now we have a constant Node on the right and the constant in con duke@435: if( con == CONST64(0) ) return NULL; // By zero is handled by Value call duke@435: if( con == CONST64(1) ) return NULL; // By one is handled by Identity call duke@435: duke@435: // Check for negative constant; if so negate the final result duke@435: bool sign_flip = false; duke@435: if( con < 0 ) { duke@435: con = -con; duke@435: sign_flip = true; duke@435: } duke@435: duke@435: // Get low bit; check for being the only bit duke@435: Node *res = NULL; duke@435: jlong bit1 = con & -con; // Extract low bit duke@435: if( bit1 == con ) { // Found a power of 2? kvn@4115: res = new (phase->C) LShiftLNode( in(1), phase->intcon(log2_long(bit1)) ); duke@435: } else { duke@435: duke@435: // Check for constant with 2 bits set duke@435: jlong bit2 = con-bit1; duke@435: bit2 = bit2 & -bit2; // Extract 2nd bit duke@435: if( bit2 + bit1 == con ) { // Found all bits in con? kvn@4115: Node *n1 = phase->transform( new (phase->C) LShiftLNode( in(1), phase->intcon(log2_long(bit1)) ) ); kvn@4115: Node *n2 = phase->transform( new (phase->C) LShiftLNode( in(1), phase->intcon(log2_long(bit2)) ) ); kvn@4115: res = new (phase->C) AddLNode( n2, n1 ); duke@435: duke@435: } else if (is_power_of_2_long(con+1)) { duke@435: // Sleezy: power-of-2 -1. Next time be generic. duke@435: jlong temp = (jlong) (con + 1); kvn@4115: Node *n1 = phase->transform( new (phase->C) LShiftLNode( in(1), phase->intcon(log2_long(temp)) ) ); kvn@4115: res = new (phase->C) SubLNode( n1, in(1) ); duke@435: } else { duke@435: return MulNode::Ideal(phase, can_reshape); duke@435: } duke@435: } duke@435: duke@435: if( sign_flip ) { // Need to negate result? duke@435: res = phase->transform(res);// Transform, before making the zero con kvn@4115: res = new (phase->C) SubLNode(phase->longcon(0),res); duke@435: } duke@435: duke@435: return res; // Return final result duke@435: } duke@435: duke@435: //------------------------------mul_ring--------------------------------------- duke@435: // Compute the product type of two integer ranges into this node. duke@435: const Type *MulLNode::mul_ring(const Type *t0, const Type *t1) const { duke@435: const TypeLong *r0 = t0->is_long(); // Handy access duke@435: const TypeLong *r1 = t1->is_long(); duke@435: duke@435: // Fetch endpoints of all ranges duke@435: jlong lo0 = r0->_lo; duke@435: double a = (double)lo0; duke@435: jlong hi0 = r0->_hi; duke@435: double b = (double)hi0; duke@435: jlong lo1 = r1->_lo; duke@435: double c = (double)lo1; duke@435: jlong hi1 = r1->_hi; duke@435: double d = (double)hi1; duke@435: duke@435: // Compute all endpoints & check for overflow duke@435: jlong A = lo0*lo1; duke@435: if( (double)A != a*c ) return TypeLong::LONG; // Overflow? duke@435: jlong B = lo0*hi1; duke@435: if( (double)B != a*d ) return TypeLong::LONG; // Overflow? duke@435: jlong C = hi0*lo1; duke@435: if( (double)C != b*c ) return TypeLong::LONG; // Overflow? duke@435: jlong D = hi0*hi1; duke@435: if( (double)D != b*d ) return TypeLong::LONG; // Overflow? duke@435: duke@435: if( A < B ) { lo0 = A; hi0 = B; } // Sort range endpoints duke@435: else { lo0 = B; hi0 = A; } duke@435: if( C < D ) { duke@435: if( C < lo0 ) lo0 = C; duke@435: if( D > hi0 ) hi0 = D; duke@435: } else { duke@435: if( D < lo0 ) lo0 = D; duke@435: if( C > hi0 ) hi0 = C; duke@435: } duke@435: return TypeLong::make(lo0, hi0, MAX2(r0->_widen,r1->_widen)); duke@435: } duke@435: duke@435: //============================================================================= duke@435: //------------------------------mul_ring--------------------------------------- duke@435: // Compute the product type of two double ranges into this node. duke@435: const Type *MulFNode::mul_ring(const Type *t0, const Type *t1) const { duke@435: if( t0 == Type::FLOAT || t1 == Type::FLOAT ) return Type::FLOAT; duke@435: return TypeF::make( t0->getf() * t1->getf() ); duke@435: } duke@435: duke@435: //============================================================================= duke@435: //------------------------------mul_ring--------------------------------------- duke@435: // Compute the product type of two double ranges into this node. duke@435: const Type *MulDNode::mul_ring(const Type *t0, const Type *t1) const { duke@435: if( t0 == Type::DOUBLE || t1 == Type::DOUBLE ) return Type::DOUBLE; rasbold@839: // We must be multiplying 2 double constants. duke@435: return TypeD::make( t0->getd() * t1->getd() ); duke@435: } duke@435: duke@435: //============================================================================= rasbold@580: //------------------------------Value------------------------------------------ rasbold@580: const Type *MulHiLNode::Value( PhaseTransform *phase ) const { rasbold@580: // Either input is TOP ==> the result is TOP rasbold@580: const Type *t1 = phase->type( in(1) ); rasbold@580: const Type *t2 = phase->type( in(2) ); rasbold@580: if( t1 == Type::TOP ) return Type::TOP; rasbold@580: if( t2 == Type::TOP ) return Type::TOP; rasbold@580: rasbold@580: // Either input is BOTTOM ==> the result is the local BOTTOM rasbold@580: const Type *bot = bottom_type(); rasbold@580: if( (t1 == bot) || (t2 == bot) || rasbold@580: (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) ) rasbold@580: return bot; rasbold@580: rasbold@580: // It is not worth trying to constant fold this stuff! rasbold@580: return TypeLong::LONG; rasbold@580: } rasbold@580: rasbold@580: //============================================================================= duke@435: //------------------------------mul_ring--------------------------------------- duke@435: // Supplied function returns the product of the inputs IN THE CURRENT RING. duke@435: // For the logical operations the ring's MUL is really a logical AND function. duke@435: // This also type-checks the inputs for sanity. Guaranteed never to duke@435: // be passed a TOP or BOTTOM type, these are filtered out by pre-check. duke@435: const Type *AndINode::mul_ring( const Type *t0, const Type *t1 ) const { duke@435: const TypeInt *r0 = t0->is_int(); // Handy access duke@435: const TypeInt *r1 = t1->is_int(); duke@435: int widen = MAX2(r0->_widen,r1->_widen); duke@435: duke@435: // If either input is a constant, might be able to trim cases duke@435: if( !r0->is_con() && !r1->is_con() ) duke@435: return TypeInt::INT; // No constants to be had duke@435: duke@435: // Both constants? Return bits duke@435: if( r0->is_con() && r1->is_con() ) duke@435: return TypeInt::make( r0->get_con() & r1->get_con() ); duke@435: duke@435: if( r0->is_con() && r0->get_con() > 0 ) duke@435: return TypeInt::make(0, r0->get_con(), widen); duke@435: duke@435: if( r1->is_con() && r1->get_con() > 0 ) duke@435: return TypeInt::make(0, r1->get_con(), widen); duke@435: duke@435: if( r0 == TypeInt::BOOL || r1 == TypeInt::BOOL ) { duke@435: return TypeInt::BOOL; duke@435: } duke@435: duke@435: return TypeInt::INT; // No constants to be had duke@435: } duke@435: duke@435: //------------------------------Identity--------------------------------------- duke@435: // Masking off the high bits of an unsigned load is not required duke@435: Node *AndINode::Identity( PhaseTransform *phase ) { duke@435: duke@435: // x & x => x duke@435: if (phase->eqv(in(1), in(2))) return in(1); duke@435: twisti@1259: Node* in1 = in(1); twisti@1259: uint op = in1->Opcode(); twisti@1259: const TypeInt* t2 = phase->type(in(2))->isa_int(); twisti@1259: if (t2 && t2->is_con()) { duke@435: int con = t2->get_con(); duke@435: // Masking off high bits which are always zero is useless. duke@435: const TypeInt* t1 = phase->type( in(1) )->isa_int(); duke@435: if (t1 != NULL && t1->_lo >= 0) { twisti@1259: jint t1_support = right_n_bits(1 + log2_intptr(t1->_hi)); duke@435: if ((t1_support & con) == t1_support) twisti@1259: return in1; duke@435: } duke@435: // Masking off the high bits of a unsigned-shift-right is not duke@435: // needed either. twisti@1259: if (op == Op_URShiftI) { twisti@1259: const TypeInt* t12 = phase->type(in1->in(2))->isa_int(); twisti@1259: if (t12 && t12->is_con()) { // Shift is by a constant twisti@994: int shift = t12->get_con(); twisti@994: shift &= BitsPerJavaInteger - 1; // semantics of Java shifts twisti@994: int mask = max_juint >> shift; twisti@1259: if ((mask & con) == mask) // If AND is useless, skip it twisti@1259: return in1; duke@435: } duke@435: } duke@435: } duke@435: return MulNode::Identity(phase); duke@435: } duke@435: duke@435: //------------------------------Ideal------------------------------------------ duke@435: Node *AndINode::Ideal(PhaseGVN *phase, bool can_reshape) { duke@435: // Special case constant AND mask duke@435: const TypeInt *t2 = phase->type( in(2) )->isa_int(); duke@435: if( !t2 || !t2->is_con() ) return MulNode::Ideal(phase, can_reshape); duke@435: const int mask = t2->get_con(); duke@435: Node *load = in(1); duke@435: uint lop = load->Opcode(); duke@435: duke@435: // Masking bits off of a Character? Hi bits are already zero. twisti@993: if( lop == Op_LoadUS && duke@435: (mask & 0xFFFF0000) ) // Can we make a smaller mask? kvn@4115: return new (phase->C) AndINode(load,phase->intcon(mask&0xFFFF)); duke@435: duke@435: // Masking bits off of a Short? Loading a Character does some masking vlivanov@4202: if (can_reshape && vlivanov@4202: load->outcnt() == 1 && load->unique_out() == this) { vlivanov@4202: if (lop == Op_LoadS && (mask & 0xFFFF0000) == 0 ) { vlivanov@4202: Node *ldus = new (phase->C) LoadUSNode(load->in(MemNode::Control), vlivanov@4202: load->in(MemNode::Memory), vlivanov@4202: load->in(MemNode::Address), goetz@6479: load->adr_type(), goetz@6479: TypeInt::CHAR, MemNode::unordered); vlivanov@4202: ldus = phase->transform(ldus); vlivanov@4202: return new (phase->C) AndINode(ldus, phase->intcon(mask & 0xFFFF)); vlivanov@4202: } duke@435: vlivanov@4202: // Masking sign bits off of a Byte? Do an unsigned byte load plus vlivanov@4202: // an and. vlivanov@4202: if (lop == Op_LoadB && (mask & 0xFFFFFF00) == 0) { vlivanov@4202: Node* ldub = new (phase->C) LoadUBNode(load->in(MemNode::Control), vlivanov@4202: load->in(MemNode::Memory), vlivanov@4202: load->in(MemNode::Address), goetz@6479: load->adr_type(), goetz@6479: TypeInt::UBYTE, MemNode::unordered); vlivanov@4202: ldub = phase->transform(ldub); vlivanov@4202: return new (phase->C) AndINode(ldub, phase->intcon(mask)); vlivanov@4202: } duke@435: } duke@435: duke@435: // Masking off sign bits? Dont make them! duke@435: if( lop == Op_RShiftI ) { duke@435: const TypeInt *t12 = phase->type(load->in(2))->isa_int(); duke@435: if( t12 && t12->is_con() ) { // Shift is by a constant duke@435: int shift = t12->get_con(); duke@435: shift &= BitsPerJavaInteger-1; // semantics of Java shifts duke@435: const int sign_bits_mask = ~right_n_bits(BitsPerJavaInteger - shift); duke@435: // If the AND'ing of the 2 masks has no bits, then only original shifted duke@435: // bits survive. NO sign-extension bits survive the maskings. duke@435: if( (sign_bits_mask & mask) == 0 ) { duke@435: // Use zero-fill shift instead kvn@4115: Node *zshift = phase->transform(new (phase->C) URShiftINode(load->in(1),load->in(2))); kvn@4115: return new (phase->C) AndINode( zshift, in(2) ); duke@435: } duke@435: } duke@435: } duke@435: duke@435: // Check for 'negate/and-1', a pattern emitted when someone asks for duke@435: // 'mod 2'. Negate leaves the low order bit unchanged (think: complement duke@435: // plus 1) and the mask is of the low order bit. Skip the negate. duke@435: if( lop == Op_SubI && mask == 1 && load->in(1) && duke@435: phase->type(load->in(1)) == TypeInt::ZERO ) kvn@4115: return new (phase->C) AndINode( load->in(2), in(2) ); duke@435: duke@435: return MulNode::Ideal(phase, can_reshape); duke@435: } duke@435: duke@435: //============================================================================= duke@435: //------------------------------mul_ring--------------------------------------- duke@435: // Supplied function returns the product of the inputs IN THE CURRENT RING. duke@435: // For the logical operations the ring's MUL is really a logical AND function. duke@435: // This also type-checks the inputs for sanity. Guaranteed never to duke@435: // be passed a TOP or BOTTOM type, these are filtered out by pre-check. duke@435: const Type *AndLNode::mul_ring( const Type *t0, const Type *t1 ) const { duke@435: const TypeLong *r0 = t0->is_long(); // Handy access duke@435: const TypeLong *r1 = t1->is_long(); duke@435: int widen = MAX2(r0->_widen,r1->_widen); duke@435: duke@435: // If either input is a constant, might be able to trim cases duke@435: if( !r0->is_con() && !r1->is_con() ) duke@435: return TypeLong::LONG; // No constants to be had duke@435: duke@435: // Both constants? Return bits duke@435: if( r0->is_con() && r1->is_con() ) duke@435: return TypeLong::make( r0->get_con() & r1->get_con() ); duke@435: duke@435: if( r0->is_con() && r0->get_con() > 0 ) duke@435: return TypeLong::make(CONST64(0), r0->get_con(), widen); duke@435: duke@435: if( r1->is_con() && r1->get_con() > 0 ) duke@435: return TypeLong::make(CONST64(0), r1->get_con(), widen); duke@435: duke@435: return TypeLong::LONG; // No constants to be had duke@435: } duke@435: duke@435: //------------------------------Identity--------------------------------------- duke@435: // Masking off the high bits of an unsigned load is not required duke@435: Node *AndLNode::Identity( PhaseTransform *phase ) { duke@435: duke@435: // x & x => x duke@435: if (phase->eqv(in(1), in(2))) return in(1); duke@435: duke@435: Node *usr = in(1); duke@435: const TypeLong *t2 = phase->type( in(2) )->isa_long(); duke@435: if( t2 && t2->is_con() ) { duke@435: jlong con = t2->get_con(); duke@435: // Masking off high bits which are always zero is useless. duke@435: const TypeLong* t1 = phase->type( in(1) )->isa_long(); duke@435: if (t1 != NULL && t1->_lo >= 0) { duke@435: jlong t1_support = ((jlong)1 << (1 + log2_long(t1->_hi))) - 1; duke@435: if ((t1_support & con) == t1_support) duke@435: return usr; duke@435: } duke@435: uint lop = usr->Opcode(); duke@435: // Masking off the high bits of a unsigned-shift-right is not duke@435: // needed either. duke@435: if( lop == Op_URShiftL ) { duke@435: const TypeInt *t12 = phase->type( usr->in(2) )->isa_int(); twisti@994: if( t12 && t12->is_con() ) { // Shift is by a constant twisti@994: int shift = t12->get_con(); twisti@994: shift &= BitsPerJavaLong - 1; // semantics of Java shifts twisti@994: jlong mask = max_julong >> shift; duke@435: if( (mask&con) == mask ) // If AND is useless, skip it duke@435: return usr; duke@435: } duke@435: } duke@435: } duke@435: return MulNode::Identity(phase); duke@435: } duke@435: duke@435: //------------------------------Ideal------------------------------------------ duke@435: Node *AndLNode::Ideal(PhaseGVN *phase, bool can_reshape) { duke@435: // Special case constant AND mask duke@435: const TypeLong *t2 = phase->type( in(2) )->isa_long(); duke@435: if( !t2 || !t2->is_con() ) return MulNode::Ideal(phase, can_reshape); duke@435: const jlong mask = t2->get_con(); duke@435: twisti@1059: Node* in1 = in(1); twisti@1059: uint op = in1->Opcode(); twisti@1059: twisti@1259: // Are we masking a long that was converted from an int with a mask twisti@1332: // that fits in 32-bits? Commute them and use an AndINode. Don't twisti@1332: // convert masks which would cause a sign extension of the integer twisti@1332: // value. This check includes UI2L masks (0x00000000FFFFFFFF) which twisti@1332: // would be optimized away later in Identity. twisti@1332: if (op == Op_ConvI2L && (mask & CONST64(0xFFFFFFFF80000000)) == 0) { kvn@4115: Node* andi = new (phase->C) AndINode(in1->in(1), phase->intcon(mask)); twisti@1332: andi = phase->transform(andi); kvn@4115: return new (phase->C) ConvI2LNode(andi); twisti@1259: } twisti@1259: duke@435: // Masking off sign bits? Dont make them! twisti@1059: if (op == Op_RShiftL) { twisti@1259: const TypeInt* t12 = phase->type(in1->in(2))->isa_int(); duke@435: if( t12 && t12->is_con() ) { // Shift is by a constant duke@435: int shift = t12->get_con(); twisti@994: shift &= BitsPerJavaLong - 1; // semantics of Java shifts twisti@994: const jlong sign_bits_mask = ~(((jlong)CONST64(1) << (jlong)(BitsPerJavaLong - shift)) -1); duke@435: // If the AND'ing of the 2 masks has no bits, then only original shifted duke@435: // bits survive. NO sign-extension bits survive the maskings. duke@435: if( (sign_bits_mask & mask) == 0 ) { duke@435: // Use zero-fill shift instead kvn@4115: Node *zshift = phase->transform(new (phase->C) URShiftLNode(in1->in(1), in1->in(2))); kvn@4115: return new (phase->C) AndLNode(zshift, in(2)); duke@435: } duke@435: } duke@435: } duke@435: duke@435: return MulNode::Ideal(phase, can_reshape); duke@435: } duke@435: duke@435: //============================================================================= duke@435: //------------------------------Identity--------------------------------------- duke@435: Node *LShiftINode::Identity( PhaseTransform *phase ) { duke@435: const TypeInt *ti = phase->type( in(2) )->isa_int(); // shift count is an int duke@435: return ( ti && ti->is_con() && ( ti->get_con() & ( BitsPerInt - 1 ) ) == 0 ) ? in(1) : this; duke@435: } duke@435: duke@435: //------------------------------Ideal------------------------------------------ duke@435: // If the right input is a constant, and the left input is an add of a duke@435: // constant, flatten the tree: (X+con1)< X<type( in(2) ); duke@435: if( t == Type::TOP ) return NULL; // Right input is dead duke@435: const TypeInt *t2 = t->isa_int(); duke@435: if( !t2 || !t2->is_con() ) return NULL; // Right input is a constant duke@435: const int con = t2->get_con() & ( BitsPerInt - 1 ); // masked shift count duke@435: duke@435: if ( con == 0 ) return NULL; // let Identity() handle 0 shift count duke@435: duke@435: // Left input is an add of a constant? duke@435: Node *add1 = in(1); duke@435: int add1_op = add1->Opcode(); duke@435: if( add1_op == Op_AddI ) { // Left input is an add? duke@435: assert( add1 != add1->in(1), "dead loop in LShiftINode::Ideal" ); duke@435: const TypeInt *t12 = phase->type(add1->in(2))->isa_int(); duke@435: if( t12 && t12->is_con() ){ // Left input is an add of a con? duke@435: // Transform is legal, but check for profit. Avoid breaking 'i2s' duke@435: // and 'i2b' patterns which typically fold into 'StoreC/StoreB'. duke@435: if( con < 16 ) { duke@435: // Compute X << con0 kvn@4115: Node *lsh = phase->transform( new (phase->C) LShiftINode( add1->in(1), in(2) ) ); duke@435: // Compute X<C) AddINode( lsh, phase->intcon(t12->get_con() << con)); duke@435: } duke@435: } duke@435: } duke@435: duke@435: // Check for "(x>>c0)<in(2) == in(2) ) duke@435: // Convert to "(x & -(1<C) AndINode(add1->in(1),phase->intcon( -(1<>c0) & Y)<in(1); duke@435: int add2_op = add2->Opcode(); duke@435: if( (add2_op == Op_RShiftI || add2_op == Op_URShiftI ) && duke@435: add2->in(2) == in(2) ) { duke@435: // Convert to "(x & (Y<transform( new (phase->C) LShiftINode( add1->in(2), in(2) ) ); kvn@4115: return new (phase->C) AndINode( add2->in(1), y_sh ); duke@435: } duke@435: } duke@435: duke@435: // Check for ((x & ((1<<(32-c0))-1)) << c0) which ANDs off high bits duke@435: // before shifting them away. duke@435: const jint bits_mask = right_n_bits(BitsPerJavaInteger-con); duke@435: if( add1_op == Op_AndI && duke@435: phase->type(add1->in(2)) == TypeInt::make( bits_mask ) ) kvn@4115: return new (phase->C) LShiftINode( add1->in(1), in(2) ); duke@435: duke@435: return NULL; duke@435: } duke@435: duke@435: //------------------------------Value------------------------------------------ duke@435: // A LShiftINode shifts its input2 left by input1 amount. duke@435: const Type *LShiftINode::Value( PhaseTransform *phase ) const { duke@435: const Type *t1 = phase->type( in(1) ); duke@435: const Type *t2 = phase->type( in(2) ); duke@435: // Either input is TOP ==> the result is TOP duke@435: if( t1 == Type::TOP ) return Type::TOP; duke@435: if( t2 == Type::TOP ) return Type::TOP; duke@435: duke@435: // Left input is ZERO ==> the result is ZERO. duke@435: if( t1 == TypeInt::ZERO ) return TypeInt::ZERO; duke@435: // Shift by zero does nothing duke@435: if( t2 == TypeInt::ZERO ) return t1; duke@435: duke@435: // Either input is BOTTOM ==> the result is BOTTOM duke@435: if( (t1 == TypeInt::INT) || (t2 == TypeInt::INT) || duke@435: (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) ) duke@435: return TypeInt::INT; duke@435: duke@435: const TypeInt *r1 = t1->is_int(); // Handy access duke@435: const TypeInt *r2 = t2->is_int(); // Handy access duke@435: duke@435: if (!r2->is_con()) duke@435: return TypeInt::INT; duke@435: duke@435: uint shift = r2->get_con(); duke@435: shift &= BitsPerJavaInteger-1; // semantics of Java shifts duke@435: // Shift by a multiple of 32 does nothing: duke@435: if (shift == 0) return t1; duke@435: duke@435: // If the shift is a constant, shift the bounds of the type, duke@435: // unless this could lead to an overflow. duke@435: if (!r1->is_con()) { duke@435: jint lo = r1->_lo, hi = r1->_hi; duke@435: if (((lo << shift) >> shift) == lo && duke@435: ((hi << shift) >> shift) == hi) { duke@435: // No overflow. The range shifts up cleanly. duke@435: return TypeInt::make((jint)lo << (jint)shift, duke@435: (jint)hi << (jint)shift, duke@435: MAX2(r1->_widen,r2->_widen)); duke@435: } duke@435: return TypeInt::INT; duke@435: } duke@435: duke@435: return TypeInt::make( (jint)r1->get_con() << (jint)shift ); duke@435: } duke@435: duke@435: //============================================================================= duke@435: //------------------------------Identity--------------------------------------- duke@435: Node *LShiftLNode::Identity( PhaseTransform *phase ) { duke@435: const TypeInt *ti = phase->type( in(2) )->isa_int(); // shift count is an int duke@435: return ( ti && ti->is_con() && ( ti->get_con() & ( BitsPerLong - 1 ) ) == 0 ) ? in(1) : this; duke@435: } duke@435: duke@435: //------------------------------Ideal------------------------------------------ duke@435: // If the right input is a constant, and the left input is an add of a duke@435: // constant, flatten the tree: (X+con1)< X<type( in(2) ); duke@435: if( t == Type::TOP ) return NULL; // Right input is dead duke@435: const TypeInt *t2 = t->isa_int(); duke@435: if( !t2 || !t2->is_con() ) return NULL; // Right input is a constant duke@435: const int con = t2->get_con() & ( BitsPerLong - 1 ); // masked shift count duke@435: duke@435: if ( con == 0 ) return NULL; // let Identity() handle 0 shift count duke@435: duke@435: // Left input is an add of a constant? duke@435: Node *add1 = in(1); duke@435: int add1_op = add1->Opcode(); duke@435: if( add1_op == Op_AddL ) { // Left input is an add? duke@435: // Avoid dead data cycles from dead loops duke@435: assert( add1 != add1->in(1), "dead loop in LShiftLNode::Ideal" ); duke@435: const TypeLong *t12 = phase->type(add1->in(2))->isa_long(); duke@435: if( t12 && t12->is_con() ){ // Left input is an add of a con? duke@435: // Compute X << con0 kvn@4115: Node *lsh = phase->transform( new (phase->C) LShiftLNode( add1->in(1), in(2) ) ); duke@435: // Compute X<C) AddLNode( lsh, phase->longcon(t12->get_con() << con)); duke@435: } duke@435: } duke@435: duke@435: // Check for "(x>>c0)<in(2) == in(2) ) duke@435: // Convert to "(x & -(1<C) AndLNode(add1->in(1),phase->longcon( -(CONST64(1)<>c0) & Y)<in(1); duke@435: int add2_op = add2->Opcode(); duke@435: if( (add2_op == Op_RShiftL || add2_op == Op_URShiftL ) && duke@435: add2->in(2) == in(2) ) { duke@435: // Convert to "(x & (Y<transform( new (phase->C) LShiftLNode( add1->in(2), in(2) ) ); kvn@4115: return new (phase->C) AndLNode( add2->in(1), y_sh ); duke@435: } duke@435: } duke@435: duke@435: // Check for ((x & ((CONST64(1)<<(64-c0))-1)) << c0) which ANDs off high bits duke@435: // before shifting them away. twisti@994: const jlong bits_mask = ((jlong)CONST64(1) << (jlong)(BitsPerJavaLong - con)) - CONST64(1); duke@435: if( add1_op == Op_AndL && duke@435: phase->type(add1->in(2)) == TypeLong::make( bits_mask ) ) kvn@4115: return new (phase->C) LShiftLNode( add1->in(1), in(2) ); duke@435: duke@435: return NULL; duke@435: } duke@435: duke@435: //------------------------------Value------------------------------------------ duke@435: // A LShiftLNode shifts its input2 left by input1 amount. duke@435: const Type *LShiftLNode::Value( PhaseTransform *phase ) const { duke@435: const Type *t1 = phase->type( in(1) ); duke@435: const Type *t2 = phase->type( in(2) ); duke@435: // Either input is TOP ==> the result is TOP duke@435: if( t1 == Type::TOP ) return Type::TOP; duke@435: if( t2 == Type::TOP ) return Type::TOP; duke@435: duke@435: // Left input is ZERO ==> the result is ZERO. duke@435: if( t1 == TypeLong::ZERO ) return TypeLong::ZERO; duke@435: // Shift by zero does nothing duke@435: if( t2 == TypeInt::ZERO ) return t1; duke@435: duke@435: // Either input is BOTTOM ==> the result is BOTTOM duke@435: if( (t1 == TypeLong::LONG) || (t2 == TypeInt::INT) || duke@435: (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) ) duke@435: return TypeLong::LONG; duke@435: duke@435: const TypeLong *r1 = t1->is_long(); // Handy access duke@435: const TypeInt *r2 = t2->is_int(); // Handy access duke@435: duke@435: if (!r2->is_con()) duke@435: return TypeLong::LONG; duke@435: duke@435: uint shift = r2->get_con(); twisti@994: shift &= BitsPerJavaLong - 1; // semantics of Java shifts duke@435: // Shift by a multiple of 64 does nothing: duke@435: if (shift == 0) return t1; duke@435: duke@435: // If the shift is a constant, shift the bounds of the type, duke@435: // unless this could lead to an overflow. duke@435: if (!r1->is_con()) { duke@435: jlong lo = r1->_lo, hi = r1->_hi; duke@435: if (((lo << shift) >> shift) == lo && duke@435: ((hi << shift) >> shift) == hi) { duke@435: // No overflow. The range shifts up cleanly. duke@435: return TypeLong::make((jlong)lo << (jint)shift, duke@435: (jlong)hi << (jint)shift, duke@435: MAX2(r1->_widen,r2->_widen)); duke@435: } duke@435: return TypeLong::LONG; duke@435: } duke@435: duke@435: return TypeLong::make( (jlong)r1->get_con() << (jint)shift ); duke@435: } duke@435: duke@435: //============================================================================= duke@435: //------------------------------Identity--------------------------------------- duke@435: Node *RShiftINode::Identity( PhaseTransform *phase ) { duke@435: const TypeInt *t2 = phase->type(in(2))->isa_int(); duke@435: if( !t2 ) return this; duke@435: if ( t2->is_con() && ( t2->get_con() & ( BitsPerInt - 1 ) ) == 0 ) duke@435: return in(1); duke@435: duke@435: // Check for useless sign-masking duke@435: if( in(1)->Opcode() == Op_LShiftI && duke@435: in(1)->req() == 3 && duke@435: in(1)->in(2) == in(2) && duke@435: t2->is_con() ) { duke@435: uint shift = t2->get_con(); duke@435: shift &= BitsPerJavaInteger-1; // semantics of Java shifts duke@435: // Compute masks for which this shifting doesn't change duke@435: int lo = (-1 << (BitsPerJavaInteger - shift-1)); // FFFF8000 duke@435: int hi = ~lo; // 00007FFF duke@435: const TypeInt *t11 = phase->type(in(1)->in(1))->isa_int(); duke@435: if( !t11 ) return this; duke@435: // Does actual value fit inside of mask? duke@435: if( lo <= t11->_lo && t11->_hi <= hi ) duke@435: return in(1)->in(1); // Then shifting is a nop duke@435: } duke@435: duke@435: return this; duke@435: } duke@435: duke@435: //------------------------------Ideal------------------------------------------ duke@435: Node *RShiftINode::Ideal(PhaseGVN *phase, bool can_reshape) { duke@435: // Inputs may be TOP if they are dead. duke@435: const TypeInt *t1 = phase->type( in(1) )->isa_int(); duke@435: if( !t1 ) return NULL; // Left input is an integer duke@435: const TypeInt *t2 = phase->type( in(2) )->isa_int(); duke@435: if( !t2 || !t2->is_con() ) return NULL; // Right input is a constant duke@435: const TypeInt *t3; // type of in(1).in(2) duke@435: int shift = t2->get_con(); duke@435: shift &= BitsPerJavaInteger-1; // semantics of Java shifts duke@435: duke@435: if ( shift == 0 ) return NULL; // let Identity() handle 0 shift count duke@435: duke@435: // Check for (x & 0xFF000000) >> 24, whose mask can be made smaller. duke@435: // Such expressions arise normally from shift chains like (byte)(x >> 24). duke@435: const Node *mask = in(1); duke@435: if( mask->Opcode() == Op_AndI && duke@435: (t3 = phase->type(mask->in(2))->isa_int()) && duke@435: t3->is_con() ) { duke@435: Node *x = mask->in(1); duke@435: jint maskbits = t3->get_con(); duke@435: // Convert to "(x >> shift) & (mask >> shift)" kvn@4115: Node *shr_nomask = phase->transform( new (phase->C) RShiftINode(mask->in(1), in(2)) ); kvn@4115: return new (phase->C) AndINode(shr_nomask, phase->intcon( maskbits >> shift)); duke@435: } duke@435: duke@435: // Check for "(short[i] <<16)>>16" which simply sign-extends duke@435: const Node *shl = in(1); duke@435: if( shl->Opcode() != Op_LShiftI ) return NULL; duke@435: duke@435: if( shift == 16 && duke@435: (t3 = phase->type(shl->in(2))->isa_int()) && duke@435: t3->is_con(16) ) { duke@435: Node *ld = shl->in(1); duke@435: if( ld->Opcode() == Op_LoadS ) { duke@435: // Sign extension is just useless here. Return a RShiftI of zero instead duke@435: // returning 'ld' directly. We cannot return an old Node directly as duke@435: // that is the job of 'Identity' calls and Identity calls only work on duke@435: // direct inputs ('ld' is an extra Node removed from 'this'). The duke@435: // combined optimization requires Identity only return direct inputs. duke@435: set_req(1, ld); duke@435: set_req(2, phase->intcon(0)); duke@435: return this; duke@435: } vlivanov@4202: else if( can_reshape && vlivanov@4202: ld->Opcode() == Op_LoadUS && vlivanov@4202: ld->outcnt() == 1 && ld->unique_out() == shl) duke@435: // Replace zero-extension-load with sign-extension-load kvn@4115: return new (phase->C) LoadSNode( ld->in(MemNode::Control), goetz@6479: ld->in(MemNode::Memory), goetz@6479: ld->in(MemNode::Address), goetz@6479: ld->adr_type(), TypeInt::SHORT, goetz@6479: MemNode::unordered); duke@435: } duke@435: duke@435: // Check for "(byte[i] <<24)>>24" which simply sign-extends duke@435: if( shift == 24 && duke@435: (t3 = phase->type(shl->in(2))->isa_int()) && duke@435: t3->is_con(24) ) { duke@435: Node *ld = shl->in(1); duke@435: if( ld->Opcode() == Op_LoadB ) { duke@435: // Sign extension is just useless here duke@435: set_req(1, ld); duke@435: set_req(2, phase->intcon(0)); duke@435: return this; duke@435: } duke@435: } duke@435: duke@435: return NULL; duke@435: } duke@435: duke@435: //------------------------------Value------------------------------------------ duke@435: // A RShiftINode shifts its input2 right by input1 amount. duke@435: const Type *RShiftINode::Value( PhaseTransform *phase ) const { duke@435: const Type *t1 = phase->type( in(1) ); duke@435: const Type *t2 = phase->type( in(2) ); duke@435: // Either input is TOP ==> the result is TOP duke@435: if( t1 == Type::TOP ) return Type::TOP; duke@435: if( t2 == Type::TOP ) return Type::TOP; duke@435: duke@435: // Left input is ZERO ==> the result is ZERO. duke@435: if( t1 == TypeInt::ZERO ) return TypeInt::ZERO; duke@435: // Shift by zero does nothing duke@435: if( t2 == TypeInt::ZERO ) return t1; duke@435: duke@435: // Either input is BOTTOM ==> the result is BOTTOM duke@435: if (t1 == Type::BOTTOM || t2 == Type::BOTTOM) duke@435: return TypeInt::INT; duke@435: duke@435: if (t2 == TypeInt::INT) duke@435: return TypeInt::INT; duke@435: duke@435: const TypeInt *r1 = t1->is_int(); // Handy access duke@435: const TypeInt *r2 = t2->is_int(); // Handy access duke@435: duke@435: // If the shift is a constant, just shift the bounds of the type. duke@435: // For example, if the shift is 31, we just propagate sign bits. duke@435: if (r2->is_con()) { duke@435: uint shift = r2->get_con(); duke@435: shift &= BitsPerJavaInteger-1; // semantics of Java shifts duke@435: // Shift by a multiple of 32 does nothing: duke@435: if (shift == 0) return t1; duke@435: // Calculate reasonably aggressive bounds for the result. duke@435: // This is necessary if we are to correctly type things duke@435: // like (x<<24>>24) == ((byte)x). duke@435: jint lo = (jint)r1->_lo >> (jint)shift; duke@435: jint hi = (jint)r1->_hi >> (jint)shift; duke@435: assert(lo <= hi, "must have valid bounds"); duke@435: const TypeInt* ti = TypeInt::make(lo, hi, MAX2(r1->_widen,r2->_widen)); duke@435: #ifdef ASSERT duke@435: // Make sure we get the sign-capture idiom correct. duke@435: if (shift == BitsPerJavaInteger-1) { duke@435: if (r1->_lo >= 0) assert(ti == TypeInt::ZERO, ">>31 of + is 0"); duke@435: if (r1->_hi < 0) assert(ti == TypeInt::MINUS_1, ">>31 of - is -1"); duke@435: } duke@435: #endif duke@435: return ti; duke@435: } duke@435: duke@435: if( !r1->is_con() || !r2->is_con() ) duke@435: return TypeInt::INT; duke@435: duke@435: // Signed shift right duke@435: return TypeInt::make( r1->get_con() >> (r2->get_con()&31) ); duke@435: } duke@435: duke@435: //============================================================================= duke@435: //------------------------------Identity--------------------------------------- duke@435: Node *RShiftLNode::Identity( PhaseTransform *phase ) { duke@435: const TypeInt *ti = phase->type( in(2) )->isa_int(); // shift count is an int duke@435: return ( ti && ti->is_con() && ( ti->get_con() & ( BitsPerLong - 1 ) ) == 0 ) ? in(1) : this; duke@435: } duke@435: duke@435: //------------------------------Value------------------------------------------ duke@435: // A RShiftLNode shifts its input2 right by input1 amount. duke@435: const Type *RShiftLNode::Value( PhaseTransform *phase ) const { duke@435: const Type *t1 = phase->type( in(1) ); duke@435: const Type *t2 = phase->type( in(2) ); duke@435: // Either input is TOP ==> the result is TOP duke@435: if( t1 == Type::TOP ) return Type::TOP; duke@435: if( t2 == Type::TOP ) return Type::TOP; duke@435: duke@435: // Left input is ZERO ==> the result is ZERO. duke@435: if( t1 == TypeLong::ZERO ) return TypeLong::ZERO; duke@435: // Shift by zero does nothing duke@435: if( t2 == TypeInt::ZERO ) return t1; duke@435: duke@435: // Either input is BOTTOM ==> the result is BOTTOM duke@435: if (t1 == Type::BOTTOM || t2 == Type::BOTTOM) duke@435: return TypeLong::LONG; duke@435: duke@435: if (t2 == TypeInt::INT) duke@435: return TypeLong::LONG; duke@435: duke@435: const TypeLong *r1 = t1->is_long(); // Handy access duke@435: const TypeInt *r2 = t2->is_int (); // Handy access duke@435: duke@435: // If the shift is a constant, just shift the bounds of the type. duke@435: // For example, if the shift is 63, we just propagate sign bits. duke@435: if (r2->is_con()) { duke@435: uint shift = r2->get_con(); duke@435: shift &= (2*BitsPerJavaInteger)-1; // semantics of Java shifts duke@435: // Shift by a multiple of 64 does nothing: duke@435: if (shift == 0) return t1; duke@435: // Calculate reasonably aggressive bounds for the result. duke@435: // This is necessary if we are to correctly type things duke@435: // like (x<<24>>24) == ((byte)x). duke@435: jlong lo = (jlong)r1->_lo >> (jlong)shift; duke@435: jlong hi = (jlong)r1->_hi >> (jlong)shift; duke@435: assert(lo <= hi, "must have valid bounds"); duke@435: const TypeLong* tl = TypeLong::make(lo, hi, MAX2(r1->_widen,r2->_widen)); duke@435: #ifdef ASSERT duke@435: // Make sure we get the sign-capture idiom correct. duke@435: if (shift == (2*BitsPerJavaInteger)-1) { duke@435: if (r1->_lo >= 0) assert(tl == TypeLong::ZERO, ">>63 of + is 0"); duke@435: if (r1->_hi < 0) assert(tl == TypeLong::MINUS_1, ">>63 of - is -1"); duke@435: } duke@435: #endif duke@435: return tl; duke@435: } duke@435: duke@435: return TypeLong::LONG; // Give up duke@435: } duke@435: duke@435: //============================================================================= duke@435: //------------------------------Identity--------------------------------------- duke@435: Node *URShiftINode::Identity( PhaseTransform *phase ) { duke@435: const TypeInt *ti = phase->type( in(2) )->isa_int(); duke@435: if ( ti && ti->is_con() && ( ti->get_con() & ( BitsPerInt - 1 ) ) == 0 ) return in(1); duke@435: duke@435: // Check for "((x << LogBytesPerWord) + (wordSize-1)) >> LogBytesPerWord" which is just "x". duke@435: // Happens during new-array length computation. duke@435: // Safe if 'x' is in the range [0..(max_int>>LogBytesPerWord)] duke@435: Node *add = in(1); duke@435: if( add->Opcode() == Op_AddI ) { duke@435: const TypeInt *t2 = phase->type(add->in(2))->isa_int(); duke@435: if( t2 && t2->is_con(wordSize - 1) && duke@435: add->in(1)->Opcode() == Op_LShiftI ) { duke@435: // Check that shift_counts are LogBytesPerWord duke@435: Node *lshift_count = add->in(1)->in(2); duke@435: const TypeInt *t_lshift_count = phase->type(lshift_count)->isa_int(); duke@435: if( t_lshift_count && t_lshift_count->is_con(LogBytesPerWord) && duke@435: t_lshift_count == phase->type(in(2)) ) { duke@435: Node *x = add->in(1)->in(1); duke@435: const TypeInt *t_x = phase->type(x)->isa_int(); duke@435: if( t_x != NULL && 0 <= t_x->_lo && t_x->_hi <= (max_jint>>LogBytesPerWord) ) { duke@435: return x; duke@435: } duke@435: } duke@435: } duke@435: } duke@435: duke@435: return (phase->type(in(2))->higher_equal(TypeInt::ZERO)) ? in(1) : this; duke@435: } duke@435: duke@435: //------------------------------Ideal------------------------------------------ duke@435: Node *URShiftINode::Ideal(PhaseGVN *phase, bool can_reshape) { duke@435: const TypeInt *t2 = phase->type( in(2) )->isa_int(); duke@435: if( !t2 || !t2->is_con() ) return NULL; // Right input is a constant duke@435: const int con = t2->get_con() & 31; // Shift count is always masked duke@435: if ( con == 0 ) return NULL; // let Identity() handle a 0 shift count duke@435: // We'll be wanting the right-shift amount as a mask of that many bits duke@435: const int mask = right_n_bits(BitsPerJavaInteger - con); duke@435: duke@435: int in1_op = in(1)->Opcode(); duke@435: duke@435: // Check for ((x>>>a)>>>b) and replace with (x>>>(a+b)) when a+b < 32 duke@435: if( in1_op == Op_URShiftI ) { duke@435: const TypeInt *t12 = phase->type( in(1)->in(2) )->isa_int(); duke@435: if( t12 && t12->is_con() ) { // Right input is a constant duke@435: assert( in(1) != in(1)->in(1), "dead loop in URShiftINode::Ideal" ); duke@435: const int con2 = t12->get_con() & 31; // Shift count is always masked duke@435: const int con3 = con+con2; duke@435: if( con3 < 32 ) // Only merge shifts if total is < 32 kvn@4115: return new (phase->C) URShiftINode( in(1)->in(1), phase->intcon(con3) ); duke@435: } duke@435: } duke@435: duke@435: // Check for ((x << z) + Y) >>> z. Replace with x + con>>>z duke@435: // The idiom for rounding to a power of 2 is "(Q+(2^z-1)) >>> z". duke@435: // If Q is "X << z" the rounding is useless. Look for patterns like duke@435: // ((X<>> Z and replace with (X + Y>>>Z) & Z-mask. duke@435: Node *add = in(1); duke@435: if( in1_op == Op_AddI ) { duke@435: Node *lshl = add->in(1); duke@435: if( lshl->Opcode() == Op_LShiftI && duke@435: phase->type(lshl->in(2)) == t2 ) { kvn@4115: Node *y_z = phase->transform( new (phase->C) URShiftINode(add->in(2),in(2)) ); kvn@4115: Node *sum = phase->transform( new (phase->C) AddINode( lshl->in(1), y_z ) ); kvn@4115: return new (phase->C) AndINode( sum, phase->intcon(mask) ); duke@435: } duke@435: } duke@435: duke@435: // Check for (x & mask) >>> z. Replace with (x >>> z) & (mask >>> z) duke@435: // This shortens the mask. Also, if we are extracting a high byte and duke@435: // storing it to a buffer, the mask will be removed completely. duke@435: Node *andi = in(1); duke@435: if( in1_op == Op_AndI ) { duke@435: const TypeInt *t3 = phase->type( andi->in(2) )->isa_int(); duke@435: if( t3 && t3->is_con() ) { // Right input is a constant duke@435: jint mask2 = t3->get_con(); duke@435: mask2 >>= con; // *signed* shift downward (high-order zeroes do not help) kvn@4115: Node *newshr = phase->transform( new (phase->C) URShiftINode(andi->in(1), in(2)) ); kvn@4115: return new (phase->C) AndINode(newshr, phase->intcon(mask2)); duke@435: // The negative values are easier to materialize than positive ones. duke@435: // A typical case from address arithmetic is ((x & ~15) >> 4). duke@435: // It's better to change that to ((x >> 4) & ~0) versus duke@435: // ((x >> 4) & 0x0FFFFFFF). The difference is greatest in LP64. duke@435: } duke@435: } duke@435: duke@435: // Check for "(X << z ) >>> z" which simply zero-extends duke@435: Node *shl = in(1); duke@435: if( in1_op == Op_LShiftI && duke@435: phase->type(shl->in(2)) == t2 ) kvn@4115: return new (phase->C) AndINode( shl->in(1), phase->intcon(mask) ); duke@435: duke@435: return NULL; duke@435: } duke@435: duke@435: //------------------------------Value------------------------------------------ duke@435: // A URShiftINode shifts its input2 right by input1 amount. duke@435: const Type *URShiftINode::Value( PhaseTransform *phase ) const { duke@435: // (This is a near clone of RShiftINode::Value.) duke@435: const Type *t1 = phase->type( in(1) ); duke@435: const Type *t2 = phase->type( in(2) ); duke@435: // Either input is TOP ==> the result is TOP duke@435: if( t1 == Type::TOP ) return Type::TOP; duke@435: if( t2 == Type::TOP ) return Type::TOP; duke@435: duke@435: // Left input is ZERO ==> the result is ZERO. duke@435: if( t1 == TypeInt::ZERO ) return TypeInt::ZERO; duke@435: // Shift by zero does nothing duke@435: if( t2 == TypeInt::ZERO ) return t1; duke@435: duke@435: // Either input is BOTTOM ==> the result is BOTTOM duke@435: if (t1 == Type::BOTTOM || t2 == Type::BOTTOM) duke@435: return TypeInt::INT; duke@435: duke@435: if (t2 == TypeInt::INT) duke@435: return TypeInt::INT; duke@435: duke@435: const TypeInt *r1 = t1->is_int(); // Handy access duke@435: const TypeInt *r2 = t2->is_int(); // Handy access duke@435: duke@435: if (r2->is_con()) { duke@435: uint shift = r2->get_con(); duke@435: shift &= BitsPerJavaInteger-1; // semantics of Java shifts duke@435: // Shift by a multiple of 32 does nothing: duke@435: if (shift == 0) return t1; duke@435: // Calculate reasonably aggressive bounds for the result. duke@435: jint lo = (juint)r1->_lo >> (juint)shift; duke@435: jint hi = (juint)r1->_hi >> (juint)shift; duke@435: if (r1->_hi >= 0 && r1->_lo < 0) { duke@435: // If the type has both negative and positive values, duke@435: // there are two separate sub-domains to worry about: duke@435: // The positive half and the negative half. duke@435: jint neg_lo = lo; duke@435: jint neg_hi = (juint)-1 >> (juint)shift; duke@435: jint pos_lo = (juint) 0 >> (juint)shift; duke@435: jint pos_hi = hi; duke@435: lo = MIN2(neg_lo, pos_lo); // == 0 duke@435: hi = MAX2(neg_hi, pos_hi); // == -1 >>> shift; duke@435: } duke@435: assert(lo <= hi, "must have valid bounds"); duke@435: const TypeInt* ti = TypeInt::make(lo, hi, MAX2(r1->_widen,r2->_widen)); duke@435: #ifdef ASSERT duke@435: // Make sure we get the sign-capture idiom correct. duke@435: if (shift == BitsPerJavaInteger-1) { duke@435: if (r1->_lo >= 0) assert(ti == TypeInt::ZERO, ">>>31 of + is 0"); duke@435: if (r1->_hi < 0) assert(ti == TypeInt::ONE, ">>>31 of - is +1"); duke@435: } duke@435: #endif duke@435: return ti; duke@435: } duke@435: duke@435: // duke@435: // Do not support shifted oops in info for GC duke@435: // duke@435: // else if( t1->base() == Type::InstPtr ) { duke@435: // duke@435: // const TypeInstPtr *o = t1->is_instptr(); duke@435: // if( t1->singleton() ) duke@435: // return TypeInt::make( ((uint32)o->const_oop() + o->_offset) >> shift ); duke@435: // } duke@435: // else if( t1->base() == Type::KlassPtr ) { duke@435: // const TypeKlassPtr *o = t1->is_klassptr(); duke@435: // if( t1->singleton() ) duke@435: // return TypeInt::make( ((uint32)o->const_oop() + o->_offset) >> shift ); duke@435: // } duke@435: duke@435: return TypeInt::INT; duke@435: } duke@435: duke@435: //============================================================================= duke@435: //------------------------------Identity--------------------------------------- duke@435: Node *URShiftLNode::Identity( PhaseTransform *phase ) { duke@435: const TypeInt *ti = phase->type( in(2) )->isa_int(); // shift count is an int duke@435: return ( ti && ti->is_con() && ( ti->get_con() & ( BitsPerLong - 1 ) ) == 0 ) ? in(1) : this; duke@435: } duke@435: duke@435: //------------------------------Ideal------------------------------------------ duke@435: Node *URShiftLNode::Ideal(PhaseGVN *phase, bool can_reshape) { duke@435: const TypeInt *t2 = phase->type( in(2) )->isa_int(); duke@435: if( !t2 || !t2->is_con() ) return NULL; // Right input is a constant duke@435: const int con = t2->get_con() & ( BitsPerLong - 1 ); // Shift count is always masked duke@435: if ( con == 0 ) return NULL; // let Identity() handle a 0 shift count duke@435: // note: mask computation below does not work for 0 shift count duke@435: // We'll be wanting the right-shift amount as a mask of that many bits twisti@994: const jlong mask = (((jlong)CONST64(1) << (jlong)(BitsPerJavaLong - con)) -1); duke@435: duke@435: // Check for ((x << z) + Y) >>> z. Replace with x + con>>>z duke@435: // The idiom for rounding to a power of 2 is "(Q+(2^z-1)) >>> z". duke@435: // If Q is "X << z" the rounding is useless. Look for patterns like duke@435: // ((X<>> Z and replace with (X + Y>>>Z) & Z-mask. duke@435: Node *add = in(1); duke@435: if( add->Opcode() == Op_AddL ) { duke@435: Node *lshl = add->in(1); duke@435: if( lshl->Opcode() == Op_LShiftL && duke@435: phase->type(lshl->in(2)) == t2 ) { kvn@4115: Node *y_z = phase->transform( new (phase->C) URShiftLNode(add->in(2),in(2)) ); kvn@4115: Node *sum = phase->transform( new (phase->C) AddLNode( lshl->in(1), y_z ) ); kvn@4115: return new (phase->C) AndLNode( sum, phase->longcon(mask) ); duke@435: } duke@435: } duke@435: duke@435: // Check for (x & mask) >>> z. Replace with (x >>> z) & (mask >>> z) duke@435: // This shortens the mask. Also, if we are extracting a high byte and duke@435: // storing it to a buffer, the mask will be removed completely. duke@435: Node *andi = in(1); duke@435: if( andi->Opcode() == Op_AndL ) { duke@435: const TypeLong *t3 = phase->type( andi->in(2) )->isa_long(); duke@435: if( t3 && t3->is_con() ) { // Right input is a constant duke@435: jlong mask2 = t3->get_con(); duke@435: mask2 >>= con; // *signed* shift downward (high-order zeroes do not help) kvn@4115: Node *newshr = phase->transform( new (phase->C) URShiftLNode(andi->in(1), in(2)) ); kvn@4115: return new (phase->C) AndLNode(newshr, phase->longcon(mask2)); duke@435: } duke@435: } duke@435: duke@435: // Check for "(X << z ) >>> z" which simply zero-extends duke@435: Node *shl = in(1); duke@435: if( shl->Opcode() == Op_LShiftL && duke@435: phase->type(shl->in(2)) == t2 ) kvn@4115: return new (phase->C) AndLNode( shl->in(1), phase->longcon(mask) ); duke@435: duke@435: return NULL; duke@435: } duke@435: duke@435: //------------------------------Value------------------------------------------ duke@435: // A URShiftINode shifts its input2 right by input1 amount. duke@435: const Type *URShiftLNode::Value( PhaseTransform *phase ) const { duke@435: // (This is a near clone of RShiftLNode::Value.) duke@435: const Type *t1 = phase->type( in(1) ); duke@435: const Type *t2 = phase->type( in(2) ); duke@435: // Either input is TOP ==> the result is TOP duke@435: if( t1 == Type::TOP ) return Type::TOP; duke@435: if( t2 == Type::TOP ) return Type::TOP; duke@435: duke@435: // Left input is ZERO ==> the result is ZERO. duke@435: if( t1 == TypeLong::ZERO ) return TypeLong::ZERO; duke@435: // Shift by zero does nothing duke@435: if( t2 == TypeInt::ZERO ) return t1; duke@435: duke@435: // Either input is BOTTOM ==> the result is BOTTOM duke@435: if (t1 == Type::BOTTOM || t2 == Type::BOTTOM) duke@435: return TypeLong::LONG; duke@435: duke@435: if (t2 == TypeInt::INT) duke@435: return TypeLong::LONG; duke@435: duke@435: const TypeLong *r1 = t1->is_long(); // Handy access duke@435: const TypeInt *r2 = t2->is_int (); // Handy access duke@435: duke@435: if (r2->is_con()) { duke@435: uint shift = r2->get_con(); twisti@994: shift &= BitsPerJavaLong - 1; // semantics of Java shifts duke@435: // Shift by a multiple of 64 does nothing: duke@435: if (shift == 0) return t1; duke@435: // Calculate reasonably aggressive bounds for the result. duke@435: jlong lo = (julong)r1->_lo >> (juint)shift; duke@435: jlong hi = (julong)r1->_hi >> (juint)shift; duke@435: if (r1->_hi >= 0 && r1->_lo < 0) { duke@435: // If the type has both negative and positive values, duke@435: // there are two separate sub-domains to worry about: duke@435: // The positive half and the negative half. duke@435: jlong neg_lo = lo; duke@435: jlong neg_hi = (julong)-1 >> (juint)shift; duke@435: jlong pos_lo = (julong) 0 >> (juint)shift; duke@435: jlong pos_hi = hi; duke@435: //lo = MIN2(neg_lo, pos_lo); // == 0 duke@435: lo = neg_lo < pos_lo ? neg_lo : pos_lo; duke@435: //hi = MAX2(neg_hi, pos_hi); // == -1 >>> shift; duke@435: hi = neg_hi > pos_hi ? neg_hi : pos_hi; duke@435: } duke@435: assert(lo <= hi, "must have valid bounds"); duke@435: const TypeLong* tl = TypeLong::make(lo, hi, MAX2(r1->_widen,r2->_widen)); duke@435: #ifdef ASSERT duke@435: // Make sure we get the sign-capture idiom correct. twisti@994: if (shift == BitsPerJavaLong - 1) { duke@435: if (r1->_lo >= 0) assert(tl == TypeLong::ZERO, ">>>63 of + is 0"); duke@435: if (r1->_hi < 0) assert(tl == TypeLong::ONE, ">>>63 of - is +1"); duke@435: } duke@435: #endif duke@435: return tl; duke@435: } duke@435: duke@435: return TypeLong::LONG; // Give up duke@435: }