Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/opto/addnode.cpp
32285 views
/*1* Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "memory/allocation.inline.hpp"26#include "opto/addnode.hpp"27#include "opto/cfgnode.hpp"28#include "opto/connode.hpp"29#include "opto/machnode.hpp"30#include "opto/mulnode.hpp"31#include "opto/phaseX.hpp"32#include "opto/subnode.hpp"33#if INCLUDE_ALL_GCS34#include "gc_implementation/shenandoah/c2/shenandoahSupport.hpp"35#endif3637// Portions of code courtesy of Clifford Click3839// Classic Add functionality. This covers all the usual 'add' behaviors for40// an algebraic ring. Add-integer, add-float, add-double, and binary-or are41// all inherited from this class. The various identity values are supplied42// by virtual functions.434445//=============================================================================46//------------------------------hash-------------------------------------------47// Hash function over AddNodes. Needs to be commutative; i.e., I swap48// (commute) inputs to AddNodes willy-nilly so the hash function must return49// the same value in the presence of edge swapping.50uint AddNode::hash() const {51return (uintptr_t)in(1) + (uintptr_t)in(2) + Opcode();52}5354//------------------------------Identity---------------------------------------55// If either input is a constant 0, return the other input.56Node *AddNode::Identity( PhaseTransform *phase ) {57const Type *zero = add_id(); // The additive identity58if( phase->type( in(1) )->higher_equal( zero ) ) return in(2);59if( phase->type( in(2) )->higher_equal( zero ) ) return in(1);60return this;61}6263//------------------------------commute----------------------------------------64// Commute operands to move loads and constants to the right.65static bool commute( Node *add, int con_left, int con_right ) {66Node *in1 = add->in(1);67Node *in2 = add->in(2);6869// Convert "1+x" into "x+1".70// Right is a constant; leave it71if( con_right ) return false;72// Left is a constant; move it right.73if( con_left ) {74add->swap_edges(1, 2);75return true;76}7778// Convert "Load+x" into "x+Load".79// Now check for loads80if (in2->is_Load()) {81if (!in1->is_Load()) {82// already x+Load to return83return false;84}85// both are loads, so fall through to sort inputs by idx86} else if( in1->is_Load() ) {87// Left is a Load and Right is not; move it right.88add->swap_edges(1, 2);89return true;90}9192PhiNode *phi;93// Check for tight loop increments: Loop-phi of Add of loop-phi94if( in1->is_Phi() && (phi = in1->as_Phi()) && !phi->is_copy() && phi->region()->is_Loop() && phi->in(2)==add)95return false;96if( in2->is_Phi() && (phi = in2->as_Phi()) && !phi->is_copy() && phi->region()->is_Loop() && phi->in(2)==add){97add->swap_edges(1, 2);98return true;99}100101// Otherwise, sort inputs (commutativity) to help value numbering.102if( in1->_idx > in2->_idx ) {103add->swap_edges(1, 2);104return true;105}106return false;107}108109//------------------------------Idealize---------------------------------------110// If we get here, we assume we are associative!111Node *AddNode::Ideal(PhaseGVN *phase, bool can_reshape) {112const Type *t1 = phase->type( in(1) );113const Type *t2 = phase->type( in(2) );114int con_left = t1->singleton();115int con_right = t2->singleton();116117// Check for commutative operation desired118if( commute(this,con_left,con_right) ) return this;119120AddNode *progress = NULL; // Progress flag121122// Convert "(x+1)+2" into "x+(1+2)". If the right input is a123// constant, and the left input is an add of a constant, flatten the124// expression tree.125Node *add1 = in(1);126Node *add2 = in(2);127int add1_op = add1->Opcode();128int this_op = Opcode();129if( con_right && t2 != Type::TOP && // Right input is a constant?130add1_op == this_op ) { // Left input is an Add?131132// Type of left _in right input133const Type *t12 = phase->type( add1->in(2) );134if( t12->singleton() && t12 != Type::TOP ) { // Left input is an add of a constant?135// Check for rare case of closed data cycle which can happen inside136// unreachable loops. In these cases the computation is undefined.137#ifdef ASSERT138Node *add11 = add1->in(1);139int add11_op = add11->Opcode();140if( (add1 == add1->in(1))141|| (add11_op == this_op && add11->in(1) == add1) ) {142assert(false, "dead loop in AddNode::Ideal");143}144#endif145// The Add of the flattened expression146Node *x1 = add1->in(1);147Node *x2 = phase->makecon( add1->as_Add()->add_ring( t2, t12 ));148PhaseIterGVN *igvn = phase->is_IterGVN();149if( igvn ) {150set_req_X(2,x2,igvn);151set_req_X(1,x1,igvn);152} else {153set_req(2,x2);154set_req(1,x1);155}156progress = this; // Made progress157add1 = in(1);158add1_op = add1->Opcode();159}160}161162// Convert "(x+1)+y" into "(x+y)+1". Push constants down the expression tree.163if( add1_op == this_op && !con_right ) {164Node *a12 = add1->in(2);165const Type *t12 = phase->type( a12 );166if( t12->singleton() && t12 != Type::TOP && (add1 != add1->in(1)) &&167!(add1->in(1)->is_Phi() && add1->in(1)->as_Phi()->is_tripcount()) ) {168assert(add1->in(1) != this, "dead loop in AddNode::Ideal");169add2 = add1->clone();170add2->set_req(2, in(2));171add2 = phase->transform(add2);172set_req(1, add2);173set_req(2, a12);174progress = this;175add2 = a12;176}177}178179// Convert "x+(y+1)" into "(x+y)+1". Push constants down the expression tree.180int add2_op = add2->Opcode();181if( add2_op == this_op && !con_left ) {182Node *a22 = add2->in(2);183const Type *t22 = phase->type( a22 );184if( t22->singleton() && t22 != Type::TOP && (add2 != add2->in(1)) &&185!(add2->in(1)->is_Phi() && add2->in(1)->as_Phi()->is_tripcount()) ) {186assert(add2->in(1) != this, "dead loop in AddNode::Ideal");187Node *addx = add2->clone();188addx->set_req(1, in(1));189addx->set_req(2, add2->in(1));190addx = phase->transform(addx);191set_req(1, addx);192set_req(2, a22);193progress = this;194PhaseIterGVN *igvn = phase->is_IterGVN();195if (add2->outcnt() == 0 && igvn) {196// add disconnected.197igvn->_worklist.push(add2);198}199}200}201202return progress;203}204205//------------------------------Value-----------------------------------------206// An add node sums it's two _in. If one input is an RSD, we must mixin207// the other input's symbols.208const Type *AddNode::Value( PhaseTransform *phase ) const {209// Either input is TOP ==> the result is TOP210const Type *t1 = phase->type( in(1) );211const Type *t2 = phase->type( in(2) );212if( t1 == Type::TOP ) return Type::TOP;213if( t2 == Type::TOP ) return Type::TOP;214215// Either input is BOTTOM ==> the result is the local BOTTOM216const Type *bot = bottom_type();217if( (t1 == bot) || (t2 == bot) ||218(t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )219return bot;220221// Check for an addition involving the additive identity222const Type *tadd = add_of_identity( t1, t2 );223if( tadd ) return tadd;224225return add_ring(t1,t2); // Local flavor of type addition226}227228//------------------------------add_identity-----------------------------------229// Check for addition of the identity230const Type *AddNode::add_of_identity( const Type *t1, const Type *t2 ) const {231const Type *zero = add_id(); // The additive identity232if( t1->higher_equal( zero ) ) return t2;233if( t2->higher_equal( zero ) ) return t1;234235return NULL;236}237238239//=============================================================================240//------------------------------Idealize---------------------------------------241Node *AddINode::Ideal(PhaseGVN *phase, bool can_reshape) {242Node* in1 = in(1);243Node* in2 = in(2);244int op1 = in1->Opcode();245int op2 = in2->Opcode();246// Fold (con1-x)+con2 into (con1+con2)-x247if ( op1 == Op_AddI && op2 == Op_SubI ) {248// Swap edges to try optimizations below249in1 = in2;250in2 = in(1);251op1 = op2;252op2 = in2->Opcode();253}254if( op1 == Op_SubI ) {255const Type *t_sub1 = phase->type( in1->in(1) );256const Type *t_2 = phase->type( in2 );257if( t_sub1->singleton() && t_2->singleton() && t_sub1 != Type::TOP && t_2 != Type::TOP )258return new (phase->C) SubINode(phase->makecon( add_ring( t_sub1, t_2 ) ),259in1->in(2) );260// Convert "(a-b)+(c-d)" into "(a+c)-(b+d)"261if( op2 == Op_SubI ) {262// Check for dead cycle: d = (a-b)+(c-d)263assert( in1->in(2) != this && in2->in(2) != this,264"dead loop in AddINode::Ideal" );265Node *sub = new (phase->C) SubINode(NULL, NULL);266sub->init_req(1, phase->transform(new (phase->C) AddINode(in1->in(1), in2->in(1) ) ));267sub->init_req(2, phase->transform(new (phase->C) AddINode(in1->in(2), in2->in(2) ) ));268return sub;269}270// Convert "(a-b)+(b+c)" into "(a+c)"271if( op2 == Op_AddI && in1->in(2) == in2->in(1) ) {272assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddINode::Ideal");273return new (phase->C) AddINode(in1->in(1), in2->in(2));274}275// Convert "(a-b)+(c+b)" into "(a+c)"276if( op2 == Op_AddI && in1->in(2) == in2->in(2) ) {277assert(in1->in(1) != this && in2->in(1) != this,"dead loop in AddINode::Ideal");278return new (phase->C) AddINode(in1->in(1), in2->in(1));279}280// Convert "(a-b)+(b-c)" into "(a-c)"281if( op2 == Op_SubI && in1->in(2) == in2->in(1) ) {282assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddINode::Ideal");283return new (phase->C) SubINode(in1->in(1), in2->in(2));284}285// Convert "(a-b)+(c-a)" into "(c-b)"286if( op2 == Op_SubI && in1->in(1) == in2->in(2) ) {287assert(in1->in(2) != this && in2->in(1) != this,"dead loop in AddINode::Ideal");288return new (phase->C) SubINode(in2->in(1), in1->in(2));289}290}291292// Convert "x+(0-y)" into "(x-y)"293if( op2 == Op_SubI && phase->type(in2->in(1)) == TypeInt::ZERO )294return new (phase->C) SubINode(in1, in2->in(2) );295296// Convert "(0-y)+x" into "(x-y)"297if( op1 == Op_SubI && phase->type(in1->in(1)) == TypeInt::ZERO )298return new (phase->C) SubINode( in2, in1->in(2) );299300// Convert (x>>>z)+y into (x+(y<<z))>>>z for small constant z and y.301// Helps with array allocation math constant folding302// See 4790063:303// Unrestricted transformation is unsafe for some runtime values of 'x'304// ( x == 0, z == 1, y == -1 ) fails305// ( x == -5, z == 1, y == 1 ) fails306// Transform works for small z and small negative y when the addition307// (x + (y << z)) does not cross zero.308// Implement support for negative y and (x >= -(y << z))309// Have not observed cases where type information exists to support310// positive y and (x <= -(y << z))311if( op1 == Op_URShiftI && op2 == Op_ConI &&312in1->in(2)->Opcode() == Op_ConI ) {313jint z = phase->type( in1->in(2) )->is_int()->get_con() & 0x1f; // only least significant 5 bits matter314jint y = phase->type( in2 )->is_int()->get_con();315316if( z < 5 && -5 < y && y < 0 ) {317const Type *t_in11 = phase->type(in1->in(1));318if( t_in11 != Type::TOP && (t_in11->is_int()->_lo >= -(y << z)) ) {319Node *a = phase->transform( new (phase->C) AddINode( in1->in(1), phase->intcon(y<<z) ) );320return new (phase->C) URShiftINode( a, in1->in(2) );321}322}323}324325return AddNode::Ideal(phase, can_reshape);326}327328329//------------------------------Identity---------------------------------------330// Fold (x-y)+y OR y+(x-y) into x331Node *AddINode::Identity( PhaseTransform *phase ) {332if( in(1)->Opcode() == Op_SubI && phase->eqv(in(1)->in(2),in(2)) ) {333return in(1)->in(1);334}335else if( in(2)->Opcode() == Op_SubI && phase->eqv(in(2)->in(2),in(1)) ) {336return in(2)->in(1);337}338return AddNode::Identity(phase);339}340341342//------------------------------add_ring---------------------------------------343// Supplied function returns the sum of the inputs. Guaranteed never344// to be passed a TOP or BOTTOM type, these are filtered out by345// pre-check.346const Type *AddINode::add_ring( const Type *t0, const Type *t1 ) const {347const TypeInt *r0 = t0->is_int(); // Handy access348const TypeInt *r1 = t1->is_int();349int lo = java_add(r0->_lo, r1->_lo);350int hi = java_add(r0->_hi, r1->_hi);351if( !(r0->is_con() && r1->is_con()) ) {352// Not both constants, compute approximate result353if( (r0->_lo & r1->_lo) < 0 && lo >= 0 ) {354lo = min_jint; hi = max_jint; // Underflow on the low side355}356if( (~(r0->_hi | r1->_hi)) < 0 && hi < 0 ) {357lo = min_jint; hi = max_jint; // Overflow on the high side358}359if( lo > hi ) { // Handle overflow360lo = min_jint; hi = max_jint;361}362} else {363// both constants, compute precise result using 'lo' and 'hi'364// Semantics define overflow and underflow for integer addition365// as expected. In particular: 0x80000000 + 0x80000000 --> 0x0366}367return TypeInt::make( lo, hi, MAX2(r0->_widen,r1->_widen) );368}369370371//=============================================================================372//------------------------------Idealize---------------------------------------373Node *AddLNode::Ideal(PhaseGVN *phase, bool can_reshape) {374Node* in1 = in(1);375Node* in2 = in(2);376int op1 = in1->Opcode();377int op2 = in2->Opcode();378// Fold (con1-x)+con2 into (con1+con2)-x379if ( op1 == Op_AddL && op2 == Op_SubL ) {380// Swap edges to try optimizations below381in1 = in2;382in2 = in(1);383op1 = op2;384op2 = in2->Opcode();385}386// Fold (con1-x)+con2 into (con1+con2)-x387if( op1 == Op_SubL ) {388const Type *t_sub1 = phase->type( in1->in(1) );389const Type *t_2 = phase->type( in2 );390if( t_sub1->singleton() && t_2->singleton() && t_sub1 != Type::TOP && t_2 != Type::TOP )391return new (phase->C) SubLNode(phase->makecon( add_ring( t_sub1, t_2 ) ),392in1->in(2) );393// Convert "(a-b)+(c-d)" into "(a+c)-(b+d)"394if( op2 == Op_SubL ) {395// Check for dead cycle: d = (a-b)+(c-d)396assert( in1->in(2) != this && in2->in(2) != this,397"dead loop in AddLNode::Ideal" );398Node *sub = new (phase->C) SubLNode(NULL, NULL);399sub->init_req(1, phase->transform(new (phase->C) AddLNode(in1->in(1), in2->in(1) ) ));400sub->init_req(2, phase->transform(new (phase->C) AddLNode(in1->in(2), in2->in(2) ) ));401return sub;402}403// Convert "(a-b)+(b+c)" into "(a+c)"404if( op2 == Op_AddL && in1->in(2) == in2->in(1) ) {405assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddLNode::Ideal");406return new (phase->C) AddLNode(in1->in(1), in2->in(2));407}408// Convert "(a-b)+(c+b)" into "(a+c)"409if( op2 == Op_AddL && in1->in(2) == in2->in(2) ) {410assert(in1->in(1) != this && in2->in(1) != this,"dead loop in AddLNode::Ideal");411return new (phase->C) AddLNode(in1->in(1), in2->in(1));412}413// Convert "(a-b)+(b-c)" into "(a-c)"414if( op2 == Op_SubL && in1->in(2) == in2->in(1) ) {415assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddLNode::Ideal");416return new (phase->C) SubLNode(in1->in(1), in2->in(2));417}418// Convert "(a-b)+(c-a)" into "(c-b)"419if( op2 == Op_SubL && in1->in(1) == in1->in(2) ) {420assert(in1->in(2) != this && in2->in(1) != this,"dead loop in AddLNode::Ideal");421return new (phase->C) SubLNode(in2->in(1), in1->in(2));422}423}424425// Convert "x+(0-y)" into "(x-y)"426if( op2 == Op_SubL && phase->type(in2->in(1)) == TypeLong::ZERO )427return new (phase->C) SubLNode( in1, in2->in(2) );428429// Convert "(0-y)+x" into "(x-y)"430if( op1 == Op_SubL && phase->type(in1->in(1)) == TypeInt::ZERO )431return new (phase->C) SubLNode( in2, in1->in(2) );432433// Convert "X+X+X+X+X...+X+Y" into "k*X+Y" or really convert "X+(X+Y)"434// into "(X<<1)+Y" and let shift-folding happen.435if( op2 == Op_AddL &&436in2->in(1) == in1 &&437op1 != Op_ConL &&4380 ) {439Node *shift = phase->transform(new (phase->C) LShiftLNode(in1,phase->intcon(1)));440return new (phase->C) AddLNode(shift,in2->in(2));441}442443return AddNode::Ideal(phase, can_reshape);444}445446447//------------------------------Identity---------------------------------------448// Fold (x-y)+y OR y+(x-y) into x449Node *AddLNode::Identity( PhaseTransform *phase ) {450if( in(1)->Opcode() == Op_SubL && phase->eqv(in(1)->in(2),in(2)) ) {451return in(1)->in(1);452}453else if( in(2)->Opcode() == Op_SubL && phase->eqv(in(2)->in(2),in(1)) ) {454return in(2)->in(1);455}456return AddNode::Identity(phase);457}458459460//------------------------------add_ring---------------------------------------461// Supplied function returns the sum of the inputs. Guaranteed never462// to be passed a TOP or BOTTOM type, these are filtered out by463// pre-check.464const Type *AddLNode::add_ring( const Type *t0, const Type *t1 ) const {465const TypeLong *r0 = t0->is_long(); // Handy access466const TypeLong *r1 = t1->is_long();467jlong lo = java_add(r0->_lo, r1->_lo);468jlong hi = java_add(r0->_hi, r1->_hi);469if( !(r0->is_con() && r1->is_con()) ) {470// Not both constants, compute approximate result471if( (r0->_lo & r1->_lo) < 0 && lo >= 0 ) {472lo =min_jlong; hi = max_jlong; // Underflow on the low side473}474if( (~(r0->_hi | r1->_hi)) < 0 && hi < 0 ) {475lo = min_jlong; hi = max_jlong; // Overflow on the high side476}477if( lo > hi ) { // Handle overflow478lo = min_jlong; hi = max_jlong;479}480} else {481// both constants, compute precise result using 'lo' and 'hi'482// Semantics define overflow and underflow for integer addition483// as expected. In particular: 0x80000000 + 0x80000000 --> 0x0484}485return TypeLong::make( lo, hi, MAX2(r0->_widen,r1->_widen) );486}487488489//=============================================================================490//------------------------------add_of_identity--------------------------------491// Check for addition of the identity492const Type *AddFNode::add_of_identity( const Type *t1, const Type *t2 ) const {493// x ADD 0 should return x unless 'x' is a -zero494//495// const Type *zero = add_id(); // The additive identity496// jfloat f1 = t1->getf();497// jfloat f2 = t2->getf();498//499// if( t1->higher_equal( zero ) ) return t2;500// if( t2->higher_equal( zero ) ) return t1;501502return NULL;503}504505//------------------------------add_ring---------------------------------------506// Supplied function returns the sum of the inputs.507// This also type-checks the inputs for sanity. Guaranteed never to508// be passed a TOP or BOTTOM type, these are filtered out by pre-check.509const Type *AddFNode::add_ring( const Type *t0, const Type *t1 ) const {510// We must be adding 2 float constants.511return TypeF::make( t0->getf() + t1->getf() );512}513514//------------------------------Ideal------------------------------------------515Node *AddFNode::Ideal(PhaseGVN *phase, bool can_reshape) {516if( IdealizedNumerics && !phase->C->method()->is_strict() ) {517return AddNode::Ideal(phase, can_reshape); // commutative and associative transforms518}519520// Floating point additions are not associative because of boundary conditions (infinity)521return commute(this,522phase->type( in(1) )->singleton(),523phase->type( in(2) )->singleton() ) ? this : NULL;524}525526527//=============================================================================528//------------------------------add_of_identity--------------------------------529// Check for addition of the identity530const Type *AddDNode::add_of_identity( const Type *t1, const Type *t2 ) const {531// x ADD 0 should return x unless 'x' is a -zero532//533// const Type *zero = add_id(); // The additive identity534// jfloat f1 = t1->getf();535// jfloat f2 = t2->getf();536//537// if( t1->higher_equal( zero ) ) return t2;538// if( t2->higher_equal( zero ) ) return t1;539540return NULL;541}542//------------------------------add_ring---------------------------------------543// Supplied function returns the sum of the inputs.544// This also type-checks the inputs for sanity. Guaranteed never to545// be passed a TOP or BOTTOM type, these are filtered out by pre-check.546const Type *AddDNode::add_ring( const Type *t0, const Type *t1 ) const {547// We must be adding 2 double constants.548return TypeD::make( t0->getd() + t1->getd() );549}550551//------------------------------Ideal------------------------------------------552Node *AddDNode::Ideal(PhaseGVN *phase, bool can_reshape) {553if( IdealizedNumerics && !phase->C->method()->is_strict() ) {554return AddNode::Ideal(phase, can_reshape); // commutative and associative transforms555}556557// Floating point additions are not associative because of boundary conditions (infinity)558return commute(this,559phase->type( in(1) )->singleton(),560phase->type( in(2) )->singleton() ) ? this : NULL;561}562563564//=============================================================================565//------------------------------Identity---------------------------------------566// If one input is a constant 0, return the other input.567Node *AddPNode::Identity( PhaseTransform *phase ) {568return ( phase->type( in(Offset) )->higher_equal( TypeX_ZERO ) ) ? in(Address) : this;569}570571//------------------------------Idealize---------------------------------------572Node *AddPNode::Ideal(PhaseGVN *phase, bool can_reshape) {573// Bail out if dead inputs574if( phase->type( in(Address) ) == Type::TOP ) return NULL;575576// If the left input is an add of a constant, flatten the expression tree.577const Node *n = in(Address);578if (n->is_AddP() && n->in(Base) == in(Base)) {579const AddPNode *addp = n->as_AddP(); // Left input is an AddP580assert( !addp->in(Address)->is_AddP() ||581addp->in(Address)->as_AddP() != addp,582"dead loop in AddPNode::Ideal" );583// Type of left input's right input584const Type *t = phase->type( addp->in(Offset) );585if( t == Type::TOP ) return NULL;586const TypeX *t12 = t->is_intptr_t();587if( t12->is_con() ) { // Left input is an add of a constant?588// If the right input is a constant, combine constants589const Type *temp_t2 = phase->type( in(Offset) );590if( temp_t2 == Type::TOP ) return NULL;591const TypeX *t2 = temp_t2->is_intptr_t();592Node* address;593Node* offset;594if( t2->is_con() ) {595// The Add of the flattened expression596address = addp->in(Address);597offset = phase->MakeConX(t2->get_con() + t12->get_con());598} else {599// Else move the constant to the right. ((A+con)+B) into ((A+B)+con)600address = phase->transform(new (phase->C) AddPNode(in(Base),addp->in(Address),in(Offset)));601offset = addp->in(Offset);602}603PhaseIterGVN *igvn = phase->is_IterGVN();604if( igvn ) {605set_req_X(Address,address,igvn);606set_req_X(Offset,offset,igvn);607} else {608set_req(Address,address);609set_req(Offset,offset);610}611return this;612}613}614615// Raw pointers?616if( in(Base)->bottom_type() == Type::TOP ) {617// If this is a NULL+long form (from unsafe accesses), switch to a rawptr.618if (phase->type(in(Address)) == TypePtr::NULL_PTR) {619Node* offset = in(Offset);620return new (phase->C) CastX2PNode(offset);621}622}623624// If the right is an add of a constant, push the offset down.625// Convert: (ptr + (offset+con)) into (ptr+offset)+con.626// The idea is to merge array_base+scaled_index groups together,627// and only have different constant offsets from the same base.628const Node *add = in(Offset);629if( add->Opcode() == Op_AddX && add->in(1) != add ) {630const Type *t22 = phase->type( add->in(2) );631if( t22->singleton() && (t22 != Type::TOP) ) { // Right input is an add of a constant?632set_req(Address, phase->transform(new (phase->C) AddPNode(in(Base),in(Address),add->in(1))));633set_req(Offset, add->in(2));634PhaseIterGVN *igvn = phase->is_IterGVN();635if (add->outcnt() == 0 && igvn) {636// add disconnected.637igvn->_worklist.push((Node*)add);638}639return this; // Made progress640}641}642643return NULL; // No progress644}645646//------------------------------bottom_type------------------------------------647// Bottom-type is the pointer-type with unknown offset.648const Type *AddPNode::bottom_type() const {649if (in(Address) == NULL) return TypePtr::BOTTOM;650const TypePtr *tp = in(Address)->bottom_type()->isa_ptr();651if( !tp ) return Type::TOP; // TOP input means TOP output652assert( in(Offset)->Opcode() != Op_ConP, "" );653const Type *t = in(Offset)->bottom_type();654if( t == Type::TOP )655return tp->add_offset(Type::OffsetTop);656const TypeX *tx = t->is_intptr_t();657intptr_t txoffset = Type::OffsetBot;658if (tx->is_con()) { // Left input is an add of a constant?659txoffset = tx->get_con();660}661return tp->add_offset(txoffset);662}663664//------------------------------Value------------------------------------------665const Type *AddPNode::Value( PhaseTransform *phase ) const {666// Either input is TOP ==> the result is TOP667const Type *t1 = phase->type( in(Address) );668const Type *t2 = phase->type( in(Offset) );669if( t1 == Type::TOP ) return Type::TOP;670if( t2 == Type::TOP ) return Type::TOP;671672// Left input is a pointer673const TypePtr *p1 = t1->isa_ptr();674// Right input is an int675const TypeX *p2 = t2->is_intptr_t();676// Add 'em677intptr_t p2offset = Type::OffsetBot;678if (p2->is_con()) { // Left input is an add of a constant?679p2offset = p2->get_con();680}681return p1->add_offset(p2offset);682}683684//------------------------Ideal_base_and_offset--------------------------------685// Split an oop pointer into a base and offset.686// (The offset might be Type::OffsetBot in the case of an array.)687// Return the base, or NULL if failure.688Node* AddPNode::Ideal_base_and_offset(Node* ptr, PhaseTransform* phase,689// second return value:690intptr_t& offset) {691if (ptr->is_AddP()) {692Node* base = ptr->in(AddPNode::Base);693Node* addr = ptr->in(AddPNode::Address);694Node* offs = ptr->in(AddPNode::Offset);695if (base == addr || base->is_top()) {696offset = phase->find_intptr_t_con(offs, Type::OffsetBot);697if (offset != Type::OffsetBot) {698return addr;699}700}701}702offset = Type::OffsetBot;703return NULL;704}705706//------------------------------unpack_offsets----------------------------------707// Collect the AddP offset values into the elements array, giving up708// if there are more than length.709int AddPNode::unpack_offsets(Node* elements[], int length) {710int count = 0;711Node* addr = this;712Node* base = addr->in(AddPNode::Base);713while (addr->is_AddP()) {714if (addr->in(AddPNode::Base) != base) {715// give up716return -1;717}718elements[count++] = addr->in(AddPNode::Offset);719if (count == length) {720// give up721return -1;722}723addr = addr->in(AddPNode::Address);724}725if (addr != base) {726return -1;727}728return count;729}730731//------------------------------match_edge-------------------------------------732// Do we Match on this edge index or not? Do not match base pointer edge733uint AddPNode::match_edge(uint idx) const {734return idx > Base;735}736737//=============================================================================738//------------------------------Identity---------------------------------------739Node *OrINode::Identity( PhaseTransform *phase ) {740// x | x => x741if (phase->eqv(in(1), in(2))) {742return in(1);743}744745return AddNode::Identity(phase);746}747748//------------------------------add_ring---------------------------------------749// Supplied function returns the sum of the inputs IN THE CURRENT RING. For750// the logical operations the ring's ADD is really a logical OR function.751// This also type-checks the inputs for sanity. Guaranteed never to752// be passed a TOP or BOTTOM type, these are filtered out by pre-check.753const Type *OrINode::add_ring( const Type *t0, const Type *t1 ) const {754const TypeInt *r0 = t0->is_int(); // Handy access755const TypeInt *r1 = t1->is_int();756757// If both args are bool, can figure out better types758if ( r0 == TypeInt::BOOL ) {759if ( r1 == TypeInt::ONE) {760return TypeInt::ONE;761} else if ( r1 == TypeInt::BOOL ) {762return TypeInt::BOOL;763}764} else if ( r0 == TypeInt::ONE ) {765if ( r1 == TypeInt::BOOL ) {766return TypeInt::ONE;767}768}769770// If either input is not a constant, just return all integers.771if( !r0->is_con() || !r1->is_con() )772return TypeInt::INT; // Any integer, but still no symbols.773774// Otherwise just OR them bits.775return TypeInt::make( r0->get_con() | r1->get_con() );776}777778//=============================================================================779//------------------------------Identity---------------------------------------780Node *OrLNode::Identity( PhaseTransform *phase ) {781// x | x => x782if (phase->eqv(in(1), in(2))) {783return in(1);784}785786return AddNode::Identity(phase);787}788789//------------------------------add_ring---------------------------------------790const Type *OrLNode::add_ring( const Type *t0, const Type *t1 ) const {791const TypeLong *r0 = t0->is_long(); // Handy access792const TypeLong *r1 = t1->is_long();793794// If either input is not a constant, just return all integers.795if( !r0->is_con() || !r1->is_con() )796return TypeLong::LONG; // Any integer, but still no symbols.797798// Otherwise just OR them bits.799return TypeLong::make( r0->get_con() | r1->get_con() );800}801802//=============================================================================803//------------------------------add_ring---------------------------------------804// Supplied function returns the sum of the inputs IN THE CURRENT RING. For805// the logical operations the ring's ADD is really a logical OR function.806// This also type-checks the inputs for sanity. Guaranteed never to807// be passed a TOP or BOTTOM type, these are filtered out by pre-check.808const Type *XorINode::add_ring( const Type *t0, const Type *t1 ) const {809const TypeInt *r0 = t0->is_int(); // Handy access810const TypeInt *r1 = t1->is_int();811812// Complementing a boolean?813if( r0 == TypeInt::BOOL && ( r1 == TypeInt::ONE814|| r1 == TypeInt::BOOL))815return TypeInt::BOOL;816817if( !r0->is_con() || !r1->is_con() ) // Not constants818return TypeInt::INT; // Any integer, but still no symbols.819820// Otherwise just XOR them bits.821return TypeInt::make( r0->get_con() ^ r1->get_con() );822}823824//=============================================================================825//------------------------------add_ring---------------------------------------826const Type *XorLNode::add_ring( const Type *t0, const Type *t1 ) const {827const TypeLong *r0 = t0->is_long(); // Handy access828const TypeLong *r1 = t1->is_long();829830// If either input is not a constant, just return all integers.831if( !r0->is_con() || !r1->is_con() )832return TypeLong::LONG; // Any integer, but still no symbols.833834// Otherwise just OR them bits.835return TypeLong::make( r0->get_con() ^ r1->get_con() );836}837838//=============================================================================839//------------------------------add_ring---------------------------------------840// Supplied function returns the sum of the inputs.841const Type *MaxINode::add_ring( const Type *t0, const Type *t1 ) const {842const TypeInt *r0 = t0->is_int(); // Handy access843const TypeInt *r1 = t1->is_int();844845// Otherwise just MAX them bits.846return TypeInt::make( MAX2(r0->_lo,r1->_lo), MAX2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) );847}848849// Check if addition of an integer with type 't' and a constant 'c' can overflow850static bool can_overflow(const TypeInt* t, jint c) {851jint t_lo = t->_lo;852jint t_hi = t->_hi;853return ((c < 0 && (java_add(t_lo, c) > t_lo)) ||854(c > 0 && (java_add(t_hi, c) < t_hi)));855}856857//=============================================================================858//------------------------------Idealize---------------------------------------859// MINs show up in range-check loop limit calculations. Look for860// "MIN2(x+c0,MIN2(y,x+c1))". Pick the smaller constant: "MIN2(x+c0,y)"861Node *MinINode::Ideal(PhaseGVN *phase, bool can_reshape) {862Node *progress = NULL;863// Force a right-spline graph864Node *l = in(1);865Node *r = in(2);866// Transform MinI1( MinI2(a,b), c) into MinI1( a, MinI2(b,c) )867// to force a right-spline graph for the rest of MinINode::Ideal().868if( l->Opcode() == Op_MinI ) {869assert( l != l->in(1), "dead loop in MinINode::Ideal" );870r = phase->transform(new (phase->C) MinINode(l->in(2),r));871l = l->in(1);872set_req(1, l);873set_req(2, r);874return this;875}876877// Get left input & constant878Node *x = l;879jint x_off = 0;880if( x->Opcode() == Op_AddI && // Check for "x+c0" and collect constant881x->in(2)->is_Con() ) {882const Type *t = x->in(2)->bottom_type();883if( t == Type::TOP ) return NULL; // No progress884x_off = t->is_int()->get_con();885x = x->in(1);886}887888// Scan a right-spline-tree for MINs889Node *y = r;890jint y_off = 0;891// Check final part of MIN tree892if( y->Opcode() == Op_AddI && // Check for "y+c1" and collect constant893y->in(2)->is_Con() ) {894const Type *t = y->in(2)->bottom_type();895if( t == Type::TOP ) return NULL; // No progress896y_off = t->is_int()->get_con();897y = y->in(1);898}899if( x->_idx > y->_idx && r->Opcode() != Op_MinI ) {900swap_edges(1, 2);901return this;902}903904const TypeInt* tx = phase->type(x)->isa_int();905906if( r->Opcode() == Op_MinI ) {907assert( r != r->in(2), "dead loop in MinINode::Ideal" );908y = r->in(1);909// Check final part of MIN tree910if( y->Opcode() == Op_AddI &&// Check for "y+c1" and collect constant911y->in(2)->is_Con() ) {912const Type *t = y->in(2)->bottom_type();913if( t == Type::TOP ) return NULL; // No progress914y_off = t->is_int()->get_con();915y = y->in(1);916}917918if( x->_idx > y->_idx )919return new (phase->C) MinINode(r->in(1),phase->transform(new (phase->C) MinINode(l,r->in(2))));920921// Transform MIN2(x + c0, MIN2(x + c1, z)) into MIN2(x + MIN2(c0, c1), z)922// if x == y and the additions can't overflow.923if (phase->eqv(x,y) && tx != NULL &&924!can_overflow(tx, x_off) &&925!can_overflow(tx, y_off)) {926return new (phase->C) MinINode(phase->transform(new (phase->C) AddINode(x, phase->intcon(MIN2(x_off, y_off)))), r->in(2));927}928} else {929// Transform MIN2(x + c0, y + c1) into x + MIN2(c0, c1)930// if x == y and the additions can't overflow.931if (phase->eqv(x,y) && tx != NULL &&932!can_overflow(tx, x_off) &&933!can_overflow(tx, y_off)) {934return new (phase->C) AddINode(x,phase->intcon(MIN2(x_off,y_off)));935}936}937return NULL;938}939940//------------------------------add_ring---------------------------------------941// Supplied function returns the sum of the inputs.942const Type *MinINode::add_ring( const Type *t0, const Type *t1 ) const {943const TypeInt *r0 = t0->is_int(); // Handy access944const TypeInt *r1 = t1->is_int();945946// Otherwise just MIN them bits.947return TypeInt::make( MIN2(r0->_lo,r1->_lo), MIN2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) );948}949950951