duke@435: /* duke@435: * Copyright 1997-2006 Sun Microsystems, Inc. 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: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: // Portions of code courtesy of Clifford Click duke@435: duke@435: #include "incls/_precompiled.incl" duke@435: #include "incls/_addnode.cpp.incl" duke@435: duke@435: #define MAXFLOAT ((float)3.40282346638528860e+38) duke@435: duke@435: // Classic Add functionality. This covers all the usual 'add' behaviors for duke@435: // an algebraic ring. Add-integer, add-float, add-double, and binary-or are duke@435: // all inherited from this class. The various identity values are supplied duke@435: // by virtual functions. duke@435: duke@435: duke@435: //============================================================================= duke@435: //------------------------------hash------------------------------------------- duke@435: // Hash function over AddNodes. Needs to be commutative; i.e., I swap duke@435: // (commute) inputs to AddNodes willy-nilly so the hash function must return duke@435: // the same value in the presence of edge swapping. duke@435: uint AddNode::hash() const { duke@435: return (uintptr_t)in(1) + (uintptr_t)in(2) + Opcode(); duke@435: } duke@435: duke@435: //------------------------------Identity--------------------------------------- duke@435: // If either input is a constant 0, return the other input. duke@435: Node *AddNode::Identity( PhaseTransform *phase ) { duke@435: const Type *zero = add_id(); // The additive identity duke@435: if( phase->type( in(1) )->higher_equal( zero ) ) return in(2); duke@435: if( phase->type( in(2) )->higher_equal( zero ) ) return in(1); duke@435: return this; duke@435: } duke@435: duke@435: //------------------------------commute---------------------------------------- duke@435: // Commute operands to move loads and constants to the right. duke@435: static bool commute( Node *add, int con_left, int con_right ) { duke@435: Node *in1 = add->in(1); duke@435: Node *in2 = add->in(2); duke@435: duke@435: // Convert "1+x" into "x+1". duke@435: // Right is a constant; leave it duke@435: if( con_right ) return false; duke@435: // Left is a constant; move it right. duke@435: if( con_left ) { duke@435: add->swap_edges(1, 2); duke@435: return true; duke@435: } duke@435: duke@435: // Convert "Load+x" into "x+Load". duke@435: // Now check for loads duke@435: if( in2->is_Load() ) return false; duke@435: // Left is a Load and Right is not; move it right. duke@435: if( in1->is_Load() ) { duke@435: add->swap_edges(1, 2); duke@435: return true; duke@435: } duke@435: duke@435: PhiNode *phi; duke@435: // Check for tight loop increments: Loop-phi of Add of loop-phi duke@435: if( in1->is_Phi() && (phi = in1->as_Phi()) && !phi->is_copy() && phi->region()->is_Loop() && phi->in(2)==add) duke@435: return false; duke@435: if( in2->is_Phi() && (phi = in2->as_Phi()) && !phi->is_copy() && phi->region()->is_Loop() && phi->in(2)==add){ duke@435: add->swap_edges(1, 2); duke@435: return true; duke@435: } duke@435: duke@435: // Otherwise, sort inputs (commutativity) to help value numbering. duke@435: if( in1->_idx > in2->_idx ) { duke@435: add->swap_edges(1, 2); duke@435: return true; duke@435: } duke@435: return false; duke@435: } duke@435: duke@435: //------------------------------Idealize--------------------------------------- duke@435: // If we get here, we assume we are associative! duke@435: Node *AddNode::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: int con_left = t1->singleton(); duke@435: int con_right = t2->singleton(); duke@435: duke@435: // Check for commutative operation desired duke@435: if( commute(this,con_left,con_right) ) return this; duke@435: duke@435: AddNode *progress = NULL; // Progress flag duke@435: duke@435: // Convert "(x+1)+2" into "x+(1+2)". If the right input is a duke@435: // constant, and the left input is an add of a constant, flatten the duke@435: // expression tree. duke@435: Node *add1 = in(1); duke@435: Node *add2 = in(2); duke@435: int add1_op = add1->Opcode(); duke@435: int this_op = Opcode(); duke@435: if( con_right && t2 != Type::TOP && // Right input is a constant? duke@435: add1_op == this_op ) { // Left input is an Add? duke@435: duke@435: // Type of left _in right input 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: // Check for rare case of closed data cycle which can happen inside duke@435: // unreachable loops. In these cases the computation is undefined. duke@435: #ifdef ASSERT duke@435: Node *add11 = add1->in(1); duke@435: int add11_op = add11->Opcode(); duke@435: if( (add1 == add1->in(1)) duke@435: || (add11_op == this_op && add11->in(1) == add1) ) { duke@435: assert(false, "dead loop in AddNode::Ideal"); duke@435: } duke@435: #endif duke@435: // The Add of the flattened expression duke@435: Node *x1 = add1->in(1); duke@435: Node *x2 = phase->makecon( add1->as_Add()->add_ring( t2, t12 )); duke@435: PhaseIterGVN *igvn = phase->is_IterGVN(); duke@435: if( igvn ) { duke@435: set_req_X(2,x2,igvn); duke@435: set_req_X(1,x1,igvn); duke@435: } else { duke@435: set_req(2,x2); duke@435: set_req(1,x1); duke@435: } duke@435: progress = this; // Made progress duke@435: add1 = in(1); duke@435: add1_op = add1->Opcode(); duke@435: } duke@435: } duke@435: duke@435: // Convert "(x+1)+y" into "(x+y)+1". Push constants down the expression tree. duke@435: if( add1_op == this_op && !con_right ) { duke@435: Node *a12 = add1->in(2); duke@435: const Type *t12 = phase->type( a12 ); duke@435: if( t12->singleton() && t12 != Type::TOP && (add1 != add1->in(1)) ) { duke@435: add2 = add1->clone(); duke@435: add2->set_req(2, in(2)); duke@435: add2 = phase->transform(add2); duke@435: set_req(1, add2); duke@435: set_req(2, a12); duke@435: progress = this; duke@435: add2 = a12; duke@435: } duke@435: } duke@435: duke@435: // Convert "x+(y+1)" into "(x+y)+1". Push constants down the expression tree. duke@435: int add2_op = add2->Opcode(); duke@435: if( add2_op == this_op && !con_left ) { duke@435: Node *a22 = add2->in(2); duke@435: const Type *t22 = phase->type( a22 ); duke@435: if( t22->singleton() && t22 != Type::TOP && (add2 != add2->in(1)) ) { duke@435: Node *addx = add2->clone(); duke@435: addx->set_req(1, in(1)); duke@435: addx->set_req(2, add2->in(1)); duke@435: addx = phase->transform(addx); duke@435: set_req(1, addx); duke@435: set_req(2, a22); duke@435: progress = this; duke@435: } duke@435: } duke@435: duke@435: return progress; duke@435: } duke@435: duke@435: //------------------------------Value----------------------------------------- duke@435: // An add node sums it's two _in. If one input is an RSD, we must mixin duke@435: // the other input's symbols. duke@435: const Type *AddNode::Value( PhaseTransform *phase ) const { duke@435: // Either input is TOP ==> the result is TOP duke@435: const Type *t1 = phase->type( in(1) ); duke@435: const Type *t2 = phase->type( in(2) ); 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 BOTTOM ==> the result is the local BOTTOM duke@435: const Type *bot = bottom_type(); duke@435: if( (t1 == bot) || (t2 == bot) || duke@435: (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) ) duke@435: return bot; duke@435: duke@435: // Check for an addition involving the additive identity duke@435: const Type *tadd = add_of_identity( t1, t2 ); duke@435: if( tadd ) return tadd; duke@435: duke@435: return add_ring(t1,t2); // Local flavor of type addition duke@435: } duke@435: duke@435: //------------------------------add_identity----------------------------------- duke@435: // Check for addition of the identity duke@435: const Type *AddNode::add_of_identity( const Type *t1, const Type *t2 ) const { duke@435: const Type *zero = add_id(); // The additive identity duke@435: if( t1->higher_equal( zero ) ) return t2; duke@435: if( t2->higher_equal( zero ) ) return t1; duke@435: duke@435: return NULL; duke@435: } duke@435: duke@435: duke@435: //============================================================================= duke@435: //------------------------------Idealize--------------------------------------- duke@435: Node *AddINode::Ideal(PhaseGVN *phase, bool can_reshape) { duke@435: int op1 = in(1)->Opcode(); duke@435: int op2 = in(2)->Opcode(); duke@435: // Fold (con1-x)+con2 into (con1+con2)-x duke@435: if( op1 == Op_SubI ) { duke@435: const Type *t_sub1 = phase->type( in(1)->in(1) ); duke@435: const Type *t_2 = phase->type( in(2) ); duke@435: if( t_sub1->singleton() && t_2->singleton() && t_sub1 != Type::TOP && t_2 != Type::TOP ) duke@435: return new (phase->C, 3) SubINode(phase->makecon( add_ring( t_sub1, t_2 ) ), duke@435: in(1)->in(2) ); duke@435: // Convert "(a-b)+(c-d)" into "(a+c)-(b+d)" duke@435: if( op2 == Op_SubI ) { duke@435: // Check for dead cycle: d = (a-b)+(c-d) duke@435: assert( in(1)->in(2) != this && in(2)->in(2) != this, duke@435: "dead loop in AddINode::Ideal" ); duke@435: Node *sub = new (phase->C, 3) SubINode(NULL, NULL); duke@435: sub->init_req(1, phase->transform(new (phase->C, 3) AddINode(in(1)->in(1), in(2)->in(1) ) )); duke@435: sub->init_req(2, phase->transform(new (phase->C, 3) AddINode(in(1)->in(2), in(2)->in(2) ) )); duke@435: return sub; duke@435: } duke@435: } duke@435: duke@435: // Convert "x+(0-y)" into "(x-y)" duke@435: if( op2 == Op_SubI && phase->type(in(2)->in(1)) == TypeInt::ZERO ) duke@435: return new (phase->C, 3) SubINode(in(1), in(2)->in(2) ); duke@435: duke@435: // Convert "(0-y)+x" into "(x-y)" duke@435: if( op1 == Op_SubI && phase->type(in(1)->in(1)) == TypeInt::ZERO ) duke@435: return new (phase->C, 3) SubINode( in(2), in(1)->in(2) ); duke@435: duke@435: // Convert (x>>>z)+y into (x+(y<>>z for small constant z and y. duke@435: // Helps with array allocation math constant folding duke@435: // See 4790063: duke@435: // Unrestricted transformation is unsafe for some runtime values of 'x' duke@435: // ( x == 0, z == 1, y == -1 ) fails duke@435: // ( x == -5, z == 1, y == 1 ) fails duke@435: // Transform works for small z and small negative y when the addition duke@435: // (x + (y << z)) does not cross zero. duke@435: // Implement support for negative y and (x >= -(y << z)) duke@435: // Have not observed cases where type information exists to support duke@435: // positive y and (x <= -(y << z)) duke@435: if( op1 == Op_URShiftI && op2 == Op_ConI && duke@435: in(1)->in(2)->Opcode() == Op_ConI ) { duke@435: jint z = phase->type( in(1)->in(2) )->is_int()->get_con() & 0x1f; // only least significant 5 bits matter duke@435: jint y = phase->type( in(2) )->is_int()->get_con(); duke@435: duke@435: if( z < 5 && -5 < y && y < 0 ) { duke@435: const Type *t_in11 = phase->type(in(1)->in(1)); duke@435: if( t_in11 != Type::TOP && (t_in11->is_int()->_lo >= -(y << z)) ) { duke@435: Node *a = phase->transform( new (phase->C, 3) AddINode( in(1)->in(1), phase->intcon(y<C, 3) URShiftINode( a, in(1)->in(2) ); duke@435: } duke@435: } duke@435: } duke@435: duke@435: return AddNode::Ideal(phase, can_reshape); duke@435: } duke@435: duke@435: duke@435: //------------------------------Identity--------------------------------------- duke@435: // Fold (x-y)+y OR y+(x-y) into x duke@435: Node *AddINode::Identity( PhaseTransform *phase ) { duke@435: if( in(1)->Opcode() == Op_SubI && phase->eqv(in(1)->in(2),in(2)) ) { duke@435: return in(1)->in(1); duke@435: } duke@435: else if( in(2)->Opcode() == Op_SubI && phase->eqv(in(2)->in(2),in(1)) ) { duke@435: return in(2)->in(1); duke@435: } duke@435: return AddNode::Identity(phase); duke@435: } duke@435: duke@435: duke@435: //------------------------------add_ring--------------------------------------- duke@435: // Supplied function returns the sum of the inputs. Guaranteed never duke@435: // to be passed a TOP or BOTTOM type, these are filtered out by duke@435: // pre-check. duke@435: const Type *AddINode::add_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 lo = r0->_lo + r1->_lo; duke@435: int hi = r0->_hi + r1->_hi; duke@435: if( !(r0->is_con() && r1->is_con()) ) { duke@435: // Not both constants, compute approximate result duke@435: if( (r0->_lo & r1->_lo) < 0 && lo >= 0 ) { duke@435: lo = min_jint; hi = max_jint; // Underflow on the low side duke@435: } duke@435: if( (~(r0->_hi | r1->_hi)) < 0 && hi < 0 ) { duke@435: lo = min_jint; hi = max_jint; // Overflow on the high side duke@435: } duke@435: if( lo > hi ) { // Handle overflow duke@435: lo = min_jint; hi = max_jint; duke@435: } duke@435: } else { duke@435: // both constants, compute precise result using 'lo' and 'hi' duke@435: // Semantics define overflow and underflow for integer addition duke@435: // as expected. In particular: 0x80000000 + 0x80000000 --> 0x0 duke@435: } duke@435: return TypeInt::make( lo, hi, MAX2(r0->_widen,r1->_widen) ); duke@435: } duke@435: duke@435: duke@435: //============================================================================= duke@435: //------------------------------Idealize--------------------------------------- duke@435: Node *AddLNode::Ideal(PhaseGVN *phase, bool can_reshape) { duke@435: int op1 = in(1)->Opcode(); duke@435: int op2 = in(2)->Opcode(); duke@435: // Fold (con1-x)+con2 into (con1+con2)-x duke@435: if( op1 == Op_SubL ) { duke@435: const Type *t_sub1 = phase->type( in(1)->in(1) ); duke@435: const Type *t_2 = phase->type( in(2) ); duke@435: if( t_sub1->singleton() && t_2->singleton() && t_sub1 != Type::TOP && t_2 != Type::TOP ) duke@435: return new (phase->C, 3) SubLNode(phase->makecon( add_ring( t_sub1, t_2 ) ), duke@435: in(1)->in(2) ); duke@435: // Convert "(a-b)+(c-d)" into "(a+c)-(b+d)" duke@435: if( op2 == Op_SubL ) { duke@435: // Check for dead cycle: d = (a-b)+(c-d) duke@435: assert( in(1)->in(2) != this && in(2)->in(2) != this, duke@435: "dead loop in AddLNode::Ideal" ); duke@435: Node *sub = new (phase->C, 3) SubLNode(NULL, NULL); duke@435: sub->init_req(1, phase->transform(new (phase->C, 3) AddLNode(in(1)->in(1), in(2)->in(1) ) )); duke@435: sub->init_req(2, phase->transform(new (phase->C, 3) AddLNode(in(1)->in(2), in(2)->in(2) ) )); duke@435: return sub; duke@435: } duke@435: } duke@435: duke@435: // Convert "x+(0-y)" into "(x-y)" duke@435: if( op2 == Op_SubL && phase->type(in(2)->in(1)) == TypeLong::ZERO ) duke@435: return new (phase->C, 3) SubLNode(in(1), in(2)->in(2) ); duke@435: duke@435: // Convert "X+X+X+X+X...+X+Y" into "k*X+Y" or really convert "X+(X+Y)" duke@435: // into "(X<<1)+Y" and let shift-folding happen. duke@435: if( op2 == Op_AddL && duke@435: in(2)->in(1) == in(1) && duke@435: op1 != Op_ConL && duke@435: 0 ) { duke@435: Node *shift = phase->transform(new (phase->C, 3) LShiftLNode(in(1),phase->intcon(1))); duke@435: return new (phase->C, 3) AddLNode(shift,in(2)->in(2)); duke@435: } duke@435: duke@435: return AddNode::Ideal(phase, can_reshape); duke@435: } duke@435: duke@435: duke@435: //------------------------------Identity--------------------------------------- duke@435: // Fold (x-y)+y OR y+(x-y) into x duke@435: Node *AddLNode::Identity( PhaseTransform *phase ) { duke@435: if( in(1)->Opcode() == Op_SubL && phase->eqv(in(1)->in(2),in(2)) ) { duke@435: return in(1)->in(1); duke@435: } duke@435: else if( in(2)->Opcode() == Op_SubL && phase->eqv(in(2)->in(2),in(1)) ) { duke@435: return in(2)->in(1); duke@435: } duke@435: return AddNode::Identity(phase); duke@435: } duke@435: duke@435: duke@435: //------------------------------add_ring--------------------------------------- duke@435: // Supplied function returns the sum of the inputs. Guaranteed never duke@435: // to be passed a TOP or BOTTOM type, these are filtered out by duke@435: // pre-check. duke@435: const Type *AddLNode::add_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: jlong lo = r0->_lo + r1->_lo; duke@435: jlong hi = r0->_hi + r1->_hi; duke@435: if( !(r0->is_con() && r1->is_con()) ) { duke@435: // Not both constants, compute approximate result duke@435: if( (r0->_lo & r1->_lo) < 0 && lo >= 0 ) { duke@435: lo =min_jlong; hi = max_jlong; // Underflow on the low side duke@435: } duke@435: if( (~(r0->_hi | r1->_hi)) < 0 && hi < 0 ) { duke@435: lo = min_jlong; hi = max_jlong; // Overflow on the high side duke@435: } duke@435: if( lo > hi ) { // Handle overflow duke@435: lo = min_jlong; hi = max_jlong; duke@435: } duke@435: } else { duke@435: // both constants, compute precise result using 'lo' and 'hi' duke@435: // Semantics define overflow and underflow for integer addition duke@435: // as expected. In particular: 0x80000000 + 0x80000000 --> 0x0 duke@435: } duke@435: return TypeLong::make( lo, hi, MAX2(r0->_widen,r1->_widen) ); duke@435: } duke@435: duke@435: duke@435: //============================================================================= duke@435: //------------------------------add_of_identity-------------------------------- duke@435: // Check for addition of the identity duke@435: const Type *AddFNode::add_of_identity( const Type *t1, const Type *t2 ) const { duke@435: // x ADD 0 should return x unless 'x' is a -zero duke@435: // duke@435: // const Type *zero = add_id(); // The additive identity duke@435: // jfloat f1 = t1->getf(); duke@435: // jfloat f2 = t2->getf(); duke@435: // duke@435: // if( t1->higher_equal( zero ) ) return t2; duke@435: // if( t2->higher_equal( zero ) ) return t1; duke@435: duke@435: return NULL; duke@435: } duke@435: duke@435: //------------------------------add_ring--------------------------------------- duke@435: // Supplied function returns the sum of the inputs. 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 *AddFNode::add_ring( const Type *t0, const Type *t1 ) const { duke@435: // We must be adding 2 float constants. duke@435: return TypeF::make( t0->getf() + t1->getf() ); duke@435: } duke@435: duke@435: //------------------------------Ideal------------------------------------------ duke@435: Node *AddFNode::Ideal(PhaseGVN *phase, bool can_reshape) { duke@435: if( IdealizedNumerics && !phase->C->method()->is_strict() ) { duke@435: return AddNode::Ideal(phase, can_reshape); // commutative and associative transforms duke@435: } duke@435: duke@435: // Floating point additions are not associative because of boundary conditions (infinity) duke@435: return commute(this, duke@435: phase->type( in(1) )->singleton(), duke@435: phase->type( in(2) )->singleton() ) ? this : NULL; duke@435: } duke@435: duke@435: duke@435: //============================================================================= duke@435: //------------------------------add_of_identity-------------------------------- duke@435: // Check for addition of the identity duke@435: const Type *AddDNode::add_of_identity( const Type *t1, const Type *t2 ) const { duke@435: // x ADD 0 should return x unless 'x' is a -zero duke@435: // duke@435: // const Type *zero = add_id(); // The additive identity duke@435: // jfloat f1 = t1->getf(); duke@435: // jfloat f2 = t2->getf(); duke@435: // duke@435: // if( t1->higher_equal( zero ) ) return t2; duke@435: // if( t2->higher_equal( zero ) ) return t1; duke@435: duke@435: return NULL; duke@435: } duke@435: //------------------------------add_ring--------------------------------------- duke@435: // Supplied function returns the sum of the inputs. 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 *AddDNode::add_ring( const Type *t0, const Type *t1 ) const { duke@435: // We must be adding 2 double constants. duke@435: return TypeD::make( t0->getd() + t1->getd() ); duke@435: } duke@435: duke@435: //------------------------------Ideal------------------------------------------ duke@435: Node *AddDNode::Ideal(PhaseGVN *phase, bool can_reshape) { duke@435: if( IdealizedNumerics && !phase->C->method()->is_strict() ) { duke@435: return AddNode::Ideal(phase, can_reshape); // commutative and associative transforms duke@435: } duke@435: duke@435: // Floating point additions are not associative because of boundary conditions (infinity) duke@435: return commute(this, duke@435: phase->type( in(1) )->singleton(), duke@435: phase->type( in(2) )->singleton() ) ? this : NULL; duke@435: } duke@435: duke@435: duke@435: //============================================================================= duke@435: //------------------------------Identity--------------------------------------- duke@435: // If one input is a constant 0, return the other input. duke@435: Node *AddPNode::Identity( PhaseTransform *phase ) { duke@435: return ( phase->type( in(Offset) )->higher_equal( TypeX_ZERO ) ) ? in(Address) : this; duke@435: } duke@435: duke@435: //------------------------------Idealize--------------------------------------- duke@435: Node *AddPNode::Ideal(PhaseGVN *phase, bool can_reshape) { duke@435: // Bail out if dead inputs duke@435: if( phase->type( in(Address) ) == Type::TOP ) return NULL; duke@435: duke@435: // If the left input is an add of a constant, flatten the expression tree. duke@435: const Node *n = in(Address); duke@435: if (n->is_AddP() && n->in(Base) == in(Base)) { duke@435: const AddPNode *addp = n->as_AddP(); // Left input is an AddP duke@435: assert( !addp->in(Address)->is_AddP() || duke@435: addp->in(Address)->as_AddP() != addp, duke@435: "dead loop in AddPNode::Ideal" ); duke@435: // Type of left input's right input duke@435: const Type *t = phase->type( addp->in(Offset) ); duke@435: if( t == Type::TOP ) return NULL; duke@435: const TypeX *t12 = t->is_intptr_t(); duke@435: if( t12->is_con() ) { // Left input is an add of a constant? duke@435: // If the right input is a constant, combine constants duke@435: const Type *temp_t2 = phase->type( in(Offset) ); duke@435: if( temp_t2 == Type::TOP ) return NULL; duke@435: const TypeX *t2 = temp_t2->is_intptr_t(); kvn@467: Node* address; kvn@467: Node* offset; duke@435: if( t2->is_con() ) { duke@435: // The Add of the flattened expression kvn@467: address = addp->in(Address); kvn@467: offset = phase->MakeConX(t2->get_con() + t12->get_con()); kvn@467: } else { kvn@467: // Else move the constant to the right. ((A+con)+B) into ((A+B)+con) kvn@467: address = phase->transform(new (phase->C, 4) AddPNode(in(Base),addp->in(Address),in(Offset))); kvn@467: offset = addp->in(Offset); duke@435: } kvn@467: PhaseIterGVN *igvn = phase->is_IterGVN(); kvn@467: if( igvn ) { kvn@467: set_req_X(Address,address,igvn); kvn@467: set_req_X(Offset,offset,igvn); kvn@467: } else { kvn@467: set_req(Address,address); kvn@467: set_req(Offset,offset); kvn@467: } duke@435: return this; duke@435: } duke@435: } duke@435: duke@435: // Raw pointers? duke@435: if( in(Base)->bottom_type() == Type::TOP ) { duke@435: // If this is a NULL+long form (from unsafe accesses), switch to a rawptr. duke@435: if (phase->type(in(Address)) == TypePtr::NULL_PTR) { duke@435: Node* offset = in(Offset); duke@435: return new (phase->C, 2) CastX2PNode(offset); duke@435: } duke@435: } duke@435: duke@435: // If the right is an add of a constant, push the offset down. duke@435: // Convert: (ptr + (offset+con)) into (ptr+offset)+con. duke@435: // The idea is to merge array_base+scaled_index groups together, duke@435: // and only have different constant offsets from the same base. duke@435: const Node *add = in(Offset); duke@435: if( add->Opcode() == Op_AddX && add->in(1) != add ) { duke@435: const Type *t22 = phase->type( add->in(2) ); duke@435: if( t22->singleton() && (t22 != Type::TOP) ) { // Right input is an add of a constant? duke@435: set_req(Address, phase->transform(new (phase->C, 4) AddPNode(in(Base),in(Address),add->in(1)))); duke@435: set_req(Offset, add->in(2)); duke@435: return this; // Made progress duke@435: } duke@435: } duke@435: duke@435: return NULL; // No progress duke@435: } duke@435: duke@435: //------------------------------bottom_type------------------------------------ duke@435: // Bottom-type is the pointer-type with unknown offset. duke@435: const Type *AddPNode::bottom_type() const { duke@435: if (in(Address) == NULL) return TypePtr::BOTTOM; duke@435: const TypePtr *tp = in(Address)->bottom_type()->isa_ptr(); duke@435: if( !tp ) return Type::TOP; // TOP input means TOP output duke@435: assert( in(Offset)->Opcode() != Op_ConP, "" ); duke@435: const Type *t = in(Offset)->bottom_type(); duke@435: if( t == Type::TOP ) duke@435: return tp->add_offset(Type::OffsetTop); duke@435: const TypeX *tx = t->is_intptr_t(); duke@435: intptr_t txoffset = Type::OffsetBot; duke@435: if (tx->is_con()) { // Left input is an add of a constant? duke@435: txoffset = tx->get_con(); duke@435: if (txoffset != (int)txoffset) duke@435: txoffset = Type::OffsetBot; // oops: add_offset will choke on it duke@435: } duke@435: return tp->add_offset(txoffset); duke@435: } duke@435: duke@435: //------------------------------Value------------------------------------------ duke@435: const Type *AddPNode::Value( PhaseTransform *phase ) const { duke@435: // Either input is TOP ==> the result is TOP duke@435: const Type *t1 = phase->type( in(Address) ); duke@435: const Type *t2 = phase->type( in(Offset) ); 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 a pointer duke@435: const TypePtr *p1 = t1->isa_ptr(); duke@435: // Right input is an int duke@435: const TypeX *p2 = t2->is_intptr_t(); duke@435: // Add 'em duke@435: intptr_t p2offset = Type::OffsetBot; duke@435: if (p2->is_con()) { // Left input is an add of a constant? duke@435: p2offset = p2->get_con(); duke@435: if (p2offset != (int)p2offset) duke@435: p2offset = Type::OffsetBot; // oops: add_offset will choke on it duke@435: } duke@435: return p1->add_offset(p2offset); duke@435: } duke@435: duke@435: //------------------------Ideal_base_and_offset-------------------------------- duke@435: // Split an oop pointer into a base and offset. duke@435: // (The offset might be Type::OffsetBot in the case of an array.) duke@435: // Return the base, or NULL if failure. duke@435: Node* AddPNode::Ideal_base_and_offset(Node* ptr, PhaseTransform* phase, duke@435: // second return value: duke@435: intptr_t& offset) { duke@435: if (ptr->is_AddP()) { duke@435: Node* base = ptr->in(AddPNode::Base); duke@435: Node* addr = ptr->in(AddPNode::Address); duke@435: Node* offs = ptr->in(AddPNode::Offset); duke@435: if (base == addr || base->is_top()) { duke@435: offset = phase->find_intptr_t_con(offs, Type::OffsetBot); duke@435: if (offset != Type::OffsetBot) { duke@435: return addr; duke@435: } duke@435: } duke@435: } duke@435: offset = Type::OffsetBot; duke@435: return NULL; duke@435: } duke@435: never@452: //------------------------------unpack_offsets---------------------------------- never@452: // Collect the AddP offset values into the elements array, giving up never@452: // if there are more than length. never@452: int AddPNode::unpack_offsets(Node* elements[], int length) { never@452: int count = 0; never@452: Node* addr = this; never@452: Node* base = addr->in(AddPNode::Base); never@452: while (addr->is_AddP()) { never@452: if (addr->in(AddPNode::Base) != base) { never@452: // give up never@452: return -1; never@452: } never@452: elements[count++] = addr->in(AddPNode::Offset); never@452: if (count == length) { never@452: // give up never@452: return -1; never@452: } never@452: addr = addr->in(AddPNode::Address); never@452: } never@452: return count; never@452: } never@452: duke@435: //------------------------------match_edge------------------------------------- duke@435: // Do we Match on this edge index or not? Do not match base pointer edge duke@435: uint AddPNode::match_edge(uint idx) const { duke@435: return idx > Base; duke@435: } duke@435: duke@435: //---------------------------mach_bottom_type---------------------------------- duke@435: // Utility function for use by ADLC. Implements bottom_type for matched AddP. duke@435: const Type *AddPNode::mach_bottom_type( const MachNode* n) { duke@435: Node* base = n->in(Base); duke@435: const Type *t = base->bottom_type(); duke@435: if ( t == Type::TOP ) { duke@435: // an untyped pointer duke@435: return TypeRawPtr::BOTTOM; duke@435: } duke@435: const TypePtr* tp = t->isa_oopptr(); duke@435: if ( tp == NULL ) return t; duke@435: if ( tp->_offset == TypePtr::OffsetBot ) return tp; duke@435: duke@435: // We must carefully add up the various offsets... duke@435: intptr_t offset = 0; duke@435: const TypePtr* tptr = NULL; duke@435: duke@435: uint numopnds = n->num_opnds(); duke@435: uint index = n->oper_input_base(); duke@435: for ( uint i = 1; i < numopnds; i++ ) { duke@435: MachOper *opnd = n->_opnds[i]; duke@435: // Check for any interesting operand info. duke@435: // In particular, check for both memory and non-memory operands. duke@435: // %%%%% Clean this up: use xadd_offset duke@435: int con = opnd->constant(); duke@435: if ( con == TypePtr::OffsetBot ) goto bottom_out; duke@435: offset += con; duke@435: con = opnd->constant_disp(); duke@435: if ( con == TypePtr::OffsetBot ) goto bottom_out; duke@435: offset += con; duke@435: if( opnd->scale() != 0 ) goto bottom_out; duke@435: duke@435: // Check each operand input edge. Find the 1 allowed pointer duke@435: // edge. Other edges must be index edges; track exact constant duke@435: // inputs and otherwise assume the worst. duke@435: for ( uint j = opnd->num_edges(); j > 0; j-- ) { duke@435: Node* edge = n->in(index++); duke@435: const Type* et = edge->bottom_type(); duke@435: const TypeX* eti = et->isa_intptr_t(); duke@435: if ( eti == NULL ) { duke@435: // there must be one pointer among the operands duke@435: guarantee(tptr == NULL, "must be only one pointer operand"); duke@435: tptr = et->isa_oopptr(); duke@435: guarantee(tptr != NULL, "non-int operand must be pointer"); duke@435: continue; duke@435: } duke@435: if ( eti->_hi != eti->_lo ) goto bottom_out; duke@435: offset += eti->_lo; duke@435: } duke@435: } duke@435: guarantee(tptr != NULL, "must be exactly one pointer operand"); duke@435: return tptr->add_offset(offset); duke@435: duke@435: bottom_out: duke@435: return tp->add_offset(TypePtr::OffsetBot); duke@435: } duke@435: duke@435: //============================================================================= duke@435: //------------------------------Identity--------------------------------------- duke@435: Node *OrINode::Identity( PhaseTransform *phase ) { duke@435: // x | x => x duke@435: if (phase->eqv(in(1), in(2))) { duke@435: return in(1); duke@435: } duke@435: duke@435: return AddNode::Identity(phase); duke@435: } duke@435: duke@435: //------------------------------add_ring--------------------------------------- duke@435: // Supplied function returns the sum of the inputs IN THE CURRENT RING. For duke@435: // the logical operations the ring's ADD is really a logical OR 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 *OrINode::add_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: // If both args are bool, can figure out better types duke@435: if ( r0 == TypeInt::BOOL ) { duke@435: if ( r1 == TypeInt::ONE) { duke@435: return TypeInt::ONE; duke@435: } else if ( r1 == TypeInt::BOOL ) { duke@435: return TypeInt::BOOL; duke@435: } duke@435: } else if ( r0 == TypeInt::ONE ) { duke@435: if ( r1 == TypeInt::BOOL ) { duke@435: return TypeInt::ONE; duke@435: } duke@435: } duke@435: duke@435: // If either input is not a constant, just return all integers. duke@435: if( !r0->is_con() || !r1->is_con() ) duke@435: return TypeInt::INT; // Any integer, but still no symbols. duke@435: duke@435: // Otherwise just OR them bits. duke@435: return TypeInt::make( r0->get_con() | r1->get_con() ); duke@435: } duke@435: duke@435: //============================================================================= duke@435: //------------------------------Identity--------------------------------------- duke@435: Node *OrLNode::Identity( PhaseTransform *phase ) { duke@435: // x | x => x duke@435: if (phase->eqv(in(1), in(2))) { duke@435: return in(1); duke@435: } duke@435: duke@435: return AddNode::Identity(phase); duke@435: } duke@435: duke@435: //------------------------------add_ring--------------------------------------- duke@435: const Type *OrLNode::add_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: // If either input is not a constant, just return all integers. duke@435: if( !r0->is_con() || !r1->is_con() ) duke@435: return TypeLong::LONG; // Any integer, but still no symbols. duke@435: duke@435: // Otherwise just OR them bits. duke@435: return TypeLong::make( r0->get_con() | r1->get_con() ); duke@435: } duke@435: duke@435: //============================================================================= duke@435: //------------------------------add_ring--------------------------------------- duke@435: // Supplied function returns the sum of the inputs IN THE CURRENT RING. For duke@435: // the logical operations the ring's ADD is really a logical OR 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 *XorINode::add_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: // Complementing a boolean? duke@435: if( r0 == TypeInt::BOOL && ( r1 == TypeInt::ONE duke@435: || r1 == TypeInt::BOOL)) duke@435: return TypeInt::BOOL; duke@435: duke@435: if( !r0->is_con() || !r1->is_con() ) // Not constants duke@435: return TypeInt::INT; // Any integer, but still no symbols. duke@435: duke@435: // Otherwise just XOR them bits. duke@435: return TypeInt::make( r0->get_con() ^ r1->get_con() ); duke@435: } duke@435: duke@435: //============================================================================= duke@435: //------------------------------add_ring--------------------------------------- duke@435: const Type *XorLNode::add_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: // If either input is not a constant, just return all integers. duke@435: if( !r0->is_con() || !r1->is_con() ) duke@435: return TypeLong::LONG; // Any integer, but still no symbols. duke@435: duke@435: // Otherwise just OR them bits. duke@435: return TypeLong::make( r0->get_con() ^ r1->get_con() ); duke@435: } duke@435: duke@435: //============================================================================= duke@435: //------------------------------add_ring--------------------------------------- duke@435: // Supplied function returns the sum of the inputs. duke@435: const Type *MaxINode::add_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: // Otherwise just MAX them bits. duke@435: return TypeInt::make( MAX2(r0->_lo,r1->_lo), MAX2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) ); duke@435: } duke@435: duke@435: //============================================================================= duke@435: //------------------------------Idealize--------------------------------------- duke@435: // MINs show up in range-check loop limit calculations. Look for duke@435: // "MIN2(x+c0,MIN2(y,x+c1))". Pick the smaller constant: "MIN2(x+c0,y)" duke@435: Node *MinINode::Ideal(PhaseGVN *phase, bool can_reshape) { duke@435: Node *progress = NULL; duke@435: // Force a right-spline graph duke@435: Node *l = in(1); duke@435: Node *r = in(2); duke@435: // Transform MinI1( MinI2(a,b), c) into MinI1( a, MinI2(b,c) ) duke@435: // to force a right-spline graph for the rest of MinINode::Ideal(). duke@435: if( l->Opcode() == Op_MinI ) { duke@435: assert( l != l->in(1), "dead loop in MinINode::Ideal" ); duke@435: r = phase->transform(new (phase->C, 3) MinINode(l->in(2),r)); duke@435: l = l->in(1); duke@435: set_req(1, l); duke@435: set_req(2, r); duke@435: return this; duke@435: } duke@435: duke@435: // Get left input & constant duke@435: Node *x = l; duke@435: int x_off = 0; duke@435: if( x->Opcode() == Op_AddI && // Check for "x+c0" and collect constant duke@435: x->in(2)->is_Con() ) { duke@435: const Type *t = x->in(2)->bottom_type(); duke@435: if( t == Type::TOP ) return NULL; // No progress duke@435: x_off = t->is_int()->get_con(); duke@435: x = x->in(1); duke@435: } duke@435: duke@435: // Scan a right-spline-tree for MINs duke@435: Node *y = r; duke@435: int y_off = 0; duke@435: // Check final part of MIN tree duke@435: if( y->Opcode() == Op_AddI && // Check for "y+c1" and collect constant duke@435: y->in(2)->is_Con() ) { duke@435: const Type *t = y->in(2)->bottom_type(); duke@435: if( t == Type::TOP ) return NULL; // No progress duke@435: y_off = t->is_int()->get_con(); duke@435: y = y->in(1); duke@435: } duke@435: if( x->_idx > y->_idx && r->Opcode() != Op_MinI ) { duke@435: swap_edges(1, 2); duke@435: return this; duke@435: } duke@435: duke@435: duke@435: if( r->Opcode() == Op_MinI ) { duke@435: assert( r != r->in(2), "dead loop in MinINode::Ideal" ); duke@435: y = r->in(1); duke@435: // Check final part of MIN tree duke@435: if( y->Opcode() == Op_AddI &&// Check for "y+c1" and collect constant duke@435: y->in(2)->is_Con() ) { duke@435: const Type *t = y->in(2)->bottom_type(); duke@435: if( t == Type::TOP ) return NULL; // No progress duke@435: y_off = t->is_int()->get_con(); duke@435: y = y->in(1); duke@435: } duke@435: duke@435: if( x->_idx > y->_idx ) duke@435: return new (phase->C, 3) MinINode(r->in(1),phase->transform(new (phase->C, 3) MinINode(l,r->in(2)))); duke@435: duke@435: // See if covers: MIN2(x+c0,MIN2(y+c1,z)) duke@435: if( !phase->eqv(x,y) ) return NULL; duke@435: // If (y == x) transform MIN2(x+c0, MIN2(x+c1,z)) into duke@435: // MIN2(x+c0 or x+c1 which less, z). duke@435: return new (phase->C, 3) MinINode(phase->transform(new (phase->C, 3) AddINode(x,phase->intcon(MIN2(x_off,y_off)))),r->in(2)); duke@435: } else { duke@435: // See if covers: MIN2(x+c0,y+c1) duke@435: if( !phase->eqv(x,y) ) return NULL; duke@435: // If (y == x) transform MIN2(x+c0,x+c1) into x+c0 or x+c1 which less. duke@435: return new (phase->C, 3) AddINode(x,phase->intcon(MIN2(x_off,y_off))); duke@435: } duke@435: duke@435: } duke@435: duke@435: //------------------------------add_ring--------------------------------------- duke@435: // Supplied function returns the sum of the inputs. duke@435: const Type *MinINode::add_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: // Otherwise just MIN them bits. duke@435: return TypeInt::make( MIN2(r0->_lo,r1->_lo), MIN2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) ); duke@435: }