Path: blob/jdk8u272-b10-aarch32-20201026/hotspot/src/share/vm/opto/addnode.cpp
83404 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"3334// Portions of code courtesy of Clifford Click3536// Classic Add functionality. This covers all the usual 'add' behaviors for37// an algebraic ring. Add-integer, add-float, add-double, and binary-or are38// all inherited from this class. The various identity values are supplied39// by virtual functions.404142//=============================================================================43//------------------------------hash-------------------------------------------44// Hash function over AddNodes. Needs to be commutative; i.e., I swap45// (commute) inputs to AddNodes willy-nilly so the hash function must return46// the same value in the presence of edge swapping.47uint AddNode::hash() const {48return (uintptr_t)in(1) + (uintptr_t)in(2) + Opcode();49}5051//------------------------------Identity---------------------------------------52// If either input is a constant 0, return the other input.53Node *AddNode::Identity( PhaseTransform *phase ) {54const Type *zero = add_id(); // The additive identity55if( phase->type( in(1) )->higher_equal( zero ) ) return in(2);56if( phase->type( in(2) )->higher_equal( zero ) ) return in(1);57return this;58}5960//------------------------------commute----------------------------------------61// Commute operands to move loads and constants to the right.62static bool commute( Node *add, int con_left, int con_right ) {63Node *in1 = add->in(1);64Node *in2 = add->in(2);6566// Convert "1+x" into "x+1".67// Right is a constant; leave it68if( con_right ) return false;69// Left is a constant; move it right.70if( con_left ) {71add->swap_edges(1, 2);72return true;73}7475// Convert "Load+x" into "x+Load".76// Now check for loads77if (in2->is_Load()) {78if (!in1->is_Load()) {79// already x+Load to return80return false;81}82// both are loads, so fall through to sort inputs by idx83} else if( in1->is_Load() ) {84// Left is a Load and Right is not; move it right.85add->swap_edges(1, 2);86return true;87}8889PhiNode *phi;90// Check for tight loop increments: Loop-phi of Add of loop-phi91if( in1->is_Phi() && (phi = in1->as_Phi()) && !phi->is_copy() && phi->region()->is_Loop() && phi->in(2)==add)92return false;93if( in2->is_Phi() && (phi = in2->as_Phi()) && !phi->is_copy() && phi->region()->is_Loop() && phi->in(2)==add){94add->swap_edges(1, 2);95return true;96}9798// Otherwise, sort inputs (commutativity) to help value numbering.99if( in1->_idx > in2->_idx ) {100add->swap_edges(1, 2);101return true;102}103return false;104}105106//------------------------------Idealize---------------------------------------107// If we get here, we assume we are associative!108Node *AddNode::Ideal(PhaseGVN *phase, bool can_reshape) {109const Type *t1 = phase->type( in(1) );110const Type *t2 = phase->type( in(2) );111int con_left = t1->singleton();112int con_right = t2->singleton();113114// Check for commutative operation desired115if( commute(this,con_left,con_right) ) return this;116117AddNode *progress = NULL; // Progress flag118119// Convert "(x+1)+2" into "x+(1+2)". If the right input is a120// constant, and the left input is an add of a constant, flatten the121// expression tree.122Node *add1 = in(1);123Node *add2 = in(2);124int add1_op = add1->Opcode();125int this_op = Opcode();126if( con_right && t2 != Type::TOP && // Right input is a constant?127add1_op == this_op ) { // Left input is an Add?128129// Type of left _in right input130const Type *t12 = phase->type( add1->in(2) );131if( t12->singleton() && t12 != Type::TOP ) { // Left input is an add of a constant?132// Check for rare case of closed data cycle which can happen inside133// unreachable loops. In these cases the computation is undefined.134#ifdef ASSERT135Node *add11 = add1->in(1);136int add11_op = add11->Opcode();137if( (add1 == add1->in(1))138|| (add11_op == this_op && add11->in(1) == add1) ) {139assert(false, "dead loop in AddNode::Ideal");140}141#endif142// The Add of the flattened expression143Node *x1 = add1->in(1);144Node *x2 = phase->makecon( add1->as_Add()->add_ring( t2, t12 ));145PhaseIterGVN *igvn = phase->is_IterGVN();146if( igvn ) {147set_req_X(2,x2,igvn);148set_req_X(1,x1,igvn);149} else {150set_req(2,x2);151set_req(1,x1);152}153progress = this; // Made progress154add1 = in(1);155add1_op = add1->Opcode();156}157}158159// Convert "(x+1)+y" into "(x+y)+1". Push constants down the expression tree.160if( add1_op == this_op && !con_right ) {161Node *a12 = add1->in(2);162const Type *t12 = phase->type( a12 );163if( t12->singleton() && t12 != Type::TOP && (add1 != add1->in(1)) &&164!(add1->in(1)->is_Phi() && add1->in(1)->as_Phi()->is_tripcount()) ) {165assert(add1->in(1) != this, "dead loop in AddNode::Ideal");166add2 = add1->clone();167add2->set_req(2, in(2));168add2 = phase->transform(add2);169set_req(1, add2);170set_req(2, a12);171progress = this;172add2 = a12;173}174}175176// Convert "x+(y+1)" into "(x+y)+1". Push constants down the expression tree.177int add2_op = add2->Opcode();178if( add2_op == this_op && !con_left ) {179Node *a22 = add2->in(2);180const Type *t22 = phase->type( a22 );181if( t22->singleton() && t22 != Type::TOP && (add2 != add2->in(1)) &&182!(add2->in(1)->is_Phi() && add2->in(1)->as_Phi()->is_tripcount()) ) {183assert(add2->in(1) != this, "dead loop in AddNode::Ideal");184Node *addx = add2->clone();185addx->set_req(1, in(1));186addx->set_req(2, add2->in(1));187addx = phase->transform(addx);188set_req(1, addx);189set_req(2, a22);190progress = this;191PhaseIterGVN *igvn = phase->is_IterGVN();192if (add2->outcnt() == 0 && igvn) {193// add disconnected.194igvn->_worklist.push(add2);195}196}197}198199return progress;200}201202//------------------------------Value-----------------------------------------203// An add node sums it's two _in. If one input is an RSD, we must mixin204// the other input's symbols.205const Type *AddNode::Value( PhaseTransform *phase ) const {206// Either input is TOP ==> the result is TOP207const Type *t1 = phase->type( in(1) );208const Type *t2 = phase->type( in(2) );209if( t1 == Type::TOP ) return Type::TOP;210if( t2 == Type::TOP ) return Type::TOP;211212// Either input is BOTTOM ==> the result is the local BOTTOM213const Type *bot = bottom_type();214if( (t1 == bot) || (t2 == bot) ||215(t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )216return bot;217218// Check for an addition involving the additive identity219const Type *tadd = add_of_identity( t1, t2 );220if( tadd ) return tadd;221222return add_ring(t1,t2); // Local flavor of type addition223}224225//------------------------------add_identity-----------------------------------226// Check for addition of the identity227const Type *AddNode::add_of_identity( const Type *t1, const Type *t2 ) const {228const Type *zero = add_id(); // The additive identity229if( t1->higher_equal( zero ) ) return t2;230if( t2->higher_equal( zero ) ) return t1;231232return NULL;233}234235236//=============================================================================237//------------------------------Idealize---------------------------------------238Node *AddINode::Ideal(PhaseGVN *phase, bool can_reshape) {239Node* in1 = in(1);240Node* in2 = in(2);241int op1 = in1->Opcode();242int op2 = in2->Opcode();243// Fold (con1-x)+con2 into (con1+con2)-x244if ( op1 == Op_AddI && op2 == Op_SubI ) {245// Swap edges to try optimizations below246in1 = in2;247in2 = in(1);248op1 = op2;249op2 = in2->Opcode();250}251if( op1 == Op_SubI ) {252const Type *t_sub1 = phase->type( in1->in(1) );253const Type *t_2 = phase->type( in2 );254if( t_sub1->singleton() && t_2->singleton() && t_sub1 != Type::TOP && t_2 != Type::TOP )255return new (phase->C) SubINode(phase->makecon( add_ring( t_sub1, t_2 ) ),256in1->in(2) );257// Convert "(a-b)+(c-d)" into "(a+c)-(b+d)"258if( op2 == Op_SubI ) {259// Check for dead cycle: d = (a-b)+(c-d)260assert( in1->in(2) != this && in2->in(2) != this,261"dead loop in AddINode::Ideal" );262Node *sub = new (phase->C) SubINode(NULL, NULL);263sub->init_req(1, phase->transform(new (phase->C) AddINode(in1->in(1), in2->in(1) ) ));264sub->init_req(2, phase->transform(new (phase->C) AddINode(in1->in(2), in2->in(2) ) ));265return sub;266}267// Convert "(a-b)+(b+c)" into "(a+c)"268if( op2 == Op_AddI && in1->in(2) == in2->in(1) ) {269assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddINode::Ideal");270return new (phase->C) AddINode(in1->in(1), in2->in(2));271}272// Convert "(a-b)+(c+b)" into "(a+c)"273if( op2 == Op_AddI && in1->in(2) == in2->in(2) ) {274assert(in1->in(1) != this && in2->in(1) != this,"dead loop in AddINode::Ideal");275return new (phase->C) AddINode(in1->in(1), in2->in(1));276}277// Convert "(a-b)+(b-c)" into "(a-c)"278if( op2 == Op_SubI && in1->in(2) == in2->in(1) ) {279assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddINode::Ideal");280return new (phase->C) SubINode(in1->in(1), in2->in(2));281}282// Convert "(a-b)+(c-a)" into "(c-b)"283if( op2 == Op_SubI && in1->in(1) == in2->in(2) ) {284assert(in1->in(2) != this && in2->in(1) != this,"dead loop in AddINode::Ideal");285return new (phase->C) SubINode(in2->in(1), in1->in(2));286}287}288289// Convert "x+(0-y)" into "(x-y)"290if( op2 == Op_SubI && phase->type(in2->in(1)) == TypeInt::ZERO )291return new (phase->C) SubINode(in1, in2->in(2) );292293// Convert "(0-y)+x" into "(x-y)"294if( op1 == Op_SubI && phase->type(in1->in(1)) == TypeInt::ZERO )295return new (phase->C) SubINode( in2, in1->in(2) );296297// Convert (x>>>z)+y into (x+(y<<z))>>>z for small constant z and y.298// Helps with array allocation math constant folding299// See 4790063:300// Unrestricted transformation is unsafe for some runtime values of 'x'301// ( x == 0, z == 1, y == -1 ) fails302// ( x == -5, z == 1, y == 1 ) fails303// Transform works for small z and small negative y when the addition304// (x + (y << z)) does not cross zero.305// Implement support for negative y and (x >= -(y << z))306// Have not observed cases where type information exists to support307// positive y and (x <= -(y << z))308if( op1 == Op_URShiftI && op2 == Op_ConI &&309in1->in(2)->Opcode() == Op_ConI ) {310jint z = phase->type( in1->in(2) )->is_int()->get_con() & 0x1f; // only least significant 5 bits matter311jint y = phase->type( in2 )->is_int()->get_con();312313if( z < 5 && -5 < y && y < 0 ) {314const Type *t_in11 = phase->type(in1->in(1));315if( t_in11 != Type::TOP && (t_in11->is_int()->_lo >= -(y << z)) ) {316Node *a = phase->transform( new (phase->C) AddINode( in1->in(1), phase->intcon(y<<z) ) );317return new (phase->C) URShiftINode( a, in1->in(2) );318}319}320}321322return AddNode::Ideal(phase, can_reshape);323}324325326//------------------------------Identity---------------------------------------327// Fold (x-y)+y OR y+(x-y) into x328Node *AddINode::Identity( PhaseTransform *phase ) {329if( in(1)->Opcode() == Op_SubI && phase->eqv(in(1)->in(2),in(2)) ) {330return in(1)->in(1);331}332else if( in(2)->Opcode() == Op_SubI && phase->eqv(in(2)->in(2),in(1)) ) {333return in(2)->in(1);334}335return AddNode::Identity(phase);336}337338339//------------------------------add_ring---------------------------------------340// Supplied function returns the sum of the inputs. Guaranteed never341// to be passed a TOP or BOTTOM type, these are filtered out by342// pre-check.343const Type *AddINode::add_ring( const Type *t0, const Type *t1 ) const {344const TypeInt *r0 = t0->is_int(); // Handy access345const TypeInt *r1 = t1->is_int();346int lo = java_add(r0->_lo, r1->_lo);347int hi = java_add(r0->_hi, r1->_hi);348if( !(r0->is_con() && r1->is_con()) ) {349// Not both constants, compute approximate result350if( (r0->_lo & r1->_lo) < 0 && lo >= 0 ) {351lo = min_jint; hi = max_jint; // Underflow on the low side352}353if( (~(r0->_hi | r1->_hi)) < 0 && hi < 0 ) {354lo = min_jint; hi = max_jint; // Overflow on the high side355}356if( lo > hi ) { // Handle overflow357lo = min_jint; hi = max_jint;358}359} else {360// both constants, compute precise result using 'lo' and 'hi'361// Semantics define overflow and underflow for integer addition362// as expected. In particular: 0x80000000 + 0x80000000 --> 0x0363}364return TypeInt::make( lo, hi, MAX2(r0->_widen,r1->_widen) );365}366367368//=============================================================================369//------------------------------Idealize---------------------------------------370Node *AddLNode::Ideal(PhaseGVN *phase, bool can_reshape) {371Node* in1 = in(1);372Node* in2 = in(2);373int op1 = in1->Opcode();374int op2 = in2->Opcode();375// Fold (con1-x)+con2 into (con1+con2)-x376if ( op1 == Op_AddL && op2 == Op_SubL ) {377// Swap edges to try optimizations below378in1 = in2;379in2 = in(1);380op1 = op2;381op2 = in2->Opcode();382}383// Fold (con1-x)+con2 into (con1+con2)-x384if( op1 == Op_SubL ) {385const Type *t_sub1 = phase->type( in1->in(1) );386const Type *t_2 = phase->type( in2 );387if( t_sub1->singleton() && t_2->singleton() && t_sub1 != Type::TOP && t_2 != Type::TOP )388return new (phase->C) SubLNode(phase->makecon( add_ring( t_sub1, t_2 ) ),389in1->in(2) );390// Convert "(a-b)+(c-d)" into "(a+c)-(b+d)"391if( op2 == Op_SubL ) {392// Check for dead cycle: d = (a-b)+(c-d)393assert( in1->in(2) != this && in2->in(2) != this,394"dead loop in AddLNode::Ideal" );395Node *sub = new (phase->C) SubLNode(NULL, NULL);396sub->init_req(1, phase->transform(new (phase->C) AddLNode(in1->in(1), in2->in(1) ) ));397sub->init_req(2, phase->transform(new (phase->C) AddLNode(in1->in(2), in2->in(2) ) ));398return sub;399}400// Convert "(a-b)+(b+c)" into "(a+c)"401if( op2 == Op_AddL && in1->in(2) == in2->in(1) ) {402assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddLNode::Ideal");403return new (phase->C) AddLNode(in1->in(1), in2->in(2));404}405// Convert "(a-b)+(c+b)" into "(a+c)"406if( op2 == Op_AddL && in1->in(2) == in2->in(2) ) {407assert(in1->in(1) != this && in2->in(1) != this,"dead loop in AddLNode::Ideal");408return new (phase->C) AddLNode(in1->in(1), in2->in(1));409}410// Convert "(a-b)+(b-c)" into "(a-c)"411if( op2 == Op_SubL && in1->in(2) == in2->in(1) ) {412assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddLNode::Ideal");413return new (phase->C) SubLNode(in1->in(1), in2->in(2));414}415// Convert "(a-b)+(c-a)" into "(c-b)"416if( op2 == Op_SubL && in1->in(1) == in1->in(2) ) {417assert(in1->in(2) != this && in2->in(1) != this,"dead loop in AddLNode::Ideal");418return new (phase->C) SubLNode(in2->in(1), in1->in(2));419}420}421422// Convert "x+(0-y)" into "(x-y)"423if( op2 == Op_SubL && phase->type(in2->in(1)) == TypeLong::ZERO )424return new (phase->C) SubLNode( in1, in2->in(2) );425426// Convert "(0-y)+x" into "(x-y)"427if( op1 == Op_SubL && phase->type(in1->in(1)) == TypeInt::ZERO )428return new (phase->C) SubLNode( in2, in1->in(2) );429430// Convert "X+X+X+X+X...+X+Y" into "k*X+Y" or really convert "X+(X+Y)"431// into "(X<<1)+Y" and let shift-folding happen.432if( op2 == Op_AddL &&433in2->in(1) == in1 &&434op1 != Op_ConL &&4350 ) {436Node *shift = phase->transform(new (phase->C) LShiftLNode(in1,phase->intcon(1)));437return new (phase->C) AddLNode(shift,in2->in(2));438}439440return AddNode::Ideal(phase, can_reshape);441}442443444//------------------------------Identity---------------------------------------445// Fold (x-y)+y OR y+(x-y) into x446Node *AddLNode::Identity( PhaseTransform *phase ) {447if( in(1)->Opcode() == Op_SubL && phase->eqv(in(1)->in(2),in(2)) ) {448return in(1)->in(1);449}450else if( in(2)->Opcode() == Op_SubL && phase->eqv(in(2)->in(2),in(1)) ) {451return in(2)->in(1);452}453return AddNode::Identity(phase);454}455456457//------------------------------add_ring---------------------------------------458// Supplied function returns the sum of the inputs. Guaranteed never459// to be passed a TOP or BOTTOM type, these are filtered out by460// pre-check.461const Type *AddLNode::add_ring( const Type *t0, const Type *t1 ) const {462const TypeLong *r0 = t0->is_long(); // Handy access463const TypeLong *r1 = t1->is_long();464jlong lo = java_add(r0->_lo, r1->_lo);465jlong hi = java_add(r0->_hi, r1->_hi);466if( !(r0->is_con() && r1->is_con()) ) {467// Not both constants, compute approximate result468if( (r0->_lo & r1->_lo) < 0 && lo >= 0 ) {469lo =min_jlong; hi = max_jlong; // Underflow on the low side470}471if( (~(r0->_hi | r1->_hi)) < 0 && hi < 0 ) {472lo = min_jlong; hi = max_jlong; // Overflow on the high side473}474if( lo > hi ) { // Handle overflow475lo = min_jlong; hi = max_jlong;476}477} else {478// both constants, compute precise result using 'lo' and 'hi'479// Semantics define overflow and underflow for integer addition480// as expected. In particular: 0x80000000 + 0x80000000 --> 0x0481}482return TypeLong::make( lo, hi, MAX2(r0->_widen,r1->_widen) );483}484485486//=============================================================================487//------------------------------add_of_identity--------------------------------488// Check for addition of the identity489const Type *AddFNode::add_of_identity( const Type *t1, const Type *t2 ) const {490// x ADD 0 should return x unless 'x' is a -zero491//492// const Type *zero = add_id(); // The additive identity493// jfloat f1 = t1->getf();494// jfloat f2 = t2->getf();495//496// if( t1->higher_equal( zero ) ) return t2;497// if( t2->higher_equal( zero ) ) return t1;498499return NULL;500}501502//------------------------------add_ring---------------------------------------503// Supplied function returns the sum of the inputs.504// This also type-checks the inputs for sanity. Guaranteed never to505// be passed a TOP or BOTTOM type, these are filtered out by pre-check.506const Type *AddFNode::add_ring( const Type *t0, const Type *t1 ) const {507// We must be adding 2 float constants.508return TypeF::make( t0->getf() + t1->getf() );509}510511//------------------------------Ideal------------------------------------------512Node *AddFNode::Ideal(PhaseGVN *phase, bool can_reshape) {513if( IdealizedNumerics && !phase->C->method()->is_strict() ) {514return AddNode::Ideal(phase, can_reshape); // commutative and associative transforms515}516517// Floating point additions are not associative because of boundary conditions (infinity)518return commute(this,519phase->type( in(1) )->singleton(),520phase->type( in(2) )->singleton() ) ? this : NULL;521}522523524//=============================================================================525//------------------------------add_of_identity--------------------------------526// Check for addition of the identity527const Type *AddDNode::add_of_identity( const Type *t1, const Type *t2 ) const {528// x ADD 0 should return x unless 'x' is a -zero529//530// const Type *zero = add_id(); // The additive identity531// jfloat f1 = t1->getf();532// jfloat f2 = t2->getf();533//534// if( t1->higher_equal( zero ) ) return t2;535// if( t2->higher_equal( zero ) ) return t1;536537return NULL;538}539//------------------------------add_ring---------------------------------------540// Supplied function returns the sum of the inputs.541// This also type-checks the inputs for sanity. Guaranteed never to542// be passed a TOP or BOTTOM type, these are filtered out by pre-check.543const Type *AddDNode::add_ring( const Type *t0, const Type *t1 ) const {544// We must be adding 2 double constants.545return TypeD::make( t0->getd() + t1->getd() );546}547548//------------------------------Ideal------------------------------------------549Node *AddDNode::Ideal(PhaseGVN *phase, bool can_reshape) {550if( IdealizedNumerics && !phase->C->method()->is_strict() ) {551return AddNode::Ideal(phase, can_reshape); // commutative and associative transforms552}553554// Floating point additions are not associative because of boundary conditions (infinity)555return commute(this,556phase->type( in(1) )->singleton(),557phase->type( in(2) )->singleton() ) ? this : NULL;558}559560561//=============================================================================562//------------------------------Identity---------------------------------------563// If one input is a constant 0, return the other input.564Node *AddPNode::Identity( PhaseTransform *phase ) {565return ( phase->type( in(Offset) )->higher_equal( TypeX_ZERO ) ) ? in(Address) : this;566}567568//------------------------------Idealize---------------------------------------569Node *AddPNode::Ideal(PhaseGVN *phase, bool can_reshape) {570// Bail out if dead inputs571if( phase->type( in(Address) ) == Type::TOP ) return NULL;572573// If the left input is an add of a constant, flatten the expression tree.574const Node *n = in(Address);575if (n->is_AddP() && n->in(Base) == in(Base)) {576const AddPNode *addp = n->as_AddP(); // Left input is an AddP577assert( !addp->in(Address)->is_AddP() ||578addp->in(Address)->as_AddP() != addp,579"dead loop in AddPNode::Ideal" );580// Type of left input's right input581const Type *t = phase->type( addp->in(Offset) );582if( t == Type::TOP ) return NULL;583const TypeX *t12 = t->is_intptr_t();584if( t12->is_con() ) { // Left input is an add of a constant?585// If the right input is a constant, combine constants586const Type *temp_t2 = phase->type( in(Offset) );587if( temp_t2 == Type::TOP ) return NULL;588const TypeX *t2 = temp_t2->is_intptr_t();589Node* address;590Node* offset;591if( t2->is_con() ) {592// The Add of the flattened expression593address = addp->in(Address);594offset = phase->MakeConX(t2->get_con() + t12->get_con());595} else {596// Else move the constant to the right. ((A+con)+B) into ((A+B)+con)597address = phase->transform(new (phase->C) AddPNode(in(Base),addp->in(Address),in(Offset)));598offset = addp->in(Offset);599}600PhaseIterGVN *igvn = phase->is_IterGVN();601if( igvn ) {602set_req_X(Address,address,igvn);603set_req_X(Offset,offset,igvn);604} else {605set_req(Address,address);606set_req(Offset,offset);607}608return this;609}610}611612// Raw pointers?613if( in(Base)->bottom_type() == Type::TOP ) {614// If this is a NULL+long form (from unsafe accesses), switch to a rawptr.615if (phase->type(in(Address)) == TypePtr::NULL_PTR) {616Node* offset = in(Offset);617return new (phase->C) CastX2PNode(offset);618}619}620621// If the right is an add of a constant, push the offset down.622// Convert: (ptr + (offset+con)) into (ptr+offset)+con.623// The idea is to merge array_base+scaled_index groups together,624// and only have different constant offsets from the same base.625const Node *add = in(Offset);626if( add->Opcode() == Op_AddX && add->in(1) != add ) {627const Type *t22 = phase->type( add->in(2) );628if( t22->singleton() && (t22 != Type::TOP) ) { // Right input is an add of a constant?629set_req(Address, phase->transform(new (phase->C) AddPNode(in(Base),in(Address),add->in(1))));630set_req(Offset, add->in(2));631PhaseIterGVN *igvn = phase->is_IterGVN();632if (add->outcnt() == 0 && igvn) {633// add disconnected.634igvn->_worklist.push((Node*)add);635}636return this; // Made progress637}638}639640return NULL; // No progress641}642643//------------------------------bottom_type------------------------------------644// Bottom-type is the pointer-type with unknown offset.645const Type *AddPNode::bottom_type() const {646if (in(Address) == NULL) return TypePtr::BOTTOM;647const TypePtr *tp = in(Address)->bottom_type()->isa_ptr();648if( !tp ) return Type::TOP; // TOP input means TOP output649assert( in(Offset)->Opcode() != Op_ConP, "" );650const Type *t = in(Offset)->bottom_type();651if( t == Type::TOP )652return tp->add_offset(Type::OffsetTop);653const TypeX *tx = t->is_intptr_t();654intptr_t txoffset = Type::OffsetBot;655if (tx->is_con()) { // Left input is an add of a constant?656txoffset = tx->get_con();657}658return tp->add_offset(txoffset);659}660661//------------------------------Value------------------------------------------662const Type *AddPNode::Value( PhaseTransform *phase ) const {663// Either input is TOP ==> the result is TOP664const Type *t1 = phase->type( in(Address) );665const Type *t2 = phase->type( in(Offset) );666if( t1 == Type::TOP ) return Type::TOP;667if( t2 == Type::TOP ) return Type::TOP;668669// Left input is a pointer670const TypePtr *p1 = t1->isa_ptr();671// Right input is an int672const TypeX *p2 = t2->is_intptr_t();673// Add 'em674intptr_t p2offset = Type::OffsetBot;675if (p2->is_con()) { // Left input is an add of a constant?676p2offset = p2->get_con();677}678return p1->add_offset(p2offset);679}680681//------------------------Ideal_base_and_offset--------------------------------682// Split an oop pointer into a base and offset.683// (The offset might be Type::OffsetBot in the case of an array.)684// Return the base, or NULL if failure.685Node* AddPNode::Ideal_base_and_offset(Node* ptr, PhaseTransform* phase,686// second return value:687intptr_t& offset) {688if (ptr->is_AddP()) {689Node* base = ptr->in(AddPNode::Base);690Node* addr = ptr->in(AddPNode::Address);691Node* offs = ptr->in(AddPNode::Offset);692if (base == addr || base->is_top()) {693offset = phase->find_intptr_t_con(offs, Type::OffsetBot);694if (offset != Type::OffsetBot) {695return addr;696}697}698}699offset = Type::OffsetBot;700return NULL;701}702703//------------------------------unpack_offsets----------------------------------704// Collect the AddP offset values into the elements array, giving up705// if there are more than length.706int AddPNode::unpack_offsets(Node* elements[], int length) {707int count = 0;708Node* addr = this;709Node* base = addr->in(AddPNode::Base);710while (addr->is_AddP()) {711if (addr->in(AddPNode::Base) != base) {712// give up713return -1;714}715elements[count++] = addr->in(AddPNode::Offset);716if (count == length) {717// give up718return -1;719}720addr = addr->in(AddPNode::Address);721}722if (addr != base) {723return -1;724}725return count;726}727728//------------------------------match_edge-------------------------------------729// Do we Match on this edge index or not? Do not match base pointer edge730uint AddPNode::match_edge(uint idx) const {731return idx > Base;732}733734//=============================================================================735//------------------------------Identity---------------------------------------736Node *OrINode::Identity( PhaseTransform *phase ) {737// x | x => x738if (phase->eqv(in(1), in(2))) {739return in(1);740}741742return AddNode::Identity(phase);743}744745//------------------------------add_ring---------------------------------------746// Supplied function returns the sum of the inputs IN THE CURRENT RING. For747// the logical operations the ring's ADD is really a logical OR function.748// This also type-checks the inputs for sanity. Guaranteed never to749// be passed a TOP or BOTTOM type, these are filtered out by pre-check.750const Type *OrINode::add_ring( const Type *t0, const Type *t1 ) const {751const TypeInt *r0 = t0->is_int(); // Handy access752const TypeInt *r1 = t1->is_int();753754// If both args are bool, can figure out better types755if ( r0 == TypeInt::BOOL ) {756if ( r1 == TypeInt::ONE) {757return TypeInt::ONE;758} else if ( r1 == TypeInt::BOOL ) {759return TypeInt::BOOL;760}761} else if ( r0 == TypeInt::ONE ) {762if ( r1 == TypeInt::BOOL ) {763return TypeInt::ONE;764}765}766767// If either input is not a constant, just return all integers.768if( !r0->is_con() || !r1->is_con() )769return TypeInt::INT; // Any integer, but still no symbols.770771// Otherwise just OR them bits.772return TypeInt::make( r0->get_con() | r1->get_con() );773}774775//=============================================================================776//------------------------------Identity---------------------------------------777Node *OrLNode::Identity( PhaseTransform *phase ) {778// x | x => x779if (phase->eqv(in(1), in(2))) {780return in(1);781}782783return AddNode::Identity(phase);784}785786//------------------------------add_ring---------------------------------------787const Type *OrLNode::add_ring( const Type *t0, const Type *t1 ) const {788const TypeLong *r0 = t0->is_long(); // Handy access789const TypeLong *r1 = t1->is_long();790791// If either input is not a constant, just return all integers.792if( !r0->is_con() || !r1->is_con() )793return TypeLong::LONG; // Any integer, but still no symbols.794795// Otherwise just OR them bits.796return TypeLong::make( r0->get_con() | r1->get_con() );797}798799//=============================================================================800//------------------------------add_ring---------------------------------------801// Supplied function returns the sum of the inputs IN THE CURRENT RING. For802// the logical operations the ring's ADD is really a logical OR function.803// This also type-checks the inputs for sanity. Guaranteed never to804// be passed a TOP or BOTTOM type, these are filtered out by pre-check.805const Type *XorINode::add_ring( const Type *t0, const Type *t1 ) const {806const TypeInt *r0 = t0->is_int(); // Handy access807const TypeInt *r1 = t1->is_int();808809// Complementing a boolean?810if( r0 == TypeInt::BOOL && ( r1 == TypeInt::ONE811|| r1 == TypeInt::BOOL))812return TypeInt::BOOL;813814if( !r0->is_con() || !r1->is_con() ) // Not constants815return TypeInt::INT; // Any integer, but still no symbols.816817// Otherwise just XOR them bits.818return TypeInt::make( r0->get_con() ^ r1->get_con() );819}820821//=============================================================================822//------------------------------add_ring---------------------------------------823const Type *XorLNode::add_ring( const Type *t0, const Type *t1 ) const {824const TypeLong *r0 = t0->is_long(); // Handy access825const TypeLong *r1 = t1->is_long();826827// If either input is not a constant, just return all integers.828if( !r0->is_con() || !r1->is_con() )829return TypeLong::LONG; // Any integer, but still no symbols.830831// Otherwise just OR them bits.832return TypeLong::make( r0->get_con() ^ r1->get_con() );833}834835//=============================================================================836//------------------------------add_ring---------------------------------------837// Supplied function returns the sum of the inputs.838const Type *MaxINode::add_ring( const Type *t0, const Type *t1 ) const {839const TypeInt *r0 = t0->is_int(); // Handy access840const TypeInt *r1 = t1->is_int();841842// Otherwise just MAX them bits.843return TypeInt::make( MAX2(r0->_lo,r1->_lo), MAX2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) );844}845846// Check if addition of an integer with type 't' and a constant 'c' can overflow847static bool can_overflow(const TypeInt* t, jint c) {848jint t_lo = t->_lo;849jint t_hi = t->_hi;850return ((c < 0 && (java_add(t_lo, c) > t_lo)) ||851(c > 0 && (java_add(t_hi, c) < t_hi)));852}853854//=============================================================================855//------------------------------Idealize---------------------------------------856// MINs show up in range-check loop limit calculations. Look for857// "MIN2(x+c0,MIN2(y,x+c1))". Pick the smaller constant: "MIN2(x+c0,y)"858Node *MinINode::Ideal(PhaseGVN *phase, bool can_reshape) {859Node *progress = NULL;860// Force a right-spline graph861Node *l = in(1);862Node *r = in(2);863// Transform MinI1( MinI2(a,b), c) into MinI1( a, MinI2(b,c) )864// to force a right-spline graph for the rest of MinINode::Ideal().865if( l->Opcode() == Op_MinI ) {866assert( l != l->in(1), "dead loop in MinINode::Ideal" );867r = phase->transform(new (phase->C) MinINode(l->in(2),r));868l = l->in(1);869set_req(1, l);870set_req(2, r);871return this;872}873874// Get left input & constant875Node *x = l;876jint x_off = 0;877if( x->Opcode() == Op_AddI && // Check for "x+c0" and collect constant878x->in(2)->is_Con() ) {879const Type *t = x->in(2)->bottom_type();880if( t == Type::TOP ) return NULL; // No progress881x_off = t->is_int()->get_con();882x = x->in(1);883}884885// Scan a right-spline-tree for MINs886Node *y = r;887jint y_off = 0;888// Check final part of MIN tree889if( y->Opcode() == Op_AddI && // Check for "y+c1" and collect constant890y->in(2)->is_Con() ) {891const Type *t = y->in(2)->bottom_type();892if( t == Type::TOP ) return NULL; // No progress893y_off = t->is_int()->get_con();894y = y->in(1);895}896if( x->_idx > y->_idx && r->Opcode() != Op_MinI ) {897swap_edges(1, 2);898return this;899}900901const TypeInt* tx = phase->type(x)->isa_int();902903if( r->Opcode() == Op_MinI ) {904assert( r != r->in(2), "dead loop in MinINode::Ideal" );905y = r->in(1);906// Check final part of MIN tree907if( y->Opcode() == Op_AddI &&// Check for "y+c1" and collect constant908y->in(2)->is_Con() ) {909const Type *t = y->in(2)->bottom_type();910if( t == Type::TOP ) return NULL; // No progress911y_off = t->is_int()->get_con();912y = y->in(1);913}914915if( x->_idx > y->_idx )916return new (phase->C) MinINode(r->in(1),phase->transform(new (phase->C) MinINode(l,r->in(2))));917918// Transform MIN2(x + c0, MIN2(x + c1, z)) into MIN2(x + MIN2(c0, c1), z)919// if x == y and the additions can't overflow.920if (phase->eqv(x,y) && tx != NULL &&921!can_overflow(tx, x_off) &&922!can_overflow(tx, y_off)) {923return new (phase->C) MinINode(phase->transform(new (phase->C) AddINode(x, phase->intcon(MIN2(x_off, y_off)))), r->in(2));924}925} else {926// Transform MIN2(x + c0, y + c1) into x + MIN2(c0, c1)927// if x == y and the additions can't overflow.928if (phase->eqv(x,y) && tx != NULL &&929!can_overflow(tx, x_off) &&930!can_overflow(tx, y_off)) {931return new (phase->C) AddINode(x,phase->intcon(MIN2(x_off,y_off)));932}933}934return NULL;935}936937//------------------------------add_ring---------------------------------------938// Supplied function returns the sum of the inputs.939const Type *MinINode::add_ring( const Type *t0, const Type *t1 ) const {940const TypeInt *r0 = t0->is_int(); // Handy access941const TypeInt *r1 = t1->is_int();942943// Otherwise just MIN them bits.944return TypeInt::make( MIN2(r0->_lo,r1->_lo), MIN2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) );945}946947948