src/share/vm/c1/c1_Canonicalizer.cpp

changeset 435
a61af66fc99e
child 1730
3cf667df43ef
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/c1/c1_Canonicalizer.cpp	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,877 @@
     1.4 +/*
     1.5 + * Copyright 1999-2006 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.24 + * have any questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#include "incls/_precompiled.incl"
    1.29 +#include "incls/_c1_Canonicalizer.cpp.incl"
    1.30 +
    1.31 +
    1.32 +static void do_print_value(Value* vp) {
    1.33 +  (*vp)->print_line();
    1.34 +}
    1.35 +
    1.36 +void Canonicalizer::set_canonical(Value x) {
    1.37 +  assert(x != NULL, "value must exist");
    1.38 +  // Note: we can not currently substitute root nodes which show up in
    1.39 +  // the instruction stream (because the instruction list is embedded
    1.40 +  // in the instructions).
    1.41 +  if (canonical() != x) {
    1.42 +    if (PrintCanonicalization) {
    1.43 +      canonical()->input_values_do(do_print_value);
    1.44 +      canonical()->print_line();
    1.45 +      tty->print_cr("canonicalized to:");
    1.46 +      x->input_values_do(do_print_value);
    1.47 +      x->print_line();
    1.48 +      tty->cr();
    1.49 +    }
    1.50 +    assert(_canonical->type()->tag() == x->type()->tag(), "types must match");
    1.51 +    _canonical = x;
    1.52 +  }
    1.53 +}
    1.54 +
    1.55 +
    1.56 +void Canonicalizer::move_const_to_right(Op2* x) {
    1.57 +  if (x->x()->type()->is_constant() && x->is_commutative()) x->swap_operands();
    1.58 +}
    1.59 +
    1.60 +
    1.61 +void Canonicalizer::do_Op2(Op2* x) {
    1.62 +  if (x->x() == x->y()) {
    1.63 +    switch (x->op()) {
    1.64 +    case Bytecodes::_isub: set_constant(0); return;
    1.65 +    case Bytecodes::_lsub: set_constant(jlong_cast(0)); return;
    1.66 +    case Bytecodes::_iand: // fall through
    1.67 +    case Bytecodes::_land: // fall through
    1.68 +    case Bytecodes::_ior:  // fall through
    1.69 +    case Bytecodes::_lor : set_canonical(x->x()); return;
    1.70 +    case Bytecodes::_ixor: set_constant(0); return;
    1.71 +    case Bytecodes::_lxor: set_constant(jlong_cast(0)); return;
    1.72 +    }
    1.73 +  }
    1.74 +
    1.75 +  if (x->x()->type()->is_constant() && x->y()->type()->is_constant()) {
    1.76 +    // do constant folding for selected operations
    1.77 +    switch (x->type()->tag()) {
    1.78 +      case intTag:
    1.79 +        { jint a = x->x()->type()->as_IntConstant()->value();
    1.80 +          jint b = x->y()->type()->as_IntConstant()->value();
    1.81 +          switch (x->op()) {
    1.82 +            case Bytecodes::_iadd: set_constant(a + b); return;
    1.83 +            case Bytecodes::_isub: set_constant(a - b); return;
    1.84 +            case Bytecodes::_imul: set_constant(a * b); return;
    1.85 +            case Bytecodes::_idiv:
    1.86 +              if (b != 0) {
    1.87 +                if (a == min_jint && b == -1) {
    1.88 +                  set_constant(min_jint);
    1.89 +                } else {
    1.90 +                  set_constant(a / b);
    1.91 +                }
    1.92 +                return;
    1.93 +              }
    1.94 +              break;
    1.95 +            case Bytecodes::_irem:
    1.96 +              if (b != 0) {
    1.97 +                if (a == min_jint && b == -1) {
    1.98 +                  set_constant(0);
    1.99 +                } else {
   1.100 +                  set_constant(a % b);
   1.101 +                }
   1.102 +                return;
   1.103 +              }
   1.104 +              break;
   1.105 +            case Bytecodes::_iand: set_constant(a & b); return;
   1.106 +            case Bytecodes::_ior : set_constant(a | b); return;
   1.107 +            case Bytecodes::_ixor: set_constant(a ^ b); return;
   1.108 +          }
   1.109 +        }
   1.110 +        break;
   1.111 +      case longTag:
   1.112 +        { jlong a = x->x()->type()->as_LongConstant()->value();
   1.113 +          jlong b = x->y()->type()->as_LongConstant()->value();
   1.114 +          switch (x->op()) {
   1.115 +            case Bytecodes::_ladd: set_constant(a + b); return;
   1.116 +            case Bytecodes::_lsub: set_constant(a - b); return;
   1.117 +            case Bytecodes::_lmul: set_constant(a * b); return;
   1.118 +            case Bytecodes::_ldiv:
   1.119 +              if (b != 0) {
   1.120 +                set_constant(SharedRuntime::ldiv(b, a));
   1.121 +                return;
   1.122 +              }
   1.123 +              break;
   1.124 +            case Bytecodes::_lrem:
   1.125 +              if (b != 0) {
   1.126 +                set_constant(SharedRuntime::lrem(b, a));
   1.127 +                return;
   1.128 +              }
   1.129 +              break;
   1.130 +            case Bytecodes::_land: set_constant(a & b); return;
   1.131 +            case Bytecodes::_lor : set_constant(a | b); return;
   1.132 +            case Bytecodes::_lxor: set_constant(a ^ b); return;
   1.133 +          }
   1.134 +        }
   1.135 +        break;
   1.136 +      // other cases not implemented (must be extremely careful with floats & doubles!)
   1.137 +    }
   1.138 +  }
   1.139 +  // make sure constant is on the right side, if any
   1.140 +  move_const_to_right(x);
   1.141 +
   1.142 +  if (x->y()->type()->is_constant()) {
   1.143 +    // do constant folding for selected operations
   1.144 +    switch (x->type()->tag()) {
   1.145 +      case intTag:
   1.146 +        if (x->y()->type()->as_IntConstant()->value() == 0) {
   1.147 +          switch (x->op()) {
   1.148 +            case Bytecodes::_iadd: set_canonical(x->x()); return;
   1.149 +            case Bytecodes::_isub: set_canonical(x->x()); return;
   1.150 +            case Bytecodes::_imul: set_constant(0); return;
   1.151 +              // Note: for div and rem, make sure that C semantics
   1.152 +              //       corresponds to Java semantics!
   1.153 +            case Bytecodes::_iand: set_constant(0); return;
   1.154 +            case Bytecodes::_ior : set_canonical(x->x()); return;
   1.155 +          }
   1.156 +        }
   1.157 +        break;
   1.158 +      case longTag:
   1.159 +        if (x->y()->type()->as_LongConstant()->value() == (jlong)0) {
   1.160 +          switch (x->op()) {
   1.161 +            case Bytecodes::_ladd: set_canonical(x->x()); return;
   1.162 +            case Bytecodes::_lsub: set_canonical(x->x()); return;
   1.163 +            case Bytecodes::_lmul: set_constant((jlong)0); return;
   1.164 +              // Note: for div and rem, make sure that C semantics
   1.165 +              //       corresponds to Java semantics!
   1.166 +            case Bytecodes::_land: set_constant((jlong)0); return;
   1.167 +            case Bytecodes::_lor : set_canonical(x->x()); return;
   1.168 +          }
   1.169 +        }
   1.170 +        break;
   1.171 +    }
   1.172 +  }
   1.173 +}
   1.174 +
   1.175 +
   1.176 +void Canonicalizer::do_Phi            (Phi*             x) {}
   1.177 +void Canonicalizer::do_Constant       (Constant*        x) {}
   1.178 +void Canonicalizer::do_Local          (Local*           x) {}
   1.179 +void Canonicalizer::do_LoadField      (LoadField*       x) {}
   1.180 +
   1.181 +// checks if v is in the block that is currently processed by
   1.182 +// GraphBuilder. This is the only block that has not BlockEnd yet.
   1.183 +static bool in_current_block(Value v) {
   1.184 +  int max_distance = 4;
   1.185 +  while (max_distance > 0 && v != NULL && v->as_BlockEnd() == NULL) {
   1.186 +    v = v->next();
   1.187 +    max_distance--;
   1.188 +  }
   1.189 +  return v == NULL;
   1.190 +}
   1.191 +
   1.192 +void Canonicalizer::do_StoreField     (StoreField*      x) {
   1.193 +  // If a value is going to be stored into a field or array some of
   1.194 +  // the conversions emitted by javac are unneeded because the fields
   1.195 +  // are packed to their natural size.
   1.196 +  Convert* conv = x->value()->as_Convert();
   1.197 +  if (conv) {
   1.198 +    Value value = NULL;
   1.199 +    BasicType type = x->field()->type()->basic_type();
   1.200 +    switch (conv->op()) {
   1.201 +    case Bytecodes::_i2b: if (type == T_BYTE)  value = conv->value(); break;
   1.202 +    case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) value = conv->value(); break;
   1.203 +    case Bytecodes::_i2c: if (type == T_CHAR  || type == T_BYTE)  value = conv->value(); break;
   1.204 +    }
   1.205 +    // limit this optimization to current block
   1.206 +    if (value != NULL && in_current_block(conv)) {
   1.207 +      set_canonical(new StoreField(x->obj(), x->offset(), x->field(), value, x->is_static(),
   1.208 +                                   x->lock_stack(), x->state_before(), x->is_loaded(), x->is_initialized()));
   1.209 +      return;
   1.210 +    }
   1.211 +  }
   1.212 +
   1.213 +}
   1.214 +
   1.215 +void Canonicalizer::do_ArrayLength    (ArrayLength*     x) {
   1.216 +  NewArray* array = x->array()->as_NewArray();
   1.217 +  if (array != NULL && array->length() != NULL) {
   1.218 +    Constant* length = array->length()->as_Constant();
   1.219 +    if (length != NULL) {
   1.220 +      // do not use the Constant itself, but create a new Constant
   1.221 +      // with same value Otherwise a Constant is live over multiple
   1.222 +      // blocks without being registered in a state array.
   1.223 +      assert(length->type()->as_IntConstant() != NULL, "array length must be integer");
   1.224 +      set_constant(length->type()->as_IntConstant()->value());
   1.225 +    }
   1.226 +  } else {
   1.227 +    LoadField* lf = x->array()->as_LoadField();
   1.228 +    if (lf != NULL && lf->field()->is_constant()) {
   1.229 +      ciObject* c = lf->field()->constant_value().as_object();
   1.230 +      if (c->is_array()) {
   1.231 +        ciArray* array = (ciArray*) c;
   1.232 +        set_constant(array->length());
   1.233 +      }
   1.234 +    }
   1.235 +  }
   1.236 +}
   1.237 +
   1.238 +void Canonicalizer::do_LoadIndexed    (LoadIndexed*     x) {}
   1.239 +void Canonicalizer::do_StoreIndexed   (StoreIndexed*    x) {
   1.240 +  // If a value is going to be stored into a field or array some of
   1.241 +  // the conversions emitted by javac are unneeded because the fields
   1.242 +  // are packed to their natural size.
   1.243 +  Convert* conv = x->value()->as_Convert();
   1.244 +  if (conv) {
   1.245 +    Value value = NULL;
   1.246 +    BasicType type = x->elt_type();
   1.247 +    switch (conv->op()) {
   1.248 +    case Bytecodes::_i2b: if (type == T_BYTE)  value = conv->value(); break;
   1.249 +    case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) value = conv->value(); break;
   1.250 +    case Bytecodes::_i2c: if (type == T_CHAR  || type == T_BYTE) value = conv->value(); break;
   1.251 +    }
   1.252 +    // limit this optimization to current block
   1.253 +    if (value != NULL && in_current_block(conv)) {
   1.254 +      set_canonical(new StoreIndexed(x->array(), x->index(), x->length(),
   1.255 +                                     x->elt_type(), value, x->lock_stack()));
   1.256 +      return;
   1.257 +    }
   1.258 +  }
   1.259 +
   1.260 +
   1.261 +}
   1.262 +
   1.263 +
   1.264 +void Canonicalizer::do_NegateOp(NegateOp* x) {
   1.265 +  ValueType* t = x->x()->type();
   1.266 +  if (t->is_constant()) {
   1.267 +    switch (t->tag()) {
   1.268 +      case intTag   : set_constant(-t->as_IntConstant   ()->value()); return;
   1.269 +      case longTag  : set_constant(-t->as_LongConstant  ()->value()); return;
   1.270 +      case floatTag : set_constant(-t->as_FloatConstant ()->value()); return;
   1.271 +      case doubleTag: set_constant(-t->as_DoubleConstant()->value()); return;
   1.272 +      default       : ShouldNotReachHere();
   1.273 +    }
   1.274 +  }
   1.275 +}
   1.276 +
   1.277 +
   1.278 +void Canonicalizer::do_ArithmeticOp   (ArithmeticOp*    x) { do_Op2(x); }
   1.279 +
   1.280 +
   1.281 +void Canonicalizer::do_ShiftOp        (ShiftOp*         x) {
   1.282 +  ValueType* t = x->x()->type();
   1.283 +  ValueType* t2 = x->y()->type();
   1.284 +  if (t->is_constant()) {
   1.285 +    switch (t->tag()) {
   1.286 +    case intTag   : if (t->as_IntConstant()->value() == 0)         { set_constant(0); return; } break;
   1.287 +    case longTag  : if (t->as_LongConstant()->value() == (jlong)0) { set_constant(jlong_cast(0)); return; } break;
   1.288 +    default       : ShouldNotReachHere();
   1.289 +    }
   1.290 +    if (t2->is_constant()) {
   1.291 +      if (t->tag() == intTag) {
   1.292 +        int value = t->as_IntConstant()->value();
   1.293 +        int shift = t2->as_IntConstant()->value() & 31;
   1.294 +        jint mask = ~(~0 << (32 - shift));
   1.295 +        if (shift == 0) mask = ~0;
   1.296 +        switch (x->op()) {
   1.297 +          case Bytecodes::_ishl:  set_constant(value << shift); return;
   1.298 +          case Bytecodes::_ishr:  set_constant(value >> shift); return;
   1.299 +          case Bytecodes::_iushr: set_constant((value >> shift) & mask); return;
   1.300 +        }
   1.301 +      } else if (t->tag() == longTag) {
   1.302 +        jlong value = t->as_LongConstant()->value();
   1.303 +        int shift = t2->as_IntConstant()->value() & 63;
   1.304 +        jlong mask = ~(~jlong_cast(0) << (64 - shift));
   1.305 +        if (shift == 0) mask = ~jlong_cast(0);
   1.306 +        switch (x->op()) {
   1.307 +          case Bytecodes::_lshl:  set_constant(value << shift); return;
   1.308 +          case Bytecodes::_lshr:  set_constant(value >> shift); return;
   1.309 +          case Bytecodes::_lushr: set_constant((value >> shift) & mask); return;
   1.310 +        }
   1.311 +      }
   1.312 +    }
   1.313 +  }
   1.314 +  if (t2->is_constant()) {
   1.315 +    switch (t2->tag()) {
   1.316 +      case intTag   : if (t2->as_IntConstant()->value() == 0)  set_canonical(x->x()); return;
   1.317 +      case longTag  : if (t2->as_IntConstant()->value() == 0)  set_canonical(x->x()); return;
   1.318 +      default       : ShouldNotReachHere();
   1.319 +    }
   1.320 +  }
   1.321 +}
   1.322 +
   1.323 +
   1.324 +void Canonicalizer::do_LogicOp        (LogicOp*         x) { do_Op2(x); }
   1.325 +void Canonicalizer::do_CompareOp      (CompareOp*       x) {
   1.326 +  if (x->x() == x->y()) {
   1.327 +    switch (x->x()->type()->tag()) {
   1.328 +      case longTag: set_constant(0); break;
   1.329 +      case floatTag: {
   1.330 +        FloatConstant* fc = x->x()->type()->as_FloatConstant();
   1.331 +        if (fc) {
   1.332 +          if (g_isnan(fc->value())) {
   1.333 +            set_constant(x->op() == Bytecodes::_fcmpl ? -1 : 1);
   1.334 +          } else {
   1.335 +            set_constant(0);
   1.336 +          }
   1.337 +        }
   1.338 +        break;
   1.339 +      }
   1.340 +      case doubleTag: {
   1.341 +        DoubleConstant* dc = x->x()->type()->as_DoubleConstant();
   1.342 +        if (dc) {
   1.343 +          if (g_isnan(dc->value())) {
   1.344 +            set_constant(x->op() == Bytecodes::_dcmpl ? -1 : 1);
   1.345 +          } else {
   1.346 +            set_constant(0);
   1.347 +          }
   1.348 +        }
   1.349 +        break;
   1.350 +      }
   1.351 +    }
   1.352 +  } else if (x->x()->type()->is_constant() && x->y()->type()->is_constant()) {
   1.353 +    switch (x->x()->type()->tag()) {
   1.354 +      case longTag: {
   1.355 +        jlong vx = x->x()->type()->as_LongConstant()->value();
   1.356 +        jlong vy = x->y()->type()->as_LongConstant()->value();
   1.357 +        if (vx == vy)
   1.358 +          set_constant(0);
   1.359 +        else if (vx < vy)
   1.360 +          set_constant(-1);
   1.361 +        else
   1.362 +          set_constant(1);
   1.363 +        break;
   1.364 +      }
   1.365 +
   1.366 +      case floatTag: {
   1.367 +        float vx = x->x()->type()->as_FloatConstant()->value();
   1.368 +        float vy = x->y()->type()->as_FloatConstant()->value();
   1.369 +        if (g_isnan(vx) || g_isnan(vy))
   1.370 +          set_constant(x->op() == Bytecodes::_fcmpl ? -1 : 1);
   1.371 +        else if (vx == vy)
   1.372 +          set_constant(0);
   1.373 +        else if (vx < vy)
   1.374 +          set_constant(-1);
   1.375 +        else
   1.376 +          set_constant(1);
   1.377 +        break;
   1.378 +      }
   1.379 +
   1.380 +      case doubleTag: {
   1.381 +        double vx = x->x()->type()->as_DoubleConstant()->value();
   1.382 +        double vy = x->y()->type()->as_DoubleConstant()->value();
   1.383 +        if (g_isnan(vx) || g_isnan(vy))
   1.384 +          set_constant(x->op() == Bytecodes::_dcmpl ? -1 : 1);
   1.385 +        else if (vx == vy)
   1.386 +          set_constant(0);
   1.387 +        else if (vx < vy)
   1.388 +          set_constant(-1);
   1.389 +        else
   1.390 +          set_constant(1);
   1.391 +        break;
   1.392 +      }
   1.393 +    }
   1.394 +
   1.395 +  }
   1.396 +}
   1.397 +
   1.398 +
   1.399 +void Canonicalizer::do_IfInstanceOf(IfInstanceOf*    x) {}
   1.400 +
   1.401 +void Canonicalizer::do_IfOp(IfOp* x) {
   1.402 +  // Caution: do not use do_Op2(x) here for now since
   1.403 +  //          we map the condition to the op for now!
   1.404 +  move_const_to_right(x);
   1.405 +}
   1.406 +
   1.407 +
   1.408 +void Canonicalizer::do_Intrinsic      (Intrinsic*       x) {
   1.409 +  switch (x->id()) {
   1.410 +  case vmIntrinsics::_floatToRawIntBits   : {
   1.411 +    FloatConstant* c = x->argument_at(0)->type()->as_FloatConstant();
   1.412 +    if (c != NULL) {
   1.413 +      JavaValue v;
   1.414 +      v.set_jfloat(c->value());
   1.415 +      set_constant(v.get_jint());
   1.416 +    }
   1.417 +    break;
   1.418 +  }
   1.419 +  case vmIntrinsics::_intBitsToFloat      : {
   1.420 +    IntConstant* c = x->argument_at(0)->type()->as_IntConstant();
   1.421 +    if (c != NULL) {
   1.422 +      JavaValue v;
   1.423 +      v.set_jint(c->value());
   1.424 +      set_constant(v.get_jfloat());
   1.425 +    }
   1.426 +    break;
   1.427 +  }
   1.428 +  case vmIntrinsics::_doubleToRawLongBits : {
   1.429 +    DoubleConstant* c = x->argument_at(0)->type()->as_DoubleConstant();
   1.430 +    if (c != NULL) {
   1.431 +      JavaValue v;
   1.432 +      v.set_jdouble(c->value());
   1.433 +      set_constant(v.get_jlong());
   1.434 +    }
   1.435 +    break;
   1.436 +  }
   1.437 +  case vmIntrinsics::_longBitsToDouble    : {
   1.438 +    LongConstant* c = x->argument_at(0)->type()->as_LongConstant();
   1.439 +    if (c != NULL) {
   1.440 +      JavaValue v;
   1.441 +      v.set_jlong(c->value());
   1.442 +      set_constant(v.get_jdouble());
   1.443 +    }
   1.444 +    break;
   1.445 +  }
   1.446 +  }
   1.447 +}
   1.448 +
   1.449 +void Canonicalizer::do_Convert        (Convert*         x) {
   1.450 +  if (x->value()->type()->is_constant()) {
   1.451 +    switch (x->op()) {
   1.452 +    case Bytecodes::_i2b:  set_constant((int)((x->value()->type()->as_IntConstant()->value() << 24) >> 24)); break;
   1.453 +    case Bytecodes::_i2s:  set_constant((int)((x->value()->type()->as_IntConstant()->value() << 16) >> 16)); break;
   1.454 +    case Bytecodes::_i2c:  set_constant((int)(x->value()->type()->as_IntConstant()->value() & ((1<<16)-1))); break;
   1.455 +    case Bytecodes::_i2l:  set_constant((jlong)(x->value()->type()->as_IntConstant()->value()));             break;
   1.456 +    case Bytecodes::_i2f:  set_constant((float)(x->value()->type()->as_IntConstant()->value()));             break;
   1.457 +    case Bytecodes::_i2d:  set_constant((double)(x->value()->type()->as_IntConstant()->value()));            break;
   1.458 +    case Bytecodes::_l2i:  set_constant((int)(x->value()->type()->as_LongConstant()->value()));              break;
   1.459 +    case Bytecodes::_l2f:  set_constant(SharedRuntime::l2f(x->value()->type()->as_LongConstant()->value())); break;
   1.460 +    case Bytecodes::_l2d:  set_constant(SharedRuntime::l2d(x->value()->type()->as_LongConstant()->value())); break;
   1.461 +    case Bytecodes::_f2d:  set_constant((double)(x->value()->type()->as_FloatConstant()->value()));          break;
   1.462 +    case Bytecodes::_f2i:  set_constant(SharedRuntime::f2i(x->value()->type()->as_FloatConstant()->value())); break;
   1.463 +    case Bytecodes::_f2l:  set_constant(SharedRuntime::f2l(x->value()->type()->as_FloatConstant()->value())); break;
   1.464 +    case Bytecodes::_d2f:  set_constant((float)(x->value()->type()->as_DoubleConstant()->value()));          break;
   1.465 +    case Bytecodes::_d2i:  set_constant(SharedRuntime::d2i(x->value()->type()->as_DoubleConstant()->value())); break;
   1.466 +    case Bytecodes::_d2l:  set_constant(SharedRuntime::d2l(x->value()->type()->as_DoubleConstant()->value())); break;
   1.467 +    default:
   1.468 +      ShouldNotReachHere();
   1.469 +    }
   1.470 +  }
   1.471 +
   1.472 +  Value value = x->value();
   1.473 +  BasicType type = T_ILLEGAL;
   1.474 +  LoadField* lf = value->as_LoadField();
   1.475 +  if (lf) {
   1.476 +    type = lf->field_type();
   1.477 +  } else {
   1.478 +    LoadIndexed* li = value->as_LoadIndexed();
   1.479 +    if (li) {
   1.480 +      type = li->elt_type();
   1.481 +    } else {
   1.482 +      Convert* conv = value->as_Convert();
   1.483 +      if (conv) {
   1.484 +        switch (conv->op()) {
   1.485 +          case Bytecodes::_i2b: type = T_BYTE;  break;
   1.486 +          case Bytecodes::_i2s: type = T_SHORT; break;
   1.487 +          case Bytecodes::_i2c: type = T_CHAR;  break;
   1.488 +        }
   1.489 +      }
   1.490 +    }
   1.491 +  }
   1.492 +  if (type != T_ILLEGAL) {
   1.493 +    switch (x->op()) {
   1.494 +      case Bytecodes::_i2b: if (type == T_BYTE)                    set_canonical(x->value()); break;
   1.495 +      case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) set_canonical(x->value()); break;
   1.496 +      case Bytecodes::_i2c: if (type == T_CHAR)                    set_canonical(x->value()); break;
   1.497 +    }
   1.498 +  } else {
   1.499 +    Op2* op2 = x->value()->as_Op2();
   1.500 +    if (op2 && op2->op() == Bytecodes::_iand && op2->y()->type()->is_constant()) {
   1.501 +      jint safebits = 0;
   1.502 +      jint mask = op2->y()->type()->as_IntConstant()->value();
   1.503 +      switch (x->op()) {
   1.504 +        case Bytecodes::_i2b: safebits = 0x7f;   break;
   1.505 +        case Bytecodes::_i2s: safebits = 0x7fff; break;
   1.506 +        case Bytecodes::_i2c: safebits = 0xffff; break;
   1.507 +      }
   1.508 +      // When casting a masked integer to a smaller signed type, if
   1.509 +      // the mask doesn't include the sign bit the cast isn't needed.
   1.510 +      if (safebits && (mask & ~safebits) == 0) {
   1.511 +        set_canonical(x->value());
   1.512 +      }
   1.513 +    }
   1.514 +  }
   1.515 +
   1.516 +}
   1.517 +
   1.518 +void Canonicalizer::do_NullCheck      (NullCheck*       x) {
   1.519 +  if (x->obj()->as_NewArray() != NULL || x->obj()->as_NewInstance() != NULL) {
   1.520 +    set_canonical(x->obj());
   1.521 +  } else {
   1.522 +    Constant* con = x->obj()->as_Constant();
   1.523 +    if (con) {
   1.524 +      ObjectType* c = con->type()->as_ObjectType();
   1.525 +      if (c && c->is_loaded()) {
   1.526 +        ObjectConstant* oc = c->as_ObjectConstant();
   1.527 +        if (!oc || !oc->value()->is_null_object()) {
   1.528 +          set_canonical(con);
   1.529 +        }
   1.530 +      }
   1.531 +    }
   1.532 +  }
   1.533 +}
   1.534 +
   1.535 +void Canonicalizer::do_Invoke         (Invoke*          x) {}
   1.536 +void Canonicalizer::do_NewInstance    (NewInstance*     x) {}
   1.537 +void Canonicalizer::do_NewTypeArray   (NewTypeArray*    x) {}
   1.538 +void Canonicalizer::do_NewObjectArray (NewObjectArray*  x) {}
   1.539 +void Canonicalizer::do_NewMultiArray  (NewMultiArray*   x) {}
   1.540 +void Canonicalizer::do_CheckCast      (CheckCast*       x) {
   1.541 +  if (x->klass()->is_loaded()) {
   1.542 +    Value obj = x->obj();
   1.543 +    ciType* klass = obj->exact_type();
   1.544 +    if (klass == NULL) klass = obj->declared_type();
   1.545 +    if (klass != NULL && klass->is_loaded() && klass->is_subtype_of(x->klass())) {
   1.546 +      set_canonical(obj);
   1.547 +      return;
   1.548 +    }
   1.549 +    // checkcast of null returns null
   1.550 +    if (obj->as_Constant() && obj->type()->as_ObjectType()->constant_value()->is_null_object()) {
   1.551 +      set_canonical(obj);
   1.552 +    }
   1.553 +  }
   1.554 +}
   1.555 +void Canonicalizer::do_InstanceOf     (InstanceOf*      x) {
   1.556 +  if (x->klass()->is_loaded()) {
   1.557 +    Value obj = x->obj();
   1.558 +    ciType* exact = obj->exact_type();
   1.559 +    if (exact != NULL && exact->is_loaded() && (obj->as_NewInstance() || obj->as_NewArray())) {
   1.560 +      set_constant(exact->is_subtype_of(x->klass()) ? 1 : 0);
   1.561 +      return;
   1.562 +    }
   1.563 +    // instanceof null returns false
   1.564 +    if (obj->as_Constant() && obj->type()->as_ObjectType()->constant_value()->is_null_object()) {
   1.565 +      set_constant(0);
   1.566 +    }
   1.567 +  }
   1.568 +
   1.569 +}
   1.570 +void Canonicalizer::do_MonitorEnter   (MonitorEnter*    x) {}
   1.571 +void Canonicalizer::do_MonitorExit    (MonitorExit*     x) {}
   1.572 +void Canonicalizer::do_BlockBegin     (BlockBegin*      x) {}
   1.573 +void Canonicalizer::do_Goto           (Goto*            x) {}
   1.574 +
   1.575 +
   1.576 +static bool is_true(jlong x, If::Condition cond, jlong y) {
   1.577 +  switch (cond) {
   1.578 +    case If::eql: return x == y;
   1.579 +    case If::neq: return x != y;
   1.580 +    case If::lss: return x <  y;
   1.581 +    case If::leq: return x <= y;
   1.582 +    case If::gtr: return x >  y;
   1.583 +    case If::geq: return x >= y;
   1.584 +  }
   1.585 +  ShouldNotReachHere();
   1.586 +  return false;
   1.587 +}
   1.588 +
   1.589 +
   1.590 +void Canonicalizer::do_If(If* x) {
   1.591 +  // move const to right
   1.592 +  if (x->x()->type()->is_constant()) x->swap_operands();
   1.593 +  // simplify
   1.594 +  const Value l = x->x(); ValueType* lt = l->type();
   1.595 +  const Value r = x->y(); ValueType* rt = r->type();
   1.596 +
   1.597 +  if (l == r && !lt->is_float_kind()) {
   1.598 +    // pattern: If (a cond a) => simplify to Goto
   1.599 +    BlockBegin* sux;
   1.600 +    switch (x->cond()) {
   1.601 +    case If::eql: sux = x->sux_for(true);  break;
   1.602 +    case If::neq: sux = x->sux_for(false); break;
   1.603 +    case If::lss: sux = x->sux_for(false); break;
   1.604 +    case If::leq: sux = x->sux_for(true);  break;
   1.605 +    case If::gtr: sux = x->sux_for(false); break;
   1.606 +    case If::geq: sux = x->sux_for(true);  break;
   1.607 +    }
   1.608 +    // If is a safepoint then the debug information should come from the state_before of the If.
   1.609 +    set_canonical(new Goto(sux, x->state_before(), x->is_safepoint()));
   1.610 +    return;
   1.611 +  }
   1.612 +
   1.613 +  if (lt->is_constant() && rt->is_constant()) {
   1.614 +    if (x->x()->as_Constant() != NULL) {
   1.615 +      // pattern: If (lc cond rc) => simplify to: Goto
   1.616 +      BlockBegin* sux = x->x()->as_Constant()->compare(x->cond(), x->y(),
   1.617 +                                                       x->sux_for(true),
   1.618 +                                                       x->sux_for(false));
   1.619 +      if (sux != NULL) {
   1.620 +        // If is a safepoint then the debug information should come from the state_before of the If.
   1.621 +        set_canonical(new Goto(sux, x->state_before(), x->is_safepoint()));
   1.622 +      }
   1.623 +    }
   1.624 +  } else if (rt->as_IntConstant() != NULL) {
   1.625 +    // pattern: If (l cond rc) => investigate further
   1.626 +    const jint rc = rt->as_IntConstant()->value();
   1.627 +    if (l->as_CompareOp() != NULL) {
   1.628 +      // pattern: If ((a cmp b) cond rc) => simplify to: If (x cond y) or: Goto
   1.629 +      CompareOp* cmp = l->as_CompareOp();
   1.630 +      bool unordered_is_less = cmp->op() == Bytecodes::_fcmpl || cmp->op() == Bytecodes::_dcmpl;
   1.631 +      BlockBegin* lss_sux = x->sux_for(is_true(-1, x->cond(), rc)); // successor for a < b
   1.632 +      BlockBegin* eql_sux = x->sux_for(is_true( 0, x->cond(), rc)); // successor for a = b
   1.633 +      BlockBegin* gtr_sux = x->sux_for(is_true(+1, x->cond(), rc)); // successor for a > b
   1.634 +      BlockBegin* nan_sux = unordered_is_less ? lss_sux : gtr_sux ; // successor for unordered
   1.635 +      // Note: At this point all successors (lss_sux, eql_sux, gtr_sux, nan_sux) are
   1.636 +      //       equal to x->tsux() or x->fsux(). Furthermore, nan_sux equals either
   1.637 +      //       lss_sux or gtr_sux.
   1.638 +      if (lss_sux == eql_sux && eql_sux == gtr_sux) {
   1.639 +        // all successors identical => simplify to: Goto
   1.640 +        set_canonical(new Goto(lss_sux, x->state_before(), x->is_safepoint()));
   1.641 +      } else {
   1.642 +        // two successors differ and two successors are the same => simplify to: If (x cmp y)
   1.643 +        // determine new condition & successors
   1.644 +        If::Condition cond;
   1.645 +        BlockBegin* tsux = NULL;
   1.646 +        BlockBegin* fsux = NULL;
   1.647 +             if (lss_sux == eql_sux) { cond = If::leq; tsux = lss_sux; fsux = gtr_sux; }
   1.648 +        else if (lss_sux == gtr_sux) { cond = If::neq; tsux = lss_sux; fsux = eql_sux; }
   1.649 +        else if (eql_sux == gtr_sux) { cond = If::geq; tsux = eql_sux; fsux = lss_sux; }
   1.650 +        else                         { ShouldNotReachHere();                           }
   1.651 +           If* canon = new If(cmp->x(), cond, nan_sux == tsux, cmp->y(), tsux, fsux, cmp->state_before(), x->is_safepoint());
   1.652 +        if (cmp->x() == cmp->y()) {
   1.653 +          do_If(canon);
   1.654 +        } else {
   1.655 +          set_canonical(canon);
   1.656 +          set_bci(cmp->bci());
   1.657 +        }
   1.658 +      }
   1.659 +    } else if (l->as_InstanceOf() != NULL) {
   1.660 +      // NOTE: Code permanently disabled for now since it leaves the old InstanceOf
   1.661 +      //       instruction in the graph (it is pinned). Need to fix this at some point.
   1.662 +      return;
   1.663 +      // pattern: If ((obj instanceof klass) cond rc) => simplify to: IfInstanceOf or: Goto
   1.664 +      InstanceOf* inst = l->as_InstanceOf();
   1.665 +      BlockBegin* is_inst_sux = x->sux_for(is_true(1, x->cond(), rc)); // successor for instanceof == 1
   1.666 +      BlockBegin* no_inst_sux = x->sux_for(is_true(0, x->cond(), rc)); // successor for instanceof == 0
   1.667 +      if (is_inst_sux == no_inst_sux && inst->is_loaded()) {
   1.668 +        // both successors identical and klass is loaded => simplify to: Goto
   1.669 +        set_canonical(new Goto(is_inst_sux, x->state_before(), x->is_safepoint()));
   1.670 +      } else {
   1.671 +        // successors differ => simplify to: IfInstanceOf
   1.672 +        set_canonical(new IfInstanceOf(inst->klass(), inst->obj(), true, inst->bci(), is_inst_sux, no_inst_sux));
   1.673 +      }
   1.674 +    }
   1.675 +  } else if (rt == objectNull && (l->as_NewInstance() || l->as_NewArray())) {
   1.676 +    if (x->cond() == Instruction::eql) {
   1.677 +      set_canonical(new Goto(x->fsux(), x->state_before(), x->is_safepoint()));
   1.678 +    } else {
   1.679 +      assert(x->cond() == Instruction::neq, "only other valid case");
   1.680 +      set_canonical(new Goto(x->tsux(), x->state_before(), x->is_safepoint()));
   1.681 +    }
   1.682 +  }
   1.683 +}
   1.684 +
   1.685 +
   1.686 +void Canonicalizer::do_TableSwitch(TableSwitch* x) {
   1.687 +  if (x->tag()->type()->is_constant()) {
   1.688 +    int v = x->tag()->type()->as_IntConstant()->value();
   1.689 +    BlockBegin* sux = x->default_sux();
   1.690 +    if (v >= x->lo_key() && v <= x->hi_key()) {
   1.691 +      sux = x->sux_at(v - x->lo_key());
   1.692 +    }
   1.693 +    set_canonical(new Goto(sux, x->state_before(), x->is_safepoint()));
   1.694 +  } else if (x->number_of_sux() == 1) {
   1.695 +    // NOTE: Code permanently disabled for now since the switch statement's
   1.696 +    //       tag expression may produce side-effects in which case it must
   1.697 +    //       be executed.
   1.698 +    return;
   1.699 +    // simplify to Goto
   1.700 +    set_canonical(new Goto(x->default_sux(), x->state_before(), x->is_safepoint()));
   1.701 +  } else if (x->number_of_sux() == 2) {
   1.702 +    // NOTE: Code permanently disabled for now since it produces two new nodes
   1.703 +    //       (Constant & If) and the Canonicalizer cannot return them correctly
   1.704 +    //       yet. For now we copied the corresponding code directly into the
   1.705 +    //       GraphBuilder (i.e., we should never reach here).
   1.706 +    return;
   1.707 +    // simplify to If
   1.708 +    assert(x->lo_key() == x->hi_key(), "keys must be the same");
   1.709 +    Constant* key = new Constant(new IntConstant(x->lo_key()));
   1.710 +    set_canonical(new If(x->tag(), If::eql, true, key, x->sux_at(0), x->default_sux(), x->state_before(), x->is_safepoint()));
   1.711 +  }
   1.712 +}
   1.713 +
   1.714 +
   1.715 +void Canonicalizer::do_LookupSwitch(LookupSwitch* x) {
   1.716 +  if (x->tag()->type()->is_constant()) {
   1.717 +    int v = x->tag()->type()->as_IntConstant()->value();
   1.718 +    BlockBegin* sux = x->default_sux();
   1.719 +    for (int i = 0; i < x->length(); i++) {
   1.720 +      if (v == x->key_at(i)) {
   1.721 +        sux = x->sux_at(i);
   1.722 +      }
   1.723 +    }
   1.724 +    set_canonical(new Goto(sux, x->state_before(), x->is_safepoint()));
   1.725 +  } else if (x->number_of_sux() == 1) {
   1.726 +    // NOTE: Code permanently disabled for now since the switch statement's
   1.727 +    //       tag expression may produce side-effects in which case it must
   1.728 +    //       be executed.
   1.729 +    return;
   1.730 +    // simplify to Goto
   1.731 +    set_canonical(new Goto(x->default_sux(), x->state_before(), x->is_safepoint()));
   1.732 +  } else if (x->number_of_sux() == 2) {
   1.733 +    // NOTE: Code permanently disabled for now since it produces two new nodes
   1.734 +    //       (Constant & If) and the Canonicalizer cannot return them correctly
   1.735 +    //       yet. For now we copied the corresponding code directly into the
   1.736 +    //       GraphBuilder (i.e., we should never reach here).
   1.737 +    return;
   1.738 +    // simplify to If
   1.739 +    assert(x->length() == 1, "length must be the same");
   1.740 +    Constant* key = new Constant(new IntConstant(x->key_at(0)));
   1.741 +    set_canonical(new If(x->tag(), If::eql, true, key, x->sux_at(0), x->default_sux(), x->state_before(), x->is_safepoint()));
   1.742 +  }
   1.743 +}
   1.744 +
   1.745 +
   1.746 +void Canonicalizer::do_Return         (Return*          x) {}
   1.747 +void Canonicalizer::do_Throw          (Throw*           x) {}
   1.748 +void Canonicalizer::do_Base           (Base*            x) {}
   1.749 +void Canonicalizer::do_OsrEntry       (OsrEntry*        x) {}
   1.750 +void Canonicalizer::do_ExceptionObject(ExceptionObject* x) {}
   1.751 +
   1.752 +static bool match_index_and_scale(Instruction*  instr,
   1.753 +                                  Instruction** index,
   1.754 +                                  int*          log2_scale,
   1.755 +                                  Instruction** instr_to_unpin) {
   1.756 +  *instr_to_unpin = NULL;
   1.757 +
   1.758 +  // Skip conversion ops
   1.759 +  Convert* convert = instr->as_Convert();
   1.760 +  if (convert != NULL) {
   1.761 +    instr = convert->value();
   1.762 +  }
   1.763 +
   1.764 +  ShiftOp* shift = instr->as_ShiftOp();
   1.765 +  if (shift != NULL) {
   1.766 +    if (shift->is_pinned()) {
   1.767 +      *instr_to_unpin = shift;
   1.768 +    }
   1.769 +    // Constant shift value?
   1.770 +    Constant* con = shift->y()->as_Constant();
   1.771 +    if (con == NULL) return false;
   1.772 +    // Well-known type and value?
   1.773 +    IntConstant* val = con->type()->as_IntConstant();
   1.774 +    if (val == NULL) return false;
   1.775 +    if (shift->x()->type() != intType) return false;
   1.776 +    *index = shift->x();
   1.777 +    int tmp_scale = val->value();
   1.778 +    if (tmp_scale >= 0 && tmp_scale < 4) {
   1.779 +      *log2_scale = tmp_scale;
   1.780 +      return true;
   1.781 +    } else {
   1.782 +      return false;
   1.783 +    }
   1.784 +  }
   1.785 +
   1.786 +  ArithmeticOp* arith = instr->as_ArithmeticOp();
   1.787 +  if (arith != NULL) {
   1.788 +    if (arith->is_pinned()) {
   1.789 +      *instr_to_unpin = arith;
   1.790 +    }
   1.791 +    // Check for integer multiply
   1.792 +    if (arith->op() == Bytecodes::_imul) {
   1.793 +      // See if either arg is a known constant
   1.794 +      Constant* con = arith->x()->as_Constant();
   1.795 +      if (con != NULL) {
   1.796 +        *index = arith->y();
   1.797 +      } else {
   1.798 +        con = arith->y()->as_Constant();
   1.799 +        if (con == NULL) return false;
   1.800 +        *index = arith->x();
   1.801 +      }
   1.802 +      if ((*index)->type() != intType) return false;
   1.803 +      // Well-known type and value?
   1.804 +      IntConstant* val = con->type()->as_IntConstant();
   1.805 +      if (val == NULL) return false;
   1.806 +      switch (val->value()) {
   1.807 +      case 1: *log2_scale = 0; return true;
   1.808 +      case 2: *log2_scale = 1; return true;
   1.809 +      case 4: *log2_scale = 2; return true;
   1.810 +      case 8: *log2_scale = 3; return true;
   1.811 +      default:            return false;
   1.812 +      }
   1.813 +    }
   1.814 +  }
   1.815 +
   1.816 +  // Unknown instruction sequence; don't touch it
   1.817 +  return false;
   1.818 +}
   1.819 +
   1.820 +
   1.821 +static bool match(UnsafeRawOp* x,
   1.822 +                  Instruction** base,
   1.823 +                  Instruction** index,
   1.824 +                  int*          log2_scale) {
   1.825 +  Instruction* instr_to_unpin = NULL;
   1.826 +  ArithmeticOp* root = x->base()->as_ArithmeticOp();
   1.827 +  if (root == NULL) return false;
   1.828 +  // Limit ourselves to addition for now
   1.829 +  if (root->op() != Bytecodes::_ladd) return false;
   1.830 +  // Try to find shift or scale op
   1.831 +  if (match_index_and_scale(root->y(), index, log2_scale, &instr_to_unpin)) {
   1.832 +    *base = root->x();
   1.833 +  } else if (match_index_and_scale(root->x(), index, log2_scale, &instr_to_unpin)) {
   1.834 +    *base = root->y();
   1.835 +  } else if (root->y()->as_Convert() != NULL) {
   1.836 +    Convert* convert = root->y()->as_Convert();
   1.837 +    if (convert->op() == Bytecodes::_i2l && convert->value()->type() == intType) {
   1.838 +      // pick base and index, setting scale at 1
   1.839 +      *base  = root->x();
   1.840 +      *index = convert->value();
   1.841 +      *log2_scale = 0;
   1.842 +    } else {
   1.843 +      return false;
   1.844 +    }
   1.845 +  } else {
   1.846 +    // doesn't match any expected sequences
   1.847 +    return false;
   1.848 +  }
   1.849 +
   1.850 +  // If the value is pinned then it will be always be computed so
   1.851 +  // there's no profit to reshaping the expression.
   1.852 +  return !root->is_pinned();
   1.853 +}
   1.854 +
   1.855 +
   1.856 +void Canonicalizer::do_UnsafeRawOp(UnsafeRawOp* x) {
   1.857 +  Instruction* base = NULL;
   1.858 +  Instruction* index = NULL;
   1.859 +  int          log2_scale;
   1.860 +
   1.861 +  if (match(x, &base, &index, &log2_scale)) {
   1.862 +    x->set_base(base);
   1.863 +    x->set_index(index);
   1.864 +    x->set_log2_scale(log2_scale);
   1.865 +    if (PrintUnsafeOptimization) {
   1.866 +      tty->print_cr("Canonicalizer: UnsafeRawOp id %d: base = id %d, index = id %d, log2_scale = %d",
   1.867 +                    x->id(), x->base()->id(), x->index()->id(), x->log2_scale());
   1.868 +    }
   1.869 +  }
   1.870 +}
   1.871 +
   1.872 +void Canonicalizer::do_RoundFP(RoundFP* x) {}
   1.873 +void Canonicalizer::do_UnsafeGetRaw(UnsafeGetRaw* x) { if (OptimizeUnsafes) do_UnsafeRawOp(x); }
   1.874 +void Canonicalizer::do_UnsafePutRaw(UnsafePutRaw* x) { if (OptimizeUnsafes) do_UnsafeRawOp(x); }
   1.875 +void Canonicalizer::do_UnsafeGetObject(UnsafeGetObject* x) {}
   1.876 +void Canonicalizer::do_UnsafePutObject(UnsafePutObject* x) {}
   1.877 +void Canonicalizer::do_UnsafePrefetchRead (UnsafePrefetchRead*  x) {}
   1.878 +void Canonicalizer::do_UnsafePrefetchWrite(UnsafePrefetchWrite* x) {}
   1.879 +void Canonicalizer::do_ProfileCall(ProfileCall* x) {}
   1.880 +void Canonicalizer::do_ProfileCounter(ProfileCounter* x) {}

mercurial