hotspot/src/share/vm/opto/divnode.cpp
author iveresov
Thu, 22 Jan 2015 11:25:23 -0800
changeset 28723 0a36120cb225
parent 25930 eae8b7490d2c
child 28956 998d9d0b33a4
permissions -rw-r--r--
8071302: assert(!_reg_node[reg_lo] || edge_from_to(_reg_node[reg_lo], def)) failed: after block local Summary: Add merge nodes to node to block mapping Reviewed-by: kvn, vlivanov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
13963
e5b53c306fb5 7197424: update copyright year to match last edit in jdk8 hotspot repository
mikael
parents: 13895
diff changeset
     2
 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 4583
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 4583
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 4583
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 7115
diff changeset
    25
#include "precompiled.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 7115
diff changeset
    26
#include "memory/allocation.inline.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 7115
diff changeset
    27
#include "opto/addnode.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 7115
diff changeset
    28
#include "opto/connode.hpp"
23528
8f1a7f5e8066 8001532: C2 node files refactoring
morris
parents: 13963
diff changeset
    29
#include "opto/convertnode.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 7115
diff changeset
    30
#include "opto/divnode.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 7115
diff changeset
    31
#include "opto/machnode.hpp"
23528
8f1a7f5e8066 8001532: C2 node files refactoring
morris
parents: 13963
diff changeset
    32
#include "opto/movenode.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 7115
diff changeset
    33
#include "opto/matcher.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 7115
diff changeset
    34
#include "opto/mulnode.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 7115
diff changeset
    35
#include "opto/phaseX.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 7115
diff changeset
    36
#include "opto/subnode.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 7115
diff changeset
    37
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
// Portions of code courtesy of Clifford Click
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
// Optimization - Graph Style
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
#include <math.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    44
//----------------------magic_int_divide_constants-----------------------------
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    45
// Compute magic multiplier and shift constant for converting a 32 bit divide
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    46
// by constant into a multiply/shift/add series. Return false if calculations
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    47
// fail.
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    48
//
2131
98f9cef66a34 6810672: Comment typos
twisti
parents: 2032
diff changeset
    49
// Borrowed almost verbatim from Hacker's Delight by Henry S. Warren, Jr. with
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    50
// minor type name and parameter changes.
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    51
static bool magic_int_divide_constants(jint d, jint &M, jint &s) {
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    52
  int32_t p;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    53
  uint32_t ad, anc, delta, q1, r1, q2, r2, t;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    54
  const uint32_t two31 = 0x80000000L;     // 2**31.
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    55
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    56
  ad = ABS(d);
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    57
  if (d == 0 || d == 1) return false;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    58
  t = two31 + ((uint32_t)d >> 31);
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    59
  anc = t - 1 - t%ad;     // Absolute value of nc.
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    60
  p = 31;                 // Init. p.
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    61
  q1 = two31/anc;         // Init. q1 = 2**p/|nc|.
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    62
  r1 = two31 - q1*anc;    // Init. r1 = rem(2**p, |nc|).
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    63
  q2 = two31/ad;          // Init. q2 = 2**p/|d|.
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    64
  r2 = two31 - q2*ad;     // Init. r2 = rem(2**p, |d|).
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    65
  do {
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    66
    p = p + 1;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    67
    q1 = 2*q1;            // Update q1 = 2**p/|nc|.
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    68
    r1 = 2*r1;            // Update r1 = rem(2**p, |nc|).
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    69
    if (r1 >= anc) {      // (Must be an unsigned
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    70
      q1 = q1 + 1;        // comparison here).
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    71
      r1 = r1 - anc;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    72
    }
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    73
    q2 = 2*q2;            // Update q2 = 2**p/|d|.
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    74
    r2 = 2*r2;            // Update r2 = rem(2**p, |d|).
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    75
    if (r2 >= ad) {       // (Must be an unsigned
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    76
      q2 = q2 + 1;        // comparison here).
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    77
      r2 = r2 - ad;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    78
    }
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    79
    delta = ad - r2;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    80
  } while (q1 < delta || (q1 == delta && r1 == 0));
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    81
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    82
  M = q2 + 1;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    83
  if (d < 0) M = -M;      // Magic number and
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    84
  s = p - 32;             // shift amount to return.
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    85
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    86
  return true;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    87
}
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    88
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    89
//--------------------------transform_int_divide-------------------------------
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    90
// Convert a division by constant divisor into an alternate Ideal graph.
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    91
// Return NULL if no transformation occurs.
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    92
static Node *transform_int_divide( PhaseGVN *phase, Node *dividend, jint divisor ) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
  // Check for invalid divisors
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    95
  assert( divisor != 0 && divisor != min_jint,
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    96
          "bad divisor for transforming to long multiply" );
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    98
  bool d_pos = divisor >= 0;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
    99
  jint d = d_pos ? divisor : -divisor;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   100
  const int N = 32;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  // Result
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   103
  Node *q = NULL;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  if (d == 1) {
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   106
    // division by +/- 1
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   107
    if (!d_pos) {
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   108
      // Just negate the value
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   109
      q = new SubINode(phase->intcon(0), dividend);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
    }
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   111
  } else if ( is_power_of_2(d) ) {
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   112
    // division by +/- a power of 2
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
    // See if we can simply do a shift without rounding
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
    bool needs_rounding = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
    const Type *dt = phase->type(dividend);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
    const TypeInt *dti = dt->isa_int();
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   118
    if (dti && dti->_lo >= 0) {
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   119
      // we don't need to round a positive dividend
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
      needs_rounding = false;
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   121
    } else if( dividend->Opcode() == Op_AndI ) {
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   122
      // An AND mask of sufficient size clears the low bits and
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   123
      // I can avoid rounding.
1432
44f076e3d2a4 6667595: Set probability FAIR for pre-, post- loops and ALWAYS for main loop
kvn
parents: 1067
diff changeset
   124
      const TypeInt *andconi_t = phase->type( dividend->in(2) )->isa_int();
44f076e3d2a4 6667595: Set probability FAIR for pre-, post- loops and ALWAYS for main loop
kvn
parents: 1067
diff changeset
   125
      if( andconi_t && andconi_t->is_con() ) {
44f076e3d2a4 6667595: Set probability FAIR for pre-, post- loops and ALWAYS for main loop
kvn
parents: 1067
diff changeset
   126
        jint andconi = andconi_t->get_con();
44f076e3d2a4 6667595: Set probability FAIR for pre-, post- loops and ALWAYS for main loop
kvn
parents: 1067
diff changeset
   127
        if( andconi < 0 && is_power_of_2(-andconi) && (-andconi) >= d ) {
4583
b36d52bd2d19 6910484: incorrect integer optimization (loosing and op-r in a given example)
kvn
parents: 2746
diff changeset
   128
          if( (-andconi) == d ) // Remove AND if it clears bits which will be shifted
b36d52bd2d19 6910484: incorrect integer optimization (loosing and op-r in a given example)
kvn
parents: 2746
diff changeset
   129
            dividend = dividend->in(1);
1432
44f076e3d2a4 6667595: Set probability FAIR for pre-, post- loops and ALWAYS for main loop
kvn
parents: 1067
diff changeset
   130
          needs_rounding = false;
44f076e3d2a4 6667595: Set probability FAIR for pre-, post- loops and ALWAYS for main loop
kvn
parents: 1067
diff changeset
   131
        }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
    // Add rounding to the shift to handle the sign bit
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   136
    int l = log2_intptr(d-1)+1;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   137
    if (needs_rounding) {
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   138
      // Divide-by-power-of-2 can be made into a shift, but you have to do
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   139
      // more math for the rounding.  You need to add 0 for positive
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   140
      // numbers, and "i-1" for negative numbers.  Example: i=4, so the
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   141
      // shift is by 2.  You need to add 3 to negative dividends and 0 to
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   142
      // positive ones.  So (-7+3)>>2 becomes -1, (-4+3)>>2 becomes -1,
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   143
      // (-2+3)>>2 becomes 0, etc.
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   144
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   145
      // Compute 0 or -1, based on sign bit
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   146
      Node *sign = phase->transform(new RShiftINode(dividend, phase->intcon(N - 1)));
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   147
      // Mask sign bit to the low sign bits
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   148
      Node *round = phase->transform(new URShiftINode(sign, phase->intcon(N - l)));
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   149
      // Round up before shifting
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   150
      dividend = phase->transform(new AddINode(dividend, round));
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   153
    // Shift for division
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   154
    q = new RShiftINode(dividend, phase->intcon(l));
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   156
    if (!d_pos) {
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   157
      q = new SubINode(phase->intcon(0), phase->transform(q));
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   158
    }
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   159
  } else {
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   160
    // Attempt the jint constant divide -> multiply transform found in
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   161
    //   "Division by Invariant Integers using Multiplication"
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   162
    //     by Granlund and Montgomery
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   163
    // See also "Hacker's Delight", chapter 10 by Warren.
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   164
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   165
    jint magic_const;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   166
    jint shift_const;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   167
    if (magic_int_divide_constants(d, magic_const, shift_const)) {
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   168
      Node *magic = phase->longcon(magic_const);
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   169
      Node *dividend_long = phase->transform(new ConvI2LNode(dividend));
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   170
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   171
      // Compute the high half of the dividend x magic multiplication
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   172
      Node *mul_hi = phase->transform(new MulLNode(dividend_long, magic));
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   173
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   174
      if (magic_const < 0) {
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   175
        mul_hi = phase->transform(new RShiftLNode(mul_hi, phase->intcon(N)));
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   176
        mul_hi = phase->transform(new ConvL2INode(mul_hi));
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   177
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   178
        // The magic multiplier is too large for a 32 bit constant. We've adjusted
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   179
        // it down by 2^32, but have to add 1 dividend back in after the multiplication.
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   180
        // This handles the "overflow" case described by Granlund and Montgomery.
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   181
        mul_hi = phase->transform(new AddINode(dividend, mul_hi));
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   182
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   183
        // Shift over the (adjusted) mulhi
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   184
        if (shift_const != 0) {
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   185
          mul_hi = phase->transform(new RShiftINode(mul_hi, phase->intcon(shift_const)));
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   186
        }
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   187
      } else {
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   188
        // No add is required, we can merge the shifts together.
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   189
        mul_hi = phase->transform(new RShiftLNode(mul_hi, phase->intcon(N + shift_const)));
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   190
        mul_hi = phase->transform(new ConvL2INode(mul_hi));
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   191
      }
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   192
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   193
      // Get a 0 or -1 from the sign of the dividend.
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   194
      Node *addend0 = mul_hi;
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   195
      Node *addend1 = phase->transform(new RShiftINode(dividend, phase->intcon(N-1)));
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   196
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   197
      // If the divisor is negative, swap the order of the input addends;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   198
      // this has the effect of negating the quotient.
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   199
      if (!d_pos) {
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   200
        Node *temp = addend0; addend0 = addend1; addend1 = temp;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   201
      }
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   202
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   203
      // Adjust the final quotient by subtracting -1 (adding 1)
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   204
      // from the mul_hi.
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   205
      q = new SubINode(addend0, addend1);
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   206
    }
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   207
  }
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   208
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   209
  return q;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   210
}
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   211
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   212
//---------------------magic_long_divide_constants-----------------------------
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   213
// Compute magic multiplier and shift constant for converting a 64 bit divide
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   214
// by constant into a multiply/shift/add series. Return false if calculations
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   215
// fail.
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   216
//
2131
98f9cef66a34 6810672: Comment typos
twisti
parents: 2032
diff changeset
   217
// Borrowed almost verbatim from Hacker's Delight by Henry S. Warren, Jr. with
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   218
// minor type name and parameter changes.  Adjusted to 64 bit word width.
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   219
static bool magic_long_divide_constants(jlong d, jlong &M, jint &s) {
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   220
  int64_t p;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   221
  uint64_t ad, anc, delta, q1, r1, q2, r2, t;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   222
  const uint64_t two63 = 0x8000000000000000LL;     // 2**63.
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   223
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   224
  ad = ABS(d);
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   225
  if (d == 0 || d == 1) return false;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   226
  t = two63 + ((uint64_t)d >> 63);
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   227
  anc = t - 1 - t%ad;     // Absolute value of nc.
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   228
  p = 63;                 // Init. p.
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   229
  q1 = two63/anc;         // Init. q1 = 2**p/|nc|.
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   230
  r1 = two63 - q1*anc;    // Init. r1 = rem(2**p, |nc|).
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   231
  q2 = two63/ad;          // Init. q2 = 2**p/|d|.
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   232
  r2 = two63 - q2*ad;     // Init. r2 = rem(2**p, |d|).
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   233
  do {
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   234
    p = p + 1;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   235
    q1 = 2*q1;            // Update q1 = 2**p/|nc|.
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   236
    r1 = 2*r1;            // Update r1 = rem(2**p, |nc|).
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   237
    if (r1 >= anc) {      // (Must be an unsigned
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   238
      q1 = q1 + 1;        // comparison here).
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   239
      r1 = r1 - anc;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   240
    }
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   241
    q2 = 2*q2;            // Update q2 = 2**p/|d|.
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   242
    r2 = 2*r2;            // Update r2 = rem(2**p, |d|).
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   243
    if (r2 >= ad) {       // (Must be an unsigned
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   244
      q2 = q2 + 1;        // comparison here).
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   245
      r2 = r2 - ad;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   246
    }
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   247
    delta = ad - r2;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   248
  } while (q1 < delta || (q1 == delta && r1 == 0));
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   249
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   250
  M = q2 + 1;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   251
  if (d < 0) M = -M;      // Magic number and
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   252
  s = p - 64;             // shift amount to return.
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   253
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   254
  return true;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   255
}
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   256
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   257
//---------------------long_by_long_mulhi--------------------------------------
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   258
// Generate ideal node graph for upper half of a 64 bit x 64 bit multiplication
2031
24e034f56dcb 6800154: Add comments to long_by_long_mulhi() for better understandability
twisti
parents: 1436
diff changeset
   259
static Node* long_by_long_mulhi(PhaseGVN* phase, Node* dividend, jlong magic_const) {
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   260
  // If the architecture supports a 64x64 mulhi, there is
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   261
  // no need to synthesize it in ideal nodes.
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   262
  if (Matcher::has_match_rule(Op_MulHiL)) {
2031
24e034f56dcb 6800154: Add comments to long_by_long_mulhi() for better understandability
twisti
parents: 1436
diff changeset
   263
    Node* v = phase->longcon(magic_const);
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   264
    return new MulHiLNode(dividend, v);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
2031
24e034f56dcb 6800154: Add comments to long_by_long_mulhi() for better understandability
twisti
parents: 1436
diff changeset
   267
  // Taken from Hacker's Delight, Fig. 8-2. Multiply high signed.
24e034f56dcb 6800154: Add comments to long_by_long_mulhi() for better understandability
twisti
parents: 1436
diff changeset
   268
  // (http://www.hackersdelight.org/HDcode/mulhs.c)
24e034f56dcb 6800154: Add comments to long_by_long_mulhi() for better understandability
twisti
parents: 1436
diff changeset
   269
  //
24e034f56dcb 6800154: Add comments to long_by_long_mulhi() for better understandability
twisti
parents: 1436
diff changeset
   270
  // int mulhs(int u, int v) {
24e034f56dcb 6800154: Add comments to long_by_long_mulhi() for better understandability
twisti
parents: 1436
diff changeset
   271
  //    unsigned u0, v0, w0;
24e034f56dcb 6800154: Add comments to long_by_long_mulhi() for better understandability
twisti
parents: 1436
diff changeset
   272
  //    int u1, v1, w1, w2, t;
24e034f56dcb 6800154: Add comments to long_by_long_mulhi() for better understandability
twisti
parents: 1436
diff changeset
   273
  //
24e034f56dcb 6800154: Add comments to long_by_long_mulhi() for better understandability
twisti
parents: 1436
diff changeset
   274
  //    u0 = u & 0xFFFF;  u1 = u >> 16;
24e034f56dcb 6800154: Add comments to long_by_long_mulhi() for better understandability
twisti
parents: 1436
diff changeset
   275
  //    v0 = v & 0xFFFF;  v1 = v >> 16;
24e034f56dcb 6800154: Add comments to long_by_long_mulhi() for better understandability
twisti
parents: 1436
diff changeset
   276
  //    w0 = u0*v0;
24e034f56dcb 6800154: Add comments to long_by_long_mulhi() for better understandability
twisti
parents: 1436
diff changeset
   277
  //    t  = u1*v0 + (w0 >> 16);
24e034f56dcb 6800154: Add comments to long_by_long_mulhi() for better understandability
twisti
parents: 1436
diff changeset
   278
  //    w1 = t & 0xFFFF;
24e034f56dcb 6800154: Add comments to long_by_long_mulhi() for better understandability
twisti
parents: 1436
diff changeset
   279
  //    w2 = t >> 16;
24e034f56dcb 6800154: Add comments to long_by_long_mulhi() for better understandability
twisti
parents: 1436
diff changeset
   280
  //    w1 = u0*v1 + w1;
24e034f56dcb 6800154: Add comments to long_by_long_mulhi() for better understandability
twisti
parents: 1436
diff changeset
   281
  //    return u1*v1 + w2 + (w1 >> 16);
24e034f56dcb 6800154: Add comments to long_by_long_mulhi() for better understandability
twisti
parents: 1436
diff changeset
   282
  // }
24e034f56dcb 6800154: Add comments to long_by_long_mulhi() for better understandability
twisti
parents: 1436
diff changeset
   283
  //
24e034f56dcb 6800154: Add comments to long_by_long_mulhi() for better understandability
twisti
parents: 1436
diff changeset
   284
  // Note: The version above is for 32x32 multiplications, while the
24e034f56dcb 6800154: Add comments to long_by_long_mulhi() for better understandability
twisti
parents: 1436
diff changeset
   285
  // following inline comments are adapted to 64x64.
24e034f56dcb 6800154: Add comments to long_by_long_mulhi() for better understandability
twisti
parents: 1436
diff changeset
   286
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   287
  const int N = 64;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   288
12956
1078d92535e8 7169782: C2: SIGSEGV in LShiftLNode::Ideal(PhaseGVN*, bool)
kvn
parents: 7397
diff changeset
   289
  // Dummy node to keep intermediate nodes alive during construction
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   290
  Node* hook = new Node(4);
12956
1078d92535e8 7169782: C2: SIGSEGV in LShiftLNode::Ideal(PhaseGVN*, bool)
kvn
parents: 7397
diff changeset
   291
2031
24e034f56dcb 6800154: Add comments to long_by_long_mulhi() for better understandability
twisti
parents: 1436
diff changeset
   292
  // u0 = u & 0xFFFFFFFF;  u1 = u >> 32;
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   293
  Node* u0 = phase->transform(new AndLNode(dividend, phase->longcon(0xFFFFFFFF)));
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   294
  Node* u1 = phase->transform(new RShiftLNode(dividend, phase->intcon(N / 2)));
12956
1078d92535e8 7169782: C2: SIGSEGV in LShiftLNode::Ideal(PhaseGVN*, bool)
kvn
parents: 7397
diff changeset
   295
  hook->init_req(0, u0);
1078d92535e8 7169782: C2: SIGSEGV in LShiftLNode::Ideal(PhaseGVN*, bool)
kvn
parents: 7397
diff changeset
   296
  hook->init_req(1, u1);
2031
24e034f56dcb 6800154: Add comments to long_by_long_mulhi() for better understandability
twisti
parents: 1436
diff changeset
   297
24e034f56dcb 6800154: Add comments to long_by_long_mulhi() for better understandability
twisti
parents: 1436
diff changeset
   298
  // v0 = v & 0xFFFFFFFF;  v1 = v >> 32;
24e034f56dcb 6800154: Add comments to long_by_long_mulhi() for better understandability
twisti
parents: 1436
diff changeset
   299
  Node* v0 = phase->longcon(magic_const & 0xFFFFFFFF);
24e034f56dcb 6800154: Add comments to long_by_long_mulhi() for better understandability
twisti
parents: 1436
diff changeset
   300
  Node* v1 = phase->longcon(magic_const >> (N / 2));
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   301
2031
24e034f56dcb 6800154: Add comments to long_by_long_mulhi() for better understandability
twisti
parents: 1436
diff changeset
   302
  // w0 = u0*v0;
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   303
  Node* w0 = phase->transform(new MulLNode(u0, v0));
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   304
2031
24e034f56dcb 6800154: Add comments to long_by_long_mulhi() for better understandability
twisti
parents: 1436
diff changeset
   305
  // t = u1*v0 + (w0 >> 32);
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   306
  Node* u1v0 = phase->transform(new MulLNode(u1, v0));
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   307
  Node* temp = phase->transform(new URShiftLNode(w0, phase->intcon(N / 2)));
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   308
  Node* t    = phase->transform(new AddLNode(u1v0, temp));
12956
1078d92535e8 7169782: C2: SIGSEGV in LShiftLNode::Ideal(PhaseGVN*, bool)
kvn
parents: 7397
diff changeset
   309
  hook->init_req(2, t);
2031
24e034f56dcb 6800154: Add comments to long_by_long_mulhi() for better understandability
twisti
parents: 1436
diff changeset
   310
24e034f56dcb 6800154: Add comments to long_by_long_mulhi() for better understandability
twisti
parents: 1436
diff changeset
   311
  // w1 = t & 0xFFFFFFFF;
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   312
  Node* w1 = phase->transform(new AndLNode(t, phase->longcon(0xFFFFFFFF)));
12956
1078d92535e8 7169782: C2: SIGSEGV in LShiftLNode::Ideal(PhaseGVN*, bool)
kvn
parents: 7397
diff changeset
   313
  hook->init_req(3, w1);
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   314
2031
24e034f56dcb 6800154: Add comments to long_by_long_mulhi() for better understandability
twisti
parents: 1436
diff changeset
   315
  // w2 = t >> 32;
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   316
  Node* w2 = phase->transform(new RShiftLNode(t, phase->intcon(N / 2)));
1056
da0241911ea8 6732154: REG: Printing an Image using image/gif doc flavor crashes the VM, Solsparc
rasbold
parents: 670
diff changeset
   317
2031
24e034f56dcb 6800154: Add comments to long_by_long_mulhi() for better understandability
twisti
parents: 1436
diff changeset
   318
  // w1 = u0*v1 + w1;
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   319
  Node* u0v1 = phase->transform(new MulLNode(u0, v1));
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   320
  w1         = phase->transform(new AddLNode(u0v1, w1));
1056
da0241911ea8 6732154: REG: Printing an Image using image/gif doc flavor crashes the VM, Solsparc
rasbold
parents: 670
diff changeset
   321
2031
24e034f56dcb 6800154: Add comments to long_by_long_mulhi() for better understandability
twisti
parents: 1436
diff changeset
   322
  // return u1*v1 + w2 + (w1 >> 32);
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   323
  Node* u1v1  = phase->transform(new MulLNode(u1, v1));
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   324
  Node* temp1 = phase->transform(new AddLNode(u1v1, w2));
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   325
  Node* temp2 = phase->transform(new RShiftLNode(w1, phase->intcon(N / 2)));
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   326
12956
1078d92535e8 7169782: C2: SIGSEGV in LShiftLNode::Ideal(PhaseGVN*, bool)
kvn
parents: 7397
diff changeset
   327
  // Remove the bogus extra edges used to keep things alive
1078d92535e8 7169782: C2: SIGSEGV in LShiftLNode::Ideal(PhaseGVN*, bool)
kvn
parents: 7397
diff changeset
   328
  PhaseIterGVN* igvn = phase->is_IterGVN();
1078d92535e8 7169782: C2: SIGSEGV in LShiftLNode::Ideal(PhaseGVN*, bool)
kvn
parents: 7397
diff changeset
   329
  if (igvn != NULL) {
1078d92535e8 7169782: C2: SIGSEGV in LShiftLNode::Ideal(PhaseGVN*, bool)
kvn
parents: 7397
diff changeset
   330
    igvn->remove_dead_node(hook);
1078d92535e8 7169782: C2: SIGSEGV in LShiftLNode::Ideal(PhaseGVN*, bool)
kvn
parents: 7397
diff changeset
   331
  } else {
1078d92535e8 7169782: C2: SIGSEGV in LShiftLNode::Ideal(PhaseGVN*, bool)
kvn
parents: 7397
diff changeset
   332
    for (int i = 0; i < 4; i++) {
1078d92535e8 7169782: C2: SIGSEGV in LShiftLNode::Ideal(PhaseGVN*, bool)
kvn
parents: 7397
diff changeset
   333
      hook->set_req(i, NULL);
1078d92535e8 7169782: C2: SIGSEGV in LShiftLNode::Ideal(PhaseGVN*, bool)
kvn
parents: 7397
diff changeset
   334
    }
1078d92535e8 7169782: C2: SIGSEGV in LShiftLNode::Ideal(PhaseGVN*, bool)
kvn
parents: 7397
diff changeset
   335
  }
1078d92535e8 7169782: C2: SIGSEGV in LShiftLNode::Ideal(PhaseGVN*, bool)
kvn
parents: 7397
diff changeset
   336
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   337
  return new AddLNode(temp1, temp2);
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   338
}
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   339
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   340
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   341
//--------------------------transform_long_divide------------------------------
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   342
// Convert a division by constant divisor into an alternate Ideal graph.
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   343
// Return NULL if no transformation occurs.
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   344
static Node *transform_long_divide( PhaseGVN *phase, Node *dividend, jlong divisor ) {
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   345
  // Check for invalid divisors
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   346
  assert( divisor != 0L && divisor != min_jlong,
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   347
          "bad divisor for transforming to long multiply" );
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   348
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   349
  bool d_pos = divisor >= 0;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   350
  jlong d = d_pos ? divisor : -divisor;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   351
  const int N = 64;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   352
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   353
  // Result
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   354
  Node *q = NULL;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   355
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   356
  if (d == 1) {
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   357
    // division by +/- 1
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   358
    if (!d_pos) {
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   359
      // Just negate the value
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   360
      q = new SubLNode(phase->longcon(0), dividend);
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   361
    }
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   362
  } else if ( is_power_of_2_long(d) ) {
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   363
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   364
    // division by +/- a power of 2
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   365
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   366
    // See if we can simply do a shift without rounding
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   367
    bool needs_rounding = true;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   368
    const Type *dt = phase->type(dividend);
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   369
    const TypeLong *dtl = dt->isa_long();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   371
    if (dtl && dtl->_lo > 0) {
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   372
      // we don't need to round a positive dividend
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   373
      needs_rounding = false;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   374
    } else if( dividend->Opcode() == Op_AndL ) {
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   375
      // An AND mask of sufficient size clears the low bits and
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   376
      // I can avoid rounding.
1432
44f076e3d2a4 6667595: Set probability FAIR for pre-, post- loops and ALWAYS for main loop
kvn
parents: 1067
diff changeset
   377
      const TypeLong *andconl_t = phase->type( dividend->in(2) )->isa_long();
44f076e3d2a4 6667595: Set probability FAIR for pre-, post- loops and ALWAYS for main loop
kvn
parents: 1067
diff changeset
   378
      if( andconl_t && andconl_t->is_con() ) {
44f076e3d2a4 6667595: Set probability FAIR for pre-, post- loops and ALWAYS for main loop
kvn
parents: 1067
diff changeset
   379
        jlong andconl = andconl_t->get_con();
44f076e3d2a4 6667595: Set probability FAIR for pre-, post- loops and ALWAYS for main loop
kvn
parents: 1067
diff changeset
   380
        if( andconl < 0 && is_power_of_2_long(-andconl) && (-andconl) >= d ) {
4583
b36d52bd2d19 6910484: incorrect integer optimization (loosing and op-r in a given example)
kvn
parents: 2746
diff changeset
   381
          if( (-andconl) == d ) // Remove AND if it clears bits which will be shifted
b36d52bd2d19 6910484: incorrect integer optimization (loosing and op-r in a given example)
kvn
parents: 2746
diff changeset
   382
            dividend = dividend->in(1);
1432
44f076e3d2a4 6667595: Set probability FAIR for pre-, post- loops and ALWAYS for main loop
kvn
parents: 1067
diff changeset
   383
          needs_rounding = false;
44f076e3d2a4 6667595: Set probability FAIR for pre-, post- loops and ALWAYS for main loop
kvn
parents: 1067
diff changeset
   384
        }
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   385
      }
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   386
    }
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   387
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   388
    // Add rounding to the shift to handle the sign bit
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   389
    int l = log2_long(d-1)+1;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   390
    if (needs_rounding) {
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   391
      // Divide-by-power-of-2 can be made into a shift, but you have to do
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   392
      // more math for the rounding.  You need to add 0 for positive
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   393
      // numbers, and "i-1" for negative numbers.  Example: i=4, so the
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   394
      // shift is by 2.  You need to add 3 to negative dividends and 0 to
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   395
      // positive ones.  So (-7+3)>>2 becomes -1, (-4+3)>>2 becomes -1,
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   396
      // (-2+3)>>2 becomes 0, etc.
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   397
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   398
      // Compute 0 or -1, based on sign bit
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   399
      Node *sign = phase->transform(new RShiftLNode(dividend, phase->intcon(N - 1)));
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   400
      // Mask sign bit to the low sign bits
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   401
      Node *round = phase->transform(new URShiftLNode(sign, phase->intcon(N - l)));
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   402
      // Round up before shifting
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   403
      dividend = phase->transform(new AddLNode(dividend, round));
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   404
    }
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   405
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   406
    // Shift for division
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   407
    q = new RShiftLNode(dividend, phase->intcon(l));
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   408
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   409
    if (!d_pos) {
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   410
      q = new SubLNode(phase->longcon(0), phase->transform(q));
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   411
    }
7115
32300e243300 6987135: Performance regression on Intel platform with 32-bits edition between 6u13 and 6u14.
kvn
parents: 5547
diff changeset
   412
  } else if ( !Matcher::use_asm_for_ldiv_by_con(d) ) { // Use hardware DIV instruction when
32300e243300 6987135: Performance regression on Intel platform with 32-bits edition between 6u13 and 6u14.
kvn
parents: 5547
diff changeset
   413
                                                       // it is faster than code generated below.
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   414
    // Attempt the jlong constant divide -> multiply transform found in
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   415
    //   "Division by Invariant Integers using Multiplication"
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   416
    //     by Granlund and Montgomery
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   417
    // See also "Hacker's Delight", chapter 10 by Warren.
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   418
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   419
    jlong magic_const;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   420
    jint shift_const;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   421
    if (magic_long_divide_constants(d, magic_const, shift_const)) {
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   422
      // Compute the high half of the dividend x magic multiplication
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   423
      Node *mul_hi = phase->transform(long_by_long_mulhi(phase, dividend, magic_const));
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   424
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   425
      // The high half of the 128-bit multiply is computed.
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   426
      if (magic_const < 0) {
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   427
        // The magic multiplier is too large for a 64 bit constant. We've adjusted
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   428
        // it down by 2^64, but have to add 1 dividend back in after the multiplication.
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   429
        // This handles the "overflow" case described by Granlund and Montgomery.
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   430
        mul_hi = phase->transform(new AddLNode(dividend, mul_hi));
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   431
      }
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   432
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   433
      // Shift over the (adjusted) mulhi
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   434
      if (shift_const != 0) {
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   435
        mul_hi = phase->transform(new RShiftLNode(mul_hi, phase->intcon(shift_const)));
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   436
      }
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   437
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   438
      // Get a 0 or -1 from the sign of the dividend.
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   439
      Node *addend0 = mul_hi;
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   440
      Node *addend1 = phase->transform(new RShiftLNode(dividend, phase->intcon(N-1)));
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   441
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   442
      // If the divisor is negative, swap the order of the input addends;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   443
      // this has the effect of negating the quotient.
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   444
      if (!d_pos) {
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   445
        Node *temp = addend0; addend0 = addend1; addend1 = temp;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   446
      }
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   447
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   448
      // Adjust the final quotient by subtracting -1 (adding 1)
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   449
      // from the mul_hi.
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   450
      q = new SubLNode(addend0, addend1);
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   451
    }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   452
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   453
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   454
  return q;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   455
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   456
489c9b5090e2 Initial load
duke
parents:
diff changeset
   457
//=============================================================================
489c9b5090e2 Initial load
duke
parents:
diff changeset
   458
//------------------------------Identity---------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   459
// If the divisor is 1, we are an identity on the dividend.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   460
Node *DivINode::Identity( PhaseTransform *phase ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   461
  return (phase->type( in(2) )->higher_equal(TypeInt::ONE)) ? in(1) : this;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   462
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   463
489c9b5090e2 Initial load
duke
parents:
diff changeset
   464
//------------------------------Idealize---------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   465
// Divides can be changed to multiplies and/or shifts
489c9b5090e2 Initial load
duke
parents:
diff changeset
   466
Node *DivINode::Ideal(PhaseGVN *phase, bool can_reshape) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   467
  if (in(0) && remove_dead_region(phase, can_reshape))  return this;
1067
f82e0a8cd438 6736417: Fastdebug C2 crashes in StoreBNode::Ideal
kvn
parents: 1056
diff changeset
   468
  // Don't bother trying to transform a dead node
f82e0a8cd438 6736417: Fastdebug C2 crashes in StoreBNode::Ideal
kvn
parents: 1056
diff changeset
   469
  if( in(0) && in(0)->is_top() )  return NULL;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   470
489c9b5090e2 Initial load
duke
parents:
diff changeset
   471
  const Type *t = phase->type( in(2) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   472
  if( t == TypeInt::ONE )       // Identity?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   473
    return NULL;                // Skip it
489c9b5090e2 Initial load
duke
parents:
diff changeset
   474
489c9b5090e2 Initial load
duke
parents:
diff changeset
   475
  const TypeInt *ti = t->isa_int();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   476
  if( !ti ) return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   477
  if( !ti->is_con() ) return NULL;
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   478
  jint i = ti->get_con();       // Get divisor
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   479
489c9b5090e2 Initial load
duke
parents:
diff changeset
   480
  if (i == 0) return NULL;      // Dividing by zero constant does not idealize
489c9b5090e2 Initial load
duke
parents:
diff changeset
   481
25913
81dbc151e91c 8040213: C2 does not put all modified nodes on IGVN worklist
thartmann
parents: 24923
diff changeset
   482
  if (in(0) != NULL) {
81dbc151e91c 8040213: C2 does not put all modified nodes on IGVN worklist
thartmann
parents: 24923
diff changeset
   483
    phase->igvn_rehash_node_delayed(this);
81dbc151e91c 8040213: C2 does not put all modified nodes on IGVN worklist
thartmann
parents: 24923
diff changeset
   484
    set_req(0, NULL);           // Dividing by a not-zero constant; no faulting
81dbc151e91c 8040213: C2 does not put all modified nodes on IGVN worklist
thartmann
parents: 24923
diff changeset
   485
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   486
489c9b5090e2 Initial load
duke
parents:
diff changeset
   487
  // Dividing by MININT does not optimize as a power-of-2 shift.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   488
  if( i == min_jint ) return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   489
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   490
  return transform_int_divide( phase, in(1), i );
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   491
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   492
489c9b5090e2 Initial load
duke
parents:
diff changeset
   493
//------------------------------Value------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   494
// A DivINode divides its inputs.  The third input is a Control input, used to
489c9b5090e2 Initial load
duke
parents:
diff changeset
   495
// prevent hoisting the divide above an unsafe test.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   496
const Type *DivINode::Value( PhaseTransform *phase ) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   497
  // Either input is TOP ==> the result is TOP
489c9b5090e2 Initial load
duke
parents:
diff changeset
   498
  const Type *t1 = phase->type( in(1) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   499
  const Type *t2 = phase->type( in(2) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   500
  if( t1 == Type::TOP ) return Type::TOP;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   501
  if( t2 == Type::TOP ) return Type::TOP;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   502
489c9b5090e2 Initial load
duke
parents:
diff changeset
   503
  // x/x == 1 since we always generate the dynamic divisor check for 0.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   504
  if( phase->eqv( in(1), in(2) ) )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   505
    return TypeInt::ONE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   506
489c9b5090e2 Initial load
duke
parents:
diff changeset
   507
  // Either input is BOTTOM ==> the result is the local BOTTOM
489c9b5090e2 Initial load
duke
parents:
diff changeset
   508
  const Type *bot = bottom_type();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   509
  if( (t1 == bot) || (t2 == bot) ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   510
      (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   511
    return bot;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   512
489c9b5090e2 Initial load
duke
parents:
diff changeset
   513
  // Divide the two numbers.  We approximate.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   514
  // If divisor is a constant and not zero
489c9b5090e2 Initial load
duke
parents:
diff changeset
   515
  const TypeInt *i1 = t1->is_int();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   516
  const TypeInt *i2 = t2->is_int();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   517
  int widen = MAX2(i1->_widen, i2->_widen);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   518
489c9b5090e2 Initial load
duke
parents:
diff changeset
   519
  if( i2->is_con() && i2->get_con() != 0 ) {
24425
53764d2358f9 8041415: remove port.{cpp,hpp} files
zgu
parents: 23528
diff changeset
   520
    int32_t d = i2->get_con(); // Divisor
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   521
    jint lo, hi;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   522
    if( d >= 0 ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   523
      lo = i1->_lo/d;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   524
      hi = i1->_hi/d;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   525
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   526
      if( d == -1 && i1->_lo == min_jint ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   527
        // 'min_jint/-1' throws arithmetic exception during compilation
489c9b5090e2 Initial load
duke
parents:
diff changeset
   528
        lo = min_jint;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   529
        // do not support holes, 'hi' must go to either min_jint or max_jint:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   530
        // [min_jint, -10]/[-1,-1] ==> [min_jint] UNION [10,max_jint]
489c9b5090e2 Initial load
duke
parents:
diff changeset
   531
        hi = i1->_hi == min_jint ? min_jint : max_jint;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   532
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   533
        lo = i1->_hi/d;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   534
        hi = i1->_lo/d;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   535
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   536
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   537
    return TypeInt::make(lo, hi, widen);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   538
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   539
489c9b5090e2 Initial load
duke
parents:
diff changeset
   540
  // If the dividend is a constant
489c9b5090e2 Initial load
duke
parents:
diff changeset
   541
  if( i1->is_con() ) {
24425
53764d2358f9 8041415: remove port.{cpp,hpp} files
zgu
parents: 23528
diff changeset
   542
    int32_t d = i1->get_con();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   543
    if( d < 0 ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   544
      if( d == min_jint ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   545
        //  (-min_jint) == min_jint == (min_jint / -1)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   546
        return TypeInt::make(min_jint, max_jint/2 + 1, widen);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   547
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   548
        return TypeInt::make(d, -d, widen);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   549
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   550
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   551
    return TypeInt::make(-d, d, widen);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   552
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   553
489c9b5090e2 Initial load
duke
parents:
diff changeset
   554
  // Otherwise we give up all hope
489c9b5090e2 Initial load
duke
parents:
diff changeset
   555
  return TypeInt::INT;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   556
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   557
489c9b5090e2 Initial load
duke
parents:
diff changeset
   558
489c9b5090e2 Initial load
duke
parents:
diff changeset
   559
//=============================================================================
489c9b5090e2 Initial load
duke
parents:
diff changeset
   560
//------------------------------Identity---------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   561
// If the divisor is 1, we are an identity on the dividend.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   562
Node *DivLNode::Identity( PhaseTransform *phase ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   563
  return (phase->type( in(2) )->higher_equal(TypeLong::ONE)) ? in(1) : this;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   564
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   565
489c9b5090e2 Initial load
duke
parents:
diff changeset
   566
//------------------------------Idealize---------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   567
// Dividing by a power of 2 is a shift.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   568
Node *DivLNode::Ideal( PhaseGVN *phase, bool can_reshape) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   569
  if (in(0) && remove_dead_region(phase, can_reshape))  return this;
1067
f82e0a8cd438 6736417: Fastdebug C2 crashes in StoreBNode::Ideal
kvn
parents: 1056
diff changeset
   570
  // Don't bother trying to transform a dead node
f82e0a8cd438 6736417: Fastdebug C2 crashes in StoreBNode::Ideal
kvn
parents: 1056
diff changeset
   571
  if( in(0) && in(0)->is_top() )  return NULL;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   572
489c9b5090e2 Initial load
duke
parents:
diff changeset
   573
  const Type *t = phase->type( in(2) );
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   574
  if( t == TypeLong::ONE )      // Identity?
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   575
    return NULL;                // Skip it
489c9b5090e2 Initial load
duke
parents:
diff changeset
   576
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   577
  const TypeLong *tl = t->isa_long();
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   578
  if( !tl ) return NULL;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   579
  if( !tl->is_con() ) return NULL;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   580
  jlong l = tl->get_con();      // Get divisor
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   581
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   582
  if (l == 0) return NULL;      // Dividing by zero constant does not idealize
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   583
25913
81dbc151e91c 8040213: C2 does not put all modified nodes on IGVN worklist
thartmann
parents: 24923
diff changeset
   584
  if (in(0) != NULL) {
81dbc151e91c 8040213: C2 does not put all modified nodes on IGVN worklist
thartmann
parents: 24923
diff changeset
   585
    phase->igvn_rehash_node_delayed(this);
81dbc151e91c 8040213: C2 does not put all modified nodes on IGVN worklist
thartmann
parents: 24923
diff changeset
   586
    set_req(0, NULL);           // Dividing by a not-zero constant; no faulting
81dbc151e91c 8040213: C2 does not put all modified nodes on IGVN worklist
thartmann
parents: 24923
diff changeset
   587
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   588
7115
32300e243300 6987135: Performance regression on Intel platform with 32-bits edition between 6u13 and 6u14.
kvn
parents: 5547
diff changeset
   589
  // Dividing by MINLONG does not optimize as a power-of-2 shift.
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   590
  if( l == min_jlong ) return NULL;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   591
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   592
  return transform_long_divide( phase, in(1), l );
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   593
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   594
489c9b5090e2 Initial load
duke
parents:
diff changeset
   595
//------------------------------Value------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   596
// A DivLNode divides its inputs.  The third input is a Control input, used to
489c9b5090e2 Initial load
duke
parents:
diff changeset
   597
// prevent hoisting the divide above an unsafe test.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   598
const Type *DivLNode::Value( PhaseTransform *phase ) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   599
  // Either input is TOP ==> the result is TOP
489c9b5090e2 Initial load
duke
parents:
diff changeset
   600
  const Type *t1 = phase->type( in(1) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   601
  const Type *t2 = phase->type( in(2) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   602
  if( t1 == Type::TOP ) return Type::TOP;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   603
  if( t2 == Type::TOP ) return Type::TOP;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   604
489c9b5090e2 Initial load
duke
parents:
diff changeset
   605
  // x/x == 1 since we always generate the dynamic divisor check for 0.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   606
  if( phase->eqv( in(1), in(2) ) )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   607
    return TypeLong::ONE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   608
489c9b5090e2 Initial load
duke
parents:
diff changeset
   609
  // Either input is BOTTOM ==> the result is the local BOTTOM
489c9b5090e2 Initial load
duke
parents:
diff changeset
   610
  const Type *bot = bottom_type();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   611
  if( (t1 == bot) || (t2 == bot) ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   612
      (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   613
    return bot;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   614
489c9b5090e2 Initial load
duke
parents:
diff changeset
   615
  // Divide the two numbers.  We approximate.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   616
  // If divisor is a constant and not zero
489c9b5090e2 Initial load
duke
parents:
diff changeset
   617
  const TypeLong *i1 = t1->is_long();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   618
  const TypeLong *i2 = t2->is_long();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   619
  int widen = MAX2(i1->_widen, i2->_widen);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   620
489c9b5090e2 Initial load
duke
parents:
diff changeset
   621
  if( i2->is_con() && i2->get_con() != 0 ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   622
    jlong d = i2->get_con();    // Divisor
489c9b5090e2 Initial load
duke
parents:
diff changeset
   623
    jlong lo, hi;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   624
    if( d >= 0 ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   625
      lo = i1->_lo/d;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   626
      hi = i1->_hi/d;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   627
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   628
      if( d == CONST64(-1) && i1->_lo == min_jlong ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   629
        // 'min_jlong/-1' throws arithmetic exception during compilation
489c9b5090e2 Initial load
duke
parents:
diff changeset
   630
        lo = min_jlong;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   631
        // do not support holes, 'hi' must go to either min_jlong or max_jlong:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   632
        // [min_jlong, -10]/[-1,-1] ==> [min_jlong] UNION [10,max_jlong]
489c9b5090e2 Initial load
duke
parents:
diff changeset
   633
        hi = i1->_hi == min_jlong ? min_jlong : max_jlong;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   634
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   635
        lo = i1->_hi/d;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   636
        hi = i1->_lo/d;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   637
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   638
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   639
    return TypeLong::make(lo, hi, widen);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   640
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   641
489c9b5090e2 Initial load
duke
parents:
diff changeset
   642
  // If the dividend is a constant
489c9b5090e2 Initial load
duke
parents:
diff changeset
   643
  if( i1->is_con() ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   644
    jlong d = i1->get_con();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   645
    if( d < 0 ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   646
      if( d == min_jlong ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   647
        //  (-min_jlong) == min_jlong == (min_jlong / -1)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   648
        return TypeLong::make(min_jlong, max_jlong/2 + 1, widen);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   649
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   650
        return TypeLong::make(d, -d, widen);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   651
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   652
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   653
    return TypeLong::make(-d, d, widen);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   654
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   655
489c9b5090e2 Initial load
duke
parents:
diff changeset
   656
  // Otherwise we give up all hope
489c9b5090e2 Initial load
duke
parents:
diff changeset
   657
  return TypeLong::LONG;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   658
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   659
489c9b5090e2 Initial load
duke
parents:
diff changeset
   660
489c9b5090e2 Initial load
duke
parents:
diff changeset
   661
//=============================================================================
489c9b5090e2 Initial load
duke
parents:
diff changeset
   662
//------------------------------Value------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   663
// An DivFNode divides its inputs.  The third input is a Control input, used to
489c9b5090e2 Initial load
duke
parents:
diff changeset
   664
// prevent hoisting the divide above an unsafe test.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   665
const Type *DivFNode::Value( PhaseTransform *phase ) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   666
  // Either input is TOP ==> the result is TOP
489c9b5090e2 Initial load
duke
parents:
diff changeset
   667
  const Type *t1 = phase->type( in(1) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   668
  const Type *t2 = phase->type( in(2) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   669
  if( t1 == Type::TOP ) return Type::TOP;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   670
  if( t2 == Type::TOP ) return Type::TOP;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   671
489c9b5090e2 Initial load
duke
parents:
diff changeset
   672
  // Either input is BOTTOM ==> the result is the local BOTTOM
489c9b5090e2 Initial load
duke
parents:
diff changeset
   673
  const Type *bot = bottom_type();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   674
  if( (t1 == bot) || (t2 == bot) ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   675
      (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   676
    return bot;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   677
489c9b5090e2 Initial load
duke
parents:
diff changeset
   678
  // x/x == 1, we ignore 0/0.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   679
  // Note: if t1 and t2 are zero then result is NaN (JVMS page 213)
378
39fb2dc78042 6695288: runThese tests expr30303 and drem00301m1 fail when compiled code executes without deopt
jrose
parents: 1
diff changeset
   680
  // Does not work for variables because of NaN's
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   681
  if( phase->eqv( in(1), in(2) ) && t1->base() == Type::FloatCon)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   682
    if (!g_isnan(t1->getf()) && g_isfinite(t1->getf()) && t1->getf() != 0.0) // could be negative ZERO or NaN
489c9b5090e2 Initial load
duke
parents:
diff changeset
   683
      return TypeF::ONE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   684
489c9b5090e2 Initial load
duke
parents:
diff changeset
   685
  if( t2 == TypeF::ONE )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   686
    return t1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   687
489c9b5090e2 Initial load
duke
parents:
diff changeset
   688
  // If divisor is a constant and not zero, divide them numbers
489c9b5090e2 Initial load
duke
parents:
diff changeset
   689
  if( t1->base() == Type::FloatCon &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   690
      t2->base() == Type::FloatCon &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   691
      t2->getf() != 0.0 ) // could be negative zero
489c9b5090e2 Initial load
duke
parents:
diff changeset
   692
    return TypeF::make( t1->getf()/t2->getf() );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   693
489c9b5090e2 Initial load
duke
parents:
diff changeset
   694
  // If the dividend is a constant zero
489c9b5090e2 Initial load
duke
parents:
diff changeset
   695
  // Note: if t1 and t2 are zero then result is NaN (JVMS page 213)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   696
  // Test TypeF::ZERO is not sufficient as it could be negative zero
489c9b5090e2 Initial load
duke
parents:
diff changeset
   697
489c9b5090e2 Initial load
duke
parents:
diff changeset
   698
  if( t1 == TypeF::ZERO && !g_isnan(t2->getf()) && t2->getf() != 0.0 )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   699
    return TypeF::ZERO;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   700
489c9b5090e2 Initial load
duke
parents:
diff changeset
   701
  // Otherwise we give up all hope
489c9b5090e2 Initial load
duke
parents:
diff changeset
   702
  return Type::FLOAT;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   703
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   704
489c9b5090e2 Initial load
duke
parents:
diff changeset
   705
//------------------------------isA_Copy---------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   706
// Dividing by self is 1.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   707
// If the divisor is 1, we are an identity on the dividend.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   708
Node *DivFNode::Identity( PhaseTransform *phase ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   709
  return (phase->type( in(2) ) == TypeF::ONE) ? in(1) : this;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   710
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   711
489c9b5090e2 Initial load
duke
parents:
diff changeset
   712
489c9b5090e2 Initial load
duke
parents:
diff changeset
   713
//------------------------------Idealize---------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   714
Node *DivFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   715
  if (in(0) && remove_dead_region(phase, can_reshape))  return this;
1067
f82e0a8cd438 6736417: Fastdebug C2 crashes in StoreBNode::Ideal
kvn
parents: 1056
diff changeset
   716
  // Don't bother trying to transform a dead node
f82e0a8cd438 6736417: Fastdebug C2 crashes in StoreBNode::Ideal
kvn
parents: 1056
diff changeset
   717
  if( in(0) && in(0)->is_top() )  return NULL;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   718
489c9b5090e2 Initial load
duke
parents:
diff changeset
   719
  const Type *t2 = phase->type( in(2) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   720
  if( t2 == TypeF::ONE )         // Identity?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   721
    return NULL;                // Skip it
489c9b5090e2 Initial load
duke
parents:
diff changeset
   722
489c9b5090e2 Initial load
duke
parents:
diff changeset
   723
  const TypeF *tf = t2->isa_float_constant();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   724
  if( !tf ) return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   725
  if( tf->base() != Type::FloatCon ) return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   726
489c9b5090e2 Initial load
duke
parents:
diff changeset
   727
  // Check for out of range values
489c9b5090e2 Initial load
duke
parents:
diff changeset
   728
  if( tf->is_nan() || !tf->is_finite() ) return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   729
489c9b5090e2 Initial load
duke
parents:
diff changeset
   730
  // Get the value
489c9b5090e2 Initial load
duke
parents:
diff changeset
   731
  float f = tf->getf();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   732
  int exp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   733
489c9b5090e2 Initial load
duke
parents:
diff changeset
   734
  // Only for special case of dividing by a power of 2
489c9b5090e2 Initial load
duke
parents:
diff changeset
   735
  if( frexp((double)f, &exp) != 0.5 ) return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   736
489c9b5090e2 Initial load
duke
parents:
diff changeset
   737
  // Limit the range of acceptable exponents
489c9b5090e2 Initial load
duke
parents:
diff changeset
   738
  if( exp < -126 || exp > 126 ) return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   739
489c9b5090e2 Initial load
duke
parents:
diff changeset
   740
  // Compute the reciprocal
489c9b5090e2 Initial load
duke
parents:
diff changeset
   741
  float reciprocal = ((float)1.0) / f;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   742
489c9b5090e2 Initial load
duke
parents:
diff changeset
   743
  assert( frexp((double)reciprocal, &exp) == 0.5, "reciprocal should be power of 2" );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   744
489c9b5090e2 Initial load
duke
parents:
diff changeset
   745
  // return multiplication by the reciprocal
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   746
  return (new MulFNode(in(1), phase->makecon(TypeF::make(reciprocal))));
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   747
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   748
489c9b5090e2 Initial load
duke
parents:
diff changeset
   749
//=============================================================================
489c9b5090e2 Initial load
duke
parents:
diff changeset
   750
//------------------------------Value------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   751
// An DivDNode divides its inputs.  The third input is a Control input, used to
378
39fb2dc78042 6695288: runThese tests expr30303 and drem00301m1 fail when compiled code executes without deopt
jrose
parents: 1
diff changeset
   752
// prevent hoisting the divide above an unsafe test.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   753
const Type *DivDNode::Value( PhaseTransform *phase ) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   754
  // Either input is TOP ==> the result is TOP
489c9b5090e2 Initial load
duke
parents:
diff changeset
   755
  const Type *t1 = phase->type( in(1) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   756
  const Type *t2 = phase->type( in(2) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   757
  if( t1 == Type::TOP ) return Type::TOP;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   758
  if( t2 == Type::TOP ) return Type::TOP;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   759
489c9b5090e2 Initial load
duke
parents:
diff changeset
   760
  // Either input is BOTTOM ==> the result is the local BOTTOM
489c9b5090e2 Initial load
duke
parents:
diff changeset
   761
  const Type *bot = bottom_type();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   762
  if( (t1 == bot) || (t2 == bot) ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   763
      (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   764
    return bot;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   765
489c9b5090e2 Initial load
duke
parents:
diff changeset
   766
  // x/x == 1, we ignore 0/0.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   767
  // Note: if t1 and t2 are zero then result is NaN (JVMS page 213)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   768
  // Does not work for variables because of NaN's
489c9b5090e2 Initial load
duke
parents:
diff changeset
   769
  if( phase->eqv( in(1), in(2) ) && t1->base() == Type::DoubleCon)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   770
    if (!g_isnan(t1->getd()) && g_isfinite(t1->getd()) && t1->getd() != 0.0) // could be negative ZERO or NaN
489c9b5090e2 Initial load
duke
parents:
diff changeset
   771
      return TypeD::ONE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   772
489c9b5090e2 Initial load
duke
parents:
diff changeset
   773
  if( t2 == TypeD::ONE )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   774
    return t1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   775
1436
6869d58f4f58 6717150: improper constant folding of subnormal strictfp multiplications and divides
rasbold
parents: 1432
diff changeset
   776
#if defined(IA32)
6869d58f4f58 6717150: improper constant folding of subnormal strictfp multiplications and divides
rasbold
parents: 1432
diff changeset
   777
  if (!phase->C->method()->is_strict())
6869d58f4f58 6717150: improper constant folding of subnormal strictfp multiplications and divides
rasbold
parents: 1432
diff changeset
   778
    // Can't trust native compilers to properly fold strict double
6869d58f4f58 6717150: improper constant folding of subnormal strictfp multiplications and divides
rasbold
parents: 1432
diff changeset
   779
    // division with round-to-zero on this platform.
6869d58f4f58 6717150: improper constant folding of subnormal strictfp multiplications and divides
rasbold
parents: 1432
diff changeset
   780
#endif
6869d58f4f58 6717150: improper constant folding of subnormal strictfp multiplications and divides
rasbold
parents: 1432
diff changeset
   781
    {
6869d58f4f58 6717150: improper constant folding of subnormal strictfp multiplications and divides
rasbold
parents: 1432
diff changeset
   782
      // If divisor is a constant and not zero, divide them numbers
6869d58f4f58 6717150: improper constant folding of subnormal strictfp multiplications and divides
rasbold
parents: 1432
diff changeset
   783
      if( t1->base() == Type::DoubleCon &&
6869d58f4f58 6717150: improper constant folding of subnormal strictfp multiplications and divides
rasbold
parents: 1432
diff changeset
   784
          t2->base() == Type::DoubleCon &&
6869d58f4f58 6717150: improper constant folding of subnormal strictfp multiplications and divides
rasbold
parents: 1432
diff changeset
   785
          t2->getd() != 0.0 ) // could be negative zero
6869d58f4f58 6717150: improper constant folding of subnormal strictfp multiplications and divides
rasbold
parents: 1432
diff changeset
   786
        return TypeD::make( t1->getd()/t2->getd() );
6869d58f4f58 6717150: improper constant folding of subnormal strictfp multiplications and divides
rasbold
parents: 1432
diff changeset
   787
    }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   788
489c9b5090e2 Initial load
duke
parents:
diff changeset
   789
  // If the dividend is a constant zero
489c9b5090e2 Initial load
duke
parents:
diff changeset
   790
  // Note: if t1 and t2 are zero then result is NaN (JVMS page 213)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   791
  // Test TypeF::ZERO is not sufficient as it could be negative zero
489c9b5090e2 Initial load
duke
parents:
diff changeset
   792
  if( t1 == TypeD::ZERO && !g_isnan(t2->getd()) && t2->getd() != 0.0 )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   793
    return TypeD::ZERO;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   794
489c9b5090e2 Initial load
duke
parents:
diff changeset
   795
  // Otherwise we give up all hope
489c9b5090e2 Initial load
duke
parents:
diff changeset
   796
  return Type::DOUBLE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   797
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   798
489c9b5090e2 Initial load
duke
parents:
diff changeset
   799
489c9b5090e2 Initial load
duke
parents:
diff changeset
   800
//------------------------------isA_Copy---------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   801
// Dividing by self is 1.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   802
// If the divisor is 1, we are an identity on the dividend.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   803
Node *DivDNode::Identity( PhaseTransform *phase ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   804
  return (phase->type( in(2) ) == TypeD::ONE) ? in(1) : this;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   805
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   806
489c9b5090e2 Initial load
duke
parents:
diff changeset
   807
//------------------------------Idealize---------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   808
Node *DivDNode::Ideal(PhaseGVN *phase, bool can_reshape) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   809
  if (in(0) && remove_dead_region(phase, can_reshape))  return this;
1067
f82e0a8cd438 6736417: Fastdebug C2 crashes in StoreBNode::Ideal
kvn
parents: 1056
diff changeset
   810
  // Don't bother trying to transform a dead node
f82e0a8cd438 6736417: Fastdebug C2 crashes in StoreBNode::Ideal
kvn
parents: 1056
diff changeset
   811
  if( in(0) && in(0)->is_top() )  return NULL;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   812
489c9b5090e2 Initial load
duke
parents:
diff changeset
   813
  const Type *t2 = phase->type( in(2) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   814
  if( t2 == TypeD::ONE )         // Identity?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   815
    return NULL;                // Skip it
489c9b5090e2 Initial load
duke
parents:
diff changeset
   816
489c9b5090e2 Initial load
duke
parents:
diff changeset
   817
  const TypeD *td = t2->isa_double_constant();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   818
  if( !td ) return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   819
  if( td->base() != Type::DoubleCon ) return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   820
489c9b5090e2 Initial load
duke
parents:
diff changeset
   821
  // Check for out of range values
489c9b5090e2 Initial load
duke
parents:
diff changeset
   822
  if( td->is_nan() || !td->is_finite() ) return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   823
489c9b5090e2 Initial load
duke
parents:
diff changeset
   824
  // Get the value
489c9b5090e2 Initial load
duke
parents:
diff changeset
   825
  double d = td->getd();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   826
  int exp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   827
489c9b5090e2 Initial load
duke
parents:
diff changeset
   828
  // Only for special case of dividing by a power of 2
489c9b5090e2 Initial load
duke
parents:
diff changeset
   829
  if( frexp(d, &exp) != 0.5 ) return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   830
489c9b5090e2 Initial load
duke
parents:
diff changeset
   831
  // Limit the range of acceptable exponents
489c9b5090e2 Initial load
duke
parents:
diff changeset
   832
  if( exp < -1021 || exp > 1022 ) return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   833
489c9b5090e2 Initial load
duke
parents:
diff changeset
   834
  // Compute the reciprocal
489c9b5090e2 Initial load
duke
parents:
diff changeset
   835
  double reciprocal = 1.0 / d;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   836
489c9b5090e2 Initial load
duke
parents:
diff changeset
   837
  assert( frexp(reciprocal, &exp) == 0.5, "reciprocal should be power of 2" );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   838
489c9b5090e2 Initial load
duke
parents:
diff changeset
   839
  // return multiplication by the reciprocal
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   840
  return (new MulDNode(in(1), phase->makecon(TypeD::make(reciprocal))));
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   841
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   842
489c9b5090e2 Initial load
duke
parents:
diff changeset
   843
//=============================================================================
489c9b5090e2 Initial load
duke
parents:
diff changeset
   844
//------------------------------Idealize---------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   845
Node *ModINode::Ideal(PhaseGVN *phase, bool can_reshape) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   846
  // Check for dead control input
1067
f82e0a8cd438 6736417: Fastdebug C2 crashes in StoreBNode::Ideal
kvn
parents: 1056
diff changeset
   847
  if( in(0) && remove_dead_region(phase, can_reshape) )  return this;
f82e0a8cd438 6736417: Fastdebug C2 crashes in StoreBNode::Ideal
kvn
parents: 1056
diff changeset
   848
  // Don't bother trying to transform a dead node
f82e0a8cd438 6736417: Fastdebug C2 crashes in StoreBNode::Ideal
kvn
parents: 1056
diff changeset
   849
  if( in(0) && in(0)->is_top() )  return NULL;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   850
489c9b5090e2 Initial load
duke
parents:
diff changeset
   851
  // Get the modulus
489c9b5090e2 Initial load
duke
parents:
diff changeset
   852
  const Type *t = phase->type( in(2) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   853
  if( t == Type::TOP ) return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   854
  const TypeInt *ti = t->is_int();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   855
489c9b5090e2 Initial load
duke
parents:
diff changeset
   856
  // Check for useless control input
489c9b5090e2 Initial load
duke
parents:
diff changeset
   857
  // Check for excluding mod-zero case
489c9b5090e2 Initial load
duke
parents:
diff changeset
   858
  if( in(0) && (ti->_hi < 0 || ti->_lo > 0) ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   859
    set_req(0, NULL);        // Yank control input
489c9b5090e2 Initial load
duke
parents:
diff changeset
   860
    return this;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   861
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   862
489c9b5090e2 Initial load
duke
parents:
diff changeset
   863
  // See if we are MOD'ing by 2^k or 2^k-1.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   864
  if( !ti->is_con() ) return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   865
  jint con = ti->get_con();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   866
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   867
  Node *hook = new Node(1);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   868
489c9b5090e2 Initial load
duke
parents:
diff changeset
   869
  // First, special check for modulo 2^k-1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   870
  if( con >= 0 && con < max_jint && is_power_of_2(con+1) ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   871
    uint k = exact_log2(con+1);  // Extract k
489c9b5090e2 Initial load
duke
parents:
diff changeset
   872
489c9b5090e2 Initial load
duke
parents:
diff changeset
   873
    // Basic algorithm by David Detlefs.  See fastmod_int.java for gory details.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   874
    static int unroll_factor[] = { 999, 999, 29, 14, 9, 7, 5, 4, 4, 3, 3, 2, 2, 2, 2, 2, 1 /*past here we assume 1 forever*/};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   875
    int trip_count = 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   876
    if( k < ARRAY_SIZE(unroll_factor))  trip_count = unroll_factor[k];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   877
489c9b5090e2 Initial load
duke
parents:
diff changeset
   878
    // If the unroll factor is not too large, and if conditional moves are
489c9b5090e2 Initial load
duke
parents:
diff changeset
   879
    // ok, then use this case
489c9b5090e2 Initial load
duke
parents:
diff changeset
   880
    if( trip_count <= 5 && ConditionalMoveLimit != 0 ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   881
      Node *x = in(1);            // Value being mod'd
489c9b5090e2 Initial load
duke
parents:
diff changeset
   882
      Node *divisor = in(2);      // Also is mask
489c9b5090e2 Initial load
duke
parents:
diff changeset
   883
489c9b5090e2 Initial load
duke
parents:
diff changeset
   884
      hook->init_req(0, x);       // Add a use to x to prevent him from dying
489c9b5090e2 Initial load
duke
parents:
diff changeset
   885
      // Generate code to reduce X rapidly to nearly 2^k-1.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   886
      for( int i = 0; i < trip_count; i++ ) {
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   887
        Node *xl = phase->transform( new AndINode(x,divisor) );
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   888
        Node *xh = phase->transform( new RShiftINode(x,phase->intcon(k)) ); // Must be signed
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   889
        x = phase->transform( new AddINode(xh,xl) );
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   890
        hook->set_req(0, x);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   891
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   892
489c9b5090e2 Initial load
duke
parents:
diff changeset
   893
      // Generate sign-fixup code.  Was original value positive?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   894
      // int hack_res = (i >= 0) ? divisor : 1;
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   895
      Node *cmp1 = phase->transform( new CmpINode( in(1), phase->intcon(0) ) );
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   896
      Node *bol1 = phase->transform( new BoolNode( cmp1, BoolTest::ge ) );
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   897
      Node *cmov1= phase->transform( new CMoveINode(bol1, phase->intcon(1), divisor, TypeInt::POS) );
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   898
      // if( x >= hack_res ) x -= divisor;
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   899
      Node *sub  = phase->transform( new SubINode( x, divisor ) );
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   900
      Node *cmp2 = phase->transform( new CmpINode( x, cmov1 ) );
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   901
      Node *bol2 = phase->transform( new BoolNode( cmp2, BoolTest::ge ) );
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   902
      // Convention is to not transform the return value of an Ideal
489c9b5090e2 Initial load
duke
parents:
diff changeset
   903
      // since Ideal is expected to return a modified 'this' or a new node.
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   904
      Node *cmov2= new CMoveINode(bol2, x, sub, TypeInt::INT);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   905
      // cmov2 is now the mod
489c9b5090e2 Initial load
duke
parents:
diff changeset
   906
489c9b5090e2 Initial load
duke
parents:
diff changeset
   907
      // Now remove the bogus extra edges used to keep things alive
489c9b5090e2 Initial load
duke
parents:
diff changeset
   908
      if (can_reshape) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   909
        phase->is_IterGVN()->remove_dead_node(hook);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   910
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   911
        hook->set_req(0, NULL);   // Just yank bogus edge during Parse phase
489c9b5090e2 Initial load
duke
parents:
diff changeset
   912
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   913
      return cmov2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   914
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   915
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   916
489c9b5090e2 Initial load
duke
parents:
diff changeset
   917
  // Fell thru, the unroll case is not appropriate. Transform the modulo
489c9b5090e2 Initial load
duke
parents:
diff changeset
   918
  // into a long multiply/int multiply/subtract case
489c9b5090e2 Initial load
duke
parents:
diff changeset
   919
489c9b5090e2 Initial load
duke
parents:
diff changeset
   920
  // Cannot handle mod 0, and min_jint isn't handled by the transform
489c9b5090e2 Initial load
duke
parents:
diff changeset
   921
  if( con == 0 || con == min_jint ) return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   922
489c9b5090e2 Initial load
duke
parents:
diff changeset
   923
  // Get the absolute value of the constant; at this point, we can use this
489c9b5090e2 Initial load
duke
parents:
diff changeset
   924
  jint pos_con = (con >= 0) ? con : -con;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   925
489c9b5090e2 Initial load
duke
parents:
diff changeset
   926
  // integer Mod 1 is always 0
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   927
  if( pos_con == 1 ) return new ConINode(TypeInt::ZERO);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   928
489c9b5090e2 Initial load
duke
parents:
diff changeset
   929
  int log2_con = -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   930
489c9b5090e2 Initial load
duke
parents:
diff changeset
   931
  // If this is a power of two, they maybe we can mask it
489c9b5090e2 Initial load
duke
parents:
diff changeset
   932
  if( is_power_of_2(pos_con) ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   933
    log2_con = log2_intptr((intptr_t)pos_con);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   934
489c9b5090e2 Initial load
duke
parents:
diff changeset
   935
    const Type *dt = phase->type(in(1));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   936
    const TypeInt *dti = dt->isa_int();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   937
489c9b5090e2 Initial load
duke
parents:
diff changeset
   938
    // See if this can be masked, if the dividend is non-negative
489c9b5090e2 Initial load
duke
parents:
diff changeset
   939
    if( dti && dti->_lo >= 0 )
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   940
      return ( new AndINode( in(1), phase->intcon( pos_con-1 ) ) );
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   941
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   942
489c9b5090e2 Initial load
duke
parents:
diff changeset
   943
  // Save in(1) so that it cannot be changed or deleted
489c9b5090e2 Initial load
duke
parents:
diff changeset
   944
  hook->init_req(0, in(1));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   945
489c9b5090e2 Initial load
duke
parents:
diff changeset
   946
  // Divide using the transform from DivI to MulL
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   947
  Node *result = transform_int_divide( phase, in(1), pos_con );
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   948
  if (result != NULL) {
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   949
    Node *divide = phase->transform(result);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   950
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   951
    // Re-multiply, using a shift if this is a power of two
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   952
    Node *mult = NULL;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   953
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   954
    if( log2_con >= 0 )
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   955
      mult = phase->transform( new LShiftINode( divide, phase->intcon( log2_con ) ) );
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   956
    else
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   957
      mult = phase->transform( new MulINode( divide, phase->intcon( pos_con ) ) );
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   958
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   959
    // Finally, subtract the multiplied divided value from the original
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
   960
    result = new SubINode( in(1), mult );
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
   961
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   962
489c9b5090e2 Initial load
duke
parents:
diff changeset
   963
  // Now remove the bogus extra edges used to keep things alive
489c9b5090e2 Initial load
duke
parents:
diff changeset
   964
  if (can_reshape) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   965
    phase->is_IterGVN()->remove_dead_node(hook);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   966
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   967
    hook->set_req(0, NULL);       // Just yank bogus edge during Parse phase
489c9b5090e2 Initial load
duke
parents:
diff changeset
   968
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   969
489c9b5090e2 Initial load
duke
parents:
diff changeset
   970
  // return the value
489c9b5090e2 Initial load
duke
parents:
diff changeset
   971
  return result;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   972
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   973
489c9b5090e2 Initial load
duke
parents:
diff changeset
   974
//------------------------------Value------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   975
const Type *ModINode::Value( PhaseTransform *phase ) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   976
  // Either input is TOP ==> the result is TOP
489c9b5090e2 Initial load
duke
parents:
diff changeset
   977
  const Type *t1 = phase->type( in(1) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   978
  const Type *t2 = phase->type( in(2) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   979
  if( t1 == Type::TOP ) return Type::TOP;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   980
  if( t2 == Type::TOP ) return Type::TOP;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   981
489c9b5090e2 Initial load
duke
parents:
diff changeset
   982
  // We always generate the dynamic check for 0.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   983
  // 0 MOD X is 0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   984
  if( t1 == TypeInt::ZERO ) return TypeInt::ZERO;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   985
  // X MOD X is 0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   986
  if( phase->eqv( in(1), in(2) ) ) return TypeInt::ZERO;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   987
489c9b5090e2 Initial load
duke
parents:
diff changeset
   988
  // Either input is BOTTOM ==> the result is the local BOTTOM
489c9b5090e2 Initial load
duke
parents:
diff changeset
   989
  const Type *bot = bottom_type();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   990
  if( (t1 == bot) || (t2 == bot) ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   991
      (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   992
    return bot;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   993
489c9b5090e2 Initial load
duke
parents:
diff changeset
   994
  const TypeInt *i1 = t1->is_int();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   995
  const TypeInt *i2 = t2->is_int();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   996
  if( !i1->is_con() || !i2->is_con() ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   997
    if( i1->_lo >= 0 && i2->_lo >= 0 )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   998
      return TypeInt::POS;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   999
    // If both numbers are not constants, we know little.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1000
    return TypeInt::INT;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1001
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1002
  // Mod by zero?  Throw exception at runtime!
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1003
  if( !i2->get_con() ) return TypeInt::POS;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1004
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1005
  // We must be modulo'ing 2 float constants.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1006
  // Check for min_jint % '-1', result is defined to be '0'.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1007
  if( i1->get_con() == min_jint && i2->get_con() == -1 )
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1008
    return TypeInt::ZERO;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1009
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1010
  return TypeInt::make( i1->get_con() % i2->get_con() );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1011
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1012
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1013
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1014
//=============================================================================
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1015
//------------------------------Idealize---------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1016
Node *ModLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1017
  // Check for dead control input
1067
f82e0a8cd438 6736417: Fastdebug C2 crashes in StoreBNode::Ideal
kvn
parents: 1056
diff changeset
  1018
  if( in(0) && remove_dead_region(phase, can_reshape) )  return this;
f82e0a8cd438 6736417: Fastdebug C2 crashes in StoreBNode::Ideal
kvn
parents: 1056
diff changeset
  1019
  // Don't bother trying to transform a dead node
f82e0a8cd438 6736417: Fastdebug C2 crashes in StoreBNode::Ideal
kvn
parents: 1056
diff changeset
  1020
  if( in(0) && in(0)->is_top() )  return NULL;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1021
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1022
  // Get the modulus
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1023
  const Type *t = phase->type( in(2) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1024
  if( t == Type::TOP ) return NULL;
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1025
  const TypeLong *tl = t->is_long();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1026
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1027
  // Check for useless control input
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1028
  // Check for excluding mod-zero case
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1029
  if( in(0) && (tl->_hi < 0 || tl->_lo > 0) ) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1030
    set_req(0, NULL);        // Yank control input
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1031
    return this;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1032
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1033
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1034
  // See if we are MOD'ing by 2^k or 2^k-1.
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1035
  if( !tl->is_con() ) return NULL;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1036
  jlong con = tl->get_con();
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1037
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
  1038
  Node *hook = new Node(1);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1039
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1040
  // Expand mod
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1041
  if( con >= 0 && con < max_jlong && is_power_of_2_long(con+1) ) {
2032
1e27661bff28 6805724: ModLNode::Ideal() generates functionally incorrect graph when divisor is any (2^k-1) constant.
twisti
parents: 2031
diff changeset
  1042
    uint k = exact_log2_long(con+1);  // Extract k
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1043
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1044
    // Basic algorithm by David Detlefs.  See fastmod_long.java for gory details.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1045
    // Used to help a popular random number generator which does a long-mod
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1046
    // of 2^31-1 and shows up in SpecJBB and SciMark.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1047
    static int unroll_factor[] = { 999, 999, 61, 30, 20, 15, 12, 10, 8, 7, 6, 6, 5, 5, 4, 4, 4, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1 /*past here we assume 1 forever*/};
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1048
    int trip_count = 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1049
    if( k < ARRAY_SIZE(unroll_factor)) trip_count = unroll_factor[k];
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1050
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1051
    // If the unroll factor is not too large, and if conditional moves are
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1052
    // ok, then use this case
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1053
    if( trip_count <= 5 && ConditionalMoveLimit != 0 ) {
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1054
      Node *x = in(1);            // Value being mod'd
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1055
      Node *divisor = in(2);      // Also is mask
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1056
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1057
      hook->init_req(0, x);       // Add a use to x to prevent him from dying
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1058
      // Generate code to reduce X rapidly to nearly 2^k-1.
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1059
      for( int i = 0; i < trip_count; i++ ) {
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
  1060
        Node *xl = phase->transform( new AndLNode(x,divisor) );
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
  1061
        Node *xh = phase->transform( new RShiftLNode(x,phase->intcon(k)) ); // Must be signed
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
  1062
        x = phase->transform( new AddLNode(xh,xl) );
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1063
        hook->set_req(0, x);    // Add a use to x to prevent him from dying
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1064
      }
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1065
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1066
      // Generate sign-fixup code.  Was original value positive?
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1067
      // long hack_res = (i >= 0) ? divisor : CONST64(1);
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
  1068
      Node *cmp1 = phase->transform( new CmpLNode( in(1), phase->longcon(0) ) );
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
  1069
      Node *bol1 = phase->transform( new BoolNode( cmp1, BoolTest::ge ) );
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
  1070
      Node *cmov1= phase->transform( new CMoveLNode(bol1, phase->longcon(1), divisor, TypeLong::LONG) );
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1071
      // if( x >= hack_res ) x -= divisor;
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
  1072
      Node *sub  = phase->transform( new SubLNode( x, divisor ) );
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
  1073
      Node *cmp2 = phase->transform( new CmpLNode( x, cmov1 ) );
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
  1074
      Node *bol2 = phase->transform( new BoolNode( cmp2, BoolTest::ge ) );
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1075
      // Convention is to not transform the return value of an Ideal
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1076
      // since Ideal is expected to return a modified 'this' or a new node.
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
  1077
      Node *cmov2= new CMoveLNode(bol2, x, sub, TypeLong::LONG);
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1078
      // cmov2 is now the mod
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1079
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1080
      // Now remove the bogus extra edges used to keep things alive
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1081
      if (can_reshape) {
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1082
        phase->is_IterGVN()->remove_dead_node(hook);
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1083
      } else {
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1084
        hook->set_req(0, NULL);   // Just yank bogus edge during Parse phase
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1085
      }
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1086
      return cmov2;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1087
    }
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1088
  }
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1089
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1090
  // Fell thru, the unroll case is not appropriate. Transform the modulo
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1091
  // into a long multiply/int multiply/subtract case
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1092
7115
32300e243300 6987135: Performance regression on Intel platform with 32-bits edition between 6u13 and 6u14.
kvn
parents: 5547
diff changeset
  1093
  // Cannot handle mod 0, and min_jlong isn't handled by the transform
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1094
  if( con == 0 || con == min_jlong ) return NULL;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1095
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1096
  // Get the absolute value of the constant; at this point, we can use this
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1097
  jlong pos_con = (con >= 0) ? con : -con;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1098
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1099
  // integer Mod 1 is always 0
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
  1100
  if( pos_con == 1 ) return new ConLNode(TypeLong::ZERO);
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1101
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1102
  int log2_con = -1;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1103
2131
98f9cef66a34 6810672: Comment typos
twisti
parents: 2032
diff changeset
  1104
  // If this is a power of two, then maybe we can mask it
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1105
  if( is_power_of_2_long(pos_con) ) {
7115
32300e243300 6987135: Performance regression on Intel platform with 32-bits edition between 6u13 and 6u14.
kvn
parents: 5547
diff changeset
  1106
    log2_con = exact_log2_long(pos_con);
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1107
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1108
    const Type *dt = phase->type(in(1));
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1109
    const TypeLong *dtl = dt->isa_long();
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1110
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1111
    // See if this can be masked, if the dividend is non-negative
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1112
    if( dtl && dtl->_lo >= 0 )
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
  1113
      return ( new AndLNode( in(1), phase->longcon( pos_con-1 ) ) );
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1114
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1115
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1116
  // Save in(1) so that it cannot be changed or deleted
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1117
  hook->init_req(0, in(1));
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1118
7115
32300e243300 6987135: Performance regression on Intel platform with 32-bits edition between 6u13 and 6u14.
kvn
parents: 5547
diff changeset
  1119
  // Divide using the transform from DivL to MulL
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1120
  Node *result = transform_long_divide( phase, in(1), pos_con );
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1121
  if (result != NULL) {
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1122
    Node *divide = phase->transform(result);
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1123
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1124
    // Re-multiply, using a shift if this is a power of two
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1125
    Node *mult = NULL;
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1126
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1127
    if( log2_con >= 0 )
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
  1128
      mult = phase->transform( new LShiftLNode( divide, phase->intcon( log2_con ) ) );
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1129
    else
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
  1130
      mult = phase->transform( new MulLNode( divide, phase->longcon( pos_con ) ) );
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1131
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1132
    // Finally, subtract the multiplied divided value from the original
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
  1133
    result = new SubLNode( in(1), mult );
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1134
  }
392
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1135
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1136
  // Now remove the bogus extra edges used to keep things alive
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1137
  if (can_reshape) {
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1138
    phase->is_IterGVN()->remove_dead_node(hook);
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1139
  } else {
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1140
    hook->set_req(0, NULL);       // Just yank bogus edge during Parse phase
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1141
  }
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1142
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1143
  // return the value
0b3167e2f2de 6603011: RFE: Optimize long division
rasbold
parents: 378
diff changeset
  1144
  return result;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1145
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1146
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1147
//------------------------------Value------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1148
const Type *ModLNode::Value( PhaseTransform *phase ) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1149
  // Either input is TOP ==> the result is TOP
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1150
  const Type *t1 = phase->type( in(1) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1151
  const Type *t2 = phase->type( in(2) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1152
  if( t1 == Type::TOP ) return Type::TOP;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1153
  if( t2 == Type::TOP ) return Type::TOP;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1154
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1155
  // We always generate the dynamic check for 0.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1156
  // 0 MOD X is 0
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1157
  if( t1 == TypeLong::ZERO ) return TypeLong::ZERO;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1158
  // X MOD X is 0
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1159
  if( phase->eqv( in(1), in(2) ) ) return TypeLong::ZERO;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1160
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1161
  // Either input is BOTTOM ==> the result is the local BOTTOM
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1162
  const Type *bot = bottom_type();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1163
  if( (t1 == bot) || (t2 == bot) ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1164
      (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1165
    return bot;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1166
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1167
  const TypeLong *i1 = t1->is_long();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1168
  const TypeLong *i2 = t2->is_long();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1169
  if( !i1->is_con() || !i2->is_con() ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1170
    if( i1->_lo >= CONST64(0) && i2->_lo >= CONST64(0) )
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1171
      return TypeLong::POS;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1172
    // If both numbers are not constants, we know little.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1173
    return TypeLong::LONG;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1174
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1175
  // Mod by zero?  Throw exception at runtime!
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1176
  if( !i2->get_con() ) return TypeLong::POS;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1177
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1178
  // We must be modulo'ing 2 float constants.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1179
  // Check for min_jint % '-1', result is defined to be '0'.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1180
  if( i1->get_con() == min_jlong && i2->get_con() == -1 )
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1181
    return TypeLong::ZERO;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1182
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1183
  return TypeLong::make( i1->get_con() % i2->get_con() );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1184
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1185
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1186
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1187
//=============================================================================
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1188
//------------------------------Value------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1189
const Type *ModFNode::Value( PhaseTransform *phase ) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1190
  // Either input is TOP ==> the result is TOP
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1191
  const Type *t1 = phase->type( in(1) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1192
  const Type *t2 = phase->type( in(2) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1193
  if( t1 == Type::TOP ) return Type::TOP;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1194
  if( t2 == Type::TOP ) return Type::TOP;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1195
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1196
  // Either input is BOTTOM ==> the result is the local BOTTOM
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1197
  const Type *bot = bottom_type();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1198
  if( (t1 == bot) || (t2 == bot) ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1199
      (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1200
    return bot;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1201
378
39fb2dc78042 6695288: runThese tests expr30303 and drem00301m1 fail when compiled code executes without deopt
jrose
parents: 1
diff changeset
  1202
  // If either number is not a constant, we know nothing.
39fb2dc78042 6695288: runThese tests expr30303 and drem00301m1 fail when compiled code executes without deopt
jrose
parents: 1
diff changeset
  1203
  if ((t1->base() != Type::FloatCon) || (t2->base() != Type::FloatCon)) {
39fb2dc78042 6695288: runThese tests expr30303 and drem00301m1 fail when compiled code executes without deopt
jrose
parents: 1
diff changeset
  1204
    return Type::FLOAT;         // note: x%x can be either NaN or 0
39fb2dc78042 6695288: runThese tests expr30303 and drem00301m1 fail when compiled code executes without deopt
jrose
parents: 1
diff changeset
  1205
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1206
378
39fb2dc78042 6695288: runThese tests expr30303 and drem00301m1 fail when compiled code executes without deopt
jrose
parents: 1
diff changeset
  1207
  float f1 = t1->getf();
39fb2dc78042 6695288: runThese tests expr30303 and drem00301m1 fail when compiled code executes without deopt
jrose
parents: 1
diff changeset
  1208
  float f2 = t2->getf();
39fb2dc78042 6695288: runThese tests expr30303 and drem00301m1 fail when compiled code executes without deopt
jrose
parents: 1
diff changeset
  1209
  jint  x1 = jint_cast(f1);     // note:  *(int*)&f1, not just (int)f1
39fb2dc78042 6695288: runThese tests expr30303 and drem00301m1 fail when compiled code executes without deopt
jrose
parents: 1
diff changeset
  1210
  jint  x2 = jint_cast(f2);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1211
378
39fb2dc78042 6695288: runThese tests expr30303 and drem00301m1 fail when compiled code executes without deopt
jrose
parents: 1
diff changeset
  1212
  // If either is a NaN, return an input NaN
39fb2dc78042 6695288: runThese tests expr30303 and drem00301m1 fail when compiled code executes without deopt
jrose
parents: 1
diff changeset
  1213
  if (g_isnan(f1))    return t1;
39fb2dc78042 6695288: runThese tests expr30303 and drem00301m1 fail when compiled code executes without deopt
jrose
parents: 1
diff changeset
  1214
  if (g_isnan(f2))    return t2;
39fb2dc78042 6695288: runThese tests expr30303 and drem00301m1 fail when compiled code executes without deopt
jrose
parents: 1
diff changeset
  1215
39fb2dc78042 6695288: runThese tests expr30303 and drem00301m1 fail when compiled code executes without deopt
jrose
parents: 1
diff changeset
  1216
  // If an operand is infinity or the divisor is +/- zero, punt.
39fb2dc78042 6695288: runThese tests expr30303 and drem00301m1 fail when compiled code executes without deopt
jrose
parents: 1
diff changeset
  1217
  if (!g_isfinite(f1) || !g_isfinite(f2) || x2 == 0 || x2 == min_jint)
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1218
    return Type::FLOAT;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1219
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1220
  // We must be modulo'ing 2 float constants.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1221
  // Make sure that the sign of the fmod is equal to the sign of the dividend
378
39fb2dc78042 6695288: runThese tests expr30303 and drem00301m1 fail when compiled code executes without deopt
jrose
parents: 1
diff changeset
  1222
  jint xr = jint_cast(fmod(f1, f2));
39fb2dc78042 6695288: runThese tests expr30303 and drem00301m1 fail when compiled code executes without deopt
jrose
parents: 1
diff changeset
  1223
  if ((x1 ^ xr) < 0) {
39fb2dc78042 6695288: runThese tests expr30303 and drem00301m1 fail when compiled code executes without deopt
jrose
parents: 1
diff changeset
  1224
    xr ^= min_jint;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1225
  }
378
39fb2dc78042 6695288: runThese tests expr30303 and drem00301m1 fail when compiled code executes without deopt
jrose
parents: 1
diff changeset
  1226
39fb2dc78042 6695288: runThese tests expr30303 and drem00301m1 fail when compiled code executes without deopt
jrose
parents: 1
diff changeset
  1227
  return TypeF::make(jfloat_cast(xr));
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1228
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1229
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1230
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1231
//=============================================================================
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1232
//------------------------------Value------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1233
const Type *ModDNode::Value( PhaseTransform *phase ) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1234
  // Either input is TOP ==> the result is TOP
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1235
  const Type *t1 = phase->type( in(1) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1236
  const Type *t2 = phase->type( in(2) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1237
  if( t1 == Type::TOP ) return Type::TOP;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1238
  if( t2 == Type::TOP ) return Type::TOP;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1239
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1240
  // Either input is BOTTOM ==> the result is the local BOTTOM
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1241
  const Type *bot = bottom_type();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1242
  if( (t1 == bot) || (t2 == bot) ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1243
      (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1244
    return bot;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1245
378
39fb2dc78042 6695288: runThese tests expr30303 and drem00301m1 fail when compiled code executes without deopt
jrose
parents: 1
diff changeset
  1246
  // If either number is not a constant, we know nothing.
39fb2dc78042 6695288: runThese tests expr30303 and drem00301m1 fail when compiled code executes without deopt
jrose
parents: 1
diff changeset
  1247
  if ((t1->base() != Type::DoubleCon) || (t2->base() != Type::DoubleCon)) {
39fb2dc78042 6695288: runThese tests expr30303 and drem00301m1 fail when compiled code executes without deopt
jrose
parents: 1
diff changeset
  1248
    return Type::DOUBLE;        // note: x%x can be either NaN or 0
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1249
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1250
378
39fb2dc78042 6695288: runThese tests expr30303 and drem00301m1 fail when compiled code executes without deopt
jrose
parents: 1
diff changeset
  1251
  double f1 = t1->getd();
39fb2dc78042 6695288: runThese tests expr30303 and drem00301m1 fail when compiled code executes without deopt
jrose
parents: 1
diff changeset
  1252
  double f2 = t2->getd();
39fb2dc78042 6695288: runThese tests expr30303 and drem00301m1 fail when compiled code executes without deopt
jrose
parents: 1
diff changeset
  1253
  jlong  x1 = jlong_cast(f1);   // note:  *(long*)&f1, not just (long)f1
39fb2dc78042 6695288: runThese tests expr30303 and drem00301m1 fail when compiled code executes without deopt
jrose
parents: 1
diff changeset
  1254
  jlong  x2 = jlong_cast(f2);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1255
378
39fb2dc78042 6695288: runThese tests expr30303 and drem00301m1 fail when compiled code executes without deopt
jrose
parents: 1
diff changeset
  1256
  // If either is a NaN, return an input NaN
39fb2dc78042 6695288: runThese tests expr30303 and drem00301m1 fail when compiled code executes without deopt
jrose
parents: 1
diff changeset
  1257
  if (g_isnan(f1))    return t1;
39fb2dc78042 6695288: runThese tests expr30303 and drem00301m1 fail when compiled code executes without deopt
jrose
parents: 1
diff changeset
  1258
  if (g_isnan(f2))    return t2;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1259
378
39fb2dc78042 6695288: runThese tests expr30303 and drem00301m1 fail when compiled code executes without deopt
jrose
parents: 1
diff changeset
  1260
  // If an operand is infinity or the divisor is +/- zero, punt.
39fb2dc78042 6695288: runThese tests expr30303 and drem00301m1 fail when compiled code executes without deopt
jrose
parents: 1
diff changeset
  1261
  if (!g_isfinite(f1) || !g_isfinite(f2) || x2 == 0 || x2 == min_jlong)
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1262
    return Type::DOUBLE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1263
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1264
  // We must be modulo'ing 2 double constants.
378
39fb2dc78042 6695288: runThese tests expr30303 and drem00301m1 fail when compiled code executes without deopt
jrose
parents: 1
diff changeset
  1265
  // Make sure that the sign of the fmod is equal to the sign of the dividend
39fb2dc78042 6695288: runThese tests expr30303 and drem00301m1 fail when compiled code executes without deopt
jrose
parents: 1
diff changeset
  1266
  jlong xr = jlong_cast(fmod(f1, f2));
39fb2dc78042 6695288: runThese tests expr30303 and drem00301m1 fail when compiled code executes without deopt
jrose
parents: 1
diff changeset
  1267
  if ((x1 ^ xr) < 0) {
39fb2dc78042 6695288: runThese tests expr30303 and drem00301m1 fail when compiled code executes without deopt
jrose
parents: 1
diff changeset
  1268
    xr ^= min_jlong;
39fb2dc78042 6695288: runThese tests expr30303 and drem00301m1 fail when compiled code executes without deopt
jrose
parents: 1
diff changeset
  1269
  }
39fb2dc78042 6695288: runThese tests expr30303 and drem00301m1 fail when compiled code executes without deopt
jrose
parents: 1
diff changeset
  1270
39fb2dc78042 6695288: runThese tests expr30303 and drem00301m1 fail when compiled code executes without deopt
jrose
parents: 1
diff changeset
  1271
  return TypeD::make(jdouble_cast(xr));
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1272
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1273
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1274
//=============================================================================
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1275
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1276
DivModNode::DivModNode( Node *c, Node *dividend, Node *divisor ) : MultiNode(3) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1277
  init_req(0, c);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1278
  init_req(1, dividend);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1279
  init_req(2, divisor);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1280
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1281
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1282
//------------------------------make------------------------------------------
25930
eae8b7490d2c 8054033: Remove unused references to Compile*
thartmann
parents: 25913
diff changeset
  1283
DivModINode* DivModINode::make(Node* div_or_mod) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1284
  Node* n = div_or_mod;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1285
  assert(n->Opcode() == Op_DivI || n->Opcode() == Op_ModI,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1286
         "only div or mod input pattern accepted");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1287
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
  1288
  DivModINode* divmod = new DivModINode(n->in(0), n->in(1), n->in(2));
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
  1289
  Node*        dproj  = new ProjNode(divmod, DivModNode::div_proj_num);
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
  1290
  Node*        mproj  = new ProjNode(divmod, DivModNode::mod_proj_num);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1291
  return divmod;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1292
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1293
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1294
//------------------------------make------------------------------------------
25930
eae8b7490d2c 8054033: Remove unused references to Compile*
thartmann
parents: 25913
diff changeset
  1295
DivModLNode* DivModLNode::make(Node* div_or_mod) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1296
  Node* n = div_or_mod;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1297
  assert(n->Opcode() == Op_DivL || n->Opcode() == Op_ModL,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1298
         "only div or mod input pattern accepted");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1299
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
  1300
  DivModLNode* divmod = new DivModLNode(n->in(0), n->in(1), n->in(2));
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
  1301
  Node*        dproj  = new ProjNode(divmod, DivModNode::div_proj_num);
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
  1302
  Node*        mproj  = new ProjNode(divmod, DivModNode::mod_proj_num);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1303
  return divmod;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1304
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1305
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1306
//------------------------------match------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1307
// return result(s) along with their RegMask info
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1308
Node *DivModINode::match( const ProjNode *proj, const Matcher *match ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1309
  uint ideal_reg = proj->ideal_reg();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1310
  RegMask rm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1311
  if (proj->_con == div_proj_num) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1312
    rm = match->divI_proj_mask();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1313
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1314
    assert(proj->_con == mod_proj_num, "must be div or mod projection");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1315
    rm = match->modI_proj_mask();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1316
  }
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
  1317
  return new MachProjNode(this, proj->_con, rm, ideal_reg);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1318
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1319
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1320
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1321
//------------------------------match------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1322
// return result(s) along with their RegMask info
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1323
Node *DivModLNode::match( const ProjNode *proj, const Matcher *match ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1324
  uint ideal_reg = proj->ideal_reg();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1325
  RegMask rm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1326
  if (proj->_con == div_proj_num) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1327
    rm = match->divL_proj_mask();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1328
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1329
    assert(proj->_con == mod_proj_num, "must be div or mod projection");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1330
    rm = match->modL_proj_mask();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1331
  }
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24425
diff changeset
  1332
  return new MachProjNode(this, proj->_con, rm, ideal_reg);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1333
}