1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/opto/addnode.cpp Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,932 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#include "precompiled.hpp" 1.29 +#include "memory/allocation.inline.hpp" 1.30 +#include "opto/addnode.hpp" 1.31 +#include "opto/cfgnode.hpp" 1.32 +#include "opto/connode.hpp" 1.33 +#include "opto/machnode.hpp" 1.34 +#include "opto/mulnode.hpp" 1.35 +#include "opto/phaseX.hpp" 1.36 +#include "opto/subnode.hpp" 1.37 + 1.38 +// Portions of code courtesy of Clifford Click 1.39 + 1.40 +// Classic Add functionality. This covers all the usual 'add' behaviors for 1.41 +// an algebraic ring. Add-integer, add-float, add-double, and binary-or are 1.42 +// all inherited from this class. The various identity values are supplied 1.43 +// by virtual functions. 1.44 + 1.45 + 1.46 +//============================================================================= 1.47 +//------------------------------hash------------------------------------------- 1.48 +// Hash function over AddNodes. Needs to be commutative; i.e., I swap 1.49 +// (commute) inputs to AddNodes willy-nilly so the hash function must return 1.50 +// the same value in the presence of edge swapping. 1.51 +uint AddNode::hash() const { 1.52 + return (uintptr_t)in(1) + (uintptr_t)in(2) + Opcode(); 1.53 +} 1.54 + 1.55 +//------------------------------Identity--------------------------------------- 1.56 +// If either input is a constant 0, return the other input. 1.57 +Node *AddNode::Identity( PhaseTransform *phase ) { 1.58 + const Type *zero = add_id(); // The additive identity 1.59 + if( phase->type( in(1) )->higher_equal( zero ) ) return in(2); 1.60 + if( phase->type( in(2) )->higher_equal( zero ) ) return in(1); 1.61 + return this; 1.62 +} 1.63 + 1.64 +//------------------------------commute---------------------------------------- 1.65 +// Commute operands to move loads and constants to the right. 1.66 +static bool commute( Node *add, int con_left, int con_right ) { 1.67 + Node *in1 = add->in(1); 1.68 + Node *in2 = add->in(2); 1.69 + 1.70 + // Convert "1+x" into "x+1". 1.71 + // Right is a constant; leave it 1.72 + if( con_right ) return false; 1.73 + // Left is a constant; move it right. 1.74 + if( con_left ) { 1.75 + add->swap_edges(1, 2); 1.76 + return true; 1.77 + } 1.78 + 1.79 + // Convert "Load+x" into "x+Load". 1.80 + // Now check for loads 1.81 + if (in2->is_Load()) { 1.82 + if (!in1->is_Load()) { 1.83 + // already x+Load to return 1.84 + return false; 1.85 + } 1.86 + // both are loads, so fall through to sort inputs by idx 1.87 + } else if( in1->is_Load() ) { 1.88 + // Left is a Load and Right is not; move it right. 1.89 + add->swap_edges(1, 2); 1.90 + return true; 1.91 + } 1.92 + 1.93 + PhiNode *phi; 1.94 + // Check for tight loop increments: Loop-phi of Add of loop-phi 1.95 + if( in1->is_Phi() && (phi = in1->as_Phi()) && !phi->is_copy() && phi->region()->is_Loop() && phi->in(2)==add) 1.96 + return false; 1.97 + if( in2->is_Phi() && (phi = in2->as_Phi()) && !phi->is_copy() && phi->region()->is_Loop() && phi->in(2)==add){ 1.98 + add->swap_edges(1, 2); 1.99 + return true; 1.100 + } 1.101 + 1.102 + // Otherwise, sort inputs (commutativity) to help value numbering. 1.103 + if( in1->_idx > in2->_idx ) { 1.104 + add->swap_edges(1, 2); 1.105 + return true; 1.106 + } 1.107 + return false; 1.108 +} 1.109 + 1.110 +//------------------------------Idealize--------------------------------------- 1.111 +// If we get here, we assume we are associative! 1.112 +Node *AddNode::Ideal(PhaseGVN *phase, bool can_reshape) { 1.113 + const Type *t1 = phase->type( in(1) ); 1.114 + const Type *t2 = phase->type( in(2) ); 1.115 + int con_left = t1->singleton(); 1.116 + int con_right = t2->singleton(); 1.117 + 1.118 + // Check for commutative operation desired 1.119 + if( commute(this,con_left,con_right) ) return this; 1.120 + 1.121 + AddNode *progress = NULL; // Progress flag 1.122 + 1.123 + // Convert "(x+1)+2" into "x+(1+2)". If the right input is a 1.124 + // constant, and the left input is an add of a constant, flatten the 1.125 + // expression tree. 1.126 + Node *add1 = in(1); 1.127 + Node *add2 = in(2); 1.128 + int add1_op = add1->Opcode(); 1.129 + int this_op = Opcode(); 1.130 + if( con_right && t2 != Type::TOP && // Right input is a constant? 1.131 + add1_op == this_op ) { // Left input is an Add? 1.132 + 1.133 + // Type of left _in right input 1.134 + const Type *t12 = phase->type( add1->in(2) ); 1.135 + if( t12->singleton() && t12 != Type::TOP ) { // Left input is an add of a constant? 1.136 + // Check for rare case of closed data cycle which can happen inside 1.137 + // unreachable loops. In these cases the computation is undefined. 1.138 +#ifdef ASSERT 1.139 + Node *add11 = add1->in(1); 1.140 + int add11_op = add11->Opcode(); 1.141 + if( (add1 == add1->in(1)) 1.142 + || (add11_op == this_op && add11->in(1) == add1) ) { 1.143 + assert(false, "dead loop in AddNode::Ideal"); 1.144 + } 1.145 +#endif 1.146 + // The Add of the flattened expression 1.147 + Node *x1 = add1->in(1); 1.148 + Node *x2 = phase->makecon( add1->as_Add()->add_ring( t2, t12 )); 1.149 + PhaseIterGVN *igvn = phase->is_IterGVN(); 1.150 + if( igvn ) { 1.151 + set_req_X(2,x2,igvn); 1.152 + set_req_X(1,x1,igvn); 1.153 + } else { 1.154 + set_req(2,x2); 1.155 + set_req(1,x1); 1.156 + } 1.157 + progress = this; // Made progress 1.158 + add1 = in(1); 1.159 + add1_op = add1->Opcode(); 1.160 + } 1.161 + } 1.162 + 1.163 + // Convert "(x+1)+y" into "(x+y)+1". Push constants down the expression tree. 1.164 + if( add1_op == this_op && !con_right ) { 1.165 + Node *a12 = add1->in(2); 1.166 + const Type *t12 = phase->type( a12 ); 1.167 + if( t12->singleton() && t12 != Type::TOP && (add1 != add1->in(1)) && 1.168 + !(add1->in(1)->is_Phi() && add1->in(1)->as_Phi()->is_tripcount()) ) { 1.169 + assert(add1->in(1) != this, "dead loop in AddNode::Ideal"); 1.170 + add2 = add1->clone(); 1.171 + add2->set_req(2, in(2)); 1.172 + add2 = phase->transform(add2); 1.173 + set_req(1, add2); 1.174 + set_req(2, a12); 1.175 + progress = this; 1.176 + add2 = a12; 1.177 + } 1.178 + } 1.179 + 1.180 + // Convert "x+(y+1)" into "(x+y)+1". Push constants down the expression tree. 1.181 + int add2_op = add2->Opcode(); 1.182 + if( add2_op == this_op && !con_left ) { 1.183 + Node *a22 = add2->in(2); 1.184 + const Type *t22 = phase->type( a22 ); 1.185 + if( t22->singleton() && t22 != Type::TOP && (add2 != add2->in(1)) && 1.186 + !(add2->in(1)->is_Phi() && add2->in(1)->as_Phi()->is_tripcount()) ) { 1.187 + assert(add2->in(1) != this, "dead loop in AddNode::Ideal"); 1.188 + Node *addx = add2->clone(); 1.189 + addx->set_req(1, in(1)); 1.190 + addx->set_req(2, add2->in(1)); 1.191 + addx = phase->transform(addx); 1.192 + set_req(1, addx); 1.193 + set_req(2, a22); 1.194 + progress = this; 1.195 + PhaseIterGVN *igvn = phase->is_IterGVN(); 1.196 + if (add2->outcnt() == 0 && igvn) { 1.197 + // add disconnected. 1.198 + igvn->_worklist.push(add2); 1.199 + } 1.200 + } 1.201 + } 1.202 + 1.203 + return progress; 1.204 +} 1.205 + 1.206 +//------------------------------Value----------------------------------------- 1.207 +// An add node sums it's two _in. If one input is an RSD, we must mixin 1.208 +// the other input's symbols. 1.209 +const Type *AddNode::Value( PhaseTransform *phase ) const { 1.210 + // Either input is TOP ==> the result is TOP 1.211 + const Type *t1 = phase->type( in(1) ); 1.212 + const Type *t2 = phase->type( in(2) ); 1.213 + if( t1 == Type::TOP ) return Type::TOP; 1.214 + if( t2 == Type::TOP ) return Type::TOP; 1.215 + 1.216 + // Either input is BOTTOM ==> the result is the local BOTTOM 1.217 + const Type *bot = bottom_type(); 1.218 + if( (t1 == bot) || (t2 == bot) || 1.219 + (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) ) 1.220 + return bot; 1.221 + 1.222 + // Check for an addition involving the additive identity 1.223 + const Type *tadd = add_of_identity( t1, t2 ); 1.224 + if( tadd ) return tadd; 1.225 + 1.226 + return add_ring(t1,t2); // Local flavor of type addition 1.227 +} 1.228 + 1.229 +//------------------------------add_identity----------------------------------- 1.230 +// Check for addition of the identity 1.231 +const Type *AddNode::add_of_identity( const Type *t1, const Type *t2 ) const { 1.232 + const Type *zero = add_id(); // The additive identity 1.233 + if( t1->higher_equal( zero ) ) return t2; 1.234 + if( t2->higher_equal( zero ) ) return t1; 1.235 + 1.236 + return NULL; 1.237 +} 1.238 + 1.239 + 1.240 +//============================================================================= 1.241 +//------------------------------Idealize--------------------------------------- 1.242 +Node *AddINode::Ideal(PhaseGVN *phase, bool can_reshape) { 1.243 + Node* in1 = in(1); 1.244 + Node* in2 = in(2); 1.245 + int op1 = in1->Opcode(); 1.246 + int op2 = in2->Opcode(); 1.247 + // Fold (con1-x)+con2 into (con1+con2)-x 1.248 + if ( op1 == Op_AddI && op2 == Op_SubI ) { 1.249 + // Swap edges to try optimizations below 1.250 + in1 = in2; 1.251 + in2 = in(1); 1.252 + op1 = op2; 1.253 + op2 = in2->Opcode(); 1.254 + } 1.255 + if( op1 == Op_SubI ) { 1.256 + const Type *t_sub1 = phase->type( in1->in(1) ); 1.257 + const Type *t_2 = phase->type( in2 ); 1.258 + if( t_sub1->singleton() && t_2->singleton() && t_sub1 != Type::TOP && t_2 != Type::TOP ) 1.259 + return new (phase->C) SubINode(phase->makecon( add_ring( t_sub1, t_2 ) ), 1.260 + in1->in(2) ); 1.261 + // Convert "(a-b)+(c-d)" into "(a+c)-(b+d)" 1.262 + if( op2 == Op_SubI ) { 1.263 + // Check for dead cycle: d = (a-b)+(c-d) 1.264 + assert( in1->in(2) != this && in2->in(2) != this, 1.265 + "dead loop in AddINode::Ideal" ); 1.266 + Node *sub = new (phase->C) SubINode(NULL, NULL); 1.267 + sub->init_req(1, phase->transform(new (phase->C) AddINode(in1->in(1), in2->in(1) ) )); 1.268 + sub->init_req(2, phase->transform(new (phase->C) AddINode(in1->in(2), in2->in(2) ) )); 1.269 + return sub; 1.270 + } 1.271 + // Convert "(a-b)+(b+c)" into "(a+c)" 1.272 + if( op2 == Op_AddI && in1->in(2) == in2->in(1) ) { 1.273 + assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddINode::Ideal"); 1.274 + return new (phase->C) AddINode(in1->in(1), in2->in(2)); 1.275 + } 1.276 + // Convert "(a-b)+(c+b)" into "(a+c)" 1.277 + if( op2 == Op_AddI && in1->in(2) == in2->in(2) ) { 1.278 + assert(in1->in(1) != this && in2->in(1) != this,"dead loop in AddINode::Ideal"); 1.279 + return new (phase->C) AddINode(in1->in(1), in2->in(1)); 1.280 + } 1.281 + // Convert "(a-b)+(b-c)" into "(a-c)" 1.282 + if( op2 == Op_SubI && in1->in(2) == in2->in(1) ) { 1.283 + assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddINode::Ideal"); 1.284 + return new (phase->C) SubINode(in1->in(1), in2->in(2)); 1.285 + } 1.286 + // Convert "(a-b)+(c-a)" into "(c-b)" 1.287 + if( op2 == Op_SubI && in1->in(1) == in2->in(2) ) { 1.288 + assert(in1->in(2) != this && in2->in(1) != this,"dead loop in AddINode::Ideal"); 1.289 + return new (phase->C) SubINode(in2->in(1), in1->in(2)); 1.290 + } 1.291 + } 1.292 + 1.293 + // Convert "x+(0-y)" into "(x-y)" 1.294 + if( op2 == Op_SubI && phase->type(in2->in(1)) == TypeInt::ZERO ) 1.295 + return new (phase->C) SubINode(in1, in2->in(2) ); 1.296 + 1.297 + // Convert "(0-y)+x" into "(x-y)" 1.298 + if( op1 == Op_SubI && phase->type(in1->in(1)) == TypeInt::ZERO ) 1.299 + return new (phase->C) SubINode( in2, in1->in(2) ); 1.300 + 1.301 + // Convert (x>>>z)+y into (x+(y<<z))>>>z for small constant z and y. 1.302 + // Helps with array allocation math constant folding 1.303 + // See 4790063: 1.304 + // Unrestricted transformation is unsafe for some runtime values of 'x' 1.305 + // ( x == 0, z == 1, y == -1 ) fails 1.306 + // ( x == -5, z == 1, y == 1 ) fails 1.307 + // Transform works for small z and small negative y when the addition 1.308 + // (x + (y << z)) does not cross zero. 1.309 + // Implement support for negative y and (x >= -(y << z)) 1.310 + // Have not observed cases where type information exists to support 1.311 + // positive y and (x <= -(y << z)) 1.312 + if( op1 == Op_URShiftI && op2 == Op_ConI && 1.313 + in1->in(2)->Opcode() == Op_ConI ) { 1.314 + jint z = phase->type( in1->in(2) )->is_int()->get_con() & 0x1f; // only least significant 5 bits matter 1.315 + jint y = phase->type( in2 )->is_int()->get_con(); 1.316 + 1.317 + if( z < 5 && -5 < y && y < 0 ) { 1.318 + const Type *t_in11 = phase->type(in1->in(1)); 1.319 + if( t_in11 != Type::TOP && (t_in11->is_int()->_lo >= -(y << z)) ) { 1.320 + Node *a = phase->transform( new (phase->C) AddINode( in1->in(1), phase->intcon(y<<z) ) ); 1.321 + return new (phase->C) URShiftINode( a, in1->in(2) ); 1.322 + } 1.323 + } 1.324 + } 1.325 + 1.326 + return AddNode::Ideal(phase, can_reshape); 1.327 +} 1.328 + 1.329 + 1.330 +//------------------------------Identity--------------------------------------- 1.331 +// Fold (x-y)+y OR y+(x-y) into x 1.332 +Node *AddINode::Identity( PhaseTransform *phase ) { 1.333 + if( in(1)->Opcode() == Op_SubI && phase->eqv(in(1)->in(2),in(2)) ) { 1.334 + return in(1)->in(1); 1.335 + } 1.336 + else if( in(2)->Opcode() == Op_SubI && phase->eqv(in(2)->in(2),in(1)) ) { 1.337 + return in(2)->in(1); 1.338 + } 1.339 + return AddNode::Identity(phase); 1.340 +} 1.341 + 1.342 + 1.343 +//------------------------------add_ring--------------------------------------- 1.344 +// Supplied function returns the sum of the inputs. Guaranteed never 1.345 +// to be passed a TOP or BOTTOM type, these are filtered out by 1.346 +// pre-check. 1.347 +const Type *AddINode::add_ring( const Type *t0, const Type *t1 ) const { 1.348 + const TypeInt *r0 = t0->is_int(); // Handy access 1.349 + const TypeInt *r1 = t1->is_int(); 1.350 + int lo = r0->_lo + r1->_lo; 1.351 + int hi = r0->_hi + r1->_hi; 1.352 + if( !(r0->is_con() && r1->is_con()) ) { 1.353 + // Not both constants, compute approximate result 1.354 + if( (r0->_lo & r1->_lo) < 0 && lo >= 0 ) { 1.355 + lo = min_jint; hi = max_jint; // Underflow on the low side 1.356 + } 1.357 + if( (~(r0->_hi | r1->_hi)) < 0 && hi < 0 ) { 1.358 + lo = min_jint; hi = max_jint; // Overflow on the high side 1.359 + } 1.360 + if( lo > hi ) { // Handle overflow 1.361 + lo = min_jint; hi = max_jint; 1.362 + } 1.363 + } else { 1.364 + // both constants, compute precise result using 'lo' and 'hi' 1.365 + // Semantics define overflow and underflow for integer addition 1.366 + // as expected. In particular: 0x80000000 + 0x80000000 --> 0x0 1.367 + } 1.368 + return TypeInt::make( lo, hi, MAX2(r0->_widen,r1->_widen) ); 1.369 +} 1.370 + 1.371 + 1.372 +//============================================================================= 1.373 +//------------------------------Idealize--------------------------------------- 1.374 +Node *AddLNode::Ideal(PhaseGVN *phase, bool can_reshape) { 1.375 + Node* in1 = in(1); 1.376 + Node* in2 = in(2); 1.377 + int op1 = in1->Opcode(); 1.378 + int op2 = in2->Opcode(); 1.379 + // Fold (con1-x)+con2 into (con1+con2)-x 1.380 + if ( op1 == Op_AddL && op2 == Op_SubL ) { 1.381 + // Swap edges to try optimizations below 1.382 + in1 = in2; 1.383 + in2 = in(1); 1.384 + op1 = op2; 1.385 + op2 = in2->Opcode(); 1.386 + } 1.387 + // Fold (con1-x)+con2 into (con1+con2)-x 1.388 + if( op1 == Op_SubL ) { 1.389 + const Type *t_sub1 = phase->type( in1->in(1) ); 1.390 + const Type *t_2 = phase->type( in2 ); 1.391 + if( t_sub1->singleton() && t_2->singleton() && t_sub1 != Type::TOP && t_2 != Type::TOP ) 1.392 + return new (phase->C) SubLNode(phase->makecon( add_ring( t_sub1, t_2 ) ), 1.393 + in1->in(2) ); 1.394 + // Convert "(a-b)+(c-d)" into "(a+c)-(b+d)" 1.395 + if( op2 == Op_SubL ) { 1.396 + // Check for dead cycle: d = (a-b)+(c-d) 1.397 + assert( in1->in(2) != this && in2->in(2) != this, 1.398 + "dead loop in AddLNode::Ideal" ); 1.399 + Node *sub = new (phase->C) SubLNode(NULL, NULL); 1.400 + sub->init_req(1, phase->transform(new (phase->C) AddLNode(in1->in(1), in2->in(1) ) )); 1.401 + sub->init_req(2, phase->transform(new (phase->C) AddLNode(in1->in(2), in2->in(2) ) )); 1.402 + return sub; 1.403 + } 1.404 + // Convert "(a-b)+(b+c)" into "(a+c)" 1.405 + if( op2 == Op_AddL && in1->in(2) == in2->in(1) ) { 1.406 + assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddLNode::Ideal"); 1.407 + return new (phase->C) AddLNode(in1->in(1), in2->in(2)); 1.408 + } 1.409 + // Convert "(a-b)+(c+b)" into "(a+c)" 1.410 + if( op2 == Op_AddL && in1->in(2) == in2->in(2) ) { 1.411 + assert(in1->in(1) != this && in2->in(1) != this,"dead loop in AddLNode::Ideal"); 1.412 + return new (phase->C) AddLNode(in1->in(1), in2->in(1)); 1.413 + } 1.414 + // Convert "(a-b)+(b-c)" into "(a-c)" 1.415 + if( op2 == Op_SubL && in1->in(2) == in2->in(1) ) { 1.416 + assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddLNode::Ideal"); 1.417 + return new (phase->C) SubLNode(in1->in(1), in2->in(2)); 1.418 + } 1.419 + // Convert "(a-b)+(c-a)" into "(c-b)" 1.420 + if( op2 == Op_SubL && in1->in(1) == in1->in(2) ) { 1.421 + assert(in1->in(2) != this && in2->in(1) != this,"dead loop in AddLNode::Ideal"); 1.422 + return new (phase->C) SubLNode(in2->in(1), in1->in(2)); 1.423 + } 1.424 + } 1.425 + 1.426 + // Convert "x+(0-y)" into "(x-y)" 1.427 + if( op2 == Op_SubL && phase->type(in2->in(1)) == TypeLong::ZERO ) 1.428 + return new (phase->C) SubLNode( in1, in2->in(2) ); 1.429 + 1.430 + // Convert "(0-y)+x" into "(x-y)" 1.431 + if( op1 == Op_SubL && phase->type(in1->in(1)) == TypeInt::ZERO ) 1.432 + return new (phase->C) SubLNode( in2, in1->in(2) ); 1.433 + 1.434 + // Convert "X+X+X+X+X...+X+Y" into "k*X+Y" or really convert "X+(X+Y)" 1.435 + // into "(X<<1)+Y" and let shift-folding happen. 1.436 + if( op2 == Op_AddL && 1.437 + in2->in(1) == in1 && 1.438 + op1 != Op_ConL && 1.439 + 0 ) { 1.440 + Node *shift = phase->transform(new (phase->C) LShiftLNode(in1,phase->intcon(1))); 1.441 + return new (phase->C) AddLNode(shift,in2->in(2)); 1.442 + } 1.443 + 1.444 + return AddNode::Ideal(phase, can_reshape); 1.445 +} 1.446 + 1.447 + 1.448 +//------------------------------Identity--------------------------------------- 1.449 +// Fold (x-y)+y OR y+(x-y) into x 1.450 +Node *AddLNode::Identity( PhaseTransform *phase ) { 1.451 + if( in(1)->Opcode() == Op_SubL && phase->eqv(in(1)->in(2),in(2)) ) { 1.452 + return in(1)->in(1); 1.453 + } 1.454 + else if( in(2)->Opcode() == Op_SubL && phase->eqv(in(2)->in(2),in(1)) ) { 1.455 + return in(2)->in(1); 1.456 + } 1.457 + return AddNode::Identity(phase); 1.458 +} 1.459 + 1.460 + 1.461 +//------------------------------add_ring--------------------------------------- 1.462 +// Supplied function returns the sum of the inputs. Guaranteed never 1.463 +// to be passed a TOP or BOTTOM type, these are filtered out by 1.464 +// pre-check. 1.465 +const Type *AddLNode::add_ring( const Type *t0, const Type *t1 ) const { 1.466 + const TypeLong *r0 = t0->is_long(); // Handy access 1.467 + const TypeLong *r1 = t1->is_long(); 1.468 + jlong lo = r0->_lo + r1->_lo; 1.469 + jlong hi = r0->_hi + r1->_hi; 1.470 + if( !(r0->is_con() && r1->is_con()) ) { 1.471 + // Not both constants, compute approximate result 1.472 + if( (r0->_lo & r1->_lo) < 0 && lo >= 0 ) { 1.473 + lo =min_jlong; hi = max_jlong; // Underflow on the low side 1.474 + } 1.475 + if( (~(r0->_hi | r1->_hi)) < 0 && hi < 0 ) { 1.476 + lo = min_jlong; hi = max_jlong; // Overflow on the high side 1.477 + } 1.478 + if( lo > hi ) { // Handle overflow 1.479 + lo = min_jlong; hi = max_jlong; 1.480 + } 1.481 + } else { 1.482 + // both constants, compute precise result using 'lo' and 'hi' 1.483 + // Semantics define overflow and underflow for integer addition 1.484 + // as expected. In particular: 0x80000000 + 0x80000000 --> 0x0 1.485 + } 1.486 + return TypeLong::make( lo, hi, MAX2(r0->_widen,r1->_widen) ); 1.487 +} 1.488 + 1.489 + 1.490 +//============================================================================= 1.491 +//------------------------------add_of_identity-------------------------------- 1.492 +// Check for addition of the identity 1.493 +const Type *AddFNode::add_of_identity( const Type *t1, const Type *t2 ) const { 1.494 + // x ADD 0 should return x unless 'x' is a -zero 1.495 + // 1.496 + // const Type *zero = add_id(); // The additive identity 1.497 + // jfloat f1 = t1->getf(); 1.498 + // jfloat f2 = t2->getf(); 1.499 + // 1.500 + // if( t1->higher_equal( zero ) ) return t2; 1.501 + // if( t2->higher_equal( zero ) ) return t1; 1.502 + 1.503 + return NULL; 1.504 +} 1.505 + 1.506 +//------------------------------add_ring--------------------------------------- 1.507 +// Supplied function returns the sum of the inputs. 1.508 +// This also type-checks the inputs for sanity. Guaranteed never to 1.509 +// be passed a TOP or BOTTOM type, these are filtered out by pre-check. 1.510 +const Type *AddFNode::add_ring( const Type *t0, const Type *t1 ) const { 1.511 + // We must be adding 2 float constants. 1.512 + return TypeF::make( t0->getf() + t1->getf() ); 1.513 +} 1.514 + 1.515 +//------------------------------Ideal------------------------------------------ 1.516 +Node *AddFNode::Ideal(PhaseGVN *phase, bool can_reshape) { 1.517 + if( IdealizedNumerics && !phase->C->method()->is_strict() ) { 1.518 + return AddNode::Ideal(phase, can_reshape); // commutative and associative transforms 1.519 + } 1.520 + 1.521 + // Floating point additions are not associative because of boundary conditions (infinity) 1.522 + return commute(this, 1.523 + phase->type( in(1) )->singleton(), 1.524 + phase->type( in(2) )->singleton() ) ? this : NULL; 1.525 +} 1.526 + 1.527 + 1.528 +//============================================================================= 1.529 +//------------------------------add_of_identity-------------------------------- 1.530 +// Check for addition of the identity 1.531 +const Type *AddDNode::add_of_identity( const Type *t1, const Type *t2 ) const { 1.532 + // x ADD 0 should return x unless 'x' is a -zero 1.533 + // 1.534 + // const Type *zero = add_id(); // The additive identity 1.535 + // jfloat f1 = t1->getf(); 1.536 + // jfloat f2 = t2->getf(); 1.537 + // 1.538 + // if( t1->higher_equal( zero ) ) return t2; 1.539 + // if( t2->higher_equal( zero ) ) return t1; 1.540 + 1.541 + return NULL; 1.542 +} 1.543 +//------------------------------add_ring--------------------------------------- 1.544 +// Supplied function returns the sum of the inputs. 1.545 +// This also type-checks the inputs for sanity. Guaranteed never to 1.546 +// be passed a TOP or BOTTOM type, these are filtered out by pre-check. 1.547 +const Type *AddDNode::add_ring( const Type *t0, const Type *t1 ) const { 1.548 + // We must be adding 2 double constants. 1.549 + return TypeD::make( t0->getd() + t1->getd() ); 1.550 +} 1.551 + 1.552 +//------------------------------Ideal------------------------------------------ 1.553 +Node *AddDNode::Ideal(PhaseGVN *phase, bool can_reshape) { 1.554 + if( IdealizedNumerics && !phase->C->method()->is_strict() ) { 1.555 + return AddNode::Ideal(phase, can_reshape); // commutative and associative transforms 1.556 + } 1.557 + 1.558 + // Floating point additions are not associative because of boundary conditions (infinity) 1.559 + return commute(this, 1.560 + phase->type( in(1) )->singleton(), 1.561 + phase->type( in(2) )->singleton() ) ? this : NULL; 1.562 +} 1.563 + 1.564 + 1.565 +//============================================================================= 1.566 +//------------------------------Identity--------------------------------------- 1.567 +// If one input is a constant 0, return the other input. 1.568 +Node *AddPNode::Identity( PhaseTransform *phase ) { 1.569 + return ( phase->type( in(Offset) )->higher_equal( TypeX_ZERO ) ) ? in(Address) : this; 1.570 +} 1.571 + 1.572 +//------------------------------Idealize--------------------------------------- 1.573 +Node *AddPNode::Ideal(PhaseGVN *phase, bool can_reshape) { 1.574 + // Bail out if dead inputs 1.575 + if( phase->type( in(Address) ) == Type::TOP ) return NULL; 1.576 + 1.577 + // If the left input is an add of a constant, flatten the expression tree. 1.578 + const Node *n = in(Address); 1.579 + if (n->is_AddP() && n->in(Base) == in(Base)) { 1.580 + const AddPNode *addp = n->as_AddP(); // Left input is an AddP 1.581 + assert( !addp->in(Address)->is_AddP() || 1.582 + addp->in(Address)->as_AddP() != addp, 1.583 + "dead loop in AddPNode::Ideal" ); 1.584 + // Type of left input's right input 1.585 + const Type *t = phase->type( addp->in(Offset) ); 1.586 + if( t == Type::TOP ) return NULL; 1.587 + const TypeX *t12 = t->is_intptr_t(); 1.588 + if( t12->is_con() ) { // Left input is an add of a constant? 1.589 + // If the right input is a constant, combine constants 1.590 + const Type *temp_t2 = phase->type( in(Offset) ); 1.591 + if( temp_t2 == Type::TOP ) return NULL; 1.592 + const TypeX *t2 = temp_t2->is_intptr_t(); 1.593 + Node* address; 1.594 + Node* offset; 1.595 + if( t2->is_con() ) { 1.596 + // The Add of the flattened expression 1.597 + address = addp->in(Address); 1.598 + offset = phase->MakeConX(t2->get_con() + t12->get_con()); 1.599 + } else { 1.600 + // Else move the constant to the right. ((A+con)+B) into ((A+B)+con) 1.601 + address = phase->transform(new (phase->C) AddPNode(in(Base),addp->in(Address),in(Offset))); 1.602 + offset = addp->in(Offset); 1.603 + } 1.604 + PhaseIterGVN *igvn = phase->is_IterGVN(); 1.605 + if( igvn ) { 1.606 + set_req_X(Address,address,igvn); 1.607 + set_req_X(Offset,offset,igvn); 1.608 + } else { 1.609 + set_req(Address,address); 1.610 + set_req(Offset,offset); 1.611 + } 1.612 + return this; 1.613 + } 1.614 + } 1.615 + 1.616 + // Raw pointers? 1.617 + if( in(Base)->bottom_type() == Type::TOP ) { 1.618 + // If this is a NULL+long form (from unsafe accesses), switch to a rawptr. 1.619 + if (phase->type(in(Address)) == TypePtr::NULL_PTR) { 1.620 + Node* offset = in(Offset); 1.621 + return new (phase->C) CastX2PNode(offset); 1.622 + } 1.623 + } 1.624 + 1.625 + // If the right is an add of a constant, push the offset down. 1.626 + // Convert: (ptr + (offset+con)) into (ptr+offset)+con. 1.627 + // The idea is to merge array_base+scaled_index groups together, 1.628 + // and only have different constant offsets from the same base. 1.629 + const Node *add = in(Offset); 1.630 + if( add->Opcode() == Op_AddX && add->in(1) != add ) { 1.631 + const Type *t22 = phase->type( add->in(2) ); 1.632 + if( t22->singleton() && (t22 != Type::TOP) ) { // Right input is an add of a constant? 1.633 + set_req(Address, phase->transform(new (phase->C) AddPNode(in(Base),in(Address),add->in(1)))); 1.634 + set_req(Offset, add->in(2)); 1.635 + PhaseIterGVN *igvn = phase->is_IterGVN(); 1.636 + if (add->outcnt() == 0 && igvn) { 1.637 + // add disconnected. 1.638 + igvn->_worklist.push((Node*)add); 1.639 + } 1.640 + return this; // Made progress 1.641 + } 1.642 + } 1.643 + 1.644 + return NULL; // No progress 1.645 +} 1.646 + 1.647 +//------------------------------bottom_type------------------------------------ 1.648 +// Bottom-type is the pointer-type with unknown offset. 1.649 +const Type *AddPNode::bottom_type() const { 1.650 + if (in(Address) == NULL) return TypePtr::BOTTOM; 1.651 + const TypePtr *tp = in(Address)->bottom_type()->isa_ptr(); 1.652 + if( !tp ) return Type::TOP; // TOP input means TOP output 1.653 + assert( in(Offset)->Opcode() != Op_ConP, "" ); 1.654 + const Type *t = in(Offset)->bottom_type(); 1.655 + if( t == Type::TOP ) 1.656 + return tp->add_offset(Type::OffsetTop); 1.657 + const TypeX *tx = t->is_intptr_t(); 1.658 + intptr_t txoffset = Type::OffsetBot; 1.659 + if (tx->is_con()) { // Left input is an add of a constant? 1.660 + txoffset = tx->get_con(); 1.661 + } 1.662 + return tp->add_offset(txoffset); 1.663 +} 1.664 + 1.665 +//------------------------------Value------------------------------------------ 1.666 +const Type *AddPNode::Value( PhaseTransform *phase ) const { 1.667 + // Either input is TOP ==> the result is TOP 1.668 + const Type *t1 = phase->type( in(Address) ); 1.669 + const Type *t2 = phase->type( in(Offset) ); 1.670 + if( t1 == Type::TOP ) return Type::TOP; 1.671 + if( t2 == Type::TOP ) return Type::TOP; 1.672 + 1.673 + // Left input is a pointer 1.674 + const TypePtr *p1 = t1->isa_ptr(); 1.675 + // Right input is an int 1.676 + const TypeX *p2 = t2->is_intptr_t(); 1.677 + // Add 'em 1.678 + intptr_t p2offset = Type::OffsetBot; 1.679 + if (p2->is_con()) { // Left input is an add of a constant? 1.680 + p2offset = p2->get_con(); 1.681 + } 1.682 + return p1->add_offset(p2offset); 1.683 +} 1.684 + 1.685 +//------------------------Ideal_base_and_offset-------------------------------- 1.686 +// Split an oop pointer into a base and offset. 1.687 +// (The offset might be Type::OffsetBot in the case of an array.) 1.688 +// Return the base, or NULL if failure. 1.689 +Node* AddPNode::Ideal_base_and_offset(Node* ptr, PhaseTransform* phase, 1.690 + // second return value: 1.691 + intptr_t& offset) { 1.692 + if (ptr->is_AddP()) { 1.693 + Node* base = ptr->in(AddPNode::Base); 1.694 + Node* addr = ptr->in(AddPNode::Address); 1.695 + Node* offs = ptr->in(AddPNode::Offset); 1.696 + if (base == addr || base->is_top()) { 1.697 + offset = phase->find_intptr_t_con(offs, Type::OffsetBot); 1.698 + if (offset != Type::OffsetBot) { 1.699 + return addr; 1.700 + } 1.701 + } 1.702 + } 1.703 + offset = Type::OffsetBot; 1.704 + return NULL; 1.705 +} 1.706 + 1.707 +//------------------------------unpack_offsets---------------------------------- 1.708 +// Collect the AddP offset values into the elements array, giving up 1.709 +// if there are more than length. 1.710 +int AddPNode::unpack_offsets(Node* elements[], int length) { 1.711 + int count = 0; 1.712 + Node* addr = this; 1.713 + Node* base = addr->in(AddPNode::Base); 1.714 + while (addr->is_AddP()) { 1.715 + if (addr->in(AddPNode::Base) != base) { 1.716 + // give up 1.717 + return -1; 1.718 + } 1.719 + elements[count++] = addr->in(AddPNode::Offset); 1.720 + if (count == length) { 1.721 + // give up 1.722 + return -1; 1.723 + } 1.724 + addr = addr->in(AddPNode::Address); 1.725 + } 1.726 + if (addr != base) { 1.727 + return -1; 1.728 + } 1.729 + return count; 1.730 +} 1.731 + 1.732 +//------------------------------match_edge------------------------------------- 1.733 +// Do we Match on this edge index or not? Do not match base pointer edge 1.734 +uint AddPNode::match_edge(uint idx) const { 1.735 + return idx > Base; 1.736 +} 1.737 + 1.738 +//============================================================================= 1.739 +//------------------------------Identity--------------------------------------- 1.740 +Node *OrINode::Identity( PhaseTransform *phase ) { 1.741 + // x | x => x 1.742 + if (phase->eqv(in(1), in(2))) { 1.743 + return in(1); 1.744 + } 1.745 + 1.746 + return AddNode::Identity(phase); 1.747 +} 1.748 + 1.749 +//------------------------------add_ring--------------------------------------- 1.750 +// Supplied function returns the sum of the inputs IN THE CURRENT RING. For 1.751 +// the logical operations the ring's ADD is really a logical OR function. 1.752 +// This also type-checks the inputs for sanity. Guaranteed never to 1.753 +// be passed a TOP or BOTTOM type, these are filtered out by pre-check. 1.754 +const Type *OrINode::add_ring( const Type *t0, const Type *t1 ) const { 1.755 + const TypeInt *r0 = t0->is_int(); // Handy access 1.756 + const TypeInt *r1 = t1->is_int(); 1.757 + 1.758 + // If both args are bool, can figure out better types 1.759 + if ( r0 == TypeInt::BOOL ) { 1.760 + if ( r1 == TypeInt::ONE) { 1.761 + return TypeInt::ONE; 1.762 + } else if ( r1 == TypeInt::BOOL ) { 1.763 + return TypeInt::BOOL; 1.764 + } 1.765 + } else if ( r0 == TypeInt::ONE ) { 1.766 + if ( r1 == TypeInt::BOOL ) { 1.767 + return TypeInt::ONE; 1.768 + } 1.769 + } 1.770 + 1.771 + // If either input is not a constant, just return all integers. 1.772 + if( !r0->is_con() || !r1->is_con() ) 1.773 + return TypeInt::INT; // Any integer, but still no symbols. 1.774 + 1.775 + // Otherwise just OR them bits. 1.776 + return TypeInt::make( r0->get_con() | r1->get_con() ); 1.777 +} 1.778 + 1.779 +//============================================================================= 1.780 +//------------------------------Identity--------------------------------------- 1.781 +Node *OrLNode::Identity( PhaseTransform *phase ) { 1.782 + // x | x => x 1.783 + if (phase->eqv(in(1), in(2))) { 1.784 + return in(1); 1.785 + } 1.786 + 1.787 + return AddNode::Identity(phase); 1.788 +} 1.789 + 1.790 +//------------------------------add_ring--------------------------------------- 1.791 +const Type *OrLNode::add_ring( const Type *t0, const Type *t1 ) const { 1.792 + const TypeLong *r0 = t0->is_long(); // Handy access 1.793 + const TypeLong *r1 = t1->is_long(); 1.794 + 1.795 + // If either input is not a constant, just return all integers. 1.796 + if( !r0->is_con() || !r1->is_con() ) 1.797 + return TypeLong::LONG; // Any integer, but still no symbols. 1.798 + 1.799 + // Otherwise just OR them bits. 1.800 + return TypeLong::make( r0->get_con() | r1->get_con() ); 1.801 +} 1.802 + 1.803 +//============================================================================= 1.804 +//------------------------------add_ring--------------------------------------- 1.805 +// Supplied function returns the sum of the inputs IN THE CURRENT RING. For 1.806 +// the logical operations the ring's ADD is really a logical OR function. 1.807 +// This also type-checks the inputs for sanity. Guaranteed never to 1.808 +// be passed a TOP or BOTTOM type, these are filtered out by pre-check. 1.809 +const Type *XorINode::add_ring( const Type *t0, const Type *t1 ) const { 1.810 + const TypeInt *r0 = t0->is_int(); // Handy access 1.811 + const TypeInt *r1 = t1->is_int(); 1.812 + 1.813 + // Complementing a boolean? 1.814 + if( r0 == TypeInt::BOOL && ( r1 == TypeInt::ONE 1.815 + || r1 == TypeInt::BOOL)) 1.816 + return TypeInt::BOOL; 1.817 + 1.818 + if( !r0->is_con() || !r1->is_con() ) // Not constants 1.819 + return TypeInt::INT; // Any integer, but still no symbols. 1.820 + 1.821 + // Otherwise just XOR them bits. 1.822 + return TypeInt::make( r0->get_con() ^ r1->get_con() ); 1.823 +} 1.824 + 1.825 +//============================================================================= 1.826 +//------------------------------add_ring--------------------------------------- 1.827 +const Type *XorLNode::add_ring( const Type *t0, const Type *t1 ) const { 1.828 + const TypeLong *r0 = t0->is_long(); // Handy access 1.829 + const TypeLong *r1 = t1->is_long(); 1.830 + 1.831 + // If either input is not a constant, just return all integers. 1.832 + if( !r0->is_con() || !r1->is_con() ) 1.833 + return TypeLong::LONG; // Any integer, but still no symbols. 1.834 + 1.835 + // Otherwise just OR them bits. 1.836 + return TypeLong::make( r0->get_con() ^ r1->get_con() ); 1.837 +} 1.838 + 1.839 +//============================================================================= 1.840 +//------------------------------add_ring--------------------------------------- 1.841 +// Supplied function returns the sum of the inputs. 1.842 +const Type *MaxINode::add_ring( const Type *t0, const Type *t1 ) const { 1.843 + const TypeInt *r0 = t0->is_int(); // Handy access 1.844 + const TypeInt *r1 = t1->is_int(); 1.845 + 1.846 + // Otherwise just MAX them bits. 1.847 + return TypeInt::make( MAX2(r0->_lo,r1->_lo), MAX2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) ); 1.848 +} 1.849 + 1.850 +//============================================================================= 1.851 +//------------------------------Idealize--------------------------------------- 1.852 +// MINs show up in range-check loop limit calculations. Look for 1.853 +// "MIN2(x+c0,MIN2(y,x+c1))". Pick the smaller constant: "MIN2(x+c0,y)" 1.854 +Node *MinINode::Ideal(PhaseGVN *phase, bool can_reshape) { 1.855 + Node *progress = NULL; 1.856 + // Force a right-spline graph 1.857 + Node *l = in(1); 1.858 + Node *r = in(2); 1.859 + // Transform MinI1( MinI2(a,b), c) into MinI1( a, MinI2(b,c) ) 1.860 + // to force a right-spline graph for the rest of MinINode::Ideal(). 1.861 + if( l->Opcode() == Op_MinI ) { 1.862 + assert( l != l->in(1), "dead loop in MinINode::Ideal" ); 1.863 + r = phase->transform(new (phase->C) MinINode(l->in(2),r)); 1.864 + l = l->in(1); 1.865 + set_req(1, l); 1.866 + set_req(2, r); 1.867 + return this; 1.868 + } 1.869 + 1.870 + // Get left input & constant 1.871 + Node *x = l; 1.872 + int x_off = 0; 1.873 + if( x->Opcode() == Op_AddI && // Check for "x+c0" and collect constant 1.874 + x->in(2)->is_Con() ) { 1.875 + const Type *t = x->in(2)->bottom_type(); 1.876 + if( t == Type::TOP ) return NULL; // No progress 1.877 + x_off = t->is_int()->get_con(); 1.878 + x = x->in(1); 1.879 + } 1.880 + 1.881 + // Scan a right-spline-tree for MINs 1.882 + Node *y = r; 1.883 + int y_off = 0; 1.884 + // Check final part of MIN tree 1.885 + if( y->Opcode() == Op_AddI && // Check for "y+c1" and collect constant 1.886 + y->in(2)->is_Con() ) { 1.887 + const Type *t = y->in(2)->bottom_type(); 1.888 + if( t == Type::TOP ) return NULL; // No progress 1.889 + y_off = t->is_int()->get_con(); 1.890 + y = y->in(1); 1.891 + } 1.892 + if( x->_idx > y->_idx && r->Opcode() != Op_MinI ) { 1.893 + swap_edges(1, 2); 1.894 + return this; 1.895 + } 1.896 + 1.897 + 1.898 + if( r->Opcode() == Op_MinI ) { 1.899 + assert( r != r->in(2), "dead loop in MinINode::Ideal" ); 1.900 + y = r->in(1); 1.901 + // Check final part of MIN tree 1.902 + if( y->Opcode() == Op_AddI &&// Check for "y+c1" and collect constant 1.903 + y->in(2)->is_Con() ) { 1.904 + const Type *t = y->in(2)->bottom_type(); 1.905 + if( t == Type::TOP ) return NULL; // No progress 1.906 + y_off = t->is_int()->get_con(); 1.907 + y = y->in(1); 1.908 + } 1.909 + 1.910 + if( x->_idx > y->_idx ) 1.911 + return new (phase->C) MinINode(r->in(1),phase->transform(new (phase->C) MinINode(l,r->in(2)))); 1.912 + 1.913 + // See if covers: MIN2(x+c0,MIN2(y+c1,z)) 1.914 + if( !phase->eqv(x,y) ) return NULL; 1.915 + // If (y == x) transform MIN2(x+c0, MIN2(x+c1,z)) into 1.916 + // MIN2(x+c0 or x+c1 which less, z). 1.917 + return new (phase->C) MinINode(phase->transform(new (phase->C) AddINode(x,phase->intcon(MIN2(x_off,y_off)))),r->in(2)); 1.918 + } else { 1.919 + // See if covers: MIN2(x+c0,y+c1) 1.920 + if( !phase->eqv(x,y) ) return NULL; 1.921 + // If (y == x) transform MIN2(x+c0,x+c1) into x+c0 or x+c1 which less. 1.922 + return new (phase->C) AddINode(x,phase->intcon(MIN2(x_off,y_off))); 1.923 + } 1.924 + 1.925 +} 1.926 + 1.927 +//------------------------------add_ring--------------------------------------- 1.928 +// Supplied function returns the sum of the inputs. 1.929 +const Type *MinINode::add_ring( const Type *t0, const Type *t1 ) const { 1.930 + const TypeInt *r0 = t0->is_int(); // Handy access 1.931 + const TypeInt *r1 = t1->is_int(); 1.932 + 1.933 + // Otherwise just MIN them bits. 1.934 + return TypeInt::make( MIN2(r0->_lo,r1->_lo), MIN2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) ); 1.935 +}