hotspot/src/share/vm/c1/c1_Canonicalizer.cpp
changeset 46630 75aa3e39d02c
parent 43673 bf2f6d3f8f5e
equal deleted inserted replaced
46629:8eeacdc76bf2 46630:75aa3e39d02c
     1 /*
     1 /*
     2  * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    72     switch (x->op()) {
    72     switch (x->op()) {
    73     case Bytecodes::_isub: set_constant(0); return;
    73     case Bytecodes::_isub: set_constant(0); return;
    74     case Bytecodes::_lsub: set_constant(jlong_cast(0)); return;
    74     case Bytecodes::_lsub: set_constant(jlong_cast(0)); return;
    75     case Bytecodes::_iand: // fall through
    75     case Bytecodes::_iand: // fall through
    76     case Bytecodes::_land: // fall through
    76     case Bytecodes::_land: // fall through
    77     case Bytecodes::_ior:  // fall through
    77     case Bytecodes::_ior : // fall through
    78     case Bytecodes::_lor : set_canonical(x->x()); return;
    78     case Bytecodes::_lor : set_canonical(x->x()); return;
    79     case Bytecodes::_ixor: set_constant(0); return;
    79     case Bytecodes::_ixor: set_constant(0); return;
    80     case Bytecodes::_lxor: set_constant(jlong_cast(0)); return;
    80     case Bytecodes::_lxor: set_constant(jlong_cast(0)); return;
       
    81     default              : break;
    81     }
    82     }
    82   }
    83   }
    83 
    84 
    84   if (x->x()->type()->is_constant() && x->y()->type()->is_constant()) {
    85   if (x->x()->type()->is_constant() && x->y()->type()->is_constant()) {
    85     // do constant folding for selected operations
    86     // do constant folding for selected operations
   112               }
   113               }
   113               break;
   114               break;
   114             case Bytecodes::_iand: set_constant(a & b); return;
   115             case Bytecodes::_iand: set_constant(a & b); return;
   115             case Bytecodes::_ior : set_constant(a | b); return;
   116             case Bytecodes::_ior : set_constant(a | b); return;
   116             case Bytecodes::_ixor: set_constant(a ^ b); return;
   117             case Bytecodes::_ixor: set_constant(a ^ b); return;
       
   118             default              : break;
   117           }
   119           }
   118         }
   120         }
   119         break;
   121         break;
   120       case longTag:
   122       case longTag:
   121         { jlong a = x->x()->type()->as_LongConstant()->value();
   123         { jlong a = x->x()->type()->as_LongConstant()->value();
   137               }
   139               }
   138               break;
   140               break;
   139             case Bytecodes::_land: set_constant(a & b); return;
   141             case Bytecodes::_land: set_constant(a & b); return;
   140             case Bytecodes::_lor : set_constant(a | b); return;
   142             case Bytecodes::_lor : set_constant(a | b); return;
   141             case Bytecodes::_lxor: set_constant(a ^ b); return;
   143             case Bytecodes::_lxor: set_constant(a ^ b); return;
       
   144             default              : break;
   142           }
   145           }
   143         }
   146         }
   144         break;
   147         break;
   145       // other cases not implemented (must be extremely careful with floats & doubles!)
   148       default:
       
   149         // other cases not implemented (must be extremely careful with floats & doubles!)
       
   150         break;
   146     }
   151     }
   147   }
   152   }
   148   // make sure constant is on the right side, if any
   153   // make sure constant is on the right side, if any
   149   move_const_to_right(x);
   154   move_const_to_right(x);
   150 
   155 
   159             case Bytecodes::_imul: set_constant(0); return;
   164             case Bytecodes::_imul: set_constant(0); return;
   160               // Note: for div and rem, make sure that C semantics
   165               // Note: for div and rem, make sure that C semantics
   161               //       corresponds to Java semantics!
   166               //       corresponds to Java semantics!
   162             case Bytecodes::_iand: set_constant(0); return;
   167             case Bytecodes::_iand: set_constant(0); return;
   163             case Bytecodes::_ior : set_canonical(x->x()); return;
   168             case Bytecodes::_ior : set_canonical(x->x()); return;
       
   169             default              : break;
   164           }
   170           }
   165         }
   171         }
   166         break;
   172         break;
   167       case longTag:
   173       case longTag:
   168         if (x->y()->type()->as_LongConstant()->value() == (jlong)0) {
   174         if (x->y()->type()->as_LongConstant()->value() == (jlong)0) {
   172             case Bytecodes::_lmul: set_constant((jlong)0); return;
   178             case Bytecodes::_lmul: set_constant((jlong)0); return;
   173               // Note: for div and rem, make sure that C semantics
   179               // Note: for div and rem, make sure that C semantics
   174               //       corresponds to Java semantics!
   180               //       corresponds to Java semantics!
   175             case Bytecodes::_land: set_constant((jlong)0); return;
   181             case Bytecodes::_land: set_constant((jlong)0); return;
   176             case Bytecodes::_lor : set_canonical(x->x()); return;
   182             case Bytecodes::_lor : set_canonical(x->x()); return;
       
   183             default              : break;
   177           }
   184           }
   178         }
   185         }
       
   186         break;
       
   187       default:
   179         break;
   188         break;
   180     }
   189     }
   181   }
   190   }
   182 }
   191 }
   183 
   192 
   208     BasicType type = x->field()->type()->basic_type();
   217     BasicType type = x->field()->type()->basic_type();
   209     switch (conv->op()) {
   218     switch (conv->op()) {
   210     case Bytecodes::_i2b: if (type == T_BYTE)  value = conv->value(); break;
   219     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;
   220     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;
   221     case Bytecodes::_i2c: if (type == T_CHAR  || type == T_BYTE)  value = conv->value(); break;
       
   222     default             : break;
   213     }
   223     }
   214     // limit this optimization to current block
   224     // limit this optimization to current block
   215     if (value != NULL && in_current_block(conv)) {
   225     if (value != NULL && in_current_block(conv)) {
   216       set_canonical(new StoreField(x->obj(), x->offset(), x->field(), value, x->is_static(),
   226       set_canonical(new StoreField(x->obj(), x->offset(), x->field(), value, x->is_static(),
   217                                    x->state_before(), x->needs_patching()));
   227                                    x->state_before(), x->needs_patching()));
   301     BasicType type = x->elt_type();
   311     BasicType type = x->elt_type();
   302     switch (conv->op()) {
   312     switch (conv->op()) {
   303     case Bytecodes::_i2b: if (type == T_BYTE)  value = conv->value(); break;
   313     case Bytecodes::_i2b: if (type == T_BYTE)  value = conv->value(); break;
   304     case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) value = conv->value(); break;
   314     case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) value = conv->value(); break;
   305     case Bytecodes::_i2c: if (type == T_CHAR  || type == T_BYTE) value = conv->value(); break;
   315     case Bytecodes::_i2c: if (type == T_CHAR  || type == T_BYTE) value = conv->value(); break;
       
   316     default             : break;
   306     }
   317     }
   307     // limit this optimization to current block
   318     // limit this optimization to current block
   308     if (value != NULL && in_current_block(conv)) {
   319     if (value != NULL && in_current_block(conv)) {
   309       set_canonical(new StoreIndexed(x->array(), x->index(), x->length(),
   320       set_canonical(new StoreIndexed(x->array(), x->index(), x->length(),
   310                                      x->elt_type(), value, x->state_before(),
   321                                      x->elt_type(), value, x->state_before(),
   349         if (shift == 0) mask = ~0;
   360         if (shift == 0) mask = ~0;
   350         switch (x->op()) {
   361         switch (x->op()) {
   351           case Bytecodes::_ishl:  set_constant(value << shift); return;
   362           case Bytecodes::_ishl:  set_constant(value << shift); return;
   352           case Bytecodes::_ishr:  set_constant(value >> shift); return;
   363           case Bytecodes::_ishr:  set_constant(value >> shift); return;
   353           case Bytecodes::_iushr: set_constant((value >> shift) & mask); return;
   364           case Bytecodes::_iushr: set_constant((value >> shift) & mask); return;
       
   365           default:                break;
   354         }
   366         }
   355       } else if (t->tag() == longTag) {
   367       } else if (t->tag() == longTag) {
   356         jlong value = t->as_LongConstant()->value();
   368         jlong value = t->as_LongConstant()->value();
   357         int shift = t2->as_IntConstant()->value() & 63;
   369         int shift = t2->as_IntConstant()->value() & 63;
   358         jlong mask = ~(~jlong_cast(0) << (64 - shift));
   370         jlong mask = ~(~jlong_cast(0) << (64 - shift));
   359         if (shift == 0) mask = ~jlong_cast(0);
   371         if (shift == 0) mask = ~jlong_cast(0);
   360         switch (x->op()) {
   372         switch (x->op()) {
   361           case Bytecodes::_lshl:  set_constant(value << shift); return;
   373           case Bytecodes::_lshl:  set_constant(value << shift); return;
   362           case Bytecodes::_lshr:  set_constant(value >> shift); return;
   374           case Bytecodes::_lshr:  set_constant(value >> shift); return;
   363           case Bytecodes::_lushr: set_constant((value >> shift) & mask); return;
   375           case Bytecodes::_lushr: set_constant((value >> shift) & mask); return;
       
   376           default:                break;
   364         }
   377         }
   365       }
   378       }
   366     }
   379     }
   367   }
   380   }
   368   if (t2->is_constant()) {
   381   if (t2->is_constant()) {
   369     switch (t2->tag()) {
   382     switch (t2->tag()) {
   370       case intTag   : if (t2->as_IntConstant()->value() == 0)  set_canonical(x->x()); return;
   383       case intTag   : if (t2->as_IntConstant()->value() == 0)  set_canonical(x->x()); return;
   371       case longTag  : if (t2->as_LongConstant()->value() == (jlong)0)  set_canonical(x->x()); return;
   384       case longTag  : if (t2->as_LongConstant()->value() == (jlong)0)  set_canonical(x->x()); return;
   372       default       : ShouldNotReachHere();
   385       default       : ShouldNotReachHere(); return;
   373     }
   386     }
   374   }
   387   }
   375 }
   388 }
   376 
   389 
   377 
   390 
   400             set_constant(0);
   413             set_constant(0);
   401           }
   414           }
   402         }
   415         }
   403         break;
   416         break;
   404       }
   417       }
       
   418       default:
       
   419         break;
   405     }
   420     }
   406   } else if (x->x()->type()->is_constant() && x->y()->type()->is_constant()) {
   421   } else if (x->x()->type()->is_constant() && x->y()->type()->is_constant()) {
   407     switch (x->x()->type()->tag()) {
   422     switch (x->x()->type()->tag()) {
   408       case longTag: {
   423       case longTag: {
   409         jlong vx = x->x()->type()->as_LongConstant()->value();
   424         jlong vx = x->x()->type()->as_LongConstant()->value();
   442           set_constant(-1);
   457           set_constant(-1);
   443         else
   458         else
   444           set_constant(1);
   459           set_constant(1);
   445         break;
   460         break;
   446       }
   461       }
   447     }
   462 
   448 
   463       default:
       
   464         break;
       
   465     }
   449   }
   466   }
   450 }
   467 }
   451 
   468 
   452 
   469 
   453 void Canonicalizer::do_IfInstanceOf(IfInstanceOf*    x) {}
   470 void Canonicalizer::do_IfInstanceOf(IfInstanceOf*    x) {}
   528       ciType* t = c->value()->java_mirror_type();
   545       ciType* t = c->value()->java_mirror_type();
   529       set_constant(t->is_primitive_type());
   546       set_constant(t->is_primitive_type());
   530     }
   547     }
   531     break;
   548     break;
   532   }
   549   }
       
   550   default:
       
   551     break;
   533   }
   552   }
   534 }
   553 }
   535 
   554 
   536 void Canonicalizer::do_Convert        (Convert*         x) {
   555 void Canonicalizer::do_Convert        (Convert*         x) {
   537   if (x->value()->type()->is_constant()) {
   556   if (x->value()->type()->is_constant()) {
   570       if (conv) {
   589       if (conv) {
   571         switch (conv->op()) {
   590         switch (conv->op()) {
   572           case Bytecodes::_i2b: type = T_BYTE;  break;
   591           case Bytecodes::_i2b: type = T_BYTE;  break;
   573           case Bytecodes::_i2s: type = T_SHORT; break;
   592           case Bytecodes::_i2s: type = T_SHORT; break;
   574           case Bytecodes::_i2c: type = T_CHAR;  break;
   593           case Bytecodes::_i2c: type = T_CHAR;  break;
       
   594           default             :                 break;
   575         }
   595         }
   576       }
   596       }
   577     }
   597     }
   578   }
   598   }
   579   if (type != T_ILLEGAL) {
   599   if (type != T_ILLEGAL) {
   580     switch (x->op()) {
   600     switch (x->op()) {
   581       case Bytecodes::_i2b: if (type == T_BYTE)                    set_canonical(x->value()); break;
   601       case Bytecodes::_i2b: if (type == T_BYTE)                    set_canonical(x->value()); break;
   582       case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) set_canonical(x->value()); break;
   602       case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) set_canonical(x->value()); break;
   583       case Bytecodes::_i2c: if (type == T_CHAR)                    set_canonical(x->value()); break;
   603       case Bytecodes::_i2c: if (type == T_CHAR)                    set_canonical(x->value()); break;
       
   604       default             :                                                                   break;
   584     }
   605     }
   585   } else {
   606   } else {
   586     Op2* op2 = x->value()->as_Op2();
   607     Op2* op2 = x->value()->as_Op2();
   587     if (op2 && op2->op() == Bytecodes::_iand && op2->y()->type()->is_constant()) {
   608     if (op2 && op2->op() == Bytecodes::_iand && op2->y()->type()->is_constant()) {
   588       jint safebits = 0;
   609       jint safebits = 0;
   589       jint mask = op2->y()->type()->as_IntConstant()->value();
   610       jint mask = op2->y()->type()->as_IntConstant()->value();
   590       switch (x->op()) {
   611       switch (x->op()) {
   591         case Bytecodes::_i2b: safebits = 0x7f;   break;
   612         case Bytecodes::_i2b: safebits = 0x7f;   break;
   592         case Bytecodes::_i2s: safebits = 0x7fff; break;
   613         case Bytecodes::_i2s: safebits = 0x7fff; break;
   593         case Bytecodes::_i2c: safebits = 0xffff; break;
   614         case Bytecodes::_i2c: safebits = 0xffff; break;
       
   615         default             :                    break;
   594       }
   616       }
   595       // When casting a masked integer to a smaller signed type, if
   617       // When casting a masked integer to a smaller signed type, if
   596       // the mask doesn't include the sign bit the cast isn't needed.
   618       // the mask doesn't include the sign bit the cast isn't needed.
   597       if (safebits && (mask & ~safebits) == 0) {
   619       if (safebits && (mask & ~safebits) == 0) {
   598         set_canonical(x->value());
   620         set_canonical(x->value());
   667     case If::neq: return x != y;
   689     case If::neq: return x != y;
   668     case If::lss: return x <  y;
   690     case If::lss: return x <  y;
   669     case If::leq: return x <= y;
   691     case If::leq: return x <= y;
   670     case If::gtr: return x >  y;
   692     case If::gtr: return x >  y;
   671     case If::geq: return x >= y;
   693     case If::geq: return x >= y;
   672   }
   694     default:
   673   ShouldNotReachHere();
   695       ShouldNotReachHere();
   674   return false;
   696       return false;
       
   697   }
   675 }
   698 }
   676 
   699 
   677 static bool is_safepoint(BlockEnd* x, BlockBegin* sux) {
   700 static bool is_safepoint(BlockEnd* x, BlockBegin* sux) {
   678   // An Instruction with multiple successors, x, is replaced by a Goto
   701   // An Instruction with multiple successors, x, is replaced by a Goto
   679   // to a single successor, sux. Is a safepoint check needed = was the
   702   // to a single successor, sux. Is a safepoint check needed = was the
   754             switch(cmp->op()) {
   777             switch(cmp->op()) {
   755               case Bytecodes::_fcmpl: case Bytecodes::_fcmpg:
   778               case Bytecodes::_fcmpl: case Bytecodes::_fcmpg:
   756               case Bytecodes::_dcmpl: case Bytecodes::_dcmpg:
   779               case Bytecodes::_dcmpl: case Bytecodes::_dcmpg:
   757                 set_canonical(x);
   780                 set_canonical(x);
   758                 return;
   781                 return;
       
   782               default:
       
   783                 break;
   759             }
   784             }
   760           }
   785           }
   761           set_bci(cmp->state_before()->bci());
   786           set_bci(cmp->state_before()->bci());
   762           set_canonical(canon);
   787           set_canonical(canon);
   763         }
   788         }