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