src/share/vm/c1/c1_Canonicalizer.cpp

Wed, 30 May 2012 12:17:07 -0700

author
twisti
date
Wed, 30 May 2012 12:17:07 -0700
changeset 3836
c8289830e172
parent 3592
701a83c86f28
child 3838
8f37087fc13f
permissions
-rw-r--r--

7172843: C1: fix "assert(has_printable_bci()) failed: _printable_bci should have been set"
Reviewed-by: twisti
Contributed-by: Krystal Mok <sajia@taobao.com>

     1 /*
     2  * Copyright (c) 1999, 2011, 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  */
    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"
    33 class PrintValueVisitor: public ValueVisitor {
    34   void visit(Value* vp) {
    35     (*vp)->print_line();
    36   }
    37 };
    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 }
    65 void Canonicalizer::move_const_to_right(Op2* x) {
    66   if (x->x()->type()->is_constant() && x->is_commutative()) x->swap_operands();
    67 }
    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   }
    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);
   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 }
   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) {}
   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 }
   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   }
   222 }
   224 void Canonicalizer::do_ArrayLength    (ArrayLength*     x) {
   225   NewArray* array = x->array()->as_NewArray();
   226   if (array != NULL && array->length() != NULL) {
   227     Constant* length = array->length()->as_Constant();
   228     if (length != NULL) {
   229       // do not use the Constant itself, but create a new Constant
   230       // with same value Otherwise a Constant is live over multiple
   231       // blocks without being registered in a state array.
   232       assert(length->type()->as_IntConstant() != NULL, "array length must be integer");
   233       set_constant(length->type()->as_IntConstant()->value());
   234     }
   235   } else {
   236     LoadField* lf = x->array()->as_LoadField();
   237     if (lf != NULL) {
   238       ciField* field = lf->field();
   239       if (field->is_constant() && field->is_static()) {
   240         // final static field
   241         ciObject* c = field->constant_value().as_object();
   242         if (c->is_array()) {
   243           ciArray* array = (ciArray*) c;
   244           set_constant(array->length());
   245         }
   246       }
   247     }
   248   }
   249 }
   251 void Canonicalizer::do_LoadIndexed    (LoadIndexed*     x) {}
   252 void Canonicalizer::do_StoreIndexed   (StoreIndexed*    x) {
   253   // If a value is going to be stored into a field or array some of
   254   // the conversions emitted by javac are unneeded because the fields
   255   // are packed to their natural size.
   256   Convert* conv = x->value()->as_Convert();
   257   if (conv) {
   258     Value value = NULL;
   259     BasicType type = x->elt_type();
   260     switch (conv->op()) {
   261     case Bytecodes::_i2b: if (type == T_BYTE)  value = conv->value(); break;
   262     case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) value = conv->value(); break;
   263     case Bytecodes::_i2c: if (type == T_CHAR  || type == T_BYTE) value = conv->value(); break;
   264     }
   265     // limit this optimization to current block
   266     if (value != NULL && in_current_block(conv)) {
   267       set_canonical(new StoreIndexed(x->array(), x->index(), x->length(),
   268                                      x->elt_type(), value, x->state_before()));
   269       return;
   270     }
   271   }
   274 }
   277 void Canonicalizer::do_NegateOp(NegateOp* x) {
   278   ValueType* t = x->x()->type();
   279   if (t->is_constant()) {
   280     switch (t->tag()) {
   281       case intTag   : set_constant(-t->as_IntConstant   ()->value()); return;
   282       case longTag  : set_constant(-t->as_LongConstant  ()->value()); return;
   283       case floatTag : set_constant(-t->as_FloatConstant ()->value()); return;
   284       case doubleTag: set_constant(-t->as_DoubleConstant()->value()); return;
   285       default       : ShouldNotReachHere();
   286     }
   287   }
   288 }
   291 void Canonicalizer::do_ArithmeticOp   (ArithmeticOp*    x) { do_Op2(x); }
   294 void Canonicalizer::do_ShiftOp        (ShiftOp*         x) {
   295   ValueType* t = x->x()->type();
   296   ValueType* t2 = x->y()->type();
   297   if (t->is_constant()) {
   298     switch (t->tag()) {
   299     case intTag   : if (t->as_IntConstant()->value() == 0)         { set_constant(0); return; } break;
   300     case longTag  : if (t->as_LongConstant()->value() == (jlong)0) { set_constant(jlong_cast(0)); return; } break;
   301     default       : ShouldNotReachHere();
   302     }
   303     if (t2->is_constant()) {
   304       if (t->tag() == intTag) {
   305         int value = t->as_IntConstant()->value();
   306         int shift = t2->as_IntConstant()->value() & 31;
   307         jint mask = ~(~0 << (32 - shift));
   308         if (shift == 0) mask = ~0;
   309         switch (x->op()) {
   310           case Bytecodes::_ishl:  set_constant(value << shift); return;
   311           case Bytecodes::_ishr:  set_constant(value >> shift); return;
   312           case Bytecodes::_iushr: set_constant((value >> shift) & mask); return;
   313         }
   314       } else if (t->tag() == longTag) {
   315         jlong value = t->as_LongConstant()->value();
   316         int shift = t2->as_IntConstant()->value() & 63;
   317         jlong mask = ~(~jlong_cast(0) << (64 - shift));
   318         if (shift == 0) mask = ~jlong_cast(0);
   319         switch (x->op()) {
   320           case Bytecodes::_lshl:  set_constant(value << shift); return;
   321           case Bytecodes::_lshr:  set_constant(value >> shift); return;
   322           case Bytecodes::_lushr: set_constant((value >> shift) & mask); return;
   323         }
   324       }
   325     }
   326   }
   327   if (t2->is_constant()) {
   328     switch (t2->tag()) {
   329       case intTag   : if (t2->as_IntConstant()->value() == 0)  set_canonical(x->x()); return;
   330       case longTag  : if (t2->as_IntConstant()->value() == 0)  set_canonical(x->x()); return;
   331       default       : ShouldNotReachHere();
   332     }
   333   }
   334 }
   337 void Canonicalizer::do_LogicOp        (LogicOp*         x) { do_Op2(x); }
   338 void Canonicalizer::do_CompareOp      (CompareOp*       x) {
   339   if (x->x() == x->y()) {
   340     switch (x->x()->type()->tag()) {
   341       case longTag: set_constant(0); break;
   342       case floatTag: {
   343         FloatConstant* fc = x->x()->type()->as_FloatConstant();
   344         if (fc) {
   345           if (g_isnan(fc->value())) {
   346             set_constant(x->op() == Bytecodes::_fcmpl ? -1 : 1);
   347           } else {
   348             set_constant(0);
   349           }
   350         }
   351         break;
   352       }
   353       case doubleTag: {
   354         DoubleConstant* dc = x->x()->type()->as_DoubleConstant();
   355         if (dc) {
   356           if (g_isnan(dc->value())) {
   357             set_constant(x->op() == Bytecodes::_dcmpl ? -1 : 1);
   358           } else {
   359             set_constant(0);
   360           }
   361         }
   362         break;
   363       }
   364     }
   365   } else if (x->x()->type()->is_constant() && x->y()->type()->is_constant()) {
   366     switch (x->x()->type()->tag()) {
   367       case longTag: {
   368         jlong vx = x->x()->type()->as_LongConstant()->value();
   369         jlong vy = x->y()->type()->as_LongConstant()->value();
   370         if (vx == vy)
   371           set_constant(0);
   372         else if (vx < vy)
   373           set_constant(-1);
   374         else
   375           set_constant(1);
   376         break;
   377       }
   379       case floatTag: {
   380         float vx = x->x()->type()->as_FloatConstant()->value();
   381         float vy = x->y()->type()->as_FloatConstant()->value();
   382         if (g_isnan(vx) || g_isnan(vy))
   383           set_constant(x->op() == Bytecodes::_fcmpl ? -1 : 1);
   384         else if (vx == vy)
   385           set_constant(0);
   386         else if (vx < vy)
   387           set_constant(-1);
   388         else
   389           set_constant(1);
   390         break;
   391       }
   393       case doubleTag: {
   394         double vx = x->x()->type()->as_DoubleConstant()->value();
   395         double vy = x->y()->type()->as_DoubleConstant()->value();
   396         if (g_isnan(vx) || g_isnan(vy))
   397           set_constant(x->op() == Bytecodes::_dcmpl ? -1 : 1);
   398         else if (vx == vy)
   399           set_constant(0);
   400         else if (vx < vy)
   401           set_constant(-1);
   402         else
   403           set_constant(1);
   404         break;
   405       }
   406     }
   408   }
   409 }
   412 void Canonicalizer::do_IfInstanceOf(IfInstanceOf*    x) {}
   414 void Canonicalizer::do_IfOp(IfOp* x) {
   415   // Caution: do not use do_Op2(x) here for now since
   416   //          we map the condition to the op for now!
   417   move_const_to_right(x);
   418 }
   421 void Canonicalizer::do_Intrinsic      (Intrinsic*       x) {
   422   switch (x->id()) {
   423   case vmIntrinsics::_floatToRawIntBits   : {
   424     FloatConstant* c = x->argument_at(0)->type()->as_FloatConstant();
   425     if (c != NULL) {
   426       JavaValue v;
   427       v.set_jfloat(c->value());
   428       set_constant(v.get_jint());
   429     }
   430     break;
   431   }
   432   case vmIntrinsics::_intBitsToFloat      : {
   433     IntConstant* c = x->argument_at(0)->type()->as_IntConstant();
   434     if (c != NULL) {
   435       JavaValue v;
   436       v.set_jint(c->value());
   437       set_constant(v.get_jfloat());
   438     }
   439     break;
   440   }
   441   case vmIntrinsics::_doubleToRawLongBits : {
   442     DoubleConstant* c = x->argument_at(0)->type()->as_DoubleConstant();
   443     if (c != NULL) {
   444       JavaValue v;
   445       v.set_jdouble(c->value());
   446       set_constant(v.get_jlong());
   447     }
   448     break;
   449   }
   450   case vmIntrinsics::_longBitsToDouble    : {
   451     LongConstant* c = x->argument_at(0)->type()->as_LongConstant();
   452     if (c != NULL) {
   453       JavaValue v;
   454       v.set_jlong(c->value());
   455       set_constant(v.get_jdouble());
   456     }
   457     break;
   458   }
   459   }
   460 }
   462 void Canonicalizer::do_Convert        (Convert*         x) {
   463   if (x->value()->type()->is_constant()) {
   464     switch (x->op()) {
   465     case Bytecodes::_i2b:  set_constant((int)((x->value()->type()->as_IntConstant()->value() << 24) >> 24)); break;
   466     case Bytecodes::_i2s:  set_constant((int)((x->value()->type()->as_IntConstant()->value() << 16) >> 16)); break;
   467     case Bytecodes::_i2c:  set_constant((int)(x->value()->type()->as_IntConstant()->value() & ((1<<16)-1))); break;
   468     case Bytecodes::_i2l:  set_constant((jlong)(x->value()->type()->as_IntConstant()->value()));             break;
   469     case Bytecodes::_i2f:  set_constant((float)(x->value()->type()->as_IntConstant()->value()));             break;
   470     case Bytecodes::_i2d:  set_constant((double)(x->value()->type()->as_IntConstant()->value()));            break;
   471     case Bytecodes::_l2i:  set_constant((int)(x->value()->type()->as_LongConstant()->value()));              break;
   472     case Bytecodes::_l2f:  set_constant(SharedRuntime::l2f(x->value()->type()->as_LongConstant()->value())); break;
   473     case Bytecodes::_l2d:  set_constant(SharedRuntime::l2d(x->value()->type()->as_LongConstant()->value())); break;
   474     case Bytecodes::_f2d:  set_constant((double)(x->value()->type()->as_FloatConstant()->value()));          break;
   475     case Bytecodes::_f2i:  set_constant(SharedRuntime::f2i(x->value()->type()->as_FloatConstant()->value())); break;
   476     case Bytecodes::_f2l:  set_constant(SharedRuntime::f2l(x->value()->type()->as_FloatConstant()->value())); break;
   477     case Bytecodes::_d2f:  set_constant((float)(x->value()->type()->as_DoubleConstant()->value()));          break;
   478     case Bytecodes::_d2i:  set_constant(SharedRuntime::d2i(x->value()->type()->as_DoubleConstant()->value())); break;
   479     case Bytecodes::_d2l:  set_constant(SharedRuntime::d2l(x->value()->type()->as_DoubleConstant()->value())); break;
   480     default:
   481       ShouldNotReachHere();
   482     }
   483   }
   485   Value value = x->value();
   486   BasicType type = T_ILLEGAL;
   487   LoadField* lf = value->as_LoadField();
   488   if (lf) {
   489     type = lf->field_type();
   490   } else {
   491     LoadIndexed* li = value->as_LoadIndexed();
   492     if (li) {
   493       type = li->elt_type();
   494     } else {
   495       Convert* conv = value->as_Convert();
   496       if (conv) {
   497         switch (conv->op()) {
   498           case Bytecodes::_i2b: type = T_BYTE;  break;
   499           case Bytecodes::_i2s: type = T_SHORT; break;
   500           case Bytecodes::_i2c: type = T_CHAR;  break;
   501         }
   502       }
   503     }
   504   }
   505   if (type != T_ILLEGAL) {
   506     switch (x->op()) {
   507       case Bytecodes::_i2b: if (type == T_BYTE)                    set_canonical(x->value()); break;
   508       case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) set_canonical(x->value()); break;
   509       case Bytecodes::_i2c: if (type == T_CHAR)                    set_canonical(x->value()); break;
   510     }
   511   } else {
   512     Op2* op2 = x->value()->as_Op2();
   513     if (op2 && op2->op() == Bytecodes::_iand && op2->y()->type()->is_constant()) {
   514       jint safebits = 0;
   515       jint mask = op2->y()->type()->as_IntConstant()->value();
   516       switch (x->op()) {
   517         case Bytecodes::_i2b: safebits = 0x7f;   break;
   518         case Bytecodes::_i2s: safebits = 0x7fff; break;
   519         case Bytecodes::_i2c: safebits = 0xffff; break;
   520       }
   521       // When casting a masked integer to a smaller signed type, if
   522       // the mask doesn't include the sign bit the cast isn't needed.
   523       if (safebits && (mask & ~safebits) == 0) {
   524         set_canonical(x->value());
   525       }
   526     }
   527   }
   529 }
   531 void Canonicalizer::do_NullCheck      (NullCheck*       x) {
   532   if (x->obj()->as_NewArray() != NULL || x->obj()->as_NewInstance() != NULL) {
   533     set_canonical(x->obj());
   534   } else {
   535     Constant* con = x->obj()->as_Constant();
   536     if (con) {
   537       ObjectType* c = con->type()->as_ObjectType();
   538       if (c && c->is_loaded()) {
   539         ObjectConstant* oc = c->as_ObjectConstant();
   540         if (!oc || !oc->value()->is_null_object()) {
   541           set_canonical(con);
   542         }
   543       }
   544     }
   545   }
   546 }
   548 void Canonicalizer::do_Invoke         (Invoke*          x) {}
   549 void Canonicalizer::do_NewInstance    (NewInstance*     x) {}
   550 void Canonicalizer::do_NewTypeArray   (NewTypeArray*    x) {}
   551 void Canonicalizer::do_NewObjectArray (NewObjectArray*  x) {}
   552 void Canonicalizer::do_NewMultiArray  (NewMultiArray*   x) {}
   553 void Canonicalizer::do_CheckCast      (CheckCast*       x) {
   554   if (x->klass()->is_loaded()) {
   555     Value obj = x->obj();
   556     ciType* klass = obj->exact_type();
   557     if (klass == NULL) klass = obj->declared_type();
   558     if (klass != NULL && klass->is_loaded() && klass->is_subtype_of(x->klass())) {
   559       set_canonical(obj);
   560       return;
   561     }
   562     // checkcast of null returns null
   563     if (obj->as_Constant() && obj->type()->as_ObjectType()->constant_value()->is_null_object()) {
   564       set_canonical(obj);
   565     }
   566   }
   567 }
   568 void Canonicalizer::do_InstanceOf     (InstanceOf*      x) {
   569   if (x->klass()->is_loaded()) {
   570     Value obj = x->obj();
   571     ciType* exact = obj->exact_type();
   572     if (exact != NULL && exact->is_loaded() && (obj->as_NewInstance() || obj->as_NewArray())) {
   573       set_constant(exact->is_subtype_of(x->klass()) ? 1 : 0);
   574       return;
   575     }
   576     // instanceof null returns false
   577     if (obj->as_Constant() && obj->type()->as_ObjectType()->constant_value()->is_null_object()) {
   578       set_constant(0);
   579     }
   580   }
   582 }
   583 void Canonicalizer::do_MonitorEnter   (MonitorEnter*    x) {}
   584 void Canonicalizer::do_MonitorExit    (MonitorExit*     x) {}
   585 void Canonicalizer::do_BlockBegin     (BlockBegin*      x) {}
   586 void Canonicalizer::do_Goto           (Goto*            x) {}
   589 static bool is_true(jlong x, If::Condition cond, jlong y) {
   590   switch (cond) {
   591     case If::eql: return x == y;
   592     case If::neq: return x != y;
   593     case If::lss: return x <  y;
   594     case If::leq: return x <= y;
   595     case If::gtr: return x >  y;
   596     case If::geq: return x >= y;
   597   }
   598   ShouldNotReachHere();
   599   return false;
   600 }
   602 static bool is_safepoint(BlockEnd* x, BlockBegin* sux) {
   603   // An Instruction with multiple successors, x, is replaced by a Goto
   604   // to a single successor, sux. Is a safepoint check needed = was the
   605   // instruction being replaced a safepoint and the single remaining
   606   // successor a back branch?
   607   return x->is_safepoint() && (sux->bci() < x->state_before()->bci());
   608 }
   610 void Canonicalizer::do_If(If* x) {
   611   // move const to right
   612   if (x->x()->type()->is_constant()) x->swap_operands();
   613   // simplify
   614   const Value l = x->x(); ValueType* lt = l->type();
   615   const Value r = x->y(); ValueType* rt = r->type();
   617   if (l == r && !lt->is_float_kind()) {
   618     // pattern: If (a cond a) => simplify to Goto
   619     BlockBegin* sux;
   620     switch (x->cond()) {
   621     case If::eql: sux = x->sux_for(true);  break;
   622     case If::neq: sux = x->sux_for(false); break;
   623     case If::lss: sux = x->sux_for(false); break;
   624     case If::leq: sux = x->sux_for(true);  break;
   625     case If::gtr: sux = x->sux_for(false); break;
   626     case If::geq: sux = x->sux_for(true);  break;
   627     }
   628     // If is a safepoint then the debug information should come from the state_before of the If.
   629     set_canonical(new Goto(sux, x->state_before(), is_safepoint(x, sux)));
   630     return;
   631   }
   633   if (lt->is_constant() && rt->is_constant()) {
   634     if (x->x()->as_Constant() != NULL) {
   635       // pattern: If (lc cond rc) => simplify to: Goto
   636       BlockBegin* sux = x->x()->as_Constant()->compare(x->cond(), x->y(),
   637                                                        x->sux_for(true),
   638                                                        x->sux_for(false));
   639       if (sux != NULL) {
   640         // If is a safepoint then the debug information should come from the state_before of the If.
   641         set_canonical(new Goto(sux, x->state_before(), is_safepoint(x, sux)));
   642       }
   643     }
   644   } else if (rt->as_IntConstant() != NULL) {
   645     // pattern: If (l cond rc) => investigate further
   646     const jint rc = rt->as_IntConstant()->value();
   647     if (l->as_CompareOp() != NULL) {
   648       // pattern: If ((a cmp b) cond rc) => simplify to: If (x cond y) or: Goto
   649       CompareOp* cmp = l->as_CompareOp();
   650       bool unordered_is_less = cmp->op() == Bytecodes::_fcmpl || cmp->op() == Bytecodes::_dcmpl;
   651       BlockBegin* lss_sux = x->sux_for(is_true(-1, x->cond(), rc)); // successor for a < b
   652       BlockBegin* eql_sux = x->sux_for(is_true( 0, x->cond(), rc)); // successor for a = b
   653       BlockBegin* gtr_sux = x->sux_for(is_true(+1, x->cond(), rc)); // successor for a > b
   654       BlockBegin* nan_sux = unordered_is_less ? lss_sux : gtr_sux ; // successor for unordered
   655       // Note: At this point all successors (lss_sux, eql_sux, gtr_sux, nan_sux) are
   656       //       equal to x->tsux() or x->fsux(). Furthermore, nan_sux equals either
   657       //       lss_sux or gtr_sux.
   658       if (lss_sux == eql_sux && eql_sux == gtr_sux) {
   659         // all successors identical => simplify to: Goto
   660         set_canonical(new Goto(lss_sux, x->state_before(), x->is_safepoint()));
   661       } else {
   662         // two successors differ and two successors are the same => simplify to: If (x cmp y)
   663         // determine new condition & successors
   664         If::Condition cond;
   665         BlockBegin* tsux = NULL;
   666         BlockBegin* fsux = NULL;
   667              if (lss_sux == eql_sux) { cond = If::leq; tsux = lss_sux; fsux = gtr_sux; }
   668         else if (lss_sux == gtr_sux) { cond = If::neq; tsux = lss_sux; fsux = eql_sux; }
   669         else if (eql_sux == gtr_sux) { cond = If::geq; tsux = eql_sux; fsux = lss_sux; }
   670         else                         { ShouldNotReachHere();                           }
   671         If* canon = new If(cmp->x(), cond, nan_sux == tsux, cmp->y(), tsux, fsux, cmp->state_before(), x->is_safepoint());
   672         if (cmp->x() == cmp->y()) {
   673           do_If(canon);
   674         } else {
   675           if (compilation()->profile_branches()) {
   676             // TODO: If profiling, leave floating point comparisons unoptimized.
   677             // We currently do not support profiling of the unordered case.
   678             switch(cmp->op()) {
   679               case Bytecodes::_fcmpl: case Bytecodes::_fcmpg:
   680               case Bytecodes::_dcmpl: case Bytecodes::_dcmpg:
   681                 set_canonical(x);
   682                 return;
   683             }
   684           }
   685           set_bci(cmp->state_before()->bci());
   686           set_canonical(canon);
   687         }
   688       }
   689     } else if (l->as_InstanceOf() != NULL) {
   690       // NOTE: Code permanently disabled for now since it leaves the old InstanceOf
   691       //       instruction in the graph (it is pinned). Need to fix this at some point.
   692       //       It should also be left in the graph when generating a profiled method version or Goto
   693       //       has to know that it was an InstanceOf.
   694       return;
   695       // pattern: If ((obj instanceof klass) cond rc) => simplify to: IfInstanceOf or: Goto
   696       InstanceOf* inst = l->as_InstanceOf();
   697       BlockBegin* is_inst_sux = x->sux_for(is_true(1, x->cond(), rc)); // successor for instanceof == 1
   698       BlockBegin* no_inst_sux = x->sux_for(is_true(0, x->cond(), rc)); // successor for instanceof == 0
   699       if (is_inst_sux == no_inst_sux && inst->is_loaded()) {
   700         // both successors identical and klass is loaded => simplify to: Goto
   701         set_canonical(new Goto(is_inst_sux, x->state_before(), x->is_safepoint()));
   702       } else {
   703         // successors differ => simplify to: IfInstanceOf
   704         set_canonical(new IfInstanceOf(inst->klass(), inst->obj(), true, inst->state_before()->bci(), is_inst_sux, no_inst_sux));
   705       }
   706     }
   707   } else if (rt == objectNull && (l->as_NewInstance() || l->as_NewArray())) {
   708     if (x->cond() == Instruction::eql) {
   709       BlockBegin* sux = x->fsux();
   710       set_canonical(new Goto(sux, x->state_before(), is_safepoint(x, sux)));
   711     } else {
   712       assert(x->cond() == Instruction::neq, "only other valid case");
   713       BlockBegin* sux = x->tsux();
   714       set_canonical(new Goto(sux, x->state_before(), is_safepoint(x, sux)));
   715     }
   716   }
   717 }
   720 void Canonicalizer::do_TableSwitch(TableSwitch* x) {
   721   if (x->tag()->type()->is_constant()) {
   722     int v = x->tag()->type()->as_IntConstant()->value();
   723     BlockBegin* sux = x->default_sux();
   724     if (v >= x->lo_key() && v <= x->hi_key()) {
   725       sux = x->sux_at(v - x->lo_key());
   726     }
   727     set_canonical(new Goto(sux, x->state_before(), is_safepoint(x, sux)));
   728   } else if (x->number_of_sux() == 1) {
   729     // NOTE: Code permanently disabled for now since the switch statement's
   730     //       tag expression may produce side-effects in which case it must
   731     //       be executed.
   732     return;
   733     // simplify to Goto
   734     set_canonical(new Goto(x->default_sux(), x->state_before(), x->is_safepoint()));
   735   } else if (x->number_of_sux() == 2) {
   736     // NOTE: Code permanently disabled for now since it produces two new nodes
   737     //       (Constant & If) and the Canonicalizer cannot return them correctly
   738     //       yet. For now we copied the corresponding code directly into the
   739     //       GraphBuilder (i.e., we should never reach here).
   740     return;
   741     // simplify to If
   742     assert(x->lo_key() == x->hi_key(), "keys must be the same");
   743     Constant* key = new Constant(new IntConstant(x->lo_key()));
   744     set_canonical(new If(x->tag(), If::eql, true, key, x->sux_at(0), x->default_sux(), x->state_before(), x->is_safepoint()));
   745   }
   746 }
   749 void Canonicalizer::do_LookupSwitch(LookupSwitch* x) {
   750   if (x->tag()->type()->is_constant()) {
   751     int v = x->tag()->type()->as_IntConstant()->value();
   752     BlockBegin* sux = x->default_sux();
   753     for (int i = 0; i < x->length(); i++) {
   754       if (v == x->key_at(i)) {
   755         sux = x->sux_at(i);
   756       }
   757     }
   758     set_canonical(new Goto(sux, x->state_before(), is_safepoint(x, sux)));
   759   } else if (x->number_of_sux() == 1) {
   760     // NOTE: Code permanently disabled for now since the switch statement's
   761     //       tag expression may produce side-effects in which case it must
   762     //       be executed.
   763     return;
   764     // simplify to Goto
   765     set_canonical(new Goto(x->default_sux(), x->state_before(), x->is_safepoint()));
   766   } else if (x->number_of_sux() == 2) {
   767     // NOTE: Code permanently disabled for now since it produces two new nodes
   768     //       (Constant & If) and the Canonicalizer cannot return them correctly
   769     //       yet. For now we copied the corresponding code directly into the
   770     //       GraphBuilder (i.e., we should never reach here).
   771     return;
   772     // simplify to If
   773     assert(x->length() == 1, "length must be the same");
   774     Constant* key = new Constant(new IntConstant(x->key_at(0)));
   775     set_canonical(new If(x->tag(), If::eql, true, key, x->sux_at(0), x->default_sux(), x->state_before(), x->is_safepoint()));
   776   }
   777 }
   780 void Canonicalizer::do_Return         (Return*          x) {}
   781 void Canonicalizer::do_Throw          (Throw*           x) {}
   782 void Canonicalizer::do_Base           (Base*            x) {}
   783 void Canonicalizer::do_OsrEntry       (OsrEntry*        x) {}
   784 void Canonicalizer::do_ExceptionObject(ExceptionObject* x) {}
   786 static bool match_index_and_scale(Instruction*  instr,
   787                                   Instruction** index,
   788                                   int*          log2_scale,
   789                                   Instruction** instr_to_unpin) {
   790   *instr_to_unpin = NULL;
   792   // Skip conversion ops
   793   Convert* convert = instr->as_Convert();
   794   if (convert != NULL) {
   795     instr = convert->value();
   796   }
   798   ShiftOp* shift = instr->as_ShiftOp();
   799   if (shift != NULL) {
   800     if (shift->is_pinned()) {
   801       *instr_to_unpin = shift;
   802     }
   803     // Constant shift value?
   804     Constant* con = shift->y()->as_Constant();
   805     if (con == NULL) return false;
   806     // Well-known type and value?
   807     IntConstant* val = con->type()->as_IntConstant();
   808     if (val == NULL) return false;
   809     if (shift->x()->type() != intType) return false;
   810     *index = shift->x();
   811     int tmp_scale = val->value();
   812     if (tmp_scale >= 0 && tmp_scale < 4) {
   813       *log2_scale = tmp_scale;
   814       return true;
   815     } else {
   816       return false;
   817     }
   818   }
   820   ArithmeticOp* arith = instr->as_ArithmeticOp();
   821   if (arith != NULL) {
   822     if (arith->is_pinned()) {
   823       *instr_to_unpin = arith;
   824     }
   825     // Check for integer multiply
   826     if (arith->op() == Bytecodes::_imul) {
   827       // See if either arg is a known constant
   828       Constant* con = arith->x()->as_Constant();
   829       if (con != NULL) {
   830         *index = arith->y();
   831       } else {
   832         con = arith->y()->as_Constant();
   833         if (con == NULL) return false;
   834         *index = arith->x();
   835       }
   836       if ((*index)->type() != intType) return false;
   837       // Well-known type and value?
   838       IntConstant* val = con->type()->as_IntConstant();
   839       if (val == NULL) return false;
   840       switch (val->value()) {
   841       case 1: *log2_scale = 0; return true;
   842       case 2: *log2_scale = 1; return true;
   843       case 4: *log2_scale = 2; return true;
   844       case 8: *log2_scale = 3; return true;
   845       default:            return false;
   846       }
   847     }
   848   }
   850   // Unknown instruction sequence; don't touch it
   851   return false;
   852 }
   855 static bool match(UnsafeRawOp* x,
   856                   Instruction** base,
   857                   Instruction** index,
   858                   int*          log2_scale) {
   859   Instruction* instr_to_unpin = NULL;
   860   ArithmeticOp* root = x->base()->as_ArithmeticOp();
   861   if (root == NULL) return false;
   862   // Limit ourselves to addition for now
   863   if (root->op() != Bytecodes::_ladd) return false;
   864   // Try to find shift or scale op
   865   if (match_index_and_scale(root->y(), index, log2_scale, &instr_to_unpin)) {
   866     *base = root->x();
   867   } else if (match_index_and_scale(root->x(), index, log2_scale, &instr_to_unpin)) {
   868     *base = root->y();
   869   } else if (root->y()->as_Convert() != NULL) {
   870     Convert* convert = root->y()->as_Convert();
   871     if (convert->op() == Bytecodes::_i2l && convert->value()->type() == intType) {
   872       // pick base and index, setting scale at 1
   873       *base  = root->x();
   874       *index = convert->value();
   875       *log2_scale = 0;
   876     } else {
   877       return false;
   878     }
   879   } else {
   880     // doesn't match any expected sequences
   881     return false;
   882   }
   884   // If the value is pinned then it will be always be computed so
   885   // there's no profit to reshaping the expression.
   886   return !root->is_pinned();
   887 }
   890 void Canonicalizer::do_UnsafeRawOp(UnsafeRawOp* x) {
   891   Instruction* base = NULL;
   892   Instruction* index = NULL;
   893   int          log2_scale;
   895   if (match(x, &base, &index, &log2_scale)) {
   896     x->set_base(base);
   897     x->set_index(index);
   898     x->set_log2_scale(log2_scale);
   899     if (PrintUnsafeOptimization) {
   900       tty->print_cr("Canonicalizer: UnsafeRawOp id %d: base = id %d, index = id %d, log2_scale = %d",
   901                     x->id(), x->base()->id(), x->index()->id(), x->log2_scale());
   902     }
   903   }
   904 }
   906 void Canonicalizer::do_RoundFP(RoundFP* x) {}
   907 void Canonicalizer::do_UnsafeGetRaw(UnsafeGetRaw* x) { if (OptimizeUnsafes) do_UnsafeRawOp(x); }
   908 void Canonicalizer::do_UnsafePutRaw(UnsafePutRaw* x) { if (OptimizeUnsafes) do_UnsafeRawOp(x); }
   909 void Canonicalizer::do_UnsafeGetObject(UnsafeGetObject* x) {}
   910 void Canonicalizer::do_UnsafePutObject(UnsafePutObject* x) {}
   911 void Canonicalizer::do_UnsafePrefetchRead (UnsafePrefetchRead*  x) {}
   912 void Canonicalizer::do_UnsafePrefetchWrite(UnsafePrefetchWrite* x) {}
   913 void Canonicalizer::do_ProfileCall(ProfileCall* x) {}
   914 void Canonicalizer::do_ProfileInvoke(ProfileInvoke* x) {}
   915 void Canonicalizer::do_RuntimeCall(RuntimeCall* x) {}
   916 void Canonicalizer::do_MemBar(MemBar* x) {}

mercurial