1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "opto/addnode.hpp"
  27 #include "opto/constnode.hpp"
  28 #include "opto/convertnode.hpp"
  29 #include "opto/movenode.hpp"
  30 #include "opto/phaseX.hpp"
  31 #include "opto/subnode.hpp"
  32 
  33 //------------------------------Ideal------------------------------------------
  34 // Return a node which is more "ideal" than the current node.
  35 // Move constants to the right.
  36 Node *CMoveNode::Ideal(PhaseGVN *phase, bool can_reshape) {
  37     if( in(0) && remove_dead_region(phase, can_reshape) ) return this;
  38     // Don't bother trying to transform a dead node
  39     if( in(0) && in(0)->is_top() )  return NULL;
  40     assert( !phase->eqv(in(Condition), this) &&
  41            !phase->eqv(in(IfFalse), this) &&
  42            !phase->eqv(in(IfTrue), this), "dead loop in CMoveNode::Ideal" );
  43     if( phase->type(in(Condition)) == Type::TOP )
  44     return NULL; // return NULL when Condition is dead
  45 
  46     if( in(IfFalse)->is_Con() && !in(IfTrue)->is_Con() ) {
  47         if( in(Condition)->is_Bool() ) {
  48             BoolNode* b  = in(Condition)->as_Bool();
  49             BoolNode* b2 = b->negate(phase);
  50             return make( phase->C, in(Control), phase->transform(b2), in(IfTrue), in(IfFalse), _type );
  51         }
  52     }
  53     return NULL;
  54 }
  55 
  56 //------------------------------is_cmove_id------------------------------------
  57 // Helper function to check for CMOVE identity.  Shared with PhiNode::Identity
  58 Node *CMoveNode::is_cmove_id( PhaseTransform *phase, Node *cmp, Node *t, Node *f, BoolNode *b ) {
  59     // Check for Cmp'ing and CMove'ing same values
  60     if( (phase->eqv(cmp->in(1),f) &&
  61          phase->eqv(cmp->in(2),t)) ||
  62        // Swapped Cmp is OK
  63        (phase->eqv(cmp->in(2),f) &&
  64         phase->eqv(cmp->in(1),t)) ) {
  65            // Give up this identity check for floating points because it may choose incorrect
  66            // value around 0.0 and -0.0
  67            if ( cmp->Opcode()==Op_CmpF || cmp->Opcode()==Op_CmpD )
  68            return NULL;
  69            // Check for "(t==f)?t:f;" and replace with "f"
  70            if( b->_test._test == BoolTest::eq )
  71            return f;
  72            // Allow the inverted case as well
  73            // Check for "(t!=f)?t:f;" and replace with "t"
  74            if( b->_test._test == BoolTest::ne )
  75            return t;
  76        }
  77     return NULL;
  78 }
  79 
  80 //------------------------------Identity---------------------------------------
  81 // Conditional-move is an identity if both inputs are the same, or the test
  82 // true or false.
  83 Node *CMoveNode::Identity( PhaseTransform *phase ) {
  84     if( phase->eqv(in(IfFalse),in(IfTrue)) ) // C-moving identical inputs?
  85     return in(IfFalse);         // Then it doesn't matter
  86     if( phase->type(in(Condition)) == TypeInt::ZERO )
  87     return in(IfFalse);         // Always pick left(false) input
  88     if( phase->type(in(Condition)) == TypeInt::ONE )
  89     return in(IfTrue);          // Always pick right(true) input
  90 
  91     // Check for CMove'ing a constant after comparing against the constant.
  92     // Happens all the time now, since if we compare equality vs a constant in
  93     // the parser, we "know" the variable is constant on one path and we force
  94     // it.  Thus code like "if( x==0 ) {/*EMPTY*/}" ends up inserting a
  95     // conditional move: "x = (x==0)?0:x;".  Yucko.  This fix is slightly more
  96     // general in that we don't need constants.
  97     if( in(Condition)->is_Bool() ) {
  98         BoolNode *b = in(Condition)->as_Bool();
  99         Node *cmp = b->in(1);
 100         if( cmp->is_Cmp() ) {
 101             Node *id = is_cmove_id( phase, cmp, in(IfTrue), in(IfFalse), b );
 102             if( id ) return id;
 103         }
 104     }
 105 
 106     return this;
 107 }
 108 
 109 //------------------------------Value------------------------------------------
 110 // Result is the meet of inputs
 111 const Type *CMoveNode::Value( PhaseTransform *phase ) const {
 112     if( phase->type(in(Condition)) == Type::TOP )
 113     return Type::TOP;
 114     return phase->type(in(IfFalse))->meet_speculative(phase->type(in(IfTrue)));
 115 }
 116 
 117 //------------------------------make-------------------------------------------
 118 // Make a correctly-flavored CMove.  Since _type is directly determined
 119 // from the inputs we do not need to specify it here.
 120 CMoveNode *CMoveNode::make( Compile *C, Node *c, Node *bol, Node *left, Node *right, const Type *t ) {
 121     switch( t->basic_type() ) {
 122         case T_INT:     return new (C) CMoveINode( bol, left, right, t->is_int() );
 123         case T_FLOAT:   return new (C) CMoveFNode( bol, left, right, t );
 124         case T_DOUBLE:  return new (C) CMoveDNode( bol, left, right, t );
 125         case T_LONG:    return new (C) CMoveLNode( bol, left, right, t->is_long() );
 126         case T_OBJECT:  return new (C) CMovePNode( c, bol, left, right, t->is_oopptr() );
 127         case T_ADDRESS: return new (C) CMovePNode( c, bol, left, right, t->is_ptr() );
 128         case T_NARROWOOP: return new (C) CMoveNNode( c, bol, left, right, t );
 129         default:
 130         ShouldNotReachHere();
 131         return NULL;
 132     }
 133 }
 134 
 135 //=============================================================================
 136 //------------------------------Ideal------------------------------------------
 137 // Return a node which is more "ideal" than the current node.
 138 // Check for conversions to boolean
 139 Node *CMoveINode::Ideal(PhaseGVN *phase, bool can_reshape) {
 140     // Try generic ideal's first
 141     Node *x = CMoveNode::Ideal(phase, can_reshape);
 142     if( x ) return x;
 143 
 144     // If zero is on the left (false-case, no-move-case) it must mean another
 145     // constant is on the right (otherwise the shared CMove::Ideal code would
 146     // have moved the constant to the right).  This situation is bad for Intel
 147     // and a don't-care for Sparc.  It's bad for Intel because the zero has to
 148     // be manifested in a register with a XOR which kills flags, which are live
 149     // on input to the CMoveI, leading to a situation which causes excessive
 150     // spilling on Intel.  For Sparc, if the zero in on the left the Sparc will
 151     // zero a register via G0 and conditionally-move the other constant.  If the
 152     // zero is on the right, the Sparc will load the first constant with a
 153     // 13-bit set-lo and conditionally move G0.  See bug 4677505.
 154     if( phase->type(in(IfFalse)) == TypeInt::ZERO && !(phase->type(in(IfTrue)) == TypeInt::ZERO) ) {
 155         if( in(Condition)->is_Bool() ) {
 156             BoolNode* b  = in(Condition)->as_Bool();
 157             BoolNode* b2 = b->negate(phase);
 158             return make( phase->C, in(Control), phase->transform(b2), in(IfTrue), in(IfFalse), _type );
 159         }
 160     }
 161 
 162     // Now check for booleans
 163     int flip = 0;
 164 
 165     // Check for picking from zero/one
 166     if( phase->type(in(IfFalse)) == TypeInt::ZERO && phase->type(in(IfTrue)) == TypeInt::ONE ) {
 167         flip = 1 - flip;
 168     } else if( phase->type(in(IfFalse)) == TypeInt::ONE && phase->type(in(IfTrue)) == TypeInt::ZERO ) {
 169     } else return NULL;
 170 
 171     // Check for eq/ne test
 172     if( !in(1)->is_Bool() ) return NULL;
 173     BoolNode *bol = in(1)->as_Bool();
 174     if( bol->_test._test == BoolTest::eq ) {
 175     } else if( bol->_test._test == BoolTest::ne ) {
 176         flip = 1-flip;
 177     } else return NULL;
 178 
 179     // Check for vs 0 or 1
 180     if( !bol->in(1)->is_Cmp() ) return NULL;
 181     const CmpNode *cmp = bol->in(1)->as_Cmp();
 182     if( phase->type(cmp->in(2)) == TypeInt::ZERO ) {
 183     } else if( phase->type(cmp->in(2)) == TypeInt::ONE ) {
 184         // Allow cmp-vs-1 if the other input is bounded by 0-1
 185         if( phase->type(cmp->in(1)) != TypeInt::BOOL )
 186         return NULL;
 187         flip = 1 - flip;
 188     } else return NULL;
 189 
 190     // Convert to a bool (flipped)
 191     // Build int->bool conversion
 192 #ifndef PRODUCT
 193     if( PrintOpto ) tty->print_cr("CMOV to I2B");
 194 #endif
 195     Node *n = new (phase->C) Conv2BNode( cmp->in(1) );
 196     if( flip )
 197     n = new (phase->C) XorINode( phase->transform(n), phase->intcon(1) );
 198 
 199     return n;
 200 }
 201 
 202 //=============================================================================
 203 //------------------------------Ideal------------------------------------------
 204 // Return a node which is more "ideal" than the current node.
 205 // Check for absolute value
 206 Node *CMoveFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 207     // Try generic ideal's first
 208     Node *x = CMoveNode::Ideal(phase, can_reshape);
 209     if( x ) return x;
 210 
 211     int  cmp_zero_idx = 0;        // Index of compare input where to look for zero
 212     int  phi_x_idx = 0;           // Index of phi input where to find naked x
 213 
 214     // Find the Bool
 215     if( !in(1)->is_Bool() ) return NULL;
 216     BoolNode *bol = in(1)->as_Bool();
 217     // Check bool sense
 218     switch( bol->_test._test ) {
 219         case BoolTest::lt: cmp_zero_idx = 1; phi_x_idx = IfTrue;  break;
 220         case BoolTest::le: cmp_zero_idx = 2; phi_x_idx = IfFalse; break;
 221         case BoolTest::gt: cmp_zero_idx = 2; phi_x_idx = IfTrue;  break;
 222         case BoolTest::ge: cmp_zero_idx = 1; phi_x_idx = IfFalse; break;
 223         default:           return NULL;                           break;
 224     }
 225 
 226     // Find zero input of CmpF; the other input is being abs'd
 227     Node *cmpf = bol->in(1);
 228     if( cmpf->Opcode() != Op_CmpF ) return NULL;
 229     Node *X = NULL;
 230     bool flip = false;
 231     if( phase->type(cmpf->in(cmp_zero_idx)) == TypeF::ZERO ) {
 232         X = cmpf->in(3 - cmp_zero_idx);
 233     } else if (phase->type(cmpf->in(3 - cmp_zero_idx)) == TypeF::ZERO) {
 234         // The test is inverted, we should invert the result...
 235         X = cmpf->in(cmp_zero_idx);
 236         flip = true;
 237     } else {
 238         return NULL;
 239     }
 240 
 241     // If X is found on the appropriate phi input, find the subtract on the other
 242     if( X != in(phi_x_idx) ) return NULL;
 243     int phi_sub_idx = phi_x_idx == IfTrue ? IfFalse : IfTrue;
 244     Node *sub = in(phi_sub_idx);
 245 
 246     // Allow only SubF(0,X) and fail out for all others; NegF is not OK
 247     if( sub->Opcode() != Op_SubF ||
 248        sub->in(2) != X ||
 249        phase->type(sub->in(1)) != TypeF::ZERO ) return NULL;
 250 
 251     Node *abs = new (phase->C) AbsFNode( X );
 252     if( flip )
 253     abs = new (phase->C) SubFNode(sub->in(1), phase->transform(abs));
 254 
 255     return abs;
 256 }
 257 
 258 //=============================================================================
 259 //------------------------------Ideal------------------------------------------
 260 // Return a node which is more "ideal" than the current node.
 261 // Check for absolute value
 262 Node *CMoveDNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 263     // Try generic ideal's first
 264     Node *x = CMoveNode::Ideal(phase, can_reshape);
 265     if( x ) return x;
 266 
 267     int  cmp_zero_idx = 0;        // Index of compare input where to look for zero
 268     int  phi_x_idx = 0;           // Index of phi input where to find naked x
 269 
 270     // Find the Bool
 271     if( !in(1)->is_Bool() ) return NULL;
 272     BoolNode *bol = in(1)->as_Bool();
 273     // Check bool sense
 274     switch( bol->_test._test ) {
 275         case BoolTest::lt: cmp_zero_idx = 1; phi_x_idx = IfTrue;  break;
 276         case BoolTest::le: cmp_zero_idx = 2; phi_x_idx = IfFalse; break;
 277         case BoolTest::gt: cmp_zero_idx = 2; phi_x_idx = IfTrue;  break;
 278         case BoolTest::ge: cmp_zero_idx = 1; phi_x_idx = IfFalse; break;
 279         default:           return NULL;                           break;
 280     }
 281 
 282     // Find zero input of CmpD; the other input is being abs'd
 283     Node *cmpd = bol->in(1);
 284     if( cmpd->Opcode() != Op_CmpD ) return NULL;
 285     Node *X = NULL;
 286     bool flip = false;
 287     if( phase->type(cmpd->in(cmp_zero_idx)) == TypeD::ZERO ) {
 288         X = cmpd->in(3 - cmp_zero_idx);
 289     } else if (phase->type(cmpd->in(3 - cmp_zero_idx)) == TypeD::ZERO) {
 290         // The test is inverted, we should invert the result...
 291         X = cmpd->in(cmp_zero_idx);
 292         flip = true;
 293     } else {
 294         return NULL;
 295     }
 296     
 297     // If X is found on the appropriate phi input, find the subtract on the other
 298     if( X != in(phi_x_idx) ) return NULL;
 299     int phi_sub_idx = phi_x_idx == IfTrue ? IfFalse : IfTrue;
 300     Node *sub = in(phi_sub_idx);
 301     
 302     // Allow only SubD(0,X) and fail out for all others; NegD is not OK
 303     if( sub->Opcode() != Op_SubD ||
 304        sub->in(2) != X ||
 305        phase->type(sub->in(1)) != TypeD::ZERO ) return NULL;
 306     
 307     Node *abs = new (phase->C) AbsDNode( X );
 308     if( flip )
 309     abs = new (phase->C) SubDNode(sub->in(1), phase->transform(abs));
 310     
 311     return abs;
 312 }
 313 
 314 //------------------------------Value------------------------------------------
 315 const Type *MoveL2DNode::Value( PhaseTransform *phase ) const {
 316     const Type *t = phase->type( in(1) );
 317     if( t == Type::TOP ) return Type::TOP;
 318     const TypeLong *tl = t->is_long();
 319     if( !tl->is_con() ) return bottom_type();
 320     JavaValue v;
 321     v.set_jlong(tl->get_con());
 322     return TypeD::make( v.get_jdouble() );
 323 }
 324 
 325 //------------------------------Value------------------------------------------
 326 const Type *MoveI2FNode::Value( PhaseTransform *phase ) const {
 327     const Type *t = phase->type( in(1) );
 328     if( t == Type::TOP ) return Type::TOP;
 329     const TypeInt *ti = t->is_int();
 330     if( !ti->is_con() )   return bottom_type();
 331     JavaValue v;
 332     v.set_jint(ti->get_con());
 333     return TypeF::make( v.get_jfloat() );
 334 }
 335 
 336 //------------------------------Value------------------------------------------
 337 const Type *MoveF2INode::Value( PhaseTransform *phase ) const {
 338     const Type *t = phase->type( in(1) );
 339     if( t == Type::TOP )       return Type::TOP;
 340     if( t == Type::FLOAT ) return TypeInt::INT;
 341     const TypeF *tf = t->is_float_constant();
 342     JavaValue v;
 343     v.set_jfloat(tf->getf());
 344     return TypeInt::make( v.get_jint() );
 345 }
 346 
 347 //------------------------------Value------------------------------------------
 348 const Type *MoveD2LNode::Value( PhaseTransform *phase ) const {
 349     const Type *t = phase->type( in(1) );
 350     if( t == Type::TOP ) return Type::TOP;
 351     if( t == Type::DOUBLE ) return TypeLong::LONG;
 352     const TypeD *td = t->is_double_constant();
 353     JavaValue v;
 354     v.set_jdouble(td->getd());
 355     return TypeLong::make( v.get_jlong() );
 356 }
 357