src/share/vm/opto/subnode.cpp

Thu, 24 May 2018 19:26:50 +0800

author
aoqi
date
Thu, 24 May 2018 19:26:50 +0800
changeset 8862
fd13a567f179
parent 8856
ac27a9c85bea
child 9637
eef07cd490d4
permissions
-rw-r--r--

#7046 C2 supports long branch
Contributed-by: fujie

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

mercurial