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