src/share/vm/opto/subnode.cpp

Sat, 07 Nov 2020 10:30:02 +0800

author
aoqi
date
Sat, 07 Nov 2020 10:30:02 +0800
changeset 10026
8c95980d0b66
parent 9637
eef07cd490d4
permissions
-rw-r--r--

Added tag mips-jdk8u275-b01 for changeset d3b4d62f391f

duke@435 1 /*
drchase@6680 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "compiler/compileLog.hpp"
stefank@2314 27 #include "memory/allocation.inline.hpp"
stefank@2314 28 #include "opto/addnode.hpp"
stefank@2314 29 #include "opto/callnode.hpp"
stefank@2314 30 #include "opto/cfgnode.hpp"
stefank@2314 31 #include "opto/connode.hpp"
stefank@2314 32 #include "opto/loopnode.hpp"
stefank@2314 33 #include "opto/matcher.hpp"
stefank@2314 34 #include "opto/mulnode.hpp"
stefank@2314 35 #include "opto/opcodes.hpp"
stefank@2314 36 #include "opto/phaseX.hpp"
stefank@2314 37 #include "opto/subnode.hpp"
stefank@2314 38 #include "runtime/sharedRuntime.hpp"
stefank@2314 39
duke@435 40 // Portions of code courtesy of Clifford Click
duke@435 41
duke@435 42 // Optimization - Graph Style
duke@435 43
duke@435 44 #include "math.h"
duke@435 45
duke@435 46 //=============================================================================
duke@435 47 //------------------------------Identity---------------------------------------
duke@435 48 // If right input is a constant 0, return the left input.
duke@435 49 Node *SubNode::Identity( PhaseTransform *phase ) {
duke@435 50 assert(in(1) != this, "Must already have called Value");
duke@435 51 assert(in(2) != this, "Must already have called Value");
duke@435 52
duke@435 53 // Remove double negation
duke@435 54 const Type *zero = add_id();
duke@435 55 if( phase->type( in(1) )->higher_equal( zero ) &&
duke@435 56 in(2)->Opcode() == Opcode() &&
duke@435 57 phase->type( in(2)->in(1) )->higher_equal( zero ) ) {
duke@435 58 return in(2)->in(2);
duke@435 59 }
duke@435 60
never@647 61 // Convert "(X+Y) - Y" into X and "(X+Y) - X" into Y
duke@435 62 if( in(1)->Opcode() == Op_AddI ) {
duke@435 63 if( phase->eqv(in(1)->in(2),in(2)) )
duke@435 64 return in(1)->in(1);
never@647 65 if (phase->eqv(in(1)->in(1),in(2)))
never@647 66 return in(1)->in(2);
never@647 67
duke@435 68 // Also catch: "(X + Opaque2(Y)) - Y". In this case, 'Y' is a loop-varying
duke@435 69 // trip counter and X is likely to be loop-invariant (that's how O2 Nodes
duke@435 70 // are originally used, although the optimizer sometimes jiggers things).
duke@435 71 // This folding through an O2 removes a loop-exit use of a loop-varying
duke@435 72 // value and generally lowers register pressure in and around the loop.
duke@435 73 if( in(1)->in(2)->Opcode() == Op_Opaque2 &&
duke@435 74 phase->eqv(in(1)->in(2)->in(1),in(2)) )
duke@435 75 return in(1)->in(1);
duke@435 76 }
duke@435 77
duke@435 78 return ( phase->type( in(2) )->higher_equal( zero ) ) ? in(1) : this;
duke@435 79 }
duke@435 80
duke@435 81 //------------------------------Value------------------------------------------
duke@435 82 // A subtract node differences it's two inputs.
kvn@6679 83 const Type* SubNode::Value_common(PhaseTransform *phase) const {
duke@435 84 const Node* in1 = in(1);
duke@435 85 const Node* in2 = in(2);
duke@435 86 // Either input is TOP ==> the result is TOP
duke@435 87 const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
duke@435 88 if( t1 == Type::TOP ) return Type::TOP;
duke@435 89 const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
duke@435 90 if( t2 == Type::TOP ) return Type::TOP;
duke@435 91
duke@435 92 // Not correct for SubFnode and AddFNode (must check for infinity)
duke@435 93 // Equal? Subtract is zero
kvn@3407 94 if (in1->eqv_uncast(in2)) return add_id();
duke@435 95
duke@435 96 // Either input is BOTTOM ==> the result is the local BOTTOM
duke@435 97 if( t1 == Type::BOTTOM || t2 == Type::BOTTOM )
duke@435 98 return bottom_type();
duke@435 99
kvn@6679 100 return NULL;
kvn@6679 101 }
kvn@6679 102
kvn@6679 103 const Type* SubNode::Value(PhaseTransform *phase) const {
kvn@6679 104 const Type* t = Value_common(phase);
kvn@6679 105 if (t != NULL) {
kvn@6679 106 return t;
kvn@6679 107 }
kvn@6679 108 const Type* t1 = phase->type(in(1));
kvn@6679 109 const Type* t2 = phase->type(in(2));
duke@435 110 return sub(t1,t2); // Local flavor of type subtraction
duke@435 111
duke@435 112 }
duke@435 113
duke@435 114 //=============================================================================
duke@435 115
duke@435 116 //------------------------------Helper function--------------------------------
duke@435 117 static bool ok_to_convert(Node* inc, Node* iv) {
duke@435 118 // Do not collapse (x+c0)-y if "+" is a loop increment, because the
duke@435 119 // "-" is loop invariant and collapsing extends the live-range of "x"
duke@435 120 // to overlap with the "+", forcing another register to be used in
duke@435 121 // the loop.
duke@435 122 // This test will be clearer with '&&' (apply DeMorgan's rule)
duke@435 123 // but I like the early cutouts that happen here.
duke@435 124 const PhiNode *phi;
duke@435 125 if( ( !inc->in(1)->is_Phi() ||
duke@435 126 !(phi=inc->in(1)->as_Phi()) ||
duke@435 127 phi->is_copy() ||
duke@435 128 !phi->region()->is_CountedLoop() ||
duke@435 129 inc != phi->region()->as_CountedLoop()->incr() )
duke@435 130 &&
duke@435 131 // Do not collapse (x+c0)-iv if "iv" is a loop induction variable,
duke@435 132 // because "x" maybe invariant.
duke@435 133 ( !iv->is_loop_iv() )
duke@435 134 ) {
duke@435 135 return true;
duke@435 136 } else {
duke@435 137 return false;
duke@435 138 }
duke@435 139 }
duke@435 140 //------------------------------Ideal------------------------------------------
duke@435 141 Node *SubINode::Ideal(PhaseGVN *phase, bool can_reshape){
duke@435 142 Node *in1 = in(1);
duke@435 143 Node *in2 = in(2);
duke@435 144 uint op1 = in1->Opcode();
duke@435 145 uint op2 = in2->Opcode();
duke@435 146
duke@435 147 #ifdef ASSERT
duke@435 148 // Check for dead loop
duke@435 149 if( phase->eqv( in1, this ) || phase->eqv( in2, this ) ||
duke@435 150 ( op1 == Op_AddI || op1 == Op_SubI ) &&
duke@435 151 ( phase->eqv( in1->in(1), this ) || phase->eqv( in1->in(2), this ) ||
duke@435 152 phase->eqv( in1->in(1), in1 ) || phase->eqv( in1->in(2), in1 ) ) )
duke@435 153 assert(false, "dead loop in SubINode::Ideal");
duke@435 154 #endif
duke@435 155
duke@435 156 const Type *t2 = phase->type( in2 );
duke@435 157 if( t2 == Type::TOP ) return NULL;
duke@435 158 // Convert "x-c0" into "x+ -c0".
duke@435 159 if( t2->base() == Type::Int ){ // Might be bottom or top...
duke@435 160 const TypeInt *i = t2->is_int();
duke@435 161 if( i->is_con() )
kvn@4115 162 return new (phase->C) AddINode(in1, phase->intcon(-i->get_con()));
duke@435 163 }
duke@435 164
duke@435 165 // Convert "(x+c0) - y" into (x-y) + c0"
duke@435 166 // Do not collapse (x+c0)-y if "+" is a loop increment or
duke@435 167 // if "y" is a loop induction variable.
duke@435 168 if( op1 == Op_AddI && ok_to_convert(in1, in2) ) {
duke@435 169 const Type *tadd = phase->type( in1->in(2) );
duke@435 170 if( tadd->singleton() && tadd != Type::TOP ) {
kvn@4115 171 Node *sub2 = phase->transform( new (phase->C) SubINode( in1->in(1), in2 ));
kvn@4115 172 return new (phase->C) AddINode( sub2, in1->in(2) );
duke@435 173 }
duke@435 174 }
duke@435 175
duke@435 176
duke@435 177 // Convert "x - (y+c0)" into "(x-y) - c0"
duke@435 178 // Need the same check as in above optimization but reversed.
duke@435 179 if (op2 == Op_AddI && ok_to_convert(in2, in1)) {
duke@435 180 Node* in21 = in2->in(1);
duke@435 181 Node* in22 = in2->in(2);
duke@435 182 const TypeInt* tcon = phase->type(in22)->isa_int();
duke@435 183 if (tcon != NULL && tcon->is_con()) {
kvn@4115 184 Node* sub2 = phase->transform( new (phase->C) SubINode(in1, in21) );
duke@435 185 Node* neg_c0 = phase->intcon(- tcon->get_con());
kvn@4115 186 return new (phase->C) AddINode(sub2, neg_c0);
duke@435 187 }
duke@435 188 }
duke@435 189
duke@435 190 const Type *t1 = phase->type( in1 );
duke@435 191 if( t1 == Type::TOP ) return NULL;
duke@435 192
duke@435 193 #ifdef ASSERT
duke@435 194 // Check for dead loop
duke@435 195 if( ( op2 == Op_AddI || op2 == Op_SubI ) &&
duke@435 196 ( phase->eqv( in2->in(1), this ) || phase->eqv( in2->in(2), this ) ||
duke@435 197 phase->eqv( in2->in(1), in2 ) || phase->eqv( in2->in(2), in2 ) ) )
duke@435 198 assert(false, "dead loop in SubINode::Ideal");
duke@435 199 #endif
duke@435 200
duke@435 201 // Convert "x - (x+y)" into "-y"
duke@435 202 if( op2 == Op_AddI &&
duke@435 203 phase->eqv( in1, in2->in(1) ) )
kvn@4115 204 return new (phase->C) SubINode( phase->intcon(0),in2->in(2));
duke@435 205 // Convert "(x-y) - x" into "-y"
duke@435 206 if( op1 == Op_SubI &&
duke@435 207 phase->eqv( in1->in(1), in2 ) )
kvn@4115 208 return new (phase->C) SubINode( phase->intcon(0),in1->in(2));
duke@435 209 // Convert "x - (y+x)" into "-y"
duke@435 210 if( op2 == Op_AddI &&
duke@435 211 phase->eqv( in1, in2->in(2) ) )
kvn@4115 212 return new (phase->C) SubINode( phase->intcon(0),in2->in(1));
duke@435 213
duke@435 214 // Convert "0 - (x-y)" into "y-x"
duke@435 215 if( t1 == TypeInt::ZERO && op2 == Op_SubI )
kvn@4115 216 return new (phase->C) SubINode( in2->in(2), in2->in(1) );
duke@435 217
duke@435 218 // Convert "0 - (x+con)" into "-con-x"
duke@435 219 jint con;
duke@435 220 if( t1 == TypeInt::ZERO && op2 == Op_AddI &&
duke@435 221 (con = in2->in(2)->find_int_con(0)) != 0 )
kvn@4115 222 return new (phase->C) SubINode( phase->intcon(-con), in2->in(1) );
duke@435 223
duke@435 224 // Convert "(X+A) - (X+B)" into "A - B"
duke@435 225 if( op1 == Op_AddI && op2 == Op_AddI && in1->in(1) == in2->in(1) )
kvn@4115 226 return new (phase->C) SubINode( in1->in(2), in2->in(2) );
duke@435 227
duke@435 228 // Convert "(A+X) - (B+X)" into "A - B"
duke@435 229 if( op1 == Op_AddI && op2 == Op_AddI && in1->in(2) == in2->in(2) )
kvn@4115 230 return new (phase->C) SubINode( in1->in(1), in2->in(1) );
duke@435 231
kvn@835 232 // Convert "(A+X) - (X+B)" into "A - B"
kvn@835 233 if( op1 == Op_AddI && op2 == Op_AddI && in1->in(2) == in2->in(1) )
kvn@4115 234 return new (phase->C) SubINode( in1->in(1), in2->in(2) );
kvn@835 235
kvn@835 236 // Convert "(X+A) - (B+X)" into "A - B"
kvn@835 237 if( op1 == Op_AddI && op2 == Op_AddI && in1->in(1) == in2->in(2) )
kvn@4115 238 return new (phase->C) SubINode( in1->in(2), in2->in(1) );
kvn@835 239
duke@435 240 // Convert "A-(B-C)" into (A+C)-B", since add is commutative and generally
duke@435 241 // nicer to optimize than subtract.
duke@435 242 if( op2 == Op_SubI && in2->outcnt() == 1) {
kvn@4115 243 Node *add1 = phase->transform( new (phase->C) AddINode( in1, in2->in(2) ) );
kvn@4115 244 return new (phase->C) SubINode( add1, in2->in(1) );
duke@435 245 }
duke@435 246
duke@435 247 return NULL;
duke@435 248 }
duke@435 249
duke@435 250 //------------------------------sub--------------------------------------------
duke@435 251 // A subtract node differences it's two inputs.
duke@435 252 const Type *SubINode::sub( const Type *t1, const Type *t2 ) const {
duke@435 253 const TypeInt *r0 = t1->is_int(); // Handy access
duke@435 254 const TypeInt *r1 = t2->is_int();
aph@9610 255 int32 lo = java_subtract(r0->_lo, r1->_hi);
aph@9610 256 int32 hi = java_subtract(r0->_hi, r1->_lo);
duke@435 257
duke@435 258 // We next check for 32-bit overflow.
duke@435 259 // If that happens, we just assume all integers are possible.
duke@435 260 if( (((r0->_lo ^ r1->_hi) >= 0) || // lo ends have same signs OR
duke@435 261 ((r0->_lo ^ lo) >= 0)) && // lo results have same signs AND
duke@435 262 (((r0->_hi ^ r1->_lo) >= 0) || // hi ends have same signs OR
duke@435 263 ((r0->_hi ^ hi) >= 0)) ) // hi results have same signs
duke@435 264 return TypeInt::make(lo,hi,MAX2(r0->_widen,r1->_widen));
duke@435 265 else // Overflow; assume all integers
duke@435 266 return TypeInt::INT;
duke@435 267 }
duke@435 268
duke@435 269 //=============================================================================
duke@435 270 //------------------------------Ideal------------------------------------------
duke@435 271 Node *SubLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
duke@435 272 Node *in1 = in(1);
duke@435 273 Node *in2 = in(2);
duke@435 274 uint op1 = in1->Opcode();
duke@435 275 uint op2 = in2->Opcode();
duke@435 276
duke@435 277 #ifdef ASSERT
duke@435 278 // Check for dead loop
duke@435 279 if( phase->eqv( in1, this ) || phase->eqv( in2, this ) ||
duke@435 280 ( op1 == Op_AddL || op1 == Op_SubL ) &&
duke@435 281 ( phase->eqv( in1->in(1), this ) || phase->eqv( in1->in(2), this ) ||
duke@435 282 phase->eqv( in1->in(1), in1 ) || phase->eqv( in1->in(2), in1 ) ) )
duke@435 283 assert(false, "dead loop in SubLNode::Ideal");
duke@435 284 #endif
duke@435 285
duke@435 286 if( phase->type( in2 ) == Type::TOP ) return NULL;
duke@435 287 const TypeLong *i = phase->type( in2 )->isa_long();
duke@435 288 // Convert "x-c0" into "x+ -c0".
duke@435 289 if( i && // Might be bottom or top...
duke@435 290 i->is_con() )
kvn@4115 291 return new (phase->C) AddLNode(in1, phase->longcon(-i->get_con()));
duke@435 292
duke@435 293 // Convert "(x+c0) - y" into (x-y) + c0"
duke@435 294 // Do not collapse (x+c0)-y if "+" is a loop increment or
duke@435 295 // if "y" is a loop induction variable.
duke@435 296 if( op1 == Op_AddL && ok_to_convert(in1, in2) ) {
duke@435 297 Node *in11 = in1->in(1);
duke@435 298 const Type *tadd = phase->type( in1->in(2) );
duke@435 299 if( tadd->singleton() && tadd != Type::TOP ) {
kvn@4115 300 Node *sub2 = phase->transform( new (phase->C) SubLNode( in11, in2 ));
kvn@4115 301 return new (phase->C) AddLNode( sub2, in1->in(2) );
duke@435 302 }
duke@435 303 }
duke@435 304
duke@435 305 // Convert "x - (y+c0)" into "(x-y) - c0"
duke@435 306 // Need the same check as in above optimization but reversed.
duke@435 307 if (op2 == Op_AddL && ok_to_convert(in2, in1)) {
duke@435 308 Node* in21 = in2->in(1);
duke@435 309 Node* in22 = in2->in(2);
duke@435 310 const TypeLong* tcon = phase->type(in22)->isa_long();
duke@435 311 if (tcon != NULL && tcon->is_con()) {
kvn@4115 312 Node* sub2 = phase->transform( new (phase->C) SubLNode(in1, in21) );
duke@435 313 Node* neg_c0 = phase->longcon(- tcon->get_con());
kvn@4115 314 return new (phase->C) AddLNode(sub2, neg_c0);
duke@435 315 }
duke@435 316 }
duke@435 317
duke@435 318 const Type *t1 = phase->type( in1 );
duke@435 319 if( t1 == Type::TOP ) return NULL;
duke@435 320
duke@435 321 #ifdef ASSERT
duke@435 322 // Check for dead loop
duke@435 323 if( ( op2 == Op_AddL || op2 == Op_SubL ) &&
duke@435 324 ( phase->eqv( in2->in(1), this ) || phase->eqv( in2->in(2), this ) ||
duke@435 325 phase->eqv( in2->in(1), in2 ) || phase->eqv( in2->in(2), in2 ) ) )
duke@435 326 assert(false, "dead loop in SubLNode::Ideal");
duke@435 327 #endif
duke@435 328
duke@435 329 // Convert "x - (x+y)" into "-y"
duke@435 330 if( op2 == Op_AddL &&
duke@435 331 phase->eqv( in1, in2->in(1) ) )
kvn@4115 332 return new (phase->C) SubLNode( phase->makecon(TypeLong::ZERO), in2->in(2));
duke@435 333 // Convert "x - (y+x)" into "-y"
duke@435 334 if( op2 == Op_AddL &&
duke@435 335 phase->eqv( in1, in2->in(2) ) )
kvn@4115 336 return new (phase->C) SubLNode( phase->makecon(TypeLong::ZERO),in2->in(1));
duke@435 337
duke@435 338 // Convert "0 - (x-y)" into "y-x"
duke@435 339 if( phase->type( in1 ) == TypeLong::ZERO && op2 == Op_SubL )
kvn@4115 340 return new (phase->C) SubLNode( in2->in(2), in2->in(1) );
duke@435 341
duke@435 342 // Convert "(X+A) - (X+B)" into "A - B"
duke@435 343 if( op1 == Op_AddL && op2 == Op_AddL && in1->in(1) == in2->in(1) )
kvn@4115 344 return new (phase->C) SubLNode( in1->in(2), in2->in(2) );
duke@435 345
duke@435 346 // Convert "(A+X) - (B+X)" into "A - B"
duke@435 347 if( op1 == Op_AddL && op2 == Op_AddL && in1->in(2) == in2->in(2) )
kvn@4115 348 return new (phase->C) SubLNode( in1->in(1), in2->in(1) );
duke@435 349
duke@435 350 // Convert "A-(B-C)" into (A+C)-B"
duke@435 351 if( op2 == Op_SubL && in2->outcnt() == 1) {
kvn@4115 352 Node *add1 = phase->transform( new (phase->C) AddLNode( in1, in2->in(2) ) );
kvn@4115 353 return new (phase->C) SubLNode( add1, in2->in(1) );
duke@435 354 }
duke@435 355
duke@435 356 return NULL;
duke@435 357 }
duke@435 358
duke@435 359 //------------------------------sub--------------------------------------------
duke@435 360 // A subtract node differences it's two inputs.
duke@435 361 const Type *SubLNode::sub( const Type *t1, const Type *t2 ) const {
duke@435 362 const TypeLong *r0 = t1->is_long(); // Handy access
duke@435 363 const TypeLong *r1 = t2->is_long();
aph@9610 364 jlong lo = java_subtract(r0->_lo, r1->_hi);
aph@9610 365 jlong hi = java_subtract(r0->_hi, r1->_lo);
duke@435 366
duke@435 367 // We next check for 32-bit overflow.
duke@435 368 // If that happens, we just assume all integers are possible.
duke@435 369 if( (((r0->_lo ^ r1->_hi) >= 0) || // lo ends have same signs OR
duke@435 370 ((r0->_lo ^ lo) >= 0)) && // lo results have same signs AND
duke@435 371 (((r0->_hi ^ r1->_lo) >= 0) || // hi ends have same signs OR
duke@435 372 ((r0->_hi ^ hi) >= 0)) ) // hi results have same signs
duke@435 373 return TypeLong::make(lo,hi,MAX2(r0->_widen,r1->_widen));
duke@435 374 else // Overflow; assume all integers
duke@435 375 return TypeLong::LONG;
duke@435 376 }
duke@435 377
duke@435 378 //=============================================================================
duke@435 379 //------------------------------Value------------------------------------------
duke@435 380 // A subtract node differences its two inputs.
duke@435 381 const Type *SubFPNode::Value( PhaseTransform *phase ) const {
duke@435 382 const Node* in1 = in(1);
duke@435 383 const Node* in2 = in(2);
duke@435 384 // Either input is TOP ==> the result is TOP
duke@435 385 const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
duke@435 386 if( t1 == Type::TOP ) return Type::TOP;
duke@435 387 const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
duke@435 388 if( t2 == Type::TOP ) return Type::TOP;
duke@435 389
duke@435 390 // if both operands are infinity of same sign, the result is NaN; do
duke@435 391 // not replace with zero
duke@435 392 if( (t1->is_finite() && t2->is_finite()) ) {
duke@435 393 if( phase->eqv(in1, in2) ) return add_id();
duke@435 394 }
duke@435 395
duke@435 396 // Either input is BOTTOM ==> the result is the local BOTTOM
duke@435 397 const Type *bot = bottom_type();
duke@435 398 if( (t1 == bot) || (t2 == bot) ||
duke@435 399 (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
duke@435 400 return bot;
duke@435 401
duke@435 402 return sub(t1,t2); // Local flavor of type subtraction
duke@435 403 }
duke@435 404
duke@435 405
duke@435 406 //=============================================================================
duke@435 407 //------------------------------Ideal------------------------------------------
duke@435 408 Node *SubFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
duke@435 409 const Type *t2 = phase->type( in(2) );
duke@435 410 // Convert "x-c0" into "x+ -c0".
duke@435 411 if( t2->base() == Type::FloatCon ) { // Might be bottom or top...
duke@435 412 // return new (phase->C, 3) AddFNode(in(1), phase->makecon( TypeF::make(-t2->getf()) ) );
duke@435 413 }
duke@435 414
duke@435 415 // Not associative because of boundary conditions (infinity)
duke@435 416 if( IdealizedNumerics && !phase->C->method()->is_strict() ) {
duke@435 417 // Convert "x - (x+y)" into "-y"
duke@435 418 if( in(2)->is_Add() &&
duke@435 419 phase->eqv(in(1),in(2)->in(1) ) )
kvn@4115 420 return new (phase->C) SubFNode( phase->makecon(TypeF::ZERO),in(2)->in(2));
duke@435 421 }
duke@435 422
duke@435 423 // Cannot replace 0.0-X with -X because a 'fsub' bytecode computes
duke@435 424 // 0.0-0.0 as +0.0, while a 'fneg' bytecode computes -0.0.
duke@435 425 //if( phase->type(in(1)) == TypeF::ZERO )
duke@435 426 //return new (phase->C, 2) NegFNode(in(2));
duke@435 427
duke@435 428 return NULL;
duke@435 429 }
duke@435 430
duke@435 431 //------------------------------sub--------------------------------------------
duke@435 432 // A subtract node differences its two inputs.
duke@435 433 const Type *SubFNode::sub( const Type *t1, const Type *t2 ) const {
duke@435 434 // no folding if one of operands is infinity or NaN, do not do constant folding
duke@435 435 if( g_isfinite(t1->getf()) && g_isfinite(t2->getf()) ) {
duke@435 436 return TypeF::make( t1->getf() - t2->getf() );
duke@435 437 }
duke@435 438 else if( g_isnan(t1->getf()) ) {
duke@435 439 return t1;
duke@435 440 }
duke@435 441 else if( g_isnan(t2->getf()) ) {
duke@435 442 return t2;
duke@435 443 }
duke@435 444 else {
duke@435 445 return Type::FLOAT;
duke@435 446 }
duke@435 447 }
duke@435 448
duke@435 449 //=============================================================================
duke@435 450 //------------------------------Ideal------------------------------------------
duke@435 451 Node *SubDNode::Ideal(PhaseGVN *phase, bool can_reshape){
duke@435 452 const Type *t2 = phase->type( in(2) );
duke@435 453 // Convert "x-c0" into "x+ -c0".
duke@435 454 if( t2->base() == Type::DoubleCon ) { // Might be bottom or top...
duke@435 455 // return new (phase->C, 3) AddDNode(in(1), phase->makecon( TypeD::make(-t2->getd()) ) );
duke@435 456 }
duke@435 457
duke@435 458 // Not associative because of boundary conditions (infinity)
duke@435 459 if( IdealizedNumerics && !phase->C->method()->is_strict() ) {
duke@435 460 // Convert "x - (x+y)" into "-y"
duke@435 461 if( in(2)->is_Add() &&
duke@435 462 phase->eqv(in(1),in(2)->in(1) ) )
kvn@4115 463 return new (phase->C) SubDNode( phase->makecon(TypeD::ZERO),in(2)->in(2));
duke@435 464 }
duke@435 465
duke@435 466 // Cannot replace 0.0-X with -X because a 'dsub' bytecode computes
duke@435 467 // 0.0-0.0 as +0.0, while a 'dneg' bytecode computes -0.0.
duke@435 468 //if( phase->type(in(1)) == TypeD::ZERO )
duke@435 469 //return new (phase->C, 2) NegDNode(in(2));
duke@435 470
duke@435 471 return NULL;
duke@435 472 }
duke@435 473
duke@435 474 //------------------------------sub--------------------------------------------
duke@435 475 // A subtract node differences its two inputs.
duke@435 476 const Type *SubDNode::sub( const Type *t1, const Type *t2 ) const {
duke@435 477 // no folding if one of operands is infinity or NaN, do not do constant folding
duke@435 478 if( g_isfinite(t1->getd()) && g_isfinite(t2->getd()) ) {
duke@435 479 return TypeD::make( t1->getd() - t2->getd() );
duke@435 480 }
duke@435 481 else if( g_isnan(t1->getd()) ) {
duke@435 482 return t1;
duke@435 483 }
duke@435 484 else if( g_isnan(t2->getd()) ) {
duke@435 485 return t2;
duke@435 486 }
duke@435 487 else {
duke@435 488 return Type::DOUBLE;
duke@435 489 }
duke@435 490 }
duke@435 491
duke@435 492 //=============================================================================
duke@435 493 //------------------------------Idealize---------------------------------------
duke@435 494 // Unlike SubNodes, compare must still flatten return value to the
duke@435 495 // range -1, 0, 1.
duke@435 496 // And optimizations like those for (X + Y) - X fail if overflow happens.
duke@435 497 Node *CmpNode::Identity( PhaseTransform *phase ) {
duke@435 498 return this;
duke@435 499 }
duke@435 500
duke@435 501 //=============================================================================
duke@435 502 //------------------------------cmp--------------------------------------------
duke@435 503 // Simplify a CmpI (compare 2 integers) node, based on local information.
duke@435 504 // If both inputs are constants, compare them.
duke@435 505 const Type *CmpINode::sub( const Type *t1, const Type *t2 ) const {
duke@435 506 const TypeInt *r0 = t1->is_int(); // Handy access
duke@435 507 const TypeInt *r1 = t2->is_int();
duke@435 508
duke@435 509 if( r0->_hi < r1->_lo ) // Range is always low?
duke@435 510 return TypeInt::CC_LT;
duke@435 511 else if( r0->_lo > r1->_hi ) // Range is always high?
duke@435 512 return TypeInt::CC_GT;
duke@435 513
duke@435 514 else if( r0->is_con() && r1->is_con() ) { // comparing constants?
duke@435 515 assert(r0->get_con() == r1->get_con(), "must be equal");
duke@435 516 return TypeInt::CC_EQ; // Equal results.
duke@435 517 } else if( r0->_hi == r1->_lo ) // Range is never high?
duke@435 518 return TypeInt::CC_LE;
duke@435 519 else if( r0->_lo == r1->_hi ) // Range is never low?
duke@435 520 return TypeInt::CC_GE;
duke@435 521 return TypeInt::CC; // else use worst case results
duke@435 522 }
duke@435 523
duke@435 524 // Simplify a CmpU (compare 2 integers) node, based on local information.
duke@435 525 // If both inputs are constants, compare them.
duke@435 526 const Type *CmpUNode::sub( const Type *t1, const Type *t2 ) const {
duke@435 527 assert(!t1->isa_ptr(), "obsolete usage of CmpU");
duke@435 528
duke@435 529 // comparing two unsigned ints
duke@435 530 const TypeInt *r0 = t1->is_int(); // Handy access
duke@435 531 const TypeInt *r1 = t2->is_int();
duke@435 532
duke@435 533 // Current installed version
duke@435 534 // Compare ranges for non-overlap
duke@435 535 juint lo0 = r0->_lo;
duke@435 536 juint hi0 = r0->_hi;
duke@435 537 juint lo1 = r1->_lo;
duke@435 538 juint hi1 = r1->_hi;
duke@435 539
duke@435 540 // If either one has both negative and positive values,
duke@435 541 // it therefore contains both 0 and -1, and since [0..-1] is the
duke@435 542 // full unsigned range, the type must act as an unsigned bottom.
duke@435 543 bool bot0 = ((jint)(lo0 ^ hi0) < 0);
duke@435 544 bool bot1 = ((jint)(lo1 ^ hi1) < 0);
duke@435 545
duke@435 546 if (bot0 || bot1) {
duke@435 547 // All unsigned values are LE -1 and GE 0.
duke@435 548 if (lo0 == 0 && hi0 == 0) {
duke@435 549 return TypeInt::CC_LE; // 0 <= bot
vlivanov@8435 550 } else if ((jint)lo0 == -1 && (jint)hi0 == -1) {
vlivanov@8435 551 return TypeInt::CC_GE; // -1 >= bot
duke@435 552 } else if (lo1 == 0 && hi1 == 0) {
duke@435 553 return TypeInt::CC_GE; // bot >= 0
vlivanov@8435 554 } else if ((jint)lo1 == -1 && (jint)hi1 == -1) {
vlivanov@8435 555 return TypeInt::CC_LE; // bot <= -1
duke@435 556 }
duke@435 557 } else {
duke@435 558 // We can use ranges of the form [lo..hi] if signs are the same.
duke@435 559 assert(lo0 <= hi0 && lo1 <= hi1, "unsigned ranges are valid");
duke@435 560 // results are reversed, '-' > '+' for unsigned compare
duke@435 561 if (hi0 < lo1) {
duke@435 562 return TypeInt::CC_LT; // smaller
duke@435 563 } else if (lo0 > hi1) {
duke@435 564 return TypeInt::CC_GT; // greater
duke@435 565 } else if (hi0 == lo1 && lo0 == hi1) {
duke@435 566 return TypeInt::CC_EQ; // Equal results
duke@435 567 } else if (lo0 >= hi1) {
duke@435 568 return TypeInt::CC_GE;
duke@435 569 } else if (hi0 <= lo1) {
duke@435 570 // Check for special case in Hashtable::get. (See below.)
kvn@3910 571 if ((jint)lo0 >= 0 && (jint)lo1 >= 0 && is_index_range_check())
duke@435 572 return TypeInt::CC_LT;
duke@435 573 return TypeInt::CC_LE;
duke@435 574 }
duke@435 575 }
duke@435 576 // Check for special case in Hashtable::get - the hash index is
duke@435 577 // mod'ed to the table size so the following range check is useless.
duke@435 578 // Check for: (X Mod Y) CmpU Y, where the mod result and Y both have
duke@435 579 // to be positive.
duke@435 580 // (This is a gross hack, since the sub method never
duke@435 581 // looks at the structure of the node in any other case.)
kvn@3910 582 if ((jint)lo0 >= 0 && (jint)lo1 >= 0 && is_index_range_check())
duke@435 583 return TypeInt::CC_LT;
duke@435 584 return TypeInt::CC; // else use worst case results
duke@435 585 }
duke@435 586
kvn@6679 587 const Type* CmpUNode::Value(PhaseTransform *phase) const {
kvn@6679 588 const Type* t = SubNode::Value_common(phase);
kvn@6679 589 if (t != NULL) {
kvn@6679 590 return t;
kvn@6679 591 }
kvn@6679 592 const Node* in1 = in(1);
kvn@6679 593 const Node* in2 = in(2);
kvn@6679 594 const Type* t1 = phase->type(in1);
kvn@6679 595 const Type* t2 = phase->type(in2);
kvn@6679 596 assert(t1->isa_int(), "CmpU has only Int type inputs");
kvn@6679 597 if (t2 == TypeInt::INT) { // Compare to bottom?
kvn@6679 598 return bottom_type();
kvn@6679 599 }
kvn@6679 600 uint in1_op = in1->Opcode();
kvn@6679 601 if (in1_op == Op_AddI || in1_op == Op_SubI) {
kvn@6679 602 // The problem rise when result of AddI(SubI) may overflow
kvn@6679 603 // signed integer value. Let say the input type is
kvn@6679 604 // [256, maxint] then +128 will create 2 ranges due to
kvn@6679 605 // overflow: [minint, minint+127] and [384, maxint].
kvn@6679 606 // But C2 type system keep only 1 type range and as result
kvn@6679 607 // it use general [minint, maxint] for this case which we
kvn@6679 608 // can't optimize.
kvn@6679 609 //
kvn@6679 610 // Make 2 separate type ranges based on types of AddI(SubI) inputs
kvn@6679 611 // and compare results of their compare. If results are the same
kvn@6679 612 // CmpU node can be optimized.
kvn@6679 613 const Node* in11 = in1->in(1);
kvn@6679 614 const Node* in12 = in1->in(2);
kvn@6679 615 const Type* t11 = (in11 == in1) ? Type::TOP : phase->type(in11);
kvn@6679 616 const Type* t12 = (in12 == in1) ? Type::TOP : phase->type(in12);
kvn@6679 617 // Skip cases when input types are top or bottom.
kvn@6679 618 if ((t11 != Type::TOP) && (t11 != TypeInt::INT) &&
kvn@6679 619 (t12 != Type::TOP) && (t12 != TypeInt::INT)) {
kvn@6679 620 const TypeInt *r0 = t11->is_int();
kvn@6679 621 const TypeInt *r1 = t12->is_int();
kvn@6679 622 jlong lo_r0 = r0->_lo;
kvn@6679 623 jlong hi_r0 = r0->_hi;
kvn@6679 624 jlong lo_r1 = r1->_lo;
kvn@6679 625 jlong hi_r1 = r1->_hi;
kvn@6679 626 if (in1_op == Op_SubI) {
kvn@6679 627 jlong tmp = hi_r1;
kvn@6679 628 hi_r1 = -lo_r1;
kvn@6679 629 lo_r1 = -tmp;
kvn@6679 630 // Note, for substructing [minint,x] type range
kvn@6679 631 // long arithmetic provides correct overflow answer.
kvn@6679 632 // The confusion come from the fact that in 32-bit
kvn@6679 633 // -minint == minint but in 64-bit -minint == maxint+1.
kvn@6679 634 }
kvn@6679 635 jlong lo_long = lo_r0 + lo_r1;
kvn@6679 636 jlong hi_long = hi_r0 + hi_r1;
kvn@6679 637 int lo_tr1 = min_jint;
kvn@6679 638 int hi_tr1 = (int)hi_long;
kvn@6679 639 int lo_tr2 = (int)lo_long;
kvn@6679 640 int hi_tr2 = max_jint;
kvn@6679 641 bool underflow = lo_long != (jlong)lo_tr2;
kvn@6679 642 bool overflow = hi_long != (jlong)hi_tr1;
kvn@6679 643 // Use sub(t1, t2) when there is no overflow (one type range)
kvn@6679 644 // or when both overflow and underflow (too complex).
kvn@6679 645 if ((underflow != overflow) && (hi_tr1 < lo_tr2)) {
kvn@6679 646 // Overflow only on one boundary, compare 2 separate type ranges.
kvn@6679 647 int w = MAX2(r0->_widen, r1->_widen); // _widen does not matter here
kvn@6679 648 const TypeInt* tr1 = TypeInt::make(lo_tr1, hi_tr1, w);
kvn@6679 649 const TypeInt* tr2 = TypeInt::make(lo_tr2, hi_tr2, w);
kvn@6679 650 const Type* cmp1 = sub(tr1, t2);
kvn@6679 651 const Type* cmp2 = sub(tr2, t2);
kvn@6679 652 if (cmp1 == cmp2) {
kvn@6679 653 return cmp1; // Hit!
kvn@6679 654 }
kvn@6679 655 }
kvn@6679 656 }
kvn@6679 657 }
kvn@6679 658
kvn@6679 659 return sub(t1, t2); // Local flavor of type subtraction
kvn@6679 660 }
kvn@6679 661
kvn@3910 662 bool CmpUNode::is_index_range_check() const {
kvn@3910 663 // Check for the "(X ModI Y) CmpU Y" shape
kvn@3910 664 return (in(1)->Opcode() == Op_ModI &&
kvn@3910 665 in(1)->in(2)->eqv_uncast(in(2)));
kvn@3910 666 }
kvn@3910 667
duke@435 668 //------------------------------Idealize---------------------------------------
duke@435 669 Node *CmpINode::Ideal( PhaseGVN *phase, bool can_reshape ) {
duke@435 670 if (phase->type(in(2))->higher_equal(TypeInt::ZERO)) {
duke@435 671 switch (in(1)->Opcode()) {
duke@435 672 case Op_CmpL3: // Collapse a CmpL3/CmpI into a CmpL
kvn@4115 673 return new (phase->C) CmpLNode(in(1)->in(1),in(1)->in(2));
duke@435 674 case Op_CmpF3: // Collapse a CmpF3/CmpI into a CmpF
kvn@4115 675 return new (phase->C) CmpFNode(in(1)->in(1),in(1)->in(2));
duke@435 676 case Op_CmpD3: // Collapse a CmpD3/CmpI into a CmpD
kvn@4115 677 return new (phase->C) CmpDNode(in(1)->in(1),in(1)->in(2));
duke@435 678 //case Op_SubI:
duke@435 679 // If (x - y) cannot overflow, then ((x - y) <?> 0)
duke@435 680 // can be turned into (x <?> y).
duke@435 681 // This is handled (with more general cases) by Ideal_sub_algebra.
duke@435 682 }
duke@435 683 }
duke@435 684 return NULL; // No change
duke@435 685 }
duke@435 686
duke@435 687
duke@435 688 //=============================================================================
duke@435 689 // Simplify a CmpL (compare 2 longs ) node, based on local information.
duke@435 690 // If both inputs are constants, compare them.
duke@435 691 const Type *CmpLNode::sub( const Type *t1, const Type *t2 ) const {
duke@435 692 const TypeLong *r0 = t1->is_long(); // Handy access
duke@435 693 const TypeLong *r1 = t2->is_long();
duke@435 694
duke@435 695 if( r0->_hi < r1->_lo ) // Range is always low?
duke@435 696 return TypeInt::CC_LT;
duke@435 697 else if( r0->_lo > r1->_hi ) // Range is always high?
duke@435 698 return TypeInt::CC_GT;
duke@435 699
duke@435 700 else if( r0->is_con() && r1->is_con() ) { // comparing constants?
duke@435 701 assert(r0->get_con() == r1->get_con(), "must be equal");
duke@435 702 return TypeInt::CC_EQ; // Equal results.
duke@435 703 } else if( r0->_hi == r1->_lo ) // Range is never high?
duke@435 704 return TypeInt::CC_LE;
duke@435 705 else if( r0->_lo == r1->_hi ) // Range is never low?
duke@435 706 return TypeInt::CC_GE;
duke@435 707 return TypeInt::CC; // else use worst case results
duke@435 708 }
duke@435 709
thartmann@8797 710
thartmann@8797 711 // Simplify a CmpUL (compare 2 unsigned longs) node, based on local information.
thartmann@8797 712 // If both inputs are constants, compare them.
thartmann@8797 713 const Type* CmpULNode::sub(const Type* t1, const Type* t2) const {
thartmann@8797 714 assert(!t1->isa_ptr(), "obsolete usage of CmpUL");
thartmann@8797 715
thartmann@8797 716 // comparing two unsigned longs
thartmann@8797 717 const TypeLong* r0 = t1->is_long(); // Handy access
thartmann@8797 718 const TypeLong* r1 = t2->is_long();
thartmann@8797 719
thartmann@8797 720 // Current installed version
thartmann@8797 721 // Compare ranges for non-overlap
thartmann@8797 722 julong lo0 = r0->_lo;
thartmann@8797 723 julong hi0 = r0->_hi;
thartmann@8797 724 julong lo1 = r1->_lo;
thartmann@8797 725 julong hi1 = r1->_hi;
thartmann@8797 726
thartmann@8797 727 // If either one has both negative and positive values,
thartmann@8797 728 // it therefore contains both 0 and -1, and since [0..-1] is the
thartmann@8797 729 // full unsigned range, the type must act as an unsigned bottom.
thartmann@8797 730 bool bot0 = ((jlong)(lo0 ^ hi0) < 0);
thartmann@8797 731 bool bot1 = ((jlong)(lo1 ^ hi1) < 0);
thartmann@8797 732
thartmann@8797 733 if (bot0 || bot1) {
thartmann@8797 734 // All unsigned values are LE -1 and GE 0.
thartmann@8797 735 if (lo0 == 0 && hi0 == 0) {
thartmann@8797 736 return TypeInt::CC_LE; // 0 <= bot
thartmann@8797 737 } else if ((jlong)lo0 == -1 && (jlong)hi0 == -1) {
thartmann@8797 738 return TypeInt::CC_GE; // -1 >= bot
thartmann@8797 739 } else if (lo1 == 0 && hi1 == 0) {
thartmann@8797 740 return TypeInt::CC_GE; // bot >= 0
thartmann@8797 741 } else if ((jlong)lo1 == -1 && (jlong)hi1 == -1) {
thartmann@8797 742 return TypeInt::CC_LE; // bot <= -1
thartmann@8797 743 }
thartmann@8797 744 } else {
thartmann@8797 745 // We can use ranges of the form [lo..hi] if signs are the same.
thartmann@8797 746 assert(lo0 <= hi0 && lo1 <= hi1, "unsigned ranges are valid");
thartmann@8797 747 // results are reversed, '-' > '+' for unsigned compare
thartmann@8797 748 if (hi0 < lo1) {
thartmann@8797 749 return TypeInt::CC_LT; // smaller
thartmann@8797 750 } else if (lo0 > hi1) {
thartmann@8797 751 return TypeInt::CC_GT; // greater
thartmann@8797 752 } else if (hi0 == lo1 && lo0 == hi1) {
thartmann@8797 753 return TypeInt::CC_EQ; // Equal results
thartmann@8797 754 } else if (lo0 >= hi1) {
thartmann@8797 755 return TypeInt::CC_GE;
thartmann@8797 756 } else if (hi0 <= lo1) {
thartmann@8797 757 return TypeInt::CC_LE;
thartmann@8797 758 }
thartmann@8797 759 }
thartmann@8797 760
thartmann@8797 761 return TypeInt::CC; // else use worst case results
thartmann@8797 762 }
thartmann@8797 763
duke@435 764 //=============================================================================
duke@435 765 //------------------------------sub--------------------------------------------
duke@435 766 // Simplify an CmpP (compare 2 pointers) node, based on local information.
duke@435 767 // If both inputs are constants, compare them.
duke@435 768 const Type *CmpPNode::sub( const Type *t1, const Type *t2 ) const {
duke@435 769 const TypePtr *r0 = t1->is_ptr(); // Handy access
duke@435 770 const TypePtr *r1 = t2->is_ptr();
duke@435 771
duke@435 772 // Undefined inputs makes for an undefined result
duke@435 773 if( TypePtr::above_centerline(r0->_ptr) ||
duke@435 774 TypePtr::above_centerline(r1->_ptr) )
duke@435 775 return Type::TOP;
duke@435 776
duke@435 777 if (r0 == r1 && r0->singleton()) {
duke@435 778 // Equal pointer constants (klasses, nulls, etc.)
duke@435 779 return TypeInt::CC_EQ;
duke@435 780 }
duke@435 781
duke@435 782 // See if it is 2 unrelated classes.
duke@435 783 const TypeOopPtr* p0 = r0->isa_oopptr();
duke@435 784 const TypeOopPtr* p1 = r1->isa_oopptr();
duke@435 785 if (p0 && p1) {
kvn@468 786 Node* in1 = in(1)->uncast();
kvn@468 787 Node* in2 = in(2)->uncast();
kvn@468 788 AllocateNode* alloc1 = AllocateNode::Ideal_allocation(in1, NULL);
kvn@468 789 AllocateNode* alloc2 = AllocateNode::Ideal_allocation(in2, NULL);
kvn@468 790 if (MemNode::detect_ptr_independence(in1, alloc1, in2, alloc2, NULL)) {
kvn@468 791 return TypeInt::CC_GT; // different pointers
kvn@468 792 }
duke@435 793 ciKlass* klass0 = p0->klass();
duke@435 794 bool xklass0 = p0->klass_is_exact();
duke@435 795 ciKlass* klass1 = p1->klass();
duke@435 796 bool xklass1 = p1->klass_is_exact();
duke@435 797 int kps = (p0->isa_klassptr()?1:0) + (p1->isa_klassptr()?1:0);
duke@435 798 if (klass0 && klass1 &&
duke@435 799 kps != 1 && // both or neither are klass pointers
never@1103 800 klass0->is_loaded() && !klass0->is_interface() && // do not trust interfaces
kvn@1218 801 klass1->is_loaded() && !klass1->is_interface() &&
kvn@1218 802 (!klass0->is_obj_array_klass() ||
kvn@1218 803 !klass0->as_obj_array_klass()->base_element_klass()->is_interface()) &&
kvn@1218 804 (!klass1->is_obj_array_klass() ||
kvn@1218 805 !klass1->as_obj_array_klass()->base_element_klass()->is_interface())) {
rasbold@731 806 bool unrelated_classes = false;
duke@435 807 // See if neither subclasses the other, or if the class on top
rasbold@731 808 // is precise. In either of these cases, the compare is known
rasbold@731 809 // to fail if at least one of the pointers is provably not null.
coleenp@4037 810 if (klass0->equals(klass1)) { // if types are unequal but klasses are equal
duke@435 811 // Do nothing; we know nothing for imprecise types
duke@435 812 } else if (klass0->is_subtype_of(klass1)) {
rasbold@731 813 // If klass1's type is PRECISE, then classes are unrelated.
rasbold@731 814 unrelated_classes = xklass1;
duke@435 815 } else if (klass1->is_subtype_of(klass0)) {
rasbold@731 816 // If klass0's type is PRECISE, then classes are unrelated.
rasbold@731 817 unrelated_classes = xklass0;
duke@435 818 } else { // Neither subtypes the other
rasbold@731 819 unrelated_classes = true;
rasbold@731 820 }
rasbold@731 821 if (unrelated_classes) {
rasbold@731 822 // The oops classes are known to be unrelated. If the joined PTRs of
rasbold@731 823 // two oops is not Null and not Bottom, then we are sure that one
rasbold@731 824 // of the two oops is non-null, and the comparison will always fail.
rasbold@731 825 TypePtr::PTR jp = r0->join_ptr(r1->_ptr);
rasbold@731 826 if (jp != TypePtr::Null && jp != TypePtr::BotPTR) {
rasbold@731 827 return TypeInt::CC_GT;
rasbold@731 828 }
duke@435 829 }
duke@435 830 }
duke@435 831 }
duke@435 832
duke@435 833 // Known constants can be compared exactly
duke@435 834 // Null can be distinguished from any NotNull pointers
duke@435 835 // Unknown inputs makes an unknown result
duke@435 836 if( r0->singleton() ) {
duke@435 837 intptr_t bits0 = r0->get_con();
duke@435 838 if( r1->singleton() )
duke@435 839 return bits0 == r1->get_con() ? TypeInt::CC_EQ : TypeInt::CC_GT;
duke@435 840 return ( r1->_ptr == TypePtr::NotNull && bits0==0 ) ? TypeInt::CC_GT : TypeInt::CC;
duke@435 841 } else if( r1->singleton() ) {
duke@435 842 intptr_t bits1 = r1->get_con();
duke@435 843 return ( r0->_ptr == TypePtr::NotNull && bits1==0 ) ? TypeInt::CC_GT : TypeInt::CC;
duke@435 844 } else
duke@435 845 return TypeInt::CC;
duke@435 846 }
duke@435 847
kvn@3834 848 static inline Node* isa_java_mirror_load(PhaseGVN* phase, Node* n) {
kvn@3834 849 // Return the klass node for
kvn@3834 850 // LoadP(AddP(foo:Klass, #java_mirror))
kvn@3834 851 // or NULL if not matching.
kvn@3834 852 if (n->Opcode() != Op_LoadP) return NULL;
kvn@3834 853
kvn@3834 854 const TypeInstPtr* tp = phase->type(n)->isa_instptr();
kvn@3834 855 if (!tp || tp->klass() != phase->C->env()->Class_klass()) return NULL;
kvn@3834 856
kvn@3834 857 Node* adr = n->in(MemNode::Address);
kvn@3834 858 intptr_t off = 0;
kvn@3834 859 Node* k = AddPNode::Ideal_base_and_offset(adr, phase, off);
kvn@3834 860 if (k == NULL) return NULL;
kvn@3834 861 const TypeKlassPtr* tkp = phase->type(k)->isa_klassptr();
kvn@3834 862 if (!tkp || off != in_bytes(Klass::java_mirror_offset())) return NULL;
kvn@3834 863
kvn@3834 864 // We've found the klass node of a Java mirror load.
kvn@3834 865 return k;
kvn@3834 866 }
kvn@3834 867
kvn@3834 868 static inline Node* isa_const_java_mirror(PhaseGVN* phase, Node* n) {
kvn@3834 869 // for ConP(Foo.class) return ConP(Foo.klass)
kvn@3834 870 // otherwise return NULL
kvn@3834 871 if (!n->is_Con()) return NULL;
kvn@3834 872
kvn@3834 873 const TypeInstPtr* tp = phase->type(n)->isa_instptr();
kvn@3834 874 if (!tp) return NULL;
kvn@3834 875
kvn@3834 876 ciType* mirror_type = tp->java_mirror_type();
kvn@3834 877 // TypeInstPtr::java_mirror_type() returns non-NULL for compile-
kvn@3834 878 // time Class constants only.
kvn@3834 879 if (!mirror_type) return NULL;
kvn@3834 880
kvn@3834 881 // x.getClass() == int.class can never be true (for all primitive types)
kvn@3834 882 // Return a ConP(NULL) node for this case.
kvn@3834 883 if (mirror_type->is_classless()) {
kvn@3834 884 return phase->makecon(TypePtr::NULL_PTR);
kvn@3834 885 }
kvn@3834 886
kvn@3834 887 // return the ConP(Foo.klass)
coleenp@4037 888 assert(mirror_type->is_klass(), "mirror_type should represent a Klass*");
kvn@3834 889 return phase->makecon(TypeKlassPtr::make(mirror_type->as_klass()));
kvn@3834 890 }
kvn@3834 891
duke@435 892 //------------------------------Ideal------------------------------------------
kvn@3834 893 // Normalize comparisons between Java mirror loads to compare the klass instead.
kvn@3834 894 //
kvn@3834 895 // Also check for the case of comparing an unknown klass loaded from the primary
duke@435 896 // super-type array vs a known klass with no subtypes. This amounts to
duke@435 897 // checking to see an unknown klass subtypes a known klass with no subtypes;
duke@435 898 // this only happens on an exact match. We can shorten this test by 1 load.
duke@435 899 Node *CmpPNode::Ideal( PhaseGVN *phase, bool can_reshape ) {
kvn@3834 900 // Normalize comparisons between Java mirrors into comparisons of the low-
kvn@3834 901 // level klass, where a dependent load could be shortened.
kvn@3834 902 //
kvn@3834 903 // The new pattern has a nice effect of matching the same pattern used in the
kvn@3834 904 // fast path of instanceof/checkcast/Class.isInstance(), which allows
kvn@3834 905 // redundant exact type check be optimized away by GVN.
kvn@3834 906 // For example, in
kvn@3834 907 // if (x.getClass() == Foo.class) {
kvn@3834 908 // Foo foo = (Foo) x;
kvn@3834 909 // // ... use a ...
kvn@3834 910 // }
kvn@3834 911 // a CmpPNode could be shared between if_acmpne and checkcast
kvn@3834 912 {
kvn@3834 913 Node* k1 = isa_java_mirror_load(phase, in(1));
kvn@3834 914 Node* k2 = isa_java_mirror_load(phase, in(2));
kvn@3834 915 Node* conk2 = isa_const_java_mirror(phase, in(2));
kvn@3834 916
kvn@3834 917 if (k1 && (k2 || conk2)) {
kvn@3834 918 Node* lhs = k1;
kvn@3834 919 Node* rhs = (k2 != NULL) ? k2 : conk2;
kvn@3834 920 this->set_req(1, lhs);
kvn@3834 921 this->set_req(2, rhs);
kvn@3834 922 return this;
kvn@3834 923 }
kvn@3834 924 }
kvn@3834 925
duke@435 926 // Constant pointer on right?
duke@435 927 const TypeKlassPtr* t2 = phase->type(in(2))->isa_klassptr();
duke@435 928 if (t2 == NULL || !t2->klass_is_exact())
duke@435 929 return NULL;
duke@435 930 // Get the constant klass we are comparing to.
duke@435 931 ciKlass* superklass = t2->klass();
duke@435 932
duke@435 933 // Now check for LoadKlass on left.
duke@435 934 Node* ldk1 = in(1);
roland@4159 935 if (ldk1->is_DecodeNKlass()) {
kvn@728 936 ldk1 = ldk1->in(1);
kvn@728 937 if (ldk1->Opcode() != Op_LoadNKlass )
kvn@728 938 return NULL;
kvn@728 939 } else if (ldk1->Opcode() != Op_LoadKlass )
duke@435 940 return NULL;
duke@435 941 // Take apart the address of the LoadKlass:
duke@435 942 Node* adr1 = ldk1->in(MemNode::Address);
duke@435 943 intptr_t con2 = 0;
duke@435 944 Node* ldk2 = AddPNode::Ideal_base_and_offset(adr1, phase, con2);
duke@435 945 if (ldk2 == NULL)
duke@435 946 return NULL;
duke@435 947 if (con2 == oopDesc::klass_offset_in_bytes()) {
duke@435 948 // We are inspecting an object's concrete class.
duke@435 949 // Short-circuit the check if the query is abstract.
duke@435 950 if (superklass->is_interface() ||
duke@435 951 superklass->is_abstract()) {
duke@435 952 // Make it come out always false:
duke@435 953 this->set_req(2, phase->makecon(TypePtr::NULL_PTR));
duke@435 954 return this;
duke@435 955 }
duke@435 956 }
duke@435 957
duke@435 958 // Check for a LoadKlass from primary supertype array.
duke@435 959 // Any nested loadklass from loadklass+con must be from the p.s. array.
roland@4159 960 if (ldk2->is_DecodeNKlass()) {
kvn@728 961 // Keep ldk2 as DecodeN since it could be used in CmpP below.
kvn@728 962 if (ldk2->in(1)->Opcode() != Op_LoadNKlass )
kvn@728 963 return NULL;
kvn@728 964 } else if (ldk2->Opcode() != Op_LoadKlass)
duke@435 965 return NULL;
duke@435 966
duke@435 967 // Verify that we understand the situation
duke@435 968 if (con2 != (intptr_t) superklass->super_check_offset())
duke@435 969 return NULL; // Might be element-klass loading from array klass
duke@435 970
duke@435 971 // If 'superklass' has no subklasses and is not an interface, then we are
duke@435 972 // assured that the only input which will pass the type check is
duke@435 973 // 'superklass' itself.
duke@435 974 //
duke@435 975 // We could be more liberal here, and allow the optimization on interfaces
duke@435 976 // which have a single implementor. This would require us to increase the
duke@435 977 // expressiveness of the add_dependency() mechanism.
duke@435 978 // %%% Do this after we fix TypeOopPtr: Deps are expressive enough now.
duke@435 979
duke@435 980 // Object arrays must have their base element have no subtypes
duke@435 981 while (superklass->is_obj_array_klass()) {
duke@435 982 ciType* elem = superklass->as_obj_array_klass()->element_type();
duke@435 983 superklass = elem->as_klass();
duke@435 984 }
duke@435 985 if (superklass->is_instance_klass()) {
duke@435 986 ciInstanceKlass* ik = superklass->as_instance_klass();
duke@435 987 if (ik->has_subklass() || ik->is_interface()) return NULL;
duke@435 988 // Add a dependency if there is a chance that a subclass will be added later.
duke@435 989 if (!ik->is_final()) {
duke@435 990 phase->C->dependencies()->assert_leaf_type(ik);
duke@435 991 }
duke@435 992 }
duke@435 993
duke@435 994 // Bypass the dependent load, and compare directly
duke@435 995 this->set_req(1,ldk2);
duke@435 996
duke@435 997 return this;
duke@435 998 }
duke@435 999
duke@435 1000 //=============================================================================
coleenp@548 1001 //------------------------------sub--------------------------------------------
coleenp@548 1002 // Simplify an CmpN (compare 2 pointers) node, based on local information.
coleenp@548 1003 // If both inputs are constants, compare them.
coleenp@548 1004 const Type *CmpNNode::sub( const Type *t1, const Type *t2 ) const {
kvn@656 1005 const TypePtr *r0 = t1->make_ptr(); // Handy access
kvn@656 1006 const TypePtr *r1 = t2->make_ptr();
coleenp@548 1007
coleenp@548 1008 // Undefined inputs makes for an undefined result
kvn@5111 1009 if ((r0 == NULL) || (r1 == NULL) ||
kvn@5111 1010 TypePtr::above_centerline(r0->_ptr) ||
kvn@5111 1011 TypePtr::above_centerline(r1->_ptr)) {
coleenp@548 1012 return Type::TOP;
kvn@5111 1013 }
coleenp@548 1014 if (r0 == r1 && r0->singleton()) {
coleenp@548 1015 // Equal pointer constants (klasses, nulls, etc.)
coleenp@548 1016 return TypeInt::CC_EQ;
coleenp@548 1017 }
coleenp@548 1018
coleenp@548 1019 // See if it is 2 unrelated classes.
coleenp@548 1020 const TypeOopPtr* p0 = r0->isa_oopptr();
coleenp@548 1021 const TypeOopPtr* p1 = r1->isa_oopptr();
coleenp@548 1022 if (p0 && p1) {
coleenp@548 1023 ciKlass* klass0 = p0->klass();
coleenp@548 1024 bool xklass0 = p0->klass_is_exact();
coleenp@548 1025 ciKlass* klass1 = p1->klass();
coleenp@548 1026 bool xklass1 = p1->klass_is_exact();
coleenp@548 1027 int kps = (p0->isa_klassptr()?1:0) + (p1->isa_klassptr()?1:0);
coleenp@548 1028 if (klass0 && klass1 &&
coleenp@548 1029 kps != 1 && // both or neither are klass pointers
coleenp@548 1030 !klass0->is_interface() && // do not trust interfaces
coleenp@548 1031 !klass1->is_interface()) {
rasbold@731 1032 bool unrelated_classes = false;
coleenp@548 1033 // See if neither subclasses the other, or if the class on top
rasbold@731 1034 // is precise. In either of these cases, the compare is known
rasbold@731 1035 // to fail if at least one of the pointers is provably not null.
coleenp@4037 1036 if (klass0->equals(klass1)) { // if types are unequal but klasses are equal
coleenp@548 1037 // Do nothing; we know nothing for imprecise types
coleenp@548 1038 } else if (klass0->is_subtype_of(klass1)) {
rasbold@731 1039 // If klass1's type is PRECISE, then classes are unrelated.
rasbold@731 1040 unrelated_classes = xklass1;
coleenp@548 1041 } else if (klass1->is_subtype_of(klass0)) {
rasbold@731 1042 // If klass0's type is PRECISE, then classes are unrelated.
rasbold@731 1043 unrelated_classes = xklass0;
coleenp@548 1044 } else { // Neither subtypes the other
rasbold@731 1045 unrelated_classes = true;
rasbold@731 1046 }
rasbold@731 1047 if (unrelated_classes) {
rasbold@731 1048 // The oops classes are known to be unrelated. If the joined PTRs of
rasbold@731 1049 // two oops is not Null and not Bottom, then we are sure that one
rasbold@731 1050 // of the two oops is non-null, and the comparison will always fail.
rasbold@731 1051 TypePtr::PTR jp = r0->join_ptr(r1->_ptr);
rasbold@731 1052 if (jp != TypePtr::Null && jp != TypePtr::BotPTR) {
rasbold@731 1053 return TypeInt::CC_GT;
rasbold@731 1054 }
coleenp@548 1055 }
coleenp@548 1056 }
coleenp@548 1057 }
coleenp@548 1058
coleenp@548 1059 // Known constants can be compared exactly
coleenp@548 1060 // Null can be distinguished from any NotNull pointers
coleenp@548 1061 // Unknown inputs makes an unknown result
coleenp@548 1062 if( r0->singleton() ) {
coleenp@548 1063 intptr_t bits0 = r0->get_con();
coleenp@548 1064 if( r1->singleton() )
coleenp@548 1065 return bits0 == r1->get_con() ? TypeInt::CC_EQ : TypeInt::CC_GT;
coleenp@548 1066 return ( r1->_ptr == TypePtr::NotNull && bits0==0 ) ? TypeInt::CC_GT : TypeInt::CC;
coleenp@548 1067 } else if( r1->singleton() ) {
coleenp@548 1068 intptr_t bits1 = r1->get_con();
coleenp@548 1069 return ( r0->_ptr == TypePtr::NotNull && bits1==0 ) ? TypeInt::CC_GT : TypeInt::CC;
coleenp@548 1070 } else
coleenp@548 1071 return TypeInt::CC;
coleenp@548 1072 }
coleenp@548 1073
coleenp@548 1074 //------------------------------Ideal------------------------------------------
coleenp@548 1075 Node *CmpNNode::Ideal( PhaseGVN *phase, bool can_reshape ) {
coleenp@548 1076 return NULL;
coleenp@548 1077 }
coleenp@548 1078
coleenp@548 1079 //=============================================================================
duke@435 1080 //------------------------------Value------------------------------------------
duke@435 1081 // Simplify an CmpF (compare 2 floats ) node, based on local information.
duke@435 1082 // If both inputs are constants, compare them.
duke@435 1083 const Type *CmpFNode::Value( PhaseTransform *phase ) const {
duke@435 1084 const Node* in1 = in(1);
duke@435 1085 const Node* in2 = in(2);
duke@435 1086 // Either input is TOP ==> the result is TOP
duke@435 1087 const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
duke@435 1088 if( t1 == Type::TOP ) return Type::TOP;
duke@435 1089 const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
duke@435 1090 if( t2 == Type::TOP ) return Type::TOP;
duke@435 1091
duke@435 1092 // Not constants? Don't know squat - even if they are the same
duke@435 1093 // value! If they are NaN's they compare to LT instead of EQ.
duke@435 1094 const TypeF *tf1 = t1->isa_float_constant();
duke@435 1095 const TypeF *tf2 = t2->isa_float_constant();
duke@435 1096 if( !tf1 || !tf2 ) return TypeInt::CC;
duke@435 1097
duke@435 1098 // This implements the Java bytecode fcmpl, so unordered returns -1.
duke@435 1099 if( tf1->is_nan() || tf2->is_nan() )
duke@435 1100 return TypeInt::CC_LT;
duke@435 1101
duke@435 1102 if( tf1->_f < tf2->_f ) return TypeInt::CC_LT;
duke@435 1103 if( tf1->_f > tf2->_f ) return TypeInt::CC_GT;
duke@435 1104 assert( tf1->_f == tf2->_f, "do not understand FP behavior" );
duke@435 1105 return TypeInt::CC_EQ;
duke@435 1106 }
duke@435 1107
duke@435 1108
duke@435 1109 //=============================================================================
duke@435 1110 //------------------------------Value------------------------------------------
duke@435 1111 // Simplify an CmpD (compare 2 doubles ) node, based on local information.
duke@435 1112 // If both inputs are constants, compare them.
duke@435 1113 const Type *CmpDNode::Value( PhaseTransform *phase ) const {
duke@435 1114 const Node* in1 = in(1);
duke@435 1115 const Node* in2 = in(2);
duke@435 1116 // Either input is TOP ==> the result is TOP
duke@435 1117 const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
duke@435 1118 if( t1 == Type::TOP ) return Type::TOP;
duke@435 1119 const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
duke@435 1120 if( t2 == Type::TOP ) return Type::TOP;
duke@435 1121
duke@435 1122 // Not constants? Don't know squat - even if they are the same
duke@435 1123 // value! If they are NaN's they compare to LT instead of EQ.
duke@435 1124 const TypeD *td1 = t1->isa_double_constant();
duke@435 1125 const TypeD *td2 = t2->isa_double_constant();
duke@435 1126 if( !td1 || !td2 ) return TypeInt::CC;
duke@435 1127
duke@435 1128 // This implements the Java bytecode dcmpl, so unordered returns -1.
duke@435 1129 if( td1->is_nan() || td2->is_nan() )
duke@435 1130 return TypeInt::CC_LT;
duke@435 1131
duke@435 1132 if( td1->_d < td2->_d ) return TypeInt::CC_LT;
duke@435 1133 if( td1->_d > td2->_d ) return TypeInt::CC_GT;
duke@435 1134 assert( td1->_d == td2->_d, "do not understand FP behavior" );
duke@435 1135 return TypeInt::CC_EQ;
duke@435 1136 }
duke@435 1137
duke@435 1138 //------------------------------Ideal------------------------------------------
duke@435 1139 Node *CmpDNode::Ideal(PhaseGVN *phase, bool can_reshape){
duke@435 1140 // Check if we can change this to a CmpF and remove a ConvD2F operation.
duke@435 1141 // Change (CMPD (F2D (float)) (ConD value))
duke@435 1142 // To (CMPF (float) (ConF value))
duke@435 1143 // Valid when 'value' does not lose precision as a float.
duke@435 1144 // Benefits: eliminates conversion, does not require 24-bit mode
duke@435 1145
duke@435 1146 // NaNs prevent commuting operands. This transform works regardless of the
duke@435 1147 // order of ConD and ConvF2D inputs by preserving the original order.
duke@435 1148 int idx_f2d = 1; // ConvF2D on left side?
duke@435 1149 if( in(idx_f2d)->Opcode() != Op_ConvF2D )
duke@435 1150 idx_f2d = 2; // No, swap to check for reversed args
duke@435 1151 int idx_con = 3-idx_f2d; // Check for the constant on other input
duke@435 1152
duke@435 1153 if( ConvertCmpD2CmpF &&
duke@435 1154 in(idx_f2d)->Opcode() == Op_ConvF2D &&
duke@435 1155 in(idx_con)->Opcode() == Op_ConD ) {
duke@435 1156 const TypeD *t2 = in(idx_con)->bottom_type()->is_double_constant();
duke@435 1157 double t2_value_as_double = t2->_d;
duke@435 1158 float t2_value_as_float = (float)t2_value_as_double;
duke@435 1159 if( t2_value_as_double == (double)t2_value_as_float ) {
duke@435 1160 // Test value can be represented as a float
duke@435 1161 // Eliminate the conversion to double and create new comparison
duke@435 1162 Node *new_in1 = in(idx_f2d)->in(1);
duke@435 1163 Node *new_in2 = phase->makecon( TypeF::make(t2_value_as_float) );
duke@435 1164 if( idx_f2d != 1 ) { // Must flip args to match original order
duke@435 1165 Node *tmp = new_in1;
duke@435 1166 new_in1 = new_in2;
duke@435 1167 new_in2 = tmp;
duke@435 1168 }
duke@435 1169 CmpFNode *new_cmp = (Opcode() == Op_CmpD3)
kvn@4115 1170 ? new (phase->C) CmpF3Node( new_in1, new_in2 )
kvn@4115 1171 : new (phase->C) CmpFNode ( new_in1, new_in2 ) ;
duke@435 1172 return new_cmp; // Changed to CmpFNode
duke@435 1173 }
duke@435 1174 // Testing value required the precision of a double
duke@435 1175 }
duke@435 1176 return NULL; // No change
duke@435 1177 }
duke@435 1178
duke@435 1179
duke@435 1180 //=============================================================================
duke@435 1181 //------------------------------cc2logical-------------------------------------
duke@435 1182 // Convert a condition code type to a logical type
duke@435 1183 const Type *BoolTest::cc2logical( const Type *CC ) const {
duke@435 1184 if( CC == Type::TOP ) return Type::TOP;
duke@435 1185 if( CC->base() != Type::Int ) return TypeInt::BOOL; // Bottom or worse
duke@435 1186 const TypeInt *ti = CC->is_int();
duke@435 1187 if( ti->is_con() ) { // Only 1 kind of condition codes set?
duke@435 1188 // Match low order 2 bits
duke@435 1189 int tmp = ((ti->get_con()&3) == (_test&3)) ? 1 : 0;
duke@435 1190 if( _test & 4 ) tmp = 1-tmp; // Optionally complement result
duke@435 1191 return TypeInt::make(tmp); // Boolean result
duke@435 1192 }
duke@435 1193
duke@435 1194 if( CC == TypeInt::CC_GE ) {
duke@435 1195 if( _test == ge ) return TypeInt::ONE;
duke@435 1196 if( _test == lt ) return TypeInt::ZERO;
duke@435 1197 }
duke@435 1198 if( CC == TypeInt::CC_LE ) {
duke@435 1199 if( _test == le ) return TypeInt::ONE;
duke@435 1200 if( _test == gt ) return TypeInt::ZERO;
duke@435 1201 }
duke@435 1202
duke@435 1203 return TypeInt::BOOL;
duke@435 1204 }
duke@435 1205
duke@435 1206 //------------------------------dump_spec-------------------------------------
duke@435 1207 // Print special per-node info
duke@435 1208 void BoolTest::dump_on(outputStream *st) const {
rbackman@5791 1209 const char *msg[] = {"eq","gt","of","lt","ne","le","nof","ge"};
drchase@6680 1210 st->print("%s", msg[_test]);
duke@435 1211 }
duke@435 1212
duke@435 1213 //=============================================================================
duke@435 1214 uint BoolNode::hash() const { return (Node::hash() << 3)|(_test._test+1); }
duke@435 1215 uint BoolNode::size_of() const { return sizeof(BoolNode); }
duke@435 1216
duke@435 1217 //------------------------------operator==-------------------------------------
duke@435 1218 uint BoolNode::cmp( const Node &n ) const {
duke@435 1219 const BoolNode *b = (const BoolNode *)&n; // Cast up
duke@435 1220 return (_test._test == b->_test._test);
duke@435 1221 }
duke@435 1222
duke@435 1223 //-------------------------------make_predicate--------------------------------
duke@435 1224 Node* BoolNode::make_predicate(Node* test_value, PhaseGVN* phase) {
duke@435 1225 if (test_value->is_Con()) return test_value;
duke@435 1226 if (test_value->is_Bool()) return test_value;
duke@435 1227 Compile* C = phase->C;
duke@435 1228 if (test_value->is_CMove() &&
duke@435 1229 test_value->in(CMoveNode::Condition)->is_Bool()) {
duke@435 1230 BoolNode* bol = test_value->in(CMoveNode::Condition)->as_Bool();
duke@435 1231 const Type* ftype = phase->type(test_value->in(CMoveNode::IfFalse));
duke@435 1232 const Type* ttype = phase->type(test_value->in(CMoveNode::IfTrue));
duke@435 1233 if (ftype == TypeInt::ZERO && !TypeInt::ZERO->higher_equal(ttype)) {
duke@435 1234 return bol;
duke@435 1235 } else if (ttype == TypeInt::ZERO && !TypeInt::ZERO->higher_equal(ftype)) {
duke@435 1236 return phase->transform( bol->negate(phase) );
duke@435 1237 }
duke@435 1238 // Else fall through. The CMove gets in the way of the test.
duke@435 1239 // It should be the case that make_predicate(bol->as_int_value()) == bol.
duke@435 1240 }
kvn@4115 1241 Node* cmp = new (C) CmpINode(test_value, phase->intcon(0));
duke@435 1242 cmp = phase->transform(cmp);
kvn@4115 1243 Node* bol = new (C) BoolNode(cmp, BoolTest::ne);
duke@435 1244 return phase->transform(bol);
duke@435 1245 }
duke@435 1246
duke@435 1247 //--------------------------------as_int_value---------------------------------
duke@435 1248 Node* BoolNode::as_int_value(PhaseGVN* phase) {
duke@435 1249 // Inverse to make_predicate. The CMove probably boils down to a Conv2B.
duke@435 1250 Node* cmov = CMoveNode::make(phase->C, NULL, this,
duke@435 1251 phase->intcon(0), phase->intcon(1),
duke@435 1252 TypeInt::BOOL);
duke@435 1253 return phase->transform(cmov);
duke@435 1254 }
duke@435 1255
duke@435 1256 //----------------------------------negate-------------------------------------
duke@435 1257 BoolNode* BoolNode::negate(PhaseGVN* phase) {
duke@435 1258 Compile* C = phase->C;
kvn@4115 1259 return new (C) BoolNode(in(1), _test.negate());
duke@435 1260 }
duke@435 1261
duke@435 1262
duke@435 1263 //------------------------------Ideal------------------------------------------
duke@435 1264 Node *BoolNode::Ideal(PhaseGVN *phase, bool can_reshape) {
duke@435 1265 // Change "bool tst (cmp con x)" into "bool ~tst (cmp x con)".
duke@435 1266 // This moves the constant to the right. Helps value-numbering.
duke@435 1267 Node *cmp = in(1);
duke@435 1268 if( !cmp->is_Sub() ) return NULL;
duke@435 1269 int cop = cmp->Opcode();
rbackman@6375 1270 if( cop == Op_FastLock || cop == Op_FastUnlock) return NULL;
duke@435 1271 Node *cmp1 = cmp->in(1);
duke@435 1272 Node *cmp2 = cmp->in(2);
duke@435 1273 if( !cmp1 ) return NULL;
duke@435 1274
rbackman@6375 1275 if (_test._test == BoolTest::overflow || _test._test == BoolTest::no_overflow) {
rbackman@6375 1276 return NULL;
rbackman@6375 1277 }
rbackman@6375 1278
duke@435 1279 // Constant on left?
duke@435 1280 Node *con = cmp1;
duke@435 1281 uint op2 = cmp2->Opcode();
duke@435 1282 // Move constants to the right of compare's to canonicalize.
duke@435 1283 // Do not muck with Opaque1 nodes, as this indicates a loop
duke@435 1284 // guard that cannot change shape.
duke@435 1285 if( con->is_Con() && !cmp2->is_Con() && op2 != Op_Opaque1 &&
duke@435 1286 // Because of NaN's, CmpD and CmpF are not commutative
duke@435 1287 cop != Op_CmpD && cop != Op_CmpF &&
duke@435 1288 // Protect against swapping inputs to a compare when it is used by a
duke@435 1289 // counted loop exit, which requires maintaining the loop-limit as in(2)
duke@435 1290 !is_counted_loop_exit_test() ) {
duke@435 1291 // Ok, commute the constant to the right of the cmp node.
duke@435 1292 // Clone the Node, getting a new Node of the same class
duke@435 1293 cmp = cmp->clone();
duke@435 1294 // Swap inputs to the clone
duke@435 1295 cmp->swap_edges(1, 2);
duke@435 1296 cmp = phase->transform( cmp );
kvn@4115 1297 return new (phase->C) BoolNode( cmp, _test.commute() );
duke@435 1298 }
duke@435 1299
duke@435 1300 // Change "bool eq/ne (cmp (xor X 1) 0)" into "bool ne/eq (cmp X 0)".
duke@435 1301 // The XOR-1 is an idiom used to flip the sense of a bool. We flip the
duke@435 1302 // test instead.
duke@435 1303 int cmp1_op = cmp1->Opcode();
duke@435 1304 const TypeInt* cmp2_type = phase->type(cmp2)->isa_int();
duke@435 1305 if (cmp2_type == NULL) return NULL;
duke@435 1306 Node* j_xor = cmp1;
duke@435 1307 if( cmp2_type == TypeInt::ZERO &&
duke@435 1308 cmp1_op == Op_XorI &&
duke@435 1309 j_xor->in(1) != j_xor && // An xor of itself is dead
kvn@2940 1310 phase->type( j_xor->in(1) ) == TypeInt::BOOL &&
duke@435 1311 phase->type( j_xor->in(2) ) == TypeInt::ONE &&
duke@435 1312 (_test._test == BoolTest::eq ||
duke@435 1313 _test._test == BoolTest::ne) ) {
kvn@4115 1314 Node *ncmp = phase->transform(new (phase->C) CmpINode(j_xor->in(1),cmp2));
kvn@4115 1315 return new (phase->C) BoolNode( ncmp, _test.negate() );
duke@435 1316 }
duke@435 1317
duke@435 1318 // Change "bool eq/ne (cmp (Conv2B X) 0)" into "bool eq/ne (cmp X 0)".
duke@435 1319 // This is a standard idiom for branching on a boolean value.
duke@435 1320 Node *c2b = cmp1;
duke@435 1321 if( cmp2_type == TypeInt::ZERO &&
duke@435 1322 cmp1_op == Op_Conv2B &&
duke@435 1323 (_test._test == BoolTest::eq ||
duke@435 1324 _test._test == BoolTest::ne) ) {
duke@435 1325 Node *ncmp = phase->transform(phase->type(c2b->in(1))->isa_int()
kvn@4115 1326 ? (Node*)new (phase->C) CmpINode(c2b->in(1),cmp2)
kvn@4115 1327 : (Node*)new (phase->C) CmpPNode(c2b->in(1),phase->makecon(TypePtr::NULL_PTR))
duke@435 1328 );
kvn@4115 1329 return new (phase->C) BoolNode( ncmp, _test._test );
duke@435 1330 }
duke@435 1331
duke@435 1332 // Comparing a SubI against a zero is equal to comparing the SubI
duke@435 1333 // arguments directly. This only works for eq and ne comparisons
duke@435 1334 // due to possible integer overflow.
duke@435 1335 if ((_test._test == BoolTest::eq || _test._test == BoolTest::ne) &&
duke@435 1336 (cop == Op_CmpI) &&
duke@435 1337 (cmp1->Opcode() == Op_SubI) &&
duke@435 1338 ( cmp2_type == TypeInt::ZERO ) ) {
kvn@4115 1339 Node *ncmp = phase->transform( new (phase->C) CmpINode(cmp1->in(1),cmp1->in(2)));
kvn@4115 1340 return new (phase->C) BoolNode( ncmp, _test._test );
duke@435 1341 }
duke@435 1342
duke@435 1343 // Change (-A vs 0) into (A vs 0) by commuting the test. Disallow in the
duke@435 1344 // most general case because negating 0x80000000 does nothing. Needed for
duke@435 1345 // the CmpF3/SubI/CmpI idiom.
duke@435 1346 if( cop == Op_CmpI &&
duke@435 1347 cmp1->Opcode() == Op_SubI &&
duke@435 1348 cmp2_type == TypeInt::ZERO &&
duke@435 1349 phase->type( cmp1->in(1) ) == TypeInt::ZERO &&
duke@435 1350 phase->type( cmp1->in(2) )->higher_equal(TypeInt::SYMINT) ) {
kvn@4115 1351 Node *ncmp = phase->transform( new (phase->C) CmpINode(cmp1->in(2),cmp2));
kvn@4115 1352 return new (phase->C) BoolNode( ncmp, _test.commute() );
duke@435 1353 }
duke@435 1354
duke@435 1355 // The transformation below is not valid for either signed or unsigned
duke@435 1356 // comparisons due to wraparound concerns at MAX_VALUE and MIN_VALUE.
duke@435 1357 // This transformation can be resurrected when we are able to
duke@435 1358 // make inferences about the range of values being subtracted from
duke@435 1359 // (or added to) relative to the wraparound point.
duke@435 1360 //
duke@435 1361 // // Remove +/-1's if possible.
duke@435 1362 // // "X <= Y-1" becomes "X < Y"
duke@435 1363 // // "X+1 <= Y" becomes "X < Y"
duke@435 1364 // // "X < Y+1" becomes "X <= Y"
duke@435 1365 // // "X-1 < Y" becomes "X <= Y"
duke@435 1366 // // Do not this to compares off of the counted-loop-end. These guys are
duke@435 1367 // // checking the trip counter and they want to use the post-incremented
duke@435 1368 // // counter. If they use the PRE-incremented counter, then the counter has
duke@435 1369 // // to be incremented in a private block on a loop backedge.
duke@435 1370 // if( du && du->cnt(this) && du->out(this)[0]->Opcode() == Op_CountedLoopEnd )
duke@435 1371 // return NULL;
duke@435 1372 // #ifndef PRODUCT
duke@435 1373 // // Do not do this in a wash GVN pass during verification.
duke@435 1374 // // Gets triggered by too many simple optimizations to be bothered with
duke@435 1375 // // re-trying it again and again.
duke@435 1376 // if( !phase->allow_progress() ) return NULL;
duke@435 1377 // #endif
duke@435 1378 // // Not valid for unsigned compare because of corner cases in involving zero.
duke@435 1379 // // For example, replacing "X-1 <u Y" with "X <=u Y" fails to throw an
duke@435 1380 // // exception in case X is 0 (because 0-1 turns into 4billion unsigned but
duke@435 1381 // // "0 <=u Y" is always true).
duke@435 1382 // if( cmp->Opcode() == Op_CmpU ) return NULL;
duke@435 1383 // int cmp2_op = cmp2->Opcode();
duke@435 1384 // if( _test._test == BoolTest::le ) {
duke@435 1385 // if( cmp1_op == Op_AddI &&
duke@435 1386 // phase->type( cmp1->in(2) ) == TypeInt::ONE )
duke@435 1387 // return clone_cmp( cmp, cmp1->in(1), cmp2, phase, BoolTest::lt );
duke@435 1388 // else if( cmp2_op == Op_AddI &&
duke@435 1389 // phase->type( cmp2->in(2) ) == TypeInt::MINUS_1 )
duke@435 1390 // return clone_cmp( cmp, cmp1, cmp2->in(1), phase, BoolTest::lt );
duke@435 1391 // } else if( _test._test == BoolTest::lt ) {
duke@435 1392 // if( cmp1_op == Op_AddI &&
duke@435 1393 // phase->type( cmp1->in(2) ) == TypeInt::MINUS_1 )
duke@435 1394 // return clone_cmp( cmp, cmp1->in(1), cmp2, phase, BoolTest::le );
duke@435 1395 // else if( cmp2_op == Op_AddI &&
duke@435 1396 // phase->type( cmp2->in(2) ) == TypeInt::ONE )
duke@435 1397 // return clone_cmp( cmp, cmp1, cmp2->in(1), phase, BoolTest::le );
duke@435 1398 // }
duke@435 1399
duke@435 1400 return NULL;
duke@435 1401 }
duke@435 1402
duke@435 1403 //------------------------------Value------------------------------------------
duke@435 1404 // Simplify a Bool (convert condition codes to boolean (1 or 0)) node,
duke@435 1405 // based on local information. If the input is constant, do it.
duke@435 1406 const Type *BoolNode::Value( PhaseTransform *phase ) const {
duke@435 1407 return _test.cc2logical( phase->type( in(1) ) );
duke@435 1408 }
duke@435 1409
duke@435 1410 //------------------------------dump_spec--------------------------------------
duke@435 1411 // Dump special per-node info
duke@435 1412 #ifndef PRODUCT
duke@435 1413 void BoolNode::dump_spec(outputStream *st) const {
duke@435 1414 st->print("[");
duke@435 1415 _test.dump_on(st);
duke@435 1416 st->print("]");
duke@435 1417 }
duke@435 1418 #endif
duke@435 1419
duke@435 1420 //------------------------------is_counted_loop_exit_test--------------------------------------
duke@435 1421 // Returns true if node is used by a counted loop node.
duke@435 1422 bool BoolNode::is_counted_loop_exit_test() {
duke@435 1423 for( DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++ ) {
duke@435 1424 Node* use = fast_out(i);
duke@435 1425 if (use->is_CountedLoopEnd()) {
duke@435 1426 return true;
duke@435 1427 }
duke@435 1428 }
duke@435 1429 return false;
duke@435 1430 }
duke@435 1431
duke@435 1432 //=============================================================================
duke@435 1433 //------------------------------Value------------------------------------------
duke@435 1434 // Compute sqrt
duke@435 1435 const Type *SqrtDNode::Value( PhaseTransform *phase ) const {
duke@435 1436 const Type *t1 = phase->type( in(1) );
duke@435 1437 if( t1 == Type::TOP ) return Type::TOP;
duke@435 1438 if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
duke@435 1439 double d = t1->getd();
duke@435 1440 if( d < 0.0 ) return Type::DOUBLE;
duke@435 1441 return TypeD::make( sqrt( d ) );
duke@435 1442 }
duke@435 1443
duke@435 1444 //=============================================================================
duke@435 1445 //------------------------------Value------------------------------------------
duke@435 1446 // Compute cos
duke@435 1447 const Type *CosDNode::Value( PhaseTransform *phase ) const {
duke@435 1448 const Type *t1 = phase->type( in(1) );
duke@435 1449 if( t1 == Type::TOP ) return Type::TOP;
duke@435 1450 if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
duke@435 1451 double d = t1->getd();
never@1609 1452 return TypeD::make( StubRoutines::intrinsic_cos( d ) );
duke@435 1453 }
duke@435 1454
duke@435 1455 //=============================================================================
duke@435 1456 //------------------------------Value------------------------------------------
duke@435 1457 // Compute sin
duke@435 1458 const Type *SinDNode::Value( PhaseTransform *phase ) const {
duke@435 1459 const Type *t1 = phase->type( in(1) );
duke@435 1460 if( t1 == Type::TOP ) return Type::TOP;
duke@435 1461 if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
duke@435 1462 double d = t1->getd();
never@1609 1463 return TypeD::make( StubRoutines::intrinsic_sin( d ) );
duke@435 1464 }
duke@435 1465
duke@435 1466 //=============================================================================
duke@435 1467 //------------------------------Value------------------------------------------
duke@435 1468 // Compute tan
duke@435 1469 const Type *TanDNode::Value( PhaseTransform *phase ) const {
duke@435 1470 const Type *t1 = phase->type( in(1) );
duke@435 1471 if( t1 == Type::TOP ) return Type::TOP;
duke@435 1472 if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
duke@435 1473 double d = t1->getd();
never@1609 1474 return TypeD::make( StubRoutines::intrinsic_tan( d ) );
duke@435 1475 }
duke@435 1476
duke@435 1477 //=============================================================================
duke@435 1478 //------------------------------Value------------------------------------------
duke@435 1479 // Compute log
duke@435 1480 const Type *LogDNode::Value( PhaseTransform *phase ) const {
duke@435 1481 const Type *t1 = phase->type( in(1) );
duke@435 1482 if( t1 == Type::TOP ) return Type::TOP;
duke@435 1483 if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
duke@435 1484 double d = t1->getd();
never@1609 1485 return TypeD::make( StubRoutines::intrinsic_log( d ) );
duke@435 1486 }
duke@435 1487
duke@435 1488 //=============================================================================
duke@435 1489 //------------------------------Value------------------------------------------
duke@435 1490 // Compute log10
duke@435 1491 const Type *Log10DNode::Value( PhaseTransform *phase ) const {
duke@435 1492 const Type *t1 = phase->type( in(1) );
duke@435 1493 if( t1 == Type::TOP ) return Type::TOP;
duke@435 1494 if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
duke@435 1495 double d = t1->getd();
never@1609 1496 return TypeD::make( StubRoutines::intrinsic_log10( d ) );
duke@435 1497 }
duke@435 1498
duke@435 1499 //=============================================================================
duke@435 1500 //------------------------------Value------------------------------------------
duke@435 1501 // Compute exp
duke@435 1502 const Type *ExpDNode::Value( PhaseTransform *phase ) const {
duke@435 1503 const Type *t1 = phase->type( in(1) );
duke@435 1504 if( t1 == Type::TOP ) return Type::TOP;
duke@435 1505 if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
duke@435 1506 double d = t1->getd();
never@1609 1507 return TypeD::make( StubRoutines::intrinsic_exp( d ) );
duke@435 1508 }
duke@435 1509
duke@435 1510
duke@435 1511 //=============================================================================
duke@435 1512 //------------------------------Value------------------------------------------
duke@435 1513 // Compute pow
duke@435 1514 const Type *PowDNode::Value( PhaseTransform *phase ) const {
duke@435 1515 const Type *t1 = phase->type( in(1) );
duke@435 1516 if( t1 == Type::TOP ) return Type::TOP;
duke@435 1517 if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
duke@435 1518 const Type *t2 = phase->type( in(2) );
duke@435 1519 if( t2 == Type::TOP ) return Type::TOP;
duke@435 1520 if( t2->base() != Type::DoubleCon ) return Type::DOUBLE;
duke@435 1521 double d1 = t1->getd();
duke@435 1522 double d2 = t2->getd();
never@1609 1523 return TypeD::make( StubRoutines::intrinsic_pow( d1, d2 ) );
duke@435 1524 }

mercurial