hotspot/src/share/vm/opto/mulnode.cpp
author xdono
Wed, 02 Jul 2008 12:55:16 -0700
changeset 670 ddf3e9583f2f
parent 392 0b3167e2f2de
child 1436 6869d58f4f58
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
670
ddf3e9583f2f 6719955: Update copyright year
xdono
parents: 392
diff changeset
     2
 * Copyright 1997-2008 Sun Microsystems, Inc.  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
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
// Portions of code courtesy of Clifford Click
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
#include "incls/_precompiled.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
#include "incls/_mulnode.cpp.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
//=============================================================================
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
//------------------------------hash-------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
// Hash function over MulNodes.  Needs to be commutative; i.e., I swap
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
// (commute) inputs to MulNodes willy-nilly so the hash function must return
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
// the same value in the presence of edge swapping.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
uint MulNode::hash() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
  return (uintptr_t)in(1) + (uintptr_t)in(2) + Opcode();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
//------------------------------Identity---------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
// Multiplying a one preserves the other argument
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
Node *MulNode::Identity( PhaseTransform *phase ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
  register const Type *one = mul_id();  // The multiplicative identity
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
  if( phase->type( in(1) )->higher_equal( one ) ) return in(2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
  if( phase->type( in(2) )->higher_equal( one ) ) return in(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
  return this;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
//------------------------------Ideal------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
// We also canonicalize the Node, moving constants to the right input,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
// and flatten expressions (so that 1+x+2 becomes x+3).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
Node *MulNode::Ideal(PhaseGVN *phase, bool can_reshape) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
  const Type *t1 = phase->type( in(1) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
  const Type *t2 = phase->type( in(2) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
  Node *progress = NULL;        // Progress flag
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
  // We are OK if right is a constant, or right is a load and
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
  // left is a non-constant.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
  if( !(t2->singleton() ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
        (in(2)->is_Load() && !(t1->singleton() || in(1)->is_Load())) ) ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
    if( t1->singleton() ||       // Left input is a constant?
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
        // Otherwise, sort inputs (commutativity) to help value numbering.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
        (in(1)->_idx > in(2)->_idx) ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
      swap_edges(1, 2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
      const Type *t = t1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
      t1 = t2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
      t2 = t;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
      progress = this;            // Made progress
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
  // If the right input is a constant, and the left input is a product of a
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  // constant, flatten the expression tree.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  uint op = Opcode();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  if( t2->singleton() &&        // Right input is a constant?
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
      op != Op_MulF &&          // Float & double cannot reassociate
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
      op != Op_MulD ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
    if( t2 == Type::TOP ) return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
    Node *mul1 = in(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
    // Check for dead loop
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
    int   op1 = mul1->Opcode();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
    if( phase->eqv( mul1, this ) || phase->eqv( in(2), this ) ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
        ( op1 == mul_opcode() || op1 == add_opcode() ) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
        ( phase->eqv( mul1->in(1), this ) || phase->eqv( mul1->in(2), this ) ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
          phase->eqv( mul1->in(1), mul1 ) || phase->eqv( mul1->in(2), mul1 ) ) )
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
      assert(false, "dead loop in MulNode::Ideal");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
    if( mul1->Opcode() == mul_opcode() ) {  // Left input is a multiply?
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
      // Mul of a constant?
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
      const Type *t12 = phase->type( mul1->in(2) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
      if( t12->singleton() && t12 != Type::TOP) { // Left input is an add of a constant?
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
        // Compute new constant; check for overflow
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
        const Type *tcon01 = mul1->as_Mul()->mul_ring(t2,t12);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
        if( tcon01->singleton() ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
          // The Mul of the flattened expression
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
          set_req(1, mul1->in(1));
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
          set_req(2, phase->makecon( tcon01 ));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
          t2 = tcon01;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
          progress = this;      // Made progress
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
    // If the right input is a constant, and the left input is an add of a
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
    // constant, flatten the tree: (X+con1)*con0 ==> X*con0 + con1*con0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
    const Node *add1 = in(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
    if( add1->Opcode() == add_opcode() ) {      // Left input is an add?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
      // Add of a constant?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
      const Type *t12 = phase->type( add1->in(2) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
      if( t12->singleton() && t12 != Type::TOP ) { // Left input is an add of a constant?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
        assert( add1->in(1) != add1, "dead loop in MulNode::Ideal" );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
        // Compute new constant; check for overflow
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
        const Type *tcon01 = mul_ring(t2,t12);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
        if( tcon01->singleton() ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
        // Convert (X+con1)*con0 into X*con0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
          Node *mul = clone();    // mul = ()*con0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
          mul->set_req(1,add1->in(1));  // mul = X*con0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
          mul = phase->transform(mul);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
          Node *add2 = add1->clone();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
          add2->set_req(1, mul);        // X*con0 + con0*con1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
          add2->set_req(2, phase->makecon(tcon01) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
          progress = add2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
    } // End of is left input an add
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
  } // End of is right input a Mul
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
  return progress;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
//------------------------------Value-----------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
const Type *MulNode::Value( PhaseTransform *phase ) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
  const Type *t1 = phase->type( in(1) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
  const Type *t2 = phase->type( in(2) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
  // Either input is TOP ==> the result is TOP
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
  if( t1 == Type::TOP ) return Type::TOP;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
  if( t2 == Type::TOP ) return Type::TOP;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
  // Either input is ZERO ==> the result is ZERO.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
  // Not valid for floats or doubles since +0.0 * -0.0 --> +0.0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
  int op = Opcode();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
  if( op == Op_MulI || op == Op_AndI || op == Op_MulL || op == Op_AndL ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
    const Type *zero = add_id();        // The multiplicative zero
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
    if( t1->higher_equal( zero ) ) return zero;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
    if( t2->higher_equal( zero ) ) return zero;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
  // Either input is BOTTOM ==> the result is the local BOTTOM
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
  if( t1 == Type::BOTTOM || t2 == Type::BOTTOM )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
    return bottom_type();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
  return mul_ring(t1,t2);            // Local flavor of type multiplication
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
//=============================================================================
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
//------------------------------Ideal------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
// Check for power-of-2 multiply, then try the regular MulNode::Ideal
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
Node *MulINode::Ideal(PhaseGVN *phase, bool can_reshape) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
  // Swap constant to right
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
  jint con;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
  if ((con = in(1)->find_int_con(0)) != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
    swap_edges(1, 2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
    // Finish rest of method to use info in 'con'
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
  } else if ((con = in(2)->find_int_con(0)) == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
    return MulNode::Ideal(phase, can_reshape);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
  // Now we have a constant Node on the right and the constant in con
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
  if( con == 0 ) return NULL;   // By zero is handled by Value call
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
  if( con == 1 ) return NULL;   // By one  is handled by Identity call
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
  // Check for negative constant; if so negate the final result
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
  bool sign_flip = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
  if( con < 0 ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
    con = -con;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
    sign_flip = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
  // Get low bit; check for being the only bit
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
  Node *res = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
  jint bit1 = con & -con;       // Extract low bit
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
  if( bit1 == con ) {           // Found a power of 2?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
    res = new (phase->C, 3) LShiftINode( in(1), phase->intcon(log2_intptr(bit1)) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
    // Check for constant with 2 bits set
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
    jint bit2 = con-bit1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
    bit2 = bit2 & -bit2;          // Extract 2nd bit
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
    if( bit2 + bit1 == con ) {    // Found all bits in con?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
      Node *n1 = phase->transform( new (phase->C, 3) LShiftINode( in(1), phase->intcon(log2_intptr(bit1)) ) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
      Node *n2 = phase->transform( new (phase->C, 3) LShiftINode( in(1), phase->intcon(log2_intptr(bit2)) ) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
      res = new (phase->C, 3) AddINode( n2, n1 );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
    } else if (is_power_of_2(con+1)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
      // Sleezy: power-of-2 -1.  Next time be generic.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
      jint temp = (jint) (con + 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
      Node *n1 = phase->transform( new (phase->C, 3) LShiftINode( in(1), phase->intcon(log2_intptr(temp)) ) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
      res = new (phase->C, 3) SubINode( n1, in(1) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
      return MulNode::Ideal(phase, can_reshape);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
  if( sign_flip ) {             // Need to negate result?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
    res = phase->transform(res);// Transform, before making the zero con
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
    res = new (phase->C, 3) SubINode(phase->intcon(0),res);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
  return res;                   // Return final result
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
//------------------------------mul_ring---------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
// Compute the product type of two integer ranges into this node.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
const Type *MulINode::mul_ring(const Type *t0, const Type *t1) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
  const TypeInt *r0 = t0->is_int(); // Handy access
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
  const TypeInt *r1 = t1->is_int();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
  // Fetch endpoints of all ranges
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
  int32 lo0 = r0->_lo;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
  double a = (double)lo0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
  int32 hi0 = r0->_hi;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
  double b = (double)hi0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
  int32 lo1 = r1->_lo;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
  double c = (double)lo1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
  int32 hi1 = r1->_hi;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
  double d = (double)hi1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
  // Compute all endpoints & check for overflow
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
  int32 A = lo0*lo1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
  if( (double)A != a*c ) return TypeInt::INT; // Overflow?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
  int32 B = lo0*hi1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
  if( (double)B != a*d ) return TypeInt::INT; // Overflow?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
  int32 C = hi0*lo1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
  if( (double)C != b*c ) return TypeInt::INT; // Overflow?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
  int32 D = hi0*hi1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
  if( (double)D != b*d ) return TypeInt::INT; // Overflow?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
  if( A < B ) { lo0 = A; hi0 = B; } // Sort range endpoints
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
  else { lo0 = B; hi0 = A; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
  if( C < D ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
    if( C < lo0 ) lo0 = C;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
    if( D > hi0 ) hi0 = D;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
    if( D < lo0 ) lo0 = D;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
    if( C > hi0 ) hi0 = C;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
  return TypeInt::make(lo0, hi0, MAX2(r0->_widen,r1->_widen));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
//=============================================================================
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
//------------------------------Ideal------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
// Check for power-of-2 multiply, then try the regular MulNode::Ideal
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
Node *MulLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
  // Swap constant to right
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
  jlong con;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
  if ((con = in(1)->find_long_con(0)) != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
    swap_edges(1, 2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
    // Finish rest of method to use info in 'con'
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
  } else if ((con = in(2)->find_long_con(0)) == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
    return MulNode::Ideal(phase, can_reshape);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
  // Now we have a constant Node on the right and the constant in con
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
  if( con == CONST64(0) ) return NULL;  // By zero is handled by Value call
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
  if( con == CONST64(1) ) return NULL;  // By one  is handled by Identity call
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
  // Check for negative constant; if so negate the final result
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
  bool sign_flip = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
  if( con < 0 ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
    con = -con;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
    sign_flip = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
  // Get low bit; check for being the only bit
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
  Node *res = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
  jlong bit1 = con & -con;      // Extract low bit
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
  if( bit1 == con ) {           // Found a power of 2?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
    res = new (phase->C, 3) LShiftLNode( in(1), phase->intcon(log2_long(bit1)) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
    // Check for constant with 2 bits set
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
    jlong bit2 = con-bit1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
    bit2 = bit2 & -bit2;          // Extract 2nd bit
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
    if( bit2 + bit1 == con ) {    // Found all bits in con?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
      Node *n1 = phase->transform( new (phase->C, 3) LShiftLNode( in(1), phase->intcon(log2_long(bit1)) ) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
      Node *n2 = phase->transform( new (phase->C, 3) LShiftLNode( in(1), phase->intcon(log2_long(bit2)) ) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
      res = new (phase->C, 3) AddLNode( n2, n1 );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
    } else if (is_power_of_2_long(con+1)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
      // Sleezy: power-of-2 -1.  Next time be generic.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
      jlong temp = (jlong) (con + 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
      Node *n1 = phase->transform( new (phase->C, 3) LShiftLNode( in(1), phase->intcon(log2_long(temp)) ) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
      res = new (phase->C, 3) SubLNode( n1, in(1) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
      return MulNode::Ideal(phase, can_reshape);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
  if( sign_flip ) {             // Need to negate result?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
    res = phase->transform(res);// Transform, before making the zero con
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
    res = new (phase->C, 3) SubLNode(phase->longcon(0),res);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
  return res;                   // Return final result
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
//------------------------------mul_ring---------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
// Compute the product type of two integer ranges into this node.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
const Type *MulLNode::mul_ring(const Type *t0, const Type *t1) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
  const TypeLong *r0 = t0->is_long(); // Handy access
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
  const TypeLong *r1 = t1->is_long();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
  // Fetch endpoints of all ranges
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
  jlong lo0 = r0->_lo;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
  double a = (double)lo0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
  jlong hi0 = r0->_hi;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
  double b = (double)hi0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
  jlong lo1 = r1->_lo;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
  double c = (double)lo1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
  jlong hi1 = r1->_hi;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
  double d = (double)hi1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
  // Compute all endpoints & check for overflow
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
  jlong A = lo0*lo1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
  if( (double)A != a*c ) return TypeLong::LONG; // Overflow?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
  jlong B = lo0*hi1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
  if( (double)B != a*d ) return TypeLong::LONG; // Overflow?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
  jlong C = hi0*lo1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
  if( (double)C != b*c ) return TypeLong::LONG; // Overflow?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
  jlong D = hi0*hi1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
  if( (double)D != b*d ) return TypeLong::LONG; // Overflow?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
  if( A < B ) { lo0 = A; hi0 = B; } // Sort range endpoints
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
  else { lo0 = B; hi0 = A; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
  if( C < D ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
    if( C < lo0 ) lo0 = C;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
    if( D > hi0 ) hi0 = D;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
    if( D < lo0 ) lo0 = D;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
    if( C > hi0 ) hi0 = C;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
  return TypeLong::make(lo0, hi0, MAX2(r0->_widen,r1->_widen));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
//=============================================================================
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
//------------------------------mul_ring---------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
// Compute the product type of two double ranges into this node.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
const Type *MulFNode::mul_ring(const Type *t0, const Type *t1) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
  if( t0 == Type::FLOAT || t1 == Type::FLOAT ) return Type::FLOAT;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
  return TypeF::make( t0->getf() * t1->getf() );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
//=============================================================================
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
//------------------------------mul_ring---------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
// Compute the product type of two double ranges into this node.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
const Type *MulDNode::mul_ring(const Type *t0, const Type *t1) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
  if( t0 == Type::DOUBLE || t1 == Type::DOUBLE ) return Type::DOUBLE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
  // We must be adding 2 double constants.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
  return TypeD::make( t0->getd() * t1->getd() );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
//=============================================================================
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 1
diff changeset
   368
//------------------------------Value------------------------------------------
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 1
diff changeset
   369
const Type *MulHiLNode::Value( PhaseTransform *phase ) const {
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 1
diff changeset
   370
  // Either input is TOP ==> the result is TOP
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 1
diff changeset
   371
  const Type *t1 = phase->type( in(1) );
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 1
diff changeset
   372
  const Type *t2 = phase->type( in(2) );
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 1
diff changeset
   373
  if( t1 == Type::TOP ) return Type::TOP;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 1
diff changeset
   374
  if( t2 == Type::TOP ) return Type::TOP;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 1
diff changeset
   375
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 1
diff changeset
   376
  // Either input is BOTTOM ==> the result is the local BOTTOM
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 1
diff changeset
   377
  const Type *bot = bottom_type();
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 1
diff changeset
   378
  if( (t1 == bot) || (t2 == bot) ||
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 1
diff changeset
   379
      (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 1
diff changeset
   380
    return bot;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 1
diff changeset
   381
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 1
diff changeset
   382
  // It is not worth trying to constant fold this stuff!
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 1
diff changeset
   383
  return TypeLong::LONG;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 1
diff changeset
   384
}
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 1
diff changeset
   385
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 1
diff changeset
   386
//=============================================================================
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
//------------------------------mul_ring---------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
// Supplied function returns the product of the inputs IN THE CURRENT RING.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
// For the logical operations the ring's MUL is really a logical AND function.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
// This also type-checks the inputs for sanity.  Guaranteed never to
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
// be passed a TOP or BOTTOM type, these are filtered out by pre-check.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
const Type *AndINode::mul_ring( const Type *t0, const Type *t1 ) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
  const TypeInt *r0 = t0->is_int(); // Handy access
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
  const TypeInt *r1 = t1->is_int();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
  int widen = MAX2(r0->_widen,r1->_widen);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
  // If either input is a constant, might be able to trim cases
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
  if( !r0->is_con() && !r1->is_con() )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
    return TypeInt::INT;        // No constants to be had
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
  // Both constants?  Return bits
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
  if( r0->is_con() && r1->is_con() )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
    return TypeInt::make( r0->get_con() & r1->get_con() );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
  if( r0->is_con() && r0->get_con() > 0 )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   406
    return TypeInt::make(0, r0->get_con(), widen);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   407
489c9b5090e2 Initial load
duke
parents:
diff changeset
   408
  if( r1->is_con() && r1->get_con() > 0 )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
    return TypeInt::make(0, r1->get_con(), widen);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
  if( r0 == TypeInt::BOOL || r1 == TypeInt::BOOL ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
    return TypeInt::BOOL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
  return TypeInt::INT;          // No constants to be had
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
//------------------------------Identity---------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
// Masking off the high bits of an unsigned load is not required
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
Node *AndINode::Identity( PhaseTransform *phase ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
489c9b5090e2 Initial load
duke
parents:
diff changeset
   422
  // x & x => x
489c9b5090e2 Initial load
duke
parents:
diff changeset
   423
  if (phase->eqv(in(1), in(2))) return in(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
  Node *load = in(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
  const TypeInt *t2 = phase->type( in(2) )->isa_int();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
  if( t2 && t2->is_con() ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
    int con = t2->get_con();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   429
    // Masking off high bits which are always zero is useless.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
    const TypeInt* t1 = phase->type( in(1) )->isa_int();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
    if (t1 != NULL && t1->_lo >= 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
      jint t1_support = ((jint)1 << (1 + log2_intptr(t1->_hi))) - 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
      if ((t1_support & con) == t1_support)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
        return load;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
    uint lop = load->Opcode();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
    if( lop == Op_LoadC &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   438
        con == 0x0000FFFF )     // Already zero-extended
489c9b5090e2 Initial load
duke
parents:
diff changeset
   439
      return load;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   440
    // Masking off the high bits of a unsigned-shift-right is not
489c9b5090e2 Initial load
duke
parents:
diff changeset
   441
    // needed either.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
    if( lop == Op_URShiftI ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   443
      const TypeInt *t12 = phase->type( load->in(2) )->isa_int();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   444
      if( t12 && t12->is_con() ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
        int shift_con = t12->get_con();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
        int mask = max_juint >> shift_con;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
        if( (mask&con) == mask )  // If AND is useless, skip it
489c9b5090e2 Initial load
duke
parents:
diff changeset
   448
          return load;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   449
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   450
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   451
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   452
  return MulNode::Identity(phase);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   453
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   454
489c9b5090e2 Initial load
duke
parents:
diff changeset
   455
//------------------------------Ideal------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   456
Node *AndINode::Ideal(PhaseGVN *phase, bool can_reshape) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   457
  // Special case constant AND mask
489c9b5090e2 Initial load
duke
parents:
diff changeset
   458
  const TypeInt *t2 = phase->type( in(2) )->isa_int();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   459
  if( !t2 || !t2->is_con() ) return MulNode::Ideal(phase, can_reshape);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   460
  const int mask = t2->get_con();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   461
  Node *load = in(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   462
  uint lop = load->Opcode();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   463
489c9b5090e2 Initial load
duke
parents:
diff changeset
   464
  // Masking bits off of a Character?  Hi bits are already zero.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   465
  if( lop == Op_LoadC &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   466
      (mask & 0xFFFF0000) )     // Can we make a smaller mask?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   467
    return new (phase->C, 3) AndINode(load,phase->intcon(mask&0xFFFF));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   468
489c9b5090e2 Initial load
duke
parents:
diff changeset
   469
  // Masking bits off of a Short?  Loading a Character does some masking
489c9b5090e2 Initial load
duke
parents:
diff changeset
   470
  if( lop == Op_LoadS &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   471
      (mask & 0xFFFF0000) == 0 ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   472
    Node *ldc = new (phase->C, 3) LoadCNode(load->in(MemNode::Control),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   473
                                  load->in(MemNode::Memory),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   474
                                  load->in(MemNode::Address),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   475
                                  load->adr_type());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   476
    ldc = phase->transform(ldc);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   477
    return new (phase->C, 3) AndINode(ldc,phase->intcon(mask&0xFFFF));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   478
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   479
489c9b5090e2 Initial load
duke
parents:
diff changeset
   480
  // Masking sign bits off of a Byte?  Let the matcher use an unsigned load
489c9b5090e2 Initial load
duke
parents:
diff changeset
   481
  if( lop == Op_LoadB &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   482
      (!in(0) && load->in(0)) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   483
      (mask == 0x000000FF) ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   484
    // Associate this node with the LoadB, so the matcher can see them together.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   485
    // If we don't do this, it is common for the LoadB to have one control
489c9b5090e2 Initial load
duke
parents:
diff changeset
   486
    // edge, and the store or call containing this AndI to have a different
489c9b5090e2 Initial load
duke
parents:
diff changeset
   487
    // control edge.  This will cause Label_Root to group the AndI with
489c9b5090e2 Initial load
duke
parents:
diff changeset
   488
    // the encoding store or call, so the matcher has no chance to match
489c9b5090e2 Initial load
duke
parents:
diff changeset
   489
    // this AndI together with the LoadB.  Setting the control edge here
489c9b5090e2 Initial load
duke
parents:
diff changeset
   490
    // prevents Label_Root from grouping the AndI with the store or call,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   491
    // if it has a control edge that is inconsistent with the LoadB.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   492
    set_req(0, load->in(0));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   493
    return this;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   494
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   495
489c9b5090e2 Initial load
duke
parents:
diff changeset
   496
  // Masking off sign bits?  Dont make them!
489c9b5090e2 Initial load
duke
parents:
diff changeset
   497
  if( lop == Op_RShiftI ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   498
    const TypeInt *t12 = phase->type(load->in(2))->isa_int();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   499
    if( t12 && t12->is_con() ) { // Shift is by a constant
489c9b5090e2 Initial load
duke
parents:
diff changeset
   500
      int shift = t12->get_con();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   501
      shift &= BitsPerJavaInteger-1;  // semantics of Java shifts
489c9b5090e2 Initial load
duke
parents:
diff changeset
   502
      const int sign_bits_mask = ~right_n_bits(BitsPerJavaInteger - shift);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   503
      // If the AND'ing of the 2 masks has no bits, then only original shifted
489c9b5090e2 Initial load
duke
parents:
diff changeset
   504
      // bits survive.  NO sign-extension bits survive the maskings.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   505
      if( (sign_bits_mask & mask) == 0 ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   506
        // Use zero-fill shift instead
489c9b5090e2 Initial load
duke
parents:
diff changeset
   507
        Node *zshift = phase->transform(new (phase->C, 3) URShiftINode(load->in(1),load->in(2)));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   508
        return new (phase->C, 3) AndINode( zshift, in(2) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   509
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   510
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   511
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   512
489c9b5090e2 Initial load
duke
parents:
diff changeset
   513
  // Check for 'negate/and-1', a pattern emitted when someone asks for
489c9b5090e2 Initial load
duke
parents:
diff changeset
   514
  // 'mod 2'.  Negate leaves the low order bit unchanged (think: complement
489c9b5090e2 Initial load
duke
parents:
diff changeset
   515
  // plus 1) and the mask is of the low order bit.  Skip the negate.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   516
  if( lop == Op_SubI && mask == 1 && load->in(1) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   517
      phase->type(load->in(1)) == TypeInt::ZERO )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   518
    return new (phase->C, 3) AndINode( load->in(2), in(2) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   519
489c9b5090e2 Initial load
duke
parents:
diff changeset
   520
  return MulNode::Ideal(phase, can_reshape);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   521
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   522
489c9b5090e2 Initial load
duke
parents:
diff changeset
   523
//=============================================================================
489c9b5090e2 Initial load
duke
parents:
diff changeset
   524
//------------------------------mul_ring---------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   525
// Supplied function returns the product of the inputs IN THE CURRENT RING.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   526
// For the logical operations the ring's MUL is really a logical AND function.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   527
// This also type-checks the inputs for sanity.  Guaranteed never to
489c9b5090e2 Initial load
duke
parents:
diff changeset
   528
// be passed a TOP or BOTTOM type, these are filtered out by pre-check.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   529
const Type *AndLNode::mul_ring( const Type *t0, const Type *t1 ) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   530
  const TypeLong *r0 = t0->is_long(); // Handy access
489c9b5090e2 Initial load
duke
parents:
diff changeset
   531
  const TypeLong *r1 = t1->is_long();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   532
  int widen = MAX2(r0->_widen,r1->_widen);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   533
489c9b5090e2 Initial load
duke
parents:
diff changeset
   534
  // If either input is a constant, might be able to trim cases
489c9b5090e2 Initial load
duke
parents:
diff changeset
   535
  if( !r0->is_con() && !r1->is_con() )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   536
    return TypeLong::LONG;      // No constants to be had
489c9b5090e2 Initial load
duke
parents:
diff changeset
   537
489c9b5090e2 Initial load
duke
parents:
diff changeset
   538
  // Both constants?  Return bits
489c9b5090e2 Initial load
duke
parents:
diff changeset
   539
  if( r0->is_con() && r1->is_con() )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   540
    return TypeLong::make( r0->get_con() & r1->get_con() );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   541
489c9b5090e2 Initial load
duke
parents:
diff changeset
   542
  if( r0->is_con() && r0->get_con() > 0 )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   543
    return TypeLong::make(CONST64(0), r0->get_con(), widen);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   544
489c9b5090e2 Initial load
duke
parents:
diff changeset
   545
  if( r1->is_con() && r1->get_con() > 0 )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   546
    return TypeLong::make(CONST64(0), r1->get_con(), widen);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   547
489c9b5090e2 Initial load
duke
parents:
diff changeset
   548
  return TypeLong::LONG;        // No constants to be had
489c9b5090e2 Initial load
duke
parents:
diff changeset
   549
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   550
489c9b5090e2 Initial load
duke
parents:
diff changeset
   551
//------------------------------Identity---------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   552
// Masking off the high bits of an unsigned load is not required
489c9b5090e2 Initial load
duke
parents:
diff changeset
   553
Node *AndLNode::Identity( PhaseTransform *phase ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   554
489c9b5090e2 Initial load
duke
parents:
diff changeset
   555
  // x & x => x
489c9b5090e2 Initial load
duke
parents:
diff changeset
   556
  if (phase->eqv(in(1), in(2))) return in(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   557
489c9b5090e2 Initial load
duke
parents:
diff changeset
   558
  Node *usr = in(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   559
  const TypeLong *t2 = phase->type( in(2) )->isa_long();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   560
  if( t2 && t2->is_con() ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   561
    jlong con = t2->get_con();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   562
    // Masking off high bits which are always zero is useless.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   563
    const TypeLong* t1 = phase->type( in(1) )->isa_long();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   564
    if (t1 != NULL && t1->_lo >= 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   565
      jlong t1_support = ((jlong)1 << (1 + log2_long(t1->_hi))) - 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   566
      if ((t1_support & con) == t1_support)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   567
        return usr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   568
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   569
    uint lop = usr->Opcode();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   570
    // Masking off the high bits of a unsigned-shift-right is not
489c9b5090e2 Initial load
duke
parents:
diff changeset
   571
    // needed either.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   572
    if( lop == Op_URShiftL ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   573
      const TypeInt *t12 = phase->type( usr->in(2) )->isa_int();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   574
      if( t12 && t12->is_con() ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   575
        int shift_con = t12->get_con();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   576
        jlong mask = max_julong >> shift_con;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   577
        if( (mask&con) == mask )  // If AND is useless, skip it
489c9b5090e2 Initial load
duke
parents:
diff changeset
   578
          return usr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   579
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   580
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   581
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   582
  return MulNode::Identity(phase);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   583
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   584
489c9b5090e2 Initial load
duke
parents:
diff changeset
   585
//------------------------------Ideal------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   586
Node *AndLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   587
  // Special case constant AND mask
489c9b5090e2 Initial load
duke
parents:
diff changeset
   588
  const TypeLong *t2 = phase->type( in(2) )->isa_long();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   589
  if( !t2 || !t2->is_con() ) return MulNode::Ideal(phase, can_reshape);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   590
  const jlong mask = t2->get_con();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   591
489c9b5090e2 Initial load
duke
parents:
diff changeset
   592
  Node *rsh = in(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   593
  uint rop = rsh->Opcode();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   594
489c9b5090e2 Initial load
duke
parents:
diff changeset
   595
  // Masking off sign bits?  Dont make them!
489c9b5090e2 Initial load
duke
parents:
diff changeset
   596
  if( rop == Op_RShiftL ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   597
    const TypeInt *t12 = phase->type(rsh->in(2))->isa_int();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   598
    if( t12 && t12->is_con() ) { // Shift is by a constant
489c9b5090e2 Initial load
duke
parents:
diff changeset
   599
      int shift = t12->get_con();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   600
      shift &= (BitsPerJavaInteger*2)-1;  // semantics of Java shifts
489c9b5090e2 Initial load
duke
parents:
diff changeset
   601
      const jlong sign_bits_mask = ~(((jlong)CONST64(1) << (jlong)(BitsPerJavaInteger*2 - shift)) -1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   602
      // If the AND'ing of the 2 masks has no bits, then only original shifted
489c9b5090e2 Initial load
duke
parents:
diff changeset
   603
      // bits survive.  NO sign-extension bits survive the maskings.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   604
      if( (sign_bits_mask & mask) == 0 ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   605
        // Use zero-fill shift instead
489c9b5090e2 Initial load
duke
parents:
diff changeset
   606
        Node *zshift = phase->transform(new (phase->C, 3) URShiftLNode(rsh->in(1),rsh->in(2)));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   607
        return new (phase->C, 3) AndLNode( zshift, in(2) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   608
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   609
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   610
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   611
489c9b5090e2 Initial load
duke
parents:
diff changeset
   612
  return MulNode::Ideal(phase, can_reshape);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   613
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   614
489c9b5090e2 Initial load
duke
parents:
diff changeset
   615
//=============================================================================
489c9b5090e2 Initial load
duke
parents:
diff changeset
   616
//------------------------------Identity---------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   617
Node *LShiftINode::Identity( PhaseTransform *phase ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   618
  const TypeInt *ti = phase->type( in(2) )->isa_int();  // shift count is an int
489c9b5090e2 Initial load
duke
parents:
diff changeset
   619
  return ( ti && ti->is_con() && ( ti->get_con() & ( BitsPerInt - 1 ) ) == 0 ) ? in(1) : this;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   620
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   621
489c9b5090e2 Initial load
duke
parents:
diff changeset
   622
//------------------------------Ideal------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   623
// If the right input is a constant, and the left input is an add of a
489c9b5090e2 Initial load
duke
parents:
diff changeset
   624
// constant, flatten the tree: (X+con1)<<con0 ==> X<<con0 + con1<<con0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   625
Node *LShiftINode::Ideal(PhaseGVN *phase, bool can_reshape) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   626
  const Type *t  = phase->type( in(2) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   627
  if( t == Type::TOP ) return NULL;       // Right input is dead
489c9b5090e2 Initial load
duke
parents:
diff changeset
   628
  const TypeInt *t2 = t->isa_int();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   629
  if( !t2 || !t2->is_con() ) return NULL; // Right input is a constant
489c9b5090e2 Initial load
duke
parents:
diff changeset
   630
  const int con = t2->get_con() & ( BitsPerInt - 1 );  // masked shift count
489c9b5090e2 Initial load
duke
parents:
diff changeset
   631
489c9b5090e2 Initial load
duke
parents:
diff changeset
   632
  if ( con == 0 )  return NULL; // let Identity() handle 0 shift count
489c9b5090e2 Initial load
duke
parents:
diff changeset
   633
489c9b5090e2 Initial load
duke
parents:
diff changeset
   634
  // Left input is an add of a constant?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   635
  Node *add1 = in(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   636
  int add1_op = add1->Opcode();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   637
  if( add1_op == Op_AddI ) {    // Left input is an add?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   638
    assert( add1 != add1->in(1), "dead loop in LShiftINode::Ideal" );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   639
    const TypeInt *t12 = phase->type(add1->in(2))->isa_int();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   640
    if( t12 && t12->is_con() ){ // Left input is an add of a con?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   641
      // Transform is legal, but check for profit.  Avoid breaking 'i2s'
489c9b5090e2 Initial load
duke
parents:
diff changeset
   642
      // and 'i2b' patterns which typically fold into 'StoreC/StoreB'.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   643
      if( con < 16 ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   644
        // Compute X << con0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   645
        Node *lsh = phase->transform( new (phase->C, 3) LShiftINode( add1->in(1), in(2) ) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   646
        // Compute X<<con0 + (con1<<con0)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   647
        return new (phase->C, 3) AddINode( lsh, phase->intcon(t12->get_con() << con));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   648
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   649
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   650
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   651
489c9b5090e2 Initial load
duke
parents:
diff changeset
   652
  // Check for "(x>>c0)<<c0" which just masks off low bits
489c9b5090e2 Initial load
duke
parents:
diff changeset
   653
  if( (add1_op == Op_RShiftI || add1_op == Op_URShiftI ) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   654
      add1->in(2) == in(2) )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   655
    // Convert to "(x & -(1<<c0))"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   656
    return new (phase->C, 3) AndINode(add1->in(1),phase->intcon( -(1<<con)));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   657
489c9b5090e2 Initial load
duke
parents:
diff changeset
   658
  // Check for "((x>>c0) & Y)<<c0" which just masks off more low bits
489c9b5090e2 Initial load
duke
parents:
diff changeset
   659
  if( add1_op == Op_AndI ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   660
    Node *add2 = add1->in(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   661
    int add2_op = add2->Opcode();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   662
    if( (add2_op == Op_RShiftI || add2_op == Op_URShiftI ) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   663
        add2->in(2) == in(2) ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   664
      // Convert to "(x & (Y<<c0))"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   665
      Node *y_sh = phase->transform( new (phase->C, 3) LShiftINode( add1->in(2), in(2) ) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   666
      return new (phase->C, 3) AndINode( add2->in(1), y_sh );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   667
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   668
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   669
489c9b5090e2 Initial load
duke
parents:
diff changeset
   670
  // Check for ((x & ((1<<(32-c0))-1)) << c0) which ANDs off high bits
489c9b5090e2 Initial load
duke
parents:
diff changeset
   671
  // before shifting them away.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   672
  const jint bits_mask = right_n_bits(BitsPerJavaInteger-con);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   673
  if( add1_op == Op_AndI &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   674
      phase->type(add1->in(2)) == TypeInt::make( bits_mask ) )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   675
    return new (phase->C, 3) LShiftINode( add1->in(1), in(2) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   676
489c9b5090e2 Initial load
duke
parents:
diff changeset
   677
  return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   678
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   679
489c9b5090e2 Initial load
duke
parents:
diff changeset
   680
//------------------------------Value------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   681
// A LShiftINode shifts its input2 left by input1 amount.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   682
const Type *LShiftINode::Value( PhaseTransform *phase ) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   683
  const Type *t1 = phase->type( in(1) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   684
  const Type *t2 = phase->type( in(2) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   685
  // Either input is TOP ==> the result is TOP
489c9b5090e2 Initial load
duke
parents:
diff changeset
   686
  if( t1 == Type::TOP ) return Type::TOP;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   687
  if( t2 == Type::TOP ) return Type::TOP;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   688
489c9b5090e2 Initial load
duke
parents:
diff changeset
   689
  // Left input is ZERO ==> the result is ZERO.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   690
  if( t1 == TypeInt::ZERO ) return TypeInt::ZERO;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   691
  // Shift by zero does nothing
489c9b5090e2 Initial load
duke
parents:
diff changeset
   692
  if( t2 == TypeInt::ZERO ) return t1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   693
489c9b5090e2 Initial load
duke
parents:
diff changeset
   694
  // Either input is BOTTOM ==> the result is BOTTOM
489c9b5090e2 Initial load
duke
parents:
diff changeset
   695
  if( (t1 == TypeInt::INT) || (t2 == TypeInt::INT) ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   696
      (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   697
    return TypeInt::INT;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   698
489c9b5090e2 Initial load
duke
parents:
diff changeset
   699
  const TypeInt *r1 = t1->is_int(); // Handy access
489c9b5090e2 Initial load
duke
parents:
diff changeset
   700
  const TypeInt *r2 = t2->is_int(); // Handy access
489c9b5090e2 Initial load
duke
parents:
diff changeset
   701
489c9b5090e2 Initial load
duke
parents:
diff changeset
   702
  if (!r2->is_con())
489c9b5090e2 Initial load
duke
parents:
diff changeset
   703
    return TypeInt::INT;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   704
489c9b5090e2 Initial load
duke
parents:
diff changeset
   705
  uint shift = r2->get_con();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   706
  shift &= BitsPerJavaInteger-1;  // semantics of Java shifts
489c9b5090e2 Initial load
duke
parents:
diff changeset
   707
  // Shift by a multiple of 32 does nothing:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   708
  if (shift == 0)  return t1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   709
489c9b5090e2 Initial load
duke
parents:
diff changeset
   710
  // If the shift is a constant, shift the bounds of the type,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   711
  // unless this could lead to an overflow.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   712
  if (!r1->is_con()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   713
    jint lo = r1->_lo, hi = r1->_hi;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   714
    if (((lo << shift) >> shift) == lo &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   715
        ((hi << shift) >> shift) == hi) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   716
      // No overflow.  The range shifts up cleanly.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   717
      return TypeInt::make((jint)lo << (jint)shift,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   718
                           (jint)hi << (jint)shift,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   719
                           MAX2(r1->_widen,r2->_widen));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   720
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   721
    return TypeInt::INT;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   722
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   723
489c9b5090e2 Initial load
duke
parents:
diff changeset
   724
  return TypeInt::make( (jint)r1->get_con() << (jint)shift );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   725
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   726
489c9b5090e2 Initial load
duke
parents:
diff changeset
   727
//=============================================================================
489c9b5090e2 Initial load
duke
parents:
diff changeset
   728
//------------------------------Identity---------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   729
Node *LShiftLNode::Identity( PhaseTransform *phase ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   730
  const TypeInt *ti = phase->type( in(2) )->isa_int(); // shift count is an int
489c9b5090e2 Initial load
duke
parents:
diff changeset
   731
  return ( ti && ti->is_con() && ( ti->get_con() & ( BitsPerLong - 1 ) ) == 0 ) ? in(1) : this;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   732
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   733
489c9b5090e2 Initial load
duke
parents:
diff changeset
   734
//------------------------------Ideal------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   735
// If the right input is a constant, and the left input is an add of a
489c9b5090e2 Initial load
duke
parents:
diff changeset
   736
// constant, flatten the tree: (X+con1)<<con0 ==> X<<con0 + con1<<con0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   737
Node *LShiftLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   738
  const Type *t  = phase->type( in(2) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   739
  if( t == Type::TOP ) return NULL;       // Right input is dead
489c9b5090e2 Initial load
duke
parents:
diff changeset
   740
  const TypeInt *t2 = t->isa_int();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   741
  if( !t2 || !t2->is_con() ) return NULL; // Right input is a constant
489c9b5090e2 Initial load
duke
parents:
diff changeset
   742
  const int con = t2->get_con() & ( BitsPerLong - 1 );  // masked shift count
489c9b5090e2 Initial load
duke
parents:
diff changeset
   743
489c9b5090e2 Initial load
duke
parents:
diff changeset
   744
  if ( con == 0 ) return NULL;  // let Identity() handle 0 shift count
489c9b5090e2 Initial load
duke
parents:
diff changeset
   745
489c9b5090e2 Initial load
duke
parents:
diff changeset
   746
  // Left input is an add of a constant?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   747
  Node *add1 = in(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   748
  int add1_op = add1->Opcode();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   749
  if( add1_op == Op_AddL ) {    // Left input is an add?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   750
    // Avoid dead data cycles from dead loops
489c9b5090e2 Initial load
duke
parents:
diff changeset
   751
    assert( add1 != add1->in(1), "dead loop in LShiftLNode::Ideal" );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   752
    const TypeLong *t12 = phase->type(add1->in(2))->isa_long();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   753
    if( t12 && t12->is_con() ){ // Left input is an add of a con?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   754
      // Compute X << con0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   755
      Node *lsh = phase->transform( new (phase->C, 3) LShiftLNode( add1->in(1), in(2) ) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   756
      // Compute X<<con0 + (con1<<con0)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   757
      return new (phase->C, 3) AddLNode( lsh, phase->longcon(t12->get_con() << con));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   758
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   759
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   760
489c9b5090e2 Initial load
duke
parents:
diff changeset
   761
  // Check for "(x>>c0)<<c0" which just masks off low bits
489c9b5090e2 Initial load
duke
parents:
diff changeset
   762
  if( (add1_op == Op_RShiftL || add1_op == Op_URShiftL ) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   763
      add1->in(2) == in(2) )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   764
    // Convert to "(x & -(1<<c0))"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   765
    return new (phase->C, 3) AndLNode(add1->in(1),phase->longcon( -(CONST64(1)<<con)));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   766
489c9b5090e2 Initial load
duke
parents:
diff changeset
   767
  // Check for "((x>>c0) & Y)<<c0" which just masks off more low bits
489c9b5090e2 Initial load
duke
parents:
diff changeset
   768
  if( add1_op == Op_AndL ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   769
    Node *add2 = add1->in(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   770
    int add2_op = add2->Opcode();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   771
    if( (add2_op == Op_RShiftL || add2_op == Op_URShiftL ) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   772
        add2->in(2) == in(2) ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   773
      // Convert to "(x & (Y<<c0))"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   774
      Node *y_sh = phase->transform( new (phase->C, 3) LShiftLNode( add1->in(2), in(2) ) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   775
      return new (phase->C, 3) AndLNode( add2->in(1), y_sh );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   776
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   777
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   778
489c9b5090e2 Initial load
duke
parents:
diff changeset
   779
  // Check for ((x & ((CONST64(1)<<(64-c0))-1)) << c0) which ANDs off high bits
489c9b5090e2 Initial load
duke
parents:
diff changeset
   780
  // before shifting them away.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   781
  const jlong bits_mask = ((jlong)CONST64(1) << (jlong)(BitsPerJavaInteger*2 - con)) - CONST64(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   782
  if( add1_op == Op_AndL &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   783
      phase->type(add1->in(2)) == TypeLong::make( bits_mask ) )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   784
    return new (phase->C, 3) LShiftLNode( add1->in(1), in(2) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   785
489c9b5090e2 Initial load
duke
parents:
diff changeset
   786
  return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   787
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   788
489c9b5090e2 Initial load
duke
parents:
diff changeset
   789
//------------------------------Value------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   790
// A LShiftLNode shifts its input2 left by input1 amount.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   791
const Type *LShiftLNode::Value( PhaseTransform *phase ) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   792
  const Type *t1 = phase->type( in(1) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   793
  const Type *t2 = phase->type( in(2) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   794
  // Either input is TOP ==> the result is TOP
489c9b5090e2 Initial load
duke
parents:
diff changeset
   795
  if( t1 == Type::TOP ) return Type::TOP;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   796
  if( t2 == Type::TOP ) return Type::TOP;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   797
489c9b5090e2 Initial load
duke
parents:
diff changeset
   798
  // Left input is ZERO ==> the result is ZERO.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   799
  if( t1 == TypeLong::ZERO ) return TypeLong::ZERO;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   800
  // Shift by zero does nothing
489c9b5090e2 Initial load
duke
parents:
diff changeset
   801
  if( t2 == TypeInt::ZERO ) return t1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   802
489c9b5090e2 Initial load
duke
parents:
diff changeset
   803
  // Either input is BOTTOM ==> the result is BOTTOM
489c9b5090e2 Initial load
duke
parents:
diff changeset
   804
  if( (t1 == TypeLong::LONG) || (t2 == TypeInt::INT) ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   805
      (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   806
    return TypeLong::LONG;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   807
489c9b5090e2 Initial load
duke
parents:
diff changeset
   808
  const TypeLong *r1 = t1->is_long(); // Handy access
489c9b5090e2 Initial load
duke
parents:
diff changeset
   809
  const TypeInt  *r2 = t2->is_int();  // Handy access
489c9b5090e2 Initial load
duke
parents:
diff changeset
   810
489c9b5090e2 Initial load
duke
parents:
diff changeset
   811
  if (!r2->is_con())
489c9b5090e2 Initial load
duke
parents:
diff changeset
   812
    return TypeLong::LONG;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   813
489c9b5090e2 Initial load
duke
parents:
diff changeset
   814
  uint shift = r2->get_con();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   815
  shift &= (BitsPerJavaInteger*2)-1;  // semantics of Java shifts
489c9b5090e2 Initial load
duke
parents:
diff changeset
   816
  // Shift by a multiple of 64 does nothing:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   817
  if (shift == 0)  return t1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   818
489c9b5090e2 Initial load
duke
parents:
diff changeset
   819
  // If the shift is a constant, shift the bounds of the type,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   820
  // unless this could lead to an overflow.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   821
  if (!r1->is_con()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   822
    jlong lo = r1->_lo, hi = r1->_hi;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   823
    if (((lo << shift) >> shift) == lo &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   824
        ((hi << shift) >> shift) == hi) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   825
      // No overflow.  The range shifts up cleanly.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   826
      return TypeLong::make((jlong)lo << (jint)shift,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   827
                            (jlong)hi << (jint)shift,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   828
                            MAX2(r1->_widen,r2->_widen));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   829
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   830
    return TypeLong::LONG;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   831
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   832
489c9b5090e2 Initial load
duke
parents:
diff changeset
   833
  return TypeLong::make( (jlong)r1->get_con() << (jint)shift );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   834
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   835
489c9b5090e2 Initial load
duke
parents:
diff changeset
   836
//=============================================================================
489c9b5090e2 Initial load
duke
parents:
diff changeset
   837
//------------------------------Identity---------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   838
Node *RShiftINode::Identity( PhaseTransform *phase ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   839
  const TypeInt *t2 = phase->type(in(2))->isa_int();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   840
  if( !t2 ) return this;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   841
  if ( t2->is_con() && ( t2->get_con() & ( BitsPerInt - 1 ) ) == 0 )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   842
    return in(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   843
489c9b5090e2 Initial load
duke
parents:
diff changeset
   844
  // Check for useless sign-masking
489c9b5090e2 Initial load
duke
parents:
diff changeset
   845
  if( in(1)->Opcode() == Op_LShiftI &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   846
      in(1)->req() == 3 &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   847
      in(1)->in(2) == in(2) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   848
      t2->is_con() ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   849
    uint shift = t2->get_con();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   850
    shift &= BitsPerJavaInteger-1; // semantics of Java shifts
489c9b5090e2 Initial load
duke
parents:
diff changeset
   851
    // Compute masks for which this shifting doesn't change
489c9b5090e2 Initial load
duke
parents:
diff changeset
   852
    int lo = (-1 << (BitsPerJavaInteger - shift-1)); // FFFF8000
489c9b5090e2 Initial load
duke
parents:
diff changeset
   853
    int hi = ~lo;               // 00007FFF
489c9b5090e2 Initial load
duke
parents:
diff changeset
   854
    const TypeInt *t11 = phase->type(in(1)->in(1))->isa_int();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   855
    if( !t11 ) return this;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   856
    // Does actual value fit inside of mask?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   857
    if( lo <= t11->_lo && t11->_hi <= hi )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   858
      return in(1)->in(1);      // Then shifting is a nop
489c9b5090e2 Initial load
duke
parents:
diff changeset
   859
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   860
489c9b5090e2 Initial load
duke
parents:
diff changeset
   861
  return this;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   862
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   863
489c9b5090e2 Initial load
duke
parents:
diff changeset
   864
//------------------------------Ideal------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   865
Node *RShiftINode::Ideal(PhaseGVN *phase, bool can_reshape) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   866
  // Inputs may be TOP if they are dead.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   867
  const TypeInt *t1 = phase->type( in(1) )->isa_int();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   868
  if( !t1 ) return NULL;        // Left input is an integer
489c9b5090e2 Initial load
duke
parents:
diff changeset
   869
  const TypeInt *t2 = phase->type( in(2) )->isa_int();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   870
  if( !t2 || !t2->is_con() ) return NULL; // Right input is a constant
489c9b5090e2 Initial load
duke
parents:
diff changeset
   871
  const TypeInt *t3;  // type of in(1).in(2)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   872
  int shift = t2->get_con();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   873
  shift &= BitsPerJavaInteger-1;  // semantics of Java shifts
489c9b5090e2 Initial load
duke
parents:
diff changeset
   874
489c9b5090e2 Initial load
duke
parents:
diff changeset
   875
  if ( shift == 0 ) return NULL;  // let Identity() handle 0 shift count
489c9b5090e2 Initial load
duke
parents:
diff changeset
   876
489c9b5090e2 Initial load
duke
parents:
diff changeset
   877
  // Check for (x & 0xFF000000) >> 24, whose mask can be made smaller.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   878
  // Such expressions arise normally from shift chains like (byte)(x >> 24).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   879
  const Node *mask = in(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   880
  if( mask->Opcode() == Op_AndI &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   881
      (t3 = phase->type(mask->in(2))->isa_int()) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   882
      t3->is_con() ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   883
    Node *x = mask->in(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   884
    jint maskbits = t3->get_con();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   885
    // Convert to "(x >> shift) & (mask >> shift)"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   886
    Node *shr_nomask = phase->transform( new (phase->C, 3) RShiftINode(mask->in(1), in(2)) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   887
    return new (phase->C, 3) AndINode(shr_nomask, phase->intcon( maskbits >> shift));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   888
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   889
489c9b5090e2 Initial load
duke
parents:
diff changeset
   890
  // Check for "(short[i] <<16)>>16" which simply sign-extends
489c9b5090e2 Initial load
duke
parents:
diff changeset
   891
  const Node *shl = in(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   892
  if( shl->Opcode() != Op_LShiftI ) return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   893
489c9b5090e2 Initial load
duke
parents:
diff changeset
   894
  if( shift == 16 &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   895
      (t3 = phase->type(shl->in(2))->isa_int()) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   896
      t3->is_con(16) ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   897
    Node *ld = shl->in(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   898
    if( ld->Opcode() == Op_LoadS ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   899
      // Sign extension is just useless here.  Return a RShiftI of zero instead
489c9b5090e2 Initial load
duke
parents:
diff changeset
   900
      // returning 'ld' directly.  We cannot return an old Node directly as
489c9b5090e2 Initial load
duke
parents:
diff changeset
   901
      // that is the job of 'Identity' calls and Identity calls only work on
489c9b5090e2 Initial load
duke
parents:
diff changeset
   902
      // direct inputs ('ld' is an extra Node removed from 'this').  The
489c9b5090e2 Initial load
duke
parents:
diff changeset
   903
      // combined optimization requires Identity only return direct inputs.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   904
      set_req(1, ld);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   905
      set_req(2, phase->intcon(0));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   906
      return this;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   907
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   908
    else if( ld->Opcode() == Op_LoadC )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   909
      // Replace zero-extension-load with sign-extension-load
489c9b5090e2 Initial load
duke
parents:
diff changeset
   910
      return new (phase->C, 3) LoadSNode( ld->in(MemNode::Control),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   911
                                ld->in(MemNode::Memory),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   912
                                ld->in(MemNode::Address),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   913
                                ld->adr_type());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   914
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   915
489c9b5090e2 Initial load
duke
parents:
diff changeset
   916
  // Check for "(byte[i] <<24)>>24" which simply sign-extends
489c9b5090e2 Initial load
duke
parents:
diff changeset
   917
  if( shift == 24 &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   918
      (t3 = phase->type(shl->in(2))->isa_int()) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   919
      t3->is_con(24) ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   920
    Node *ld = shl->in(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   921
    if( ld->Opcode() == Op_LoadB ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   922
      // Sign extension is just useless here
489c9b5090e2 Initial load
duke
parents:
diff changeset
   923
      set_req(1, ld);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   924
      set_req(2, phase->intcon(0));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   925
      return this;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   926
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   927
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   928
489c9b5090e2 Initial load
duke
parents:
diff changeset
   929
  return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   930
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   931
489c9b5090e2 Initial load
duke
parents:
diff changeset
   932
//------------------------------Value------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   933
// A RShiftINode shifts its input2 right by input1 amount.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   934
const Type *RShiftINode::Value( PhaseTransform *phase ) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   935
  const Type *t1 = phase->type( in(1) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   936
  const Type *t2 = phase->type( in(2) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   937
  // Either input is TOP ==> the result is TOP
489c9b5090e2 Initial load
duke
parents:
diff changeset
   938
  if( t1 == Type::TOP ) return Type::TOP;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   939
  if( t2 == Type::TOP ) return Type::TOP;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   940
489c9b5090e2 Initial load
duke
parents:
diff changeset
   941
  // Left input is ZERO ==> the result is ZERO.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   942
  if( t1 == TypeInt::ZERO ) return TypeInt::ZERO;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   943
  // Shift by zero does nothing
489c9b5090e2 Initial load
duke
parents:
diff changeset
   944
  if( t2 == TypeInt::ZERO ) return t1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   945
489c9b5090e2 Initial load
duke
parents:
diff changeset
   946
  // Either input is BOTTOM ==> the result is BOTTOM
489c9b5090e2 Initial load
duke
parents:
diff changeset
   947
  if (t1 == Type::BOTTOM || t2 == Type::BOTTOM)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   948
    return TypeInt::INT;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   949
489c9b5090e2 Initial load
duke
parents:
diff changeset
   950
  if (t2 == TypeInt::INT)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   951
    return TypeInt::INT;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   952
489c9b5090e2 Initial load
duke
parents:
diff changeset
   953
  const TypeInt *r1 = t1->is_int(); // Handy access
489c9b5090e2 Initial load
duke
parents:
diff changeset
   954
  const TypeInt *r2 = t2->is_int(); // Handy access
489c9b5090e2 Initial load
duke
parents:
diff changeset
   955
489c9b5090e2 Initial load
duke
parents:
diff changeset
   956
  // If the shift is a constant, just shift the bounds of the type.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   957
  // For example, if the shift is 31, we just propagate sign bits.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   958
  if (r2->is_con()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   959
    uint shift = r2->get_con();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   960
    shift &= BitsPerJavaInteger-1;  // semantics of Java shifts
489c9b5090e2 Initial load
duke
parents:
diff changeset
   961
    // Shift by a multiple of 32 does nothing:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   962
    if (shift == 0)  return t1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   963
    // Calculate reasonably aggressive bounds for the result.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   964
    // This is necessary if we are to correctly type things
489c9b5090e2 Initial load
duke
parents:
diff changeset
   965
    // like (x<<24>>24) == ((byte)x).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   966
    jint lo = (jint)r1->_lo >> (jint)shift;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   967
    jint hi = (jint)r1->_hi >> (jint)shift;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   968
    assert(lo <= hi, "must have valid bounds");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   969
    const TypeInt* ti = TypeInt::make(lo, hi, MAX2(r1->_widen,r2->_widen));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   970
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   971
    // Make sure we get the sign-capture idiom correct.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   972
    if (shift == BitsPerJavaInteger-1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   973
      if (r1->_lo >= 0) assert(ti == TypeInt::ZERO,    ">>31 of + is  0");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   974
      if (r1->_hi <  0) assert(ti == TypeInt::MINUS_1, ">>31 of - is -1");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   975
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   976
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   977
    return ti;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   978
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   979
489c9b5090e2 Initial load
duke
parents:
diff changeset
   980
  if( !r1->is_con() || !r2->is_con() )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   981
    return TypeInt::INT;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   982
489c9b5090e2 Initial load
duke
parents:
diff changeset
   983
  // Signed shift right
489c9b5090e2 Initial load
duke
parents:
diff changeset
   984
  return TypeInt::make( r1->get_con() >> (r2->get_con()&31) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   985
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   986
489c9b5090e2 Initial load
duke
parents:
diff changeset
   987
//=============================================================================
489c9b5090e2 Initial load
duke
parents:
diff changeset
   988
//------------------------------Identity---------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   989
Node *RShiftLNode::Identity( PhaseTransform *phase ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   990
  const TypeInt *ti = phase->type( in(2) )->isa_int(); // shift count is an int
489c9b5090e2 Initial load
duke
parents:
diff changeset
   991
  return ( ti && ti->is_con() && ( ti->get_con() & ( BitsPerLong - 1 ) ) == 0 ) ? in(1) : this;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   992
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   993
489c9b5090e2 Initial load
duke
parents:
diff changeset
   994
//------------------------------Value------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   995
// A RShiftLNode shifts its input2 right by input1 amount.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   996
const Type *RShiftLNode::Value( PhaseTransform *phase ) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   997
  const Type *t1 = phase->type( in(1) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   998
  const Type *t2 = phase->type( in(2) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   999
  // Either input is TOP ==> the result is TOP
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1000
  if( t1 == Type::TOP ) return Type::TOP;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1001
  if( t2 == Type::TOP ) return Type::TOP;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1002
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1003
  // Left input is ZERO ==> the result is ZERO.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1004
  if( t1 == TypeLong::ZERO ) return TypeLong::ZERO;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1005
  // Shift by zero does nothing
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1006
  if( t2 == TypeInt::ZERO ) return t1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1007
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1008
  // Either input is BOTTOM ==> the result is BOTTOM
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1009
  if (t1 == Type::BOTTOM || t2 == Type::BOTTOM)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1010
    return TypeLong::LONG;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1011
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1012
  if (t2 == TypeInt::INT)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1013
    return TypeLong::LONG;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1014
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1015
  const TypeLong *r1 = t1->is_long(); // Handy access
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1016
  const TypeInt  *r2 = t2->is_int (); // Handy access
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1017
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1018
  // If the shift is a constant, just shift the bounds of the type.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1019
  // For example, if the shift is 63, we just propagate sign bits.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1020
  if (r2->is_con()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1021
    uint shift = r2->get_con();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1022
    shift &= (2*BitsPerJavaInteger)-1;  // semantics of Java shifts
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1023
    // Shift by a multiple of 64 does nothing:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1024
    if (shift == 0)  return t1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1025
    // Calculate reasonably aggressive bounds for the result.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1026
    // This is necessary if we are to correctly type things
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1027
    // like (x<<24>>24) == ((byte)x).
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1028
    jlong lo = (jlong)r1->_lo >> (jlong)shift;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1029
    jlong hi = (jlong)r1->_hi >> (jlong)shift;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1030
    assert(lo <= hi, "must have valid bounds");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1031
    const TypeLong* tl = TypeLong::make(lo, hi, MAX2(r1->_widen,r2->_widen));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1032
    #ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1033
    // Make sure we get the sign-capture idiom correct.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1034
    if (shift == (2*BitsPerJavaInteger)-1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1035
      if (r1->_lo >= 0) assert(tl == TypeLong::ZERO,    ">>63 of + is 0");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1036
      if (r1->_hi < 0)  assert(tl == TypeLong::MINUS_1, ">>63 of - is -1");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1037
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1038
    #endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1039
    return tl;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1040
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1041
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1042
  return TypeLong::LONG;                // Give up
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1043
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1044
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1045
//=============================================================================
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1046
//------------------------------Identity---------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1047
Node *URShiftINode::Identity( PhaseTransform *phase ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1048
  const TypeInt *ti = phase->type( in(2) )->isa_int();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1049
  if ( ti && ti->is_con() && ( ti->get_con() & ( BitsPerInt - 1 ) ) == 0 ) return in(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1050
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1051
  // Check for "((x << LogBytesPerWord) + (wordSize-1)) >> LogBytesPerWord" which is just "x".
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1052
  // Happens during new-array length computation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1053
  // Safe if 'x' is in the range [0..(max_int>>LogBytesPerWord)]
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1054
  Node *add = in(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1055
  if( add->Opcode() == Op_AddI ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1056
    const TypeInt *t2  = phase->type(add->in(2))->isa_int();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1057
    if( t2 && t2->is_con(wordSize - 1) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1058
        add->in(1)->Opcode() == Op_LShiftI ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1059
      // Check that shift_counts are LogBytesPerWord
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1060
      Node          *lshift_count   = add->in(1)->in(2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1061
      const TypeInt *t_lshift_count = phase->type(lshift_count)->isa_int();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1062
      if( t_lshift_count && t_lshift_count->is_con(LogBytesPerWord) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1063
          t_lshift_count == phase->type(in(2)) ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1064
        Node          *x   = add->in(1)->in(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1065
        const TypeInt *t_x = phase->type(x)->isa_int();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1066
        if( t_x != NULL && 0 <= t_x->_lo && t_x->_hi <= (max_jint>>LogBytesPerWord) ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1067
          return x;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1068
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1069
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1070
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1071
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1072
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1073
  return (phase->type(in(2))->higher_equal(TypeInt::ZERO)) ? in(1) : this;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1074
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1075
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1076
//------------------------------Ideal------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1077
Node *URShiftINode::Ideal(PhaseGVN *phase, bool can_reshape) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1078
  const TypeInt *t2 = phase->type( in(2) )->isa_int();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1079
  if( !t2 || !t2->is_con() ) return NULL; // Right input is a constant
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1080
  const int con = t2->get_con() & 31; // Shift count is always masked
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1081
  if ( con == 0 ) return NULL;  // let Identity() handle a 0 shift count
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1082
  // We'll be wanting the right-shift amount as a mask of that many bits
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1083
  const int mask = right_n_bits(BitsPerJavaInteger - con);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1084
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1085
  int in1_op = in(1)->Opcode();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1086
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1087
  // Check for ((x>>>a)>>>b) and replace with (x>>>(a+b)) when a+b < 32
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1088
  if( in1_op == Op_URShiftI ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1089
    const TypeInt *t12 = phase->type( in(1)->in(2) )->isa_int();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1090
    if( t12 && t12->is_con() ) { // Right input is a constant
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1091
      assert( in(1) != in(1)->in(1), "dead loop in URShiftINode::Ideal" );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1092
      const int con2 = t12->get_con() & 31; // Shift count is always masked
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1093
      const int con3 = con+con2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1094
      if( con3 < 32 )           // Only merge shifts if total is < 32
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1095
        return new (phase->C, 3) URShiftINode( in(1)->in(1), phase->intcon(con3) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1096
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1097
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1098
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1099
  // Check for ((x << z) + Y) >>> z.  Replace with x + con>>>z
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1100
  // The idiom for rounding to a power of 2 is "(Q+(2^z-1)) >>> z".
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1101
  // If Q is "X << z" the rounding is useless.  Look for patterns like
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1102
  // ((X<<Z) + Y) >>> Z  and replace with (X + Y>>>Z) & Z-mask.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1103
  Node *add = in(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1104
  if( in1_op == Op_AddI ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1105
    Node *lshl = add->in(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1106
    if( lshl->Opcode() == Op_LShiftI &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1107
        phase->type(lshl->in(2)) == t2 ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1108
      Node *y_z = phase->transform( new (phase->C, 3) URShiftINode(add->in(2),in(2)) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1109
      Node *sum = phase->transform( new (phase->C, 3) AddINode( lshl->in(1), y_z ) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1110
      return new (phase->C, 3) AndINode( sum, phase->intcon(mask) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1111
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1112
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1113
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1114
  // Check for (x & mask) >>> z.  Replace with (x >>> z) & (mask >>> z)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1115
  // This shortens the mask.  Also, if we are extracting a high byte and
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1116
  // storing it to a buffer, the mask will be removed completely.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1117
  Node *andi = in(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1118
  if( in1_op == Op_AndI ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1119
    const TypeInt *t3 = phase->type( andi->in(2) )->isa_int();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1120
    if( t3 && t3->is_con() ) { // Right input is a constant
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1121
      jint mask2 = t3->get_con();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1122
      mask2 >>= con;  // *signed* shift downward (high-order zeroes do not help)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1123
      Node *newshr = phase->transform( new (phase->C, 3) URShiftINode(andi->in(1), in(2)) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1124
      return new (phase->C, 3) AndINode(newshr, phase->intcon(mask2));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1125
      // The negative values are easier to materialize than positive ones.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1126
      // A typical case from address arithmetic is ((x & ~15) >> 4).
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1127
      // It's better to change that to ((x >> 4) & ~0) versus
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1128
      // ((x >> 4) & 0x0FFFFFFF).  The difference is greatest in LP64.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1129
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1130
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1131
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1132
  // Check for "(X << z ) >>> z" which simply zero-extends
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1133
  Node *shl = in(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1134
  if( in1_op == Op_LShiftI &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1135
      phase->type(shl->in(2)) == t2 )
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1136
    return new (phase->C, 3) AndINode( shl->in(1), phase->intcon(mask) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1137
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1138
  return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1139
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1140
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1141
//------------------------------Value------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1142
// A URShiftINode shifts its input2 right by input1 amount.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1143
const Type *URShiftINode::Value( PhaseTransform *phase ) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1144
  // (This is a near clone of RShiftINode::Value.)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1145
  const Type *t1 = phase->type( in(1) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1146
  const Type *t2 = phase->type( in(2) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1147
  // Either input is TOP ==> the result is TOP
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1148
  if( t1 == Type::TOP ) return Type::TOP;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1149
  if( t2 == Type::TOP ) return Type::TOP;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1150
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1151
  // Left input is ZERO ==> the result is ZERO.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1152
  if( t1 == TypeInt::ZERO ) return TypeInt::ZERO;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1153
  // Shift by zero does nothing
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1154
  if( t2 == TypeInt::ZERO ) return t1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1155
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1156
  // Either input is BOTTOM ==> the result is BOTTOM
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1157
  if (t1 == Type::BOTTOM || t2 == Type::BOTTOM)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1158
    return TypeInt::INT;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1159
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1160
  if (t2 == TypeInt::INT)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1161
    return TypeInt::INT;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1162
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1163
  const TypeInt *r1 = t1->is_int();     // Handy access
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1164
  const TypeInt *r2 = t2->is_int();     // Handy access
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1165
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1166
  if (r2->is_con()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1167
    uint shift = r2->get_con();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1168
    shift &= BitsPerJavaInteger-1;  // semantics of Java shifts
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1169
    // Shift by a multiple of 32 does nothing:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1170
    if (shift == 0)  return t1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1171
    // Calculate reasonably aggressive bounds for the result.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1172
    jint lo = (juint)r1->_lo >> (juint)shift;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1173
    jint hi = (juint)r1->_hi >> (juint)shift;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1174
    if (r1->_hi >= 0 && r1->_lo < 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1175
      // If the type has both negative and positive values,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1176
      // there are two separate sub-domains to worry about:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1177
      // The positive half and the negative half.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1178
      jint neg_lo = lo;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1179
      jint neg_hi = (juint)-1 >> (juint)shift;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1180
      jint pos_lo = (juint) 0 >> (juint)shift;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1181
      jint pos_hi = hi;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1182
      lo = MIN2(neg_lo, pos_lo);  // == 0
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1183
      hi = MAX2(neg_hi, pos_hi);  // == -1 >>> shift;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1184
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1185
    assert(lo <= hi, "must have valid bounds");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1186
    const TypeInt* ti = TypeInt::make(lo, hi, MAX2(r1->_widen,r2->_widen));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1187
    #ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1188
    // Make sure we get the sign-capture idiom correct.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1189
    if (shift == BitsPerJavaInteger-1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1190
      if (r1->_lo >= 0) assert(ti == TypeInt::ZERO, ">>>31 of + is 0");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1191
      if (r1->_hi < 0)  assert(ti == TypeInt::ONE,  ">>>31 of - is +1");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1192
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1193
    #endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1194
    return ti;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1195
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1196
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1197
  //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1198
  // Do not support shifted oops in info for GC
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1199
  //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1200
  // else if( t1->base() == Type::InstPtr ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1201
  //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1202
  //   const TypeInstPtr *o = t1->is_instptr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1203
  //   if( t1->singleton() )
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1204
  //     return TypeInt::make( ((uint32)o->const_oop() + o->_offset) >> shift );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1205
  // }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1206
  // else if( t1->base() == Type::KlassPtr ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1207
  //   const TypeKlassPtr *o = t1->is_klassptr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1208
  //   if( t1->singleton() )
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1209
  //     return TypeInt::make( ((uint32)o->const_oop() + o->_offset) >> shift );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1210
  // }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1211
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1212
  return TypeInt::INT;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1213
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1214
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1215
//=============================================================================
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1216
//------------------------------Identity---------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1217
Node *URShiftLNode::Identity( PhaseTransform *phase ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1218
  const TypeInt *ti = phase->type( in(2) )->isa_int(); // shift count is an int
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1219
  return ( ti && ti->is_con() && ( ti->get_con() & ( BitsPerLong - 1 ) ) == 0 ) ? in(1) : this;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1220
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1221
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1222
//------------------------------Ideal------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1223
Node *URShiftLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1224
  const TypeInt *t2 = phase->type( in(2) )->isa_int();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1225
  if( !t2 || !t2->is_con() ) return NULL; // Right input is a constant
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1226
  const int con = t2->get_con() & ( BitsPerLong - 1 ); // Shift count is always masked
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1227
  if ( con == 0 ) return NULL;  // let Identity() handle a 0 shift count
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1228
                              // note: mask computation below does not work for 0 shift count
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1229
  // We'll be wanting the right-shift amount as a mask of that many bits
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1230
  const jlong mask = (((jlong)CONST64(1) << (jlong)(BitsPerJavaInteger*2 - con)) -1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1231
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1232
  // Check for ((x << z) + Y) >>> z.  Replace with x + con>>>z
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1233
  // The idiom for rounding to a power of 2 is "(Q+(2^z-1)) >>> z".
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1234
  // If Q is "X << z" the rounding is useless.  Look for patterns like
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1235
  // ((X<<Z) + Y) >>> Z  and replace with (X + Y>>>Z) & Z-mask.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1236
  Node *add = in(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1237
  if( add->Opcode() == Op_AddL ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1238
    Node *lshl = add->in(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1239
    if( lshl->Opcode() == Op_LShiftL &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1240
        phase->type(lshl->in(2)) == t2 ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1241
      Node *y_z = phase->transform( new (phase->C, 3) URShiftLNode(add->in(2),in(2)) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1242
      Node *sum = phase->transform( new (phase->C, 3) AddLNode( lshl->in(1), y_z ) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1243
      return new (phase->C, 3) AndLNode( sum, phase->longcon(mask) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1244
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1245
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1246
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1247
  // Check for (x & mask) >>> z.  Replace with (x >>> z) & (mask >>> z)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1248
  // This shortens the mask.  Also, if we are extracting a high byte and
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1249
  // storing it to a buffer, the mask will be removed completely.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1250
  Node *andi = in(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1251
  if( andi->Opcode() == Op_AndL ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1252
    const TypeLong *t3 = phase->type( andi->in(2) )->isa_long();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1253
    if( t3 && t3->is_con() ) { // Right input is a constant
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1254
      jlong mask2 = t3->get_con();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1255
      mask2 >>= con;  // *signed* shift downward (high-order zeroes do not help)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1256
      Node *newshr = phase->transform( new (phase->C, 3) URShiftLNode(andi->in(1), in(2)) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1257
      return new (phase->C, 3) AndLNode(newshr, phase->longcon(mask2));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1258
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1259
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1260
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1261
  // Check for "(X << z ) >>> z" which simply zero-extends
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1262
  Node *shl = in(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1263
  if( shl->Opcode() == Op_LShiftL &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1264
      phase->type(shl->in(2)) == t2 )
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1265
    return new (phase->C, 3) AndLNode( shl->in(1), phase->longcon(mask) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1266
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1267
  return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1268
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1269
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1270
//------------------------------Value------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1271
// A URShiftINode shifts its input2 right by input1 amount.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1272
const Type *URShiftLNode::Value( PhaseTransform *phase ) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1273
  // (This is a near clone of RShiftLNode::Value.)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1274
  const Type *t1 = phase->type( in(1) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1275
  const Type *t2 = phase->type( in(2) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1276
  // Either input is TOP ==> the result is TOP
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1277
  if( t1 == Type::TOP ) return Type::TOP;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1278
  if( t2 == Type::TOP ) return Type::TOP;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1279
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1280
  // Left input is ZERO ==> the result is ZERO.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1281
  if( t1 == TypeLong::ZERO ) return TypeLong::ZERO;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1282
  // Shift by zero does nothing
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1283
  if( t2 == TypeInt::ZERO ) return t1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1284
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1285
  // Either input is BOTTOM ==> the result is BOTTOM
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1286
  if (t1 == Type::BOTTOM || t2 == Type::BOTTOM)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1287
    return TypeLong::LONG;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1288
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1289
  if (t2 == TypeInt::INT)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1290
    return TypeLong::LONG;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1291
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1292
  const TypeLong *r1 = t1->is_long(); // Handy access
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1293
  const TypeInt  *r2 = t2->is_int (); // Handy access
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1294
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1295
  if (r2->is_con()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1296
    uint shift = r2->get_con();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1297
    shift &= (2*BitsPerJavaInteger)-1;  // semantics of Java shifts
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1298
    // Shift by a multiple of 64 does nothing:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1299
    if (shift == 0)  return t1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1300
    // Calculate reasonably aggressive bounds for the result.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1301
    jlong lo = (julong)r1->_lo >> (juint)shift;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1302
    jlong hi = (julong)r1->_hi >> (juint)shift;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1303
    if (r1->_hi >= 0 && r1->_lo < 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1304
      // If the type has both negative and positive values,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1305
      // there are two separate sub-domains to worry about:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1306
      // The positive half and the negative half.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1307
      jlong neg_lo = lo;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1308
      jlong neg_hi = (julong)-1 >> (juint)shift;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1309
      jlong pos_lo = (julong) 0 >> (juint)shift;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1310
      jlong pos_hi = hi;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1311
      //lo = MIN2(neg_lo, pos_lo);  // == 0
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1312
      lo = neg_lo < pos_lo ? neg_lo : pos_lo;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1313
      //hi = MAX2(neg_hi, pos_hi);  // == -1 >>> shift;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1314
      hi = neg_hi > pos_hi ? neg_hi : pos_hi;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1315
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1316
    assert(lo <= hi, "must have valid bounds");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1317
    const TypeLong* tl = TypeLong::make(lo, hi, MAX2(r1->_widen,r2->_widen));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1318
    #ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1319
    // Make sure we get the sign-capture idiom correct.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1320
    if (shift == (2*BitsPerJavaInteger)-1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1321
      if (r1->_lo >= 0) assert(tl == TypeLong::ZERO, ">>>63 of + is 0");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1322
      if (r1->_hi < 0)  assert(tl == TypeLong::ONE,  ">>>63 of - is +1");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1323
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1324
    #endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1325
    return tl;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1326
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1327
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1328
  return TypeLong::LONG;                // Give up
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1329
}