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