1 /*
   2  * Copyright (c) 1999, 2010, 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 "incls/_precompiled.incl"
  26 #include "incls/_c1_Canonicalizer.cpp.incl"
  27 
  28 
  29 class PrintValueVisitor: public ValueVisitor {
  30   void visit(Value* vp) {
  31     (*vp)->print_line();
  32   }
  33 };
  34 
  35 void Canonicalizer::set_canonical(Value x) {
  36   assert(x != NULL, "value must exist");
  37   // Note: we can not currently substitute root nodes which show up in
  38   // the instruction stream (because the instruction list is embedded
  39   // in the instructions).
  40   if (canonical() != x) {
  41     if (PrintCanonicalization) {
  42       PrintValueVisitor do_print_value;
  43       canonical()->input_values_do(&do_print_value);
  44       canonical()->print_line();
  45       tty->print_cr("canonicalized to:");
  46       x->input_values_do(&do_print_value);
  47       x->print_line();
  48       tty->cr();
  49     }
  50     assert(_canonical->type()->tag() == x->type()->tag(), "types must match");
  51     _canonical = x;
  52   }
  53 }
  54 
  55 
  56 void Canonicalizer::move_const_to_right(Op2* x) {
  57   if (x->x()->type()->is_constant() && x->is_commutative()) x->swap_operands();
  58 }
  59 
  60 
  61 void Canonicalizer::do_Op2(Op2* x) {
  62   if (x->x() == x->y()) {
  63     switch (x->op()) {
  64     case Bytecodes::_isub: set_constant(0); return;
  65     case Bytecodes::_lsub: set_constant(jlong_cast(0)); return;
  66     case Bytecodes::_iand: // fall through
  67     case Bytecodes::_land: // fall through
  68     case Bytecodes::_ior:  // fall through
  69     case Bytecodes::_lor : set_canonical(x->x()); return;
  70     case Bytecodes::_ixor: set_constant(0); return;
  71     case Bytecodes::_lxor: set_constant(jlong_cast(0)); return;
  72     }
  73   }
  74 
  75   if (x->x()->type()->is_constant() && x->y()->type()->is_constant()) {
  76     // do constant folding for selected operations
  77     switch (x->type()->tag()) {
  78       case intTag:
  79         { jint a = x->x()->type()->as_IntConstant()->value();
  80           jint b = x->y()->type()->as_IntConstant()->value();
  81           switch (x->op()) {
  82             case Bytecodes::_iadd: set_constant(a + b); return;
  83             case Bytecodes::_isub: set_constant(a - b); return;
  84             case Bytecodes::_imul: set_constant(a * b); return;
  85             case Bytecodes::_idiv:
  86               if (b != 0) {
  87                 if (a == min_jint && b == -1) {
  88                   set_constant(min_jint);
  89                 } else {
  90                   set_constant(a / b);
  91                 }
  92                 return;
  93               }
  94               break;
  95             case Bytecodes::_irem:
  96               if (b != 0) {
  97                 if (a == min_jint && b == -1) {
  98                   set_constant(0);
  99                 } else {
 100                   set_constant(a % b);
 101                 }
 102                 return;
 103               }
 104               break;
 105             case Bytecodes::_iand: set_constant(a & b); return;
 106             case Bytecodes::_ior : set_constant(a | b); return;
 107             case Bytecodes::_ixor: set_constant(a ^ b); return;
 108           }
 109         }
 110         break;
 111       case longTag:
 112         { jlong a = x->x()->type()->as_LongConstant()->value();
 113           jlong b = x->y()->type()->as_LongConstant()->value();
 114           switch (x->op()) {
 115             case Bytecodes::_ladd: set_constant(a + b); return;
 116             case Bytecodes::_lsub: set_constant(a - b); return;
 117             case Bytecodes::_lmul: set_constant(a * b); return;
 118             case Bytecodes::_ldiv:
 119               if (b != 0) {
 120                 set_constant(SharedRuntime::ldiv(b, a));
 121                 return;
 122               }
 123               break;
 124             case Bytecodes::_lrem:
 125               if (b != 0) {
 126                 set_constant(SharedRuntime::lrem(b, a));
 127                 return;
 128               }
 129               break;
 130             case Bytecodes::_land: set_constant(a & b); return;
 131             case Bytecodes::_lor : set_constant(a | b); return;
 132             case Bytecodes::_lxor: set_constant(a ^ b); return;
 133           }
 134         }
 135         break;
 136       // other cases not implemented (must be extremely careful with floats & doubles!)
 137     }
 138   }
 139   // make sure constant is on the right side, if any
 140   move_const_to_right(x);
 141 
 142   if (x->y()->type()->is_constant()) {
 143     // do constant folding for selected operations
 144     switch (x->type()->tag()) {
 145       case intTag:
 146         if (x->y()->type()->as_IntConstant()->value() == 0) {
 147           switch (x->op()) {
 148             case Bytecodes::_iadd: set_canonical(x->x()); return;
 149             case Bytecodes::_isub: set_canonical(x->x()); return;
 150             case Bytecodes::_imul: set_constant(0); return;
 151               // Note: for div and rem, make sure that C semantics
 152               //       corresponds to Java semantics!
 153             case Bytecodes::_iand: set_constant(0); return;
 154             case Bytecodes::_ior : set_canonical(x->x()); return;
 155           }
 156         }
 157         break;
 158       case longTag:
 159         if (x->y()->type()->as_LongConstant()->value() == (jlong)0) {
 160           switch (x->op()) {
 161             case Bytecodes::_ladd: set_canonical(x->x()); return;
 162             case Bytecodes::_lsub: set_canonical(x->x()); return;
 163             case Bytecodes::_lmul: set_constant((jlong)0); return;
 164               // Note: for div and rem, make sure that C semantics
 165               //       corresponds to Java semantics!
 166             case Bytecodes::_land: set_constant((jlong)0); return;
 167             case Bytecodes::_lor : set_canonical(x->x()); return;
 168           }
 169         }
 170         break;
 171     }
 172   }
 173 }
 174 
 175 
 176 void Canonicalizer::do_Phi            (Phi*             x) {}
 177 void Canonicalizer::do_Constant       (Constant*        x) {}
 178 void Canonicalizer::do_Local          (Local*           x) {}
 179 void Canonicalizer::do_LoadField      (LoadField*       x) {}
 180 
 181 // checks if v is in the block that is currently processed by
 182 // GraphBuilder. This is the only block that has not BlockEnd yet.
 183 static bool in_current_block(Value v) {
 184   int max_distance = 4;
 185   while (max_distance > 0 && v != NULL && v->as_BlockEnd() == NULL) {
 186     v = v->next();
 187     max_distance--;
 188   }
 189   return v == NULL;
 190 }
 191 
 192 void Canonicalizer::do_StoreField     (StoreField*      x) {
 193   // If a value is going to be stored into a field or array some of
 194   // the conversions emitted by javac are unneeded because the fields
 195   // are packed to their natural size.
 196   Convert* conv = x->value()->as_Convert();
 197   if (conv) {
 198     Value value = NULL;
 199     BasicType type = x->field()->type()->basic_type();
 200     switch (conv->op()) {
 201     case Bytecodes::_i2b: if (type == T_BYTE)  value = conv->value(); break;
 202     case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) value = conv->value(); break;
 203     case Bytecodes::_i2c: if (type == T_CHAR  || type == T_BYTE)  value = conv->value(); break;
 204     }
 205     // limit this optimization to current block
 206     if (value != NULL && in_current_block(conv)) {
 207       set_canonical(new StoreField(x->obj(), x->offset(), x->field(), value, x->is_static(),
 208                                        x->state_before(), x->is_loaded(), x->is_initialized()));
 209       return;
 210     }
 211   }
 212 
 213 }
 214 
 215 void Canonicalizer::do_ArrayLength    (ArrayLength*     x) {
 216   NewArray* array = x->array()->as_NewArray();
 217   if (array != NULL && array->length() != NULL) {
 218     Constant* length = array->length()->as_Constant();
 219     if (length != NULL) {
 220       // do not use the Constant itself, but create a new Constant
 221       // with same value Otherwise a Constant is live over multiple
 222       // blocks without being registered in a state array.
 223       assert(length->type()->as_IntConstant() != NULL, "array length must be integer");
 224       set_constant(length->type()->as_IntConstant()->value());
 225     }
 226   } else {
 227     LoadField* lf = x->array()->as_LoadField();
 228     if (lf != NULL) {
 229       ciField* field = lf->field();
 230       if (field->is_constant() && field->is_static()) {
 231         // final static field
 232         ciObject* c = field->constant_value().as_object();
 233         if (c->is_array()) {
 234           ciArray* array = (ciArray*) c;
 235           set_constant(array->length());
 236         }
 237       }
 238     }
 239   }
 240 }
 241 
 242 void Canonicalizer::do_LoadIndexed    (LoadIndexed*     x) {}
 243 void Canonicalizer::do_StoreIndexed   (StoreIndexed*    x) {
 244   // If a value is going to be stored into a field or array some of
 245   // the conversions emitted by javac are unneeded because the fields
 246   // are packed to their natural size.
 247   Convert* conv = x->value()->as_Convert();
 248   if (conv) {
 249     Value value = NULL;
 250     BasicType type = x->elt_type();
 251     switch (conv->op()) {
 252     case Bytecodes::_i2b: if (type == T_BYTE)  value = conv->value(); break;
 253     case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) value = conv->value(); break;
 254     case Bytecodes::_i2c: if (type == T_CHAR  || type == T_BYTE) value = conv->value(); break;
 255     }
 256     // limit this optimization to current block
 257     if (value != NULL && in_current_block(conv)) {
 258       set_canonical(new StoreIndexed(x->array(), x->index(), x->length(),
 259                                      x->elt_type(), value, x->state_before()));
 260       return;
 261     }
 262   }
 263 
 264 
 265 }
 266 
 267 
 268 void Canonicalizer::do_NegateOp(NegateOp* x) {
 269   ValueType* t = x->x()->type();
 270   if (t->is_constant()) {
 271     switch (t->tag()) {
 272       case intTag   : set_constant(-t->as_IntConstant   ()->value()); return;
 273       case longTag  : set_constant(-t->as_LongConstant  ()->value()); return;
 274       case floatTag : set_constant(-t->as_FloatConstant ()->value()); return;
 275       case doubleTag: set_constant(-t->as_DoubleConstant()->value()); return;
 276       default       : ShouldNotReachHere();
 277     }
 278   }
 279 }
 280 
 281 
 282 void Canonicalizer::do_ArithmeticOp   (ArithmeticOp*    x) { do_Op2(x); }
 283 
 284 
 285 void Canonicalizer::do_ShiftOp        (ShiftOp*         x) {
 286   ValueType* t = x->x()->type();
 287   ValueType* t2 = x->y()->type();
 288   if (t->is_constant()) {
 289     switch (t->tag()) {
 290     case intTag   : if (t->as_IntConstant()->value() == 0)         { set_constant(0); return; } break;
 291     case longTag  : if (t->as_LongConstant()->value() == (jlong)0) { set_constant(jlong_cast(0)); return; } break;
 292     default       : ShouldNotReachHere();
 293     }
 294     if (t2->is_constant()) {
 295       if (t->tag() == intTag) {
 296         int value = t->as_IntConstant()->value();
 297         int shift = t2->as_IntConstant()->value() & 31;
 298         jint mask = ~(~0 << (32 - shift));
 299         if (shift == 0) mask = ~0;
 300         switch (x->op()) {
 301           case Bytecodes::_ishl:  set_constant(value << shift); return;
 302           case Bytecodes::_ishr:  set_constant(value >> shift); return;
 303           case Bytecodes::_iushr: set_constant((value >> shift) & mask); return;
 304         }
 305       } else if (t->tag() == longTag) {
 306         jlong value = t->as_LongConstant()->value();
 307         int shift = t2->as_IntConstant()->value() & 63;
 308         jlong mask = ~(~jlong_cast(0) << (64 - shift));
 309         if (shift == 0) mask = ~jlong_cast(0);
 310         switch (x->op()) {
 311           case Bytecodes::_lshl:  set_constant(value << shift); return;
 312           case Bytecodes::_lshr:  set_constant(value >> shift); return;
 313           case Bytecodes::_lushr: set_constant((value >> shift) & mask); return;
 314         }
 315       }
 316     }
 317   }
 318   if (t2->is_constant()) {
 319     switch (t2->tag()) {
 320       case intTag   : if (t2->as_IntConstant()->value() == 0)  set_canonical(x->x()); return;
 321       case longTag  : if (t2->as_IntConstant()->value() == 0)  set_canonical(x->x()); return;
 322       default       : ShouldNotReachHere();
 323     }
 324   }
 325 }
 326 
 327 
 328 void Canonicalizer::do_LogicOp        (LogicOp*         x) { do_Op2(x); }
 329 void Canonicalizer::do_CompareOp      (CompareOp*       x) {
 330   if (x->x() == x->y()) {
 331     switch (x->x()->type()->tag()) {
 332       case longTag: set_constant(0); break;
 333       case floatTag: {
 334         FloatConstant* fc = x->x()->type()->as_FloatConstant();
 335         if (fc) {
 336           if (g_isnan(fc->value())) {
 337             set_constant(x->op() == Bytecodes::_fcmpl ? -1 : 1);
 338           } else {
 339             set_constant(0);
 340           }
 341         }
 342         break;
 343       }
 344       case doubleTag: {
 345         DoubleConstant* dc = x->x()->type()->as_DoubleConstant();
 346         if (dc) {
 347           if (g_isnan(dc->value())) {
 348             set_constant(x->op() == Bytecodes::_dcmpl ? -1 : 1);
 349           } else {
 350             set_constant(0);
 351           }
 352         }
 353         break;
 354       }
 355     }
 356   } else if (x->x()->type()->is_constant() && x->y()->type()->is_constant()) {
 357     switch (x->x()->type()->tag()) {
 358       case longTag: {
 359         jlong vx = x->x()->type()->as_LongConstant()->value();
 360         jlong vy = x->y()->type()->as_LongConstant()->value();
 361         if (vx == vy)
 362           set_constant(0);
 363         else if (vx < vy)
 364           set_constant(-1);
 365         else
 366           set_constant(1);
 367         break;
 368       }
 369 
 370       case floatTag: {
 371         float vx = x->x()->type()->as_FloatConstant()->value();
 372         float vy = x->y()->type()->as_FloatConstant()->value();
 373         if (g_isnan(vx) || g_isnan(vy))
 374           set_constant(x->op() == Bytecodes::_fcmpl ? -1 : 1);
 375         else if (vx == vy)
 376           set_constant(0);
 377         else if (vx < vy)
 378           set_constant(-1);
 379         else
 380           set_constant(1);
 381         break;
 382       }
 383 
 384       case doubleTag: {
 385         double vx = x->x()->type()->as_DoubleConstant()->value();
 386         double vy = x->y()->type()->as_DoubleConstant()->value();
 387         if (g_isnan(vx) || g_isnan(vy))
 388           set_constant(x->op() == Bytecodes::_dcmpl ? -1 : 1);
 389         else if (vx == vy)
 390           set_constant(0);
 391         else if (vx < vy)
 392           set_constant(-1);
 393         else
 394           set_constant(1);
 395         break;
 396       }
 397     }
 398 
 399   }
 400 }
 401 
 402 
 403 void Canonicalizer::do_IfInstanceOf(IfInstanceOf*    x) {}
 404 
 405 void Canonicalizer::do_IfOp(IfOp* x) {
 406   // Caution: do not use do_Op2(x) here for now since
 407   //          we map the condition to the op for now!
 408   move_const_to_right(x);
 409 }
 410 
 411 
 412 void Canonicalizer::do_Intrinsic      (Intrinsic*       x) {
 413   switch (x->id()) {
 414   case vmIntrinsics::_floatToRawIntBits   : {
 415     FloatConstant* c = x->argument_at(0)->type()->as_FloatConstant();
 416     if (c != NULL) {
 417       JavaValue v;
 418       v.set_jfloat(c->value());
 419       set_constant(v.get_jint());
 420     }
 421     break;
 422   }
 423   case vmIntrinsics::_intBitsToFloat      : {
 424     IntConstant* c = x->argument_at(0)->type()->as_IntConstant();
 425     if (c != NULL) {
 426       JavaValue v;
 427       v.set_jint(c->value());
 428       set_constant(v.get_jfloat());
 429     }
 430     break;
 431   }
 432   case vmIntrinsics::_doubleToRawLongBits : {
 433     DoubleConstant* c = x->argument_at(0)->type()->as_DoubleConstant();
 434     if (c != NULL) {
 435       JavaValue v;
 436       v.set_jdouble(c->value());
 437       set_constant(v.get_jlong());
 438     }
 439     break;
 440   }
 441   case vmIntrinsics::_longBitsToDouble    : {
 442     LongConstant* c = x->argument_at(0)->type()->as_LongConstant();
 443     if (c != NULL) {
 444       JavaValue v;
 445       v.set_jlong(c->value());
 446       set_constant(v.get_jdouble());
 447     }
 448     break;
 449   }
 450   }
 451 }
 452 
 453 void Canonicalizer::do_Convert        (Convert*         x) {
 454   if (x->value()->type()->is_constant()) {
 455     switch (x->op()) {
 456     case Bytecodes::_i2b:  set_constant((int)((x->value()->type()->as_IntConstant()->value() << 24) >> 24)); break;
 457     case Bytecodes::_i2s:  set_constant((int)((x->value()->type()->as_IntConstant()->value() << 16) >> 16)); break;
 458     case Bytecodes::_i2c:  set_constant((int)(x->value()->type()->as_IntConstant()->value() & ((1<<16)-1))); break;
 459     case Bytecodes::_i2l:  set_constant((jlong)(x->value()->type()->as_IntConstant()->value()));             break;
 460     case Bytecodes::_i2f:  set_constant((float)(x->value()->type()->as_IntConstant()->value()));             break;
 461     case Bytecodes::_i2d:  set_constant((double)(x->value()->type()->as_IntConstant()->value()));            break;
 462     case Bytecodes::_l2i:  set_constant((int)(x->value()->type()->as_LongConstant()->value()));              break;
 463     case Bytecodes::_l2f:  set_constant(SharedRuntime::l2f(x->value()->type()->as_LongConstant()->value())); break;
 464     case Bytecodes::_l2d:  set_constant(SharedRuntime::l2d(x->value()->type()->as_LongConstant()->value())); break;
 465     case Bytecodes::_f2d:  set_constant((double)(x->value()->type()->as_FloatConstant()->value()));          break;
 466     case Bytecodes::_f2i:  set_constant(SharedRuntime::f2i(x->value()->type()->as_FloatConstant()->value())); break;
 467     case Bytecodes::_f2l:  set_constant(SharedRuntime::f2l(x->value()->type()->as_FloatConstant()->value())); break;
 468     case Bytecodes::_d2f:  set_constant((float)(x->value()->type()->as_DoubleConstant()->value()));          break;
 469     case Bytecodes::_d2i:  set_constant(SharedRuntime::d2i(x->value()->type()->as_DoubleConstant()->value())); break;
 470     case Bytecodes::_d2l:  set_constant(SharedRuntime::d2l(x->value()->type()->as_DoubleConstant()->value())); break;
 471     default:
 472       ShouldNotReachHere();
 473     }
 474   }
 475 
 476   Value value = x->value();
 477   BasicType type = T_ILLEGAL;
 478   LoadField* lf = value->as_LoadField();
 479   if (lf) {
 480     type = lf->field_type();
 481   } else {
 482     LoadIndexed* li = value->as_LoadIndexed();
 483     if (li) {
 484       type = li->elt_type();
 485     } else {
 486       Convert* conv = value->as_Convert();
 487       if (conv) {
 488         switch (conv->op()) {
 489           case Bytecodes::_i2b: type = T_BYTE;  break;
 490           case Bytecodes::_i2s: type = T_SHORT; break;
 491           case Bytecodes::_i2c: type = T_CHAR;  break;
 492         }
 493       }
 494     }
 495   }
 496   if (type != T_ILLEGAL) {
 497     switch (x->op()) {
 498       case Bytecodes::_i2b: if (type == T_BYTE)                    set_canonical(x->value()); break;
 499       case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) set_canonical(x->value()); break;
 500       case Bytecodes::_i2c: if (type == T_CHAR)                    set_canonical(x->value()); break;
 501     }
 502   } else {
 503     Op2* op2 = x->value()->as_Op2();
 504     if (op2 && op2->op() == Bytecodes::_iand && op2->y()->type()->is_constant()) {
 505       jint safebits = 0;
 506       jint mask = op2->y()->type()->as_IntConstant()->value();
 507       switch (x->op()) {
 508         case Bytecodes::_i2b: safebits = 0x7f;   break;
 509         case Bytecodes::_i2s: safebits = 0x7fff; break;
 510         case Bytecodes::_i2c: safebits = 0xffff; break;
 511       }
 512       // When casting a masked integer to a smaller signed type, if
 513       // the mask doesn't include the sign bit the cast isn't needed.
 514       if (safebits && (mask & ~safebits) == 0) {
 515         set_canonical(x->value());
 516       }
 517     }
 518   }
 519 
 520 }
 521 
 522 void Canonicalizer::do_NullCheck      (NullCheck*       x) {
 523   if (x->obj()->as_NewArray() != NULL || x->obj()->as_NewInstance() != NULL) {
 524     set_canonical(x->obj());
 525   } else {
 526     Constant* con = x->obj()->as_Constant();
 527     if (con) {
 528       ObjectType* c = con->type()->as_ObjectType();
 529       if (c && c->is_loaded()) {
 530         ObjectConstant* oc = c->as_ObjectConstant();
 531         if (!oc || !oc->value()->is_null_object()) {
 532           set_canonical(con);
 533         }
 534       }
 535     }
 536   }
 537 }
 538 
 539 void Canonicalizer::do_Invoke         (Invoke*          x) {}
 540 void Canonicalizer::do_NewInstance    (NewInstance*     x) {}
 541 void Canonicalizer::do_NewTypeArray   (NewTypeArray*    x) {}
 542 void Canonicalizer::do_NewObjectArray (NewObjectArray*  x) {}
 543 void Canonicalizer::do_NewMultiArray  (NewMultiArray*   x) {}
 544 void Canonicalizer::do_CheckCast      (CheckCast*       x) {
 545   if (x->klass()->is_loaded()) {
 546     Value obj = x->obj();
 547     ciType* klass = obj->exact_type();
 548     if (klass == NULL) klass = obj->declared_type();
 549     if (klass != NULL && klass->is_loaded() && klass->is_subtype_of(x->klass())) {
 550       set_canonical(obj);
 551       return;
 552     }
 553     // checkcast of null returns null
 554     if (obj->as_Constant() && obj->type()->as_ObjectType()->constant_value()->is_null_object()) {
 555       set_canonical(obj);
 556     }
 557   }
 558 }
 559 void Canonicalizer::do_InstanceOf     (InstanceOf*      x) {
 560   if (x->klass()->is_loaded()) {
 561     Value obj = x->obj();
 562     ciType* exact = obj->exact_type();
 563     if (exact != NULL && exact->is_loaded() && (obj->as_NewInstance() || obj->as_NewArray())) {
 564       set_constant(exact->is_subtype_of(x->klass()) ? 1 : 0);
 565       return;
 566     }
 567     // instanceof null returns false
 568     if (obj->as_Constant() && obj->type()->as_ObjectType()->constant_value()->is_null_object()) {
 569       set_constant(0);
 570     }
 571   }
 572 
 573 }
 574 void Canonicalizer::do_MonitorEnter   (MonitorEnter*    x) {}
 575 void Canonicalizer::do_MonitorExit    (MonitorExit*     x) {}
 576 void Canonicalizer::do_BlockBegin     (BlockBegin*      x) {}
 577 void Canonicalizer::do_Goto           (Goto*            x) {}
 578 
 579 
 580 static bool is_true(jlong x, If::Condition cond, jlong y) {
 581   switch (cond) {
 582     case If::eql: return x == y;
 583     case If::neq: return x != y;
 584     case If::lss: return x <  y;
 585     case If::leq: return x <= y;
 586     case If::gtr: return x >  y;
 587     case If::geq: return x >= y;
 588   }
 589   ShouldNotReachHere();
 590   return false;
 591 }
 592 
 593 
 594 void Canonicalizer::do_If(If* x) {
 595   // move const to right
 596   if (x->x()->type()->is_constant()) x->swap_operands();
 597   // simplify
 598   const Value l = x->x(); ValueType* lt = l->type();
 599   const Value r = x->y(); ValueType* rt = r->type();
 600 
 601   if (l == r && !lt->is_float_kind()) {
 602     // pattern: If (a cond a) => simplify to Goto
 603     BlockBegin* sux;
 604     switch (x->cond()) {
 605     case If::eql: sux = x->sux_for(true);  break;
 606     case If::neq: sux = x->sux_for(false); break;
 607     case If::lss: sux = x->sux_for(false); break;
 608     case If::leq: sux = x->sux_for(true);  break;
 609     case If::gtr: sux = x->sux_for(false); break;
 610     case If::geq: sux = x->sux_for(true);  break;
 611     }
 612     // If is a safepoint then the debug information should come from the state_before of the If.
 613     set_canonical(new Goto(sux, x->state_before(), x->is_safepoint()));
 614     return;
 615   }
 616 
 617   if (lt->is_constant() && rt->is_constant()) {
 618     if (x->x()->as_Constant() != NULL) {
 619       // pattern: If (lc cond rc) => simplify to: Goto
 620       BlockBegin* sux = x->x()->as_Constant()->compare(x->cond(), x->y(),
 621                                                        x->sux_for(true),
 622                                                        x->sux_for(false));
 623       if (sux != NULL) {
 624         // If is a safepoint then the debug information should come from the state_before of the If.
 625         set_canonical(new Goto(sux, x->state_before(), x->is_safepoint()));
 626       }
 627     }
 628   } else if (rt->as_IntConstant() != NULL) {
 629     // pattern: If (l cond rc) => investigate further
 630     const jint rc = rt->as_IntConstant()->value();
 631     if (l->as_CompareOp() != NULL) {
 632       // pattern: If ((a cmp b) cond rc) => simplify to: If (x cond y) or: Goto
 633       CompareOp* cmp = l->as_CompareOp();
 634       bool unordered_is_less = cmp->op() == Bytecodes::_fcmpl || cmp->op() == Bytecodes::_dcmpl;
 635       BlockBegin* lss_sux = x->sux_for(is_true(-1, x->cond(), rc)); // successor for a < b
 636       BlockBegin* eql_sux = x->sux_for(is_true( 0, x->cond(), rc)); // successor for a = b
 637       BlockBegin* gtr_sux = x->sux_for(is_true(+1, x->cond(), rc)); // successor for a > b
 638       BlockBegin* nan_sux = unordered_is_less ? lss_sux : gtr_sux ; // successor for unordered
 639       // Note: At this point all successors (lss_sux, eql_sux, gtr_sux, nan_sux) are
 640       //       equal to x->tsux() or x->fsux(). Furthermore, nan_sux equals either
 641       //       lss_sux or gtr_sux.
 642       if (lss_sux == eql_sux && eql_sux == gtr_sux) {
 643         // all successors identical => simplify to: Goto
 644         set_canonical(new Goto(lss_sux, x->state_before(), x->is_safepoint()));
 645       } else {
 646         // two successors differ and two successors are the same => simplify to: If (x cmp y)
 647         // determine new condition & successors
 648         If::Condition cond;
 649         BlockBegin* tsux = NULL;
 650         BlockBegin* fsux = NULL;
 651              if (lss_sux == eql_sux) { cond = If::leq; tsux = lss_sux; fsux = gtr_sux; }
 652         else if (lss_sux == gtr_sux) { cond = If::neq; tsux = lss_sux; fsux = eql_sux; }
 653         else if (eql_sux == gtr_sux) { cond = If::geq; tsux = eql_sux; fsux = lss_sux; }
 654         else                         { ShouldNotReachHere();                           }
 655         If* canon = new If(cmp->x(), cond, nan_sux == tsux, cmp->y(), tsux, fsux, cmp->state_before(), x->is_safepoint());
 656         if (cmp->x() == cmp->y()) {
 657           do_If(canon);
 658         } else {
 659           if (compilation()->profile_branches()) {
 660             // TODO: If profiling, leave floating point comparisons unoptimized.
 661             // We currently do not support profiling of the unordered case.
 662             switch(cmp->op()) {
 663               case Bytecodes::_fcmpl: case Bytecodes::_fcmpg:
 664               case Bytecodes::_dcmpl: case Bytecodes::_dcmpg:
 665                 set_canonical(x);
 666                 return;
 667             }
 668           }
 669           set_canonical(canon);
 670           set_bci(cmp->state_before()->bci());
 671         }
 672       }
 673     } else if (l->as_InstanceOf() != NULL) {
 674       // NOTE: Code permanently disabled for now since it leaves the old InstanceOf
 675       //       instruction in the graph (it is pinned). Need to fix this at some point.
 676       //       It should also be left in the graph when generating a profiled method version or Goto
 677       //       has to know that it was an InstanceOf.
 678       return;
 679       // pattern: If ((obj instanceof klass) cond rc) => simplify to: IfInstanceOf or: Goto
 680       InstanceOf* inst = l->as_InstanceOf();
 681       BlockBegin* is_inst_sux = x->sux_for(is_true(1, x->cond(), rc)); // successor for instanceof == 1
 682       BlockBegin* no_inst_sux = x->sux_for(is_true(0, x->cond(), rc)); // successor for instanceof == 0
 683       if (is_inst_sux == no_inst_sux && inst->is_loaded()) {
 684         // both successors identical and klass is loaded => simplify to: Goto
 685         set_canonical(new Goto(is_inst_sux, x->state_before(), x->is_safepoint()));
 686       } else {
 687         // successors differ => simplify to: IfInstanceOf
 688         set_canonical(new IfInstanceOf(inst->klass(), inst->obj(), true, inst->state_before()->bci(), is_inst_sux, no_inst_sux));
 689       }
 690     }
 691   } else if (rt == objectNull && (l->as_NewInstance() || l->as_NewArray())) {
 692     if (x->cond() == Instruction::eql) {
 693       set_canonical(new Goto(x->fsux(), x->state_before(), x->is_safepoint()));
 694     } else {
 695       assert(x->cond() == Instruction::neq, "only other valid case");
 696       set_canonical(new Goto(x->tsux(), x->state_before(), x->is_safepoint()));
 697     }
 698   }
 699 }
 700 
 701 
 702 void Canonicalizer::do_TableSwitch(TableSwitch* x) {
 703   if (x->tag()->type()->is_constant()) {
 704     int v = x->tag()->type()->as_IntConstant()->value();
 705     BlockBegin* sux = x->default_sux();
 706     if (v >= x->lo_key() && v <= x->hi_key()) {
 707       sux = x->sux_at(v - x->lo_key());
 708     }
 709     set_canonical(new Goto(sux, x->state_before(), x->is_safepoint()));
 710   } else if (x->number_of_sux() == 1) {
 711     // NOTE: Code permanently disabled for now since the switch statement's
 712     //       tag expression may produce side-effects in which case it must
 713     //       be executed.
 714     return;
 715     // simplify to Goto
 716     set_canonical(new Goto(x->default_sux(), x->state_before(), x->is_safepoint()));
 717   } else if (x->number_of_sux() == 2) {
 718     // NOTE: Code permanently disabled for now since it produces two new nodes
 719     //       (Constant & If) and the Canonicalizer cannot return them correctly
 720     //       yet. For now we copied the corresponding code directly into the
 721     //       GraphBuilder (i.e., we should never reach here).
 722     return;
 723     // simplify to If
 724     assert(x->lo_key() == x->hi_key(), "keys must be the same");
 725     Constant* key = new Constant(new IntConstant(x->lo_key()));
 726     set_canonical(new If(x->tag(), If::eql, true, key, x->sux_at(0), x->default_sux(), x->state_before(), x->is_safepoint()));
 727   }
 728 }
 729 
 730 
 731 void Canonicalizer::do_LookupSwitch(LookupSwitch* x) {
 732   if (x->tag()->type()->is_constant()) {
 733     int v = x->tag()->type()->as_IntConstant()->value();
 734     BlockBegin* sux = x->default_sux();
 735     for (int i = 0; i < x->length(); i++) {
 736       if (v == x->key_at(i)) {
 737         sux = x->sux_at(i);
 738       }
 739     }
 740     set_canonical(new Goto(sux, x->state_before(), x->is_safepoint()));
 741   } else if (x->number_of_sux() == 1) {
 742     // NOTE: Code permanently disabled for now since the switch statement's
 743     //       tag expression may produce side-effects in which case it must
 744     //       be executed.
 745     return;
 746     // simplify to Goto
 747     set_canonical(new Goto(x->default_sux(), x->state_before(), x->is_safepoint()));
 748   } else if (x->number_of_sux() == 2) {
 749     // NOTE: Code permanently disabled for now since it produces two new nodes
 750     //       (Constant & If) and the Canonicalizer cannot return them correctly
 751     //       yet. For now we copied the corresponding code directly into the
 752     //       GraphBuilder (i.e., we should never reach here).
 753     return;
 754     // simplify to If
 755     assert(x->length() == 1, "length must be the same");
 756     Constant* key = new Constant(new IntConstant(x->key_at(0)));
 757     set_canonical(new If(x->tag(), If::eql, true, key, x->sux_at(0), x->default_sux(), x->state_before(), x->is_safepoint()));
 758   }
 759 }
 760 
 761 
 762 void Canonicalizer::do_Return         (Return*          x) {}
 763 void Canonicalizer::do_Throw          (Throw*           x) {}
 764 void Canonicalizer::do_Base           (Base*            x) {}
 765 void Canonicalizer::do_OsrEntry       (OsrEntry*        x) {}
 766 void Canonicalizer::do_ExceptionObject(ExceptionObject* x) {}
 767 
 768 static bool match_index_and_scale(Instruction*  instr,
 769                                   Instruction** index,
 770                                   int*          log2_scale,
 771                                   Instruction** instr_to_unpin) {
 772   *instr_to_unpin = NULL;
 773 
 774   // Skip conversion ops
 775   Convert* convert = instr->as_Convert();
 776   if (convert != NULL) {
 777     instr = convert->value();
 778   }
 779 
 780   ShiftOp* shift = instr->as_ShiftOp();
 781   if (shift != NULL) {
 782     if (shift->is_pinned()) {
 783       *instr_to_unpin = shift;
 784     }
 785     // Constant shift value?
 786     Constant* con = shift->y()->as_Constant();
 787     if (con == NULL) return false;
 788     // Well-known type and value?
 789     IntConstant* val = con->type()->as_IntConstant();
 790     if (val == NULL) return false;
 791     if (shift->x()->type() != intType) return false;
 792     *index = shift->x();
 793     int tmp_scale = val->value();
 794     if (tmp_scale >= 0 && tmp_scale < 4) {
 795       *log2_scale = tmp_scale;
 796       return true;
 797     } else {
 798       return false;
 799     }
 800   }
 801 
 802   ArithmeticOp* arith = instr->as_ArithmeticOp();
 803   if (arith != NULL) {
 804     if (arith->is_pinned()) {
 805       *instr_to_unpin = arith;
 806     }
 807     // Check for integer multiply
 808     if (arith->op() == Bytecodes::_imul) {
 809       // See if either arg is a known constant
 810       Constant* con = arith->x()->as_Constant();
 811       if (con != NULL) {
 812         *index = arith->y();
 813       } else {
 814         con = arith->y()->as_Constant();
 815         if (con == NULL) return false;
 816         *index = arith->x();
 817       }
 818       if ((*index)->type() != intType) return false;
 819       // Well-known type and value?
 820       IntConstant* val = con->type()->as_IntConstant();
 821       if (val == NULL) return false;
 822       switch (val->value()) {
 823       case 1: *log2_scale = 0; return true;
 824       case 2: *log2_scale = 1; return true;
 825       case 4: *log2_scale = 2; return true;
 826       case 8: *log2_scale = 3; return true;
 827       default:            return false;
 828       }
 829     }
 830   }
 831 
 832   // Unknown instruction sequence; don't touch it
 833   return false;
 834 }
 835 
 836 
 837 static bool match(UnsafeRawOp* x,
 838                   Instruction** base,
 839                   Instruction** index,
 840                   int*          log2_scale) {
 841   Instruction* instr_to_unpin = NULL;
 842   ArithmeticOp* root = x->base()->as_ArithmeticOp();
 843   if (root == NULL) return false;
 844   // Limit ourselves to addition for now
 845   if (root->op() != Bytecodes::_ladd) return false;
 846   // Try to find shift or scale op
 847   if (match_index_and_scale(root->y(), index, log2_scale, &instr_to_unpin)) {
 848     *base = root->x();
 849   } else if (match_index_and_scale(root->x(), index, log2_scale, &instr_to_unpin)) {
 850     *base = root->y();
 851   } else if (root->y()->as_Convert() != NULL) {
 852     Convert* convert = root->y()->as_Convert();
 853     if (convert->op() == Bytecodes::_i2l && convert->value()->type() == intType) {
 854       // pick base and index, setting scale at 1
 855       *base  = root->x();
 856       *index = convert->value();
 857       *log2_scale = 0;
 858     } else {
 859       return false;
 860     }
 861   } else {
 862     // doesn't match any expected sequences
 863     return false;
 864   }
 865 
 866   // If the value is pinned then it will be always be computed so
 867   // there's no profit to reshaping the expression.
 868   return !root->is_pinned();
 869 }
 870 
 871 
 872 void Canonicalizer::do_UnsafeRawOp(UnsafeRawOp* x) {
 873   Instruction* base = NULL;
 874   Instruction* index = NULL;
 875   int          log2_scale;
 876 
 877   if (match(x, &base, &index, &log2_scale)) {
 878     x->set_base(base);
 879     x->set_index(index);
 880     x->set_log2_scale(log2_scale);
 881     if (PrintUnsafeOptimization) {
 882       tty->print_cr("Canonicalizer: UnsafeRawOp id %d: base = id %d, index = id %d, log2_scale = %d",
 883                     x->id(), x->base()->id(), x->index()->id(), x->log2_scale());
 884     }
 885   }
 886 }
 887 
 888 void Canonicalizer::do_RoundFP(RoundFP* x) {}
 889 void Canonicalizer::do_UnsafeGetRaw(UnsafeGetRaw* x) { if (OptimizeUnsafes) do_UnsafeRawOp(x); }
 890 void Canonicalizer::do_UnsafePutRaw(UnsafePutRaw* x) { if (OptimizeUnsafes) do_UnsafeRawOp(x); }
 891 void Canonicalizer::do_UnsafeGetObject(UnsafeGetObject* x) {}
 892 void Canonicalizer::do_UnsafePutObject(UnsafePutObject* x) {}
 893 void Canonicalizer::do_UnsafePrefetchRead (UnsafePrefetchRead*  x) {}
 894 void Canonicalizer::do_UnsafePrefetchWrite(UnsafePrefetchWrite* x) {}
 895 void Canonicalizer::do_ProfileCall(ProfileCall* x) {}
 896 void Canonicalizer::do_ProfileInvoke(ProfileInvoke* x) {}
 897