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