src/share/vm/opto/divnode.cpp

Sat, 01 Dec 2007 00:00:00 +0000

author
duke
date
Sat, 01 Dec 2007 00:00:00 +0000
changeset 435
a61af66fc99e
child 566
6e825ad773c6
permissions
-rw-r--r--

Initial load

duke@435 1 /*
duke@435 2 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
duke@435 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@435 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@435 21 * have any questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 // Portions of code courtesy of Clifford Click
duke@435 26
duke@435 27 // Optimization - Graph Style
duke@435 28
duke@435 29 #include "incls/_precompiled.incl"
duke@435 30 #include "incls/_divnode.cpp.incl"
duke@435 31 #include <math.h>
duke@435 32
duke@435 33 // Implement the integer constant divide -> long multiply transform found in
duke@435 34 // "Division by Invariant Integers using Multiplication"
duke@435 35 // by Granlund and Montgomery
duke@435 36 static Node *transform_int_divide_to_long_multiply( PhaseGVN *phase, Node *dividend, int divisor ) {
duke@435 37
duke@435 38 // Check for invalid divisors
duke@435 39 assert( divisor != 0 && divisor != min_jint && divisor != 1,
duke@435 40 "bad divisor for transforming to long multiply" );
duke@435 41
duke@435 42 // Compute l = ceiling(log2(d))
duke@435 43 // presumes d is more likely small
duke@435 44 bool d_pos = divisor >= 0;
duke@435 45 int d = d_pos ? divisor : -divisor;
duke@435 46 unsigned ud = (unsigned)d;
duke@435 47 const int N = 32;
duke@435 48 int l = log2_intptr(d-1)+1;
duke@435 49 int sh_post = l;
duke@435 50
duke@435 51 const uint64_t U1 = (uint64_t)1;
duke@435 52
duke@435 53 // Cliff pointed out how to prevent overflow (from the paper)
duke@435 54 uint64_t m_low = (((U1 << l) - ud) << N) / ud + (U1 << N);
duke@435 55 uint64_t m_high = ((((U1 << l) - ud) << N) + (U1 << (l+1))) / ud + (U1 << N);
duke@435 56
duke@435 57 // Reduce to lowest terms
duke@435 58 for ( ; sh_post > 0; sh_post-- ) {
duke@435 59 uint64_t m_low_1 = m_low >> 1;
duke@435 60 uint64_t m_high_1 = m_high >> 1;
duke@435 61 if ( m_low_1 >= m_high_1 )
duke@435 62 break;
duke@435 63 m_low = m_low_1;
duke@435 64 m_high = m_high_1;
duke@435 65 }
duke@435 66
duke@435 67 // Result
duke@435 68 Node *q;
duke@435 69
duke@435 70 // division by +/- 1
duke@435 71 if (d == 1) {
duke@435 72 // Filtered out as identity above
duke@435 73 if (d_pos)
duke@435 74 return NULL;
duke@435 75
duke@435 76 // Just negate the value
duke@435 77 else {
duke@435 78 q = new (phase->C, 3) SubINode(phase->intcon(0), dividend);
duke@435 79 }
duke@435 80 }
duke@435 81
duke@435 82 // division by +/- a power of 2
duke@435 83 else if ( is_power_of_2(d) ) {
duke@435 84
duke@435 85 // See if we can simply do a shift without rounding
duke@435 86 bool needs_rounding = true;
duke@435 87 const Type *dt = phase->type(dividend);
duke@435 88 const TypeInt *dti = dt->isa_int();
duke@435 89
duke@435 90 // we don't need to round a positive dividend
duke@435 91 if (dti && dti->_lo >= 0)
duke@435 92 needs_rounding = false;
duke@435 93
duke@435 94 // An AND mask of sufficient size clears the low bits and
duke@435 95 // I can avoid rounding.
duke@435 96 else if( dividend->Opcode() == Op_AndI ) {
duke@435 97 const TypeInt *andconi = phase->type( dividend->in(2) )->isa_int();
duke@435 98 if( andconi && andconi->is_con(-d) ) {
duke@435 99 dividend = dividend->in(1);
duke@435 100 needs_rounding = false;
duke@435 101 }
duke@435 102 }
duke@435 103
duke@435 104 // Add rounding to the shift to handle the sign bit
duke@435 105 if( needs_rounding ) {
duke@435 106 Node *t1 = phase->transform(new (phase->C, 3) RShiftINode(dividend, phase->intcon(l - 1)));
duke@435 107 Node *t2 = phase->transform(new (phase->C, 3) URShiftINode(t1, phase->intcon(N - l)));
duke@435 108 dividend = phase->transform(new (phase->C, 3) AddINode(dividend, t2));
duke@435 109 }
duke@435 110
duke@435 111 q = new (phase->C, 3) RShiftINode(dividend, phase->intcon(l));
duke@435 112
duke@435 113 if (!d_pos)
duke@435 114 q = new (phase->C, 3) SubINode(phase->intcon(0), phase->transform(q));
duke@435 115 }
duke@435 116
duke@435 117 // division by something else
duke@435 118 else if (m_high < (U1 << (N-1))) {
duke@435 119 Node *t1 = phase->transform(new (phase->C, 2) ConvI2LNode(dividend));
duke@435 120 Node *t2 = phase->transform(new (phase->C, 3) MulLNode(t1, phase->longcon(m_high)));
duke@435 121 Node *t3 = phase->transform(new (phase->C, 3) RShiftLNode(t2, phase->intcon(sh_post+N)));
duke@435 122 Node *t4 = phase->transform(new (phase->C, 2) ConvL2INode(t3));
duke@435 123 Node *t5 = phase->transform(new (phase->C, 3) RShiftINode(dividend, phase->intcon(N-1)));
duke@435 124
duke@435 125 q = new (phase->C, 3) SubINode(d_pos ? t4 : t5, d_pos ? t5 : t4);
duke@435 126 }
duke@435 127
duke@435 128 // This handles that case where m_high is >= 2**(N-1). In that case,
duke@435 129 // we subtract out 2**N from the multiply and add it in later as
duke@435 130 // "dividend" in the equation (t5). This case computes the same result
duke@435 131 // as the immediately preceeding case, save that rounding and overflow
duke@435 132 // are accounted for.
duke@435 133 else {
duke@435 134 Node *t1 = phase->transform(new (phase->C, 2) ConvI2LNode(dividend));
duke@435 135 Node *t2 = phase->transform(new (phase->C, 3) MulLNode(t1, phase->longcon(m_high - (U1 << N))));
duke@435 136 Node *t3 = phase->transform(new (phase->C, 3) RShiftLNode(t2, phase->intcon(N)));
duke@435 137 Node *t4 = phase->transform(new (phase->C, 2) ConvL2INode(t3));
duke@435 138 Node *t5 = phase->transform(new (phase->C, 3) AddINode(dividend, t4));
duke@435 139 Node *t6 = phase->transform(new (phase->C, 3) RShiftINode(t5, phase->intcon(sh_post)));
duke@435 140 Node *t7 = phase->transform(new (phase->C, 3) RShiftINode(dividend, phase->intcon(N-1)));
duke@435 141
duke@435 142 q = new (phase->C, 3) SubINode(d_pos ? t6 : t7, d_pos ? t7 : t6);
duke@435 143 }
duke@435 144
duke@435 145 return (q);
duke@435 146 }
duke@435 147
duke@435 148 //=============================================================================
duke@435 149 //------------------------------Identity---------------------------------------
duke@435 150 // If the divisor is 1, we are an identity on the dividend.
duke@435 151 Node *DivINode::Identity( PhaseTransform *phase ) {
duke@435 152 return (phase->type( in(2) )->higher_equal(TypeInt::ONE)) ? in(1) : this;
duke@435 153 }
duke@435 154
duke@435 155 //------------------------------Idealize---------------------------------------
duke@435 156 // Divides can be changed to multiplies and/or shifts
duke@435 157 Node *DivINode::Ideal(PhaseGVN *phase, bool can_reshape) {
duke@435 158 if (in(0) && remove_dead_region(phase, can_reshape)) return this;
duke@435 159
duke@435 160 const Type *t = phase->type( in(2) );
duke@435 161 if( t == TypeInt::ONE ) // Identity?
duke@435 162 return NULL; // Skip it
duke@435 163
duke@435 164 const TypeInt *ti = t->isa_int();
duke@435 165 if( !ti ) return NULL;
duke@435 166 if( !ti->is_con() ) return NULL;
duke@435 167 int i = ti->get_con(); // Get divisor
duke@435 168
duke@435 169 if (i == 0) return NULL; // Dividing by zero constant does not idealize
duke@435 170
duke@435 171 set_req(0,NULL); // Dividing by a not-zero constant; no faulting
duke@435 172
duke@435 173 // Dividing by MININT does not optimize as a power-of-2 shift.
duke@435 174 if( i == min_jint ) return NULL;
duke@435 175
duke@435 176 return transform_int_divide_to_long_multiply( phase, in(1), i );
duke@435 177 }
duke@435 178
duke@435 179 //------------------------------Value------------------------------------------
duke@435 180 // A DivINode divides its inputs. The third input is a Control input, used to
duke@435 181 // prevent hoisting the divide above an unsafe test.
duke@435 182 const Type *DivINode::Value( PhaseTransform *phase ) const {
duke@435 183 // Either input is TOP ==> the result is TOP
duke@435 184 const Type *t1 = phase->type( in(1) );
duke@435 185 const Type *t2 = phase->type( in(2) );
duke@435 186 if( t1 == Type::TOP ) return Type::TOP;
duke@435 187 if( t2 == Type::TOP ) return Type::TOP;
duke@435 188
duke@435 189 // x/x == 1 since we always generate the dynamic divisor check for 0.
duke@435 190 if( phase->eqv( in(1), in(2) ) )
duke@435 191 return TypeInt::ONE;
duke@435 192
duke@435 193 // Either input is BOTTOM ==> the result is the local BOTTOM
duke@435 194 const Type *bot = bottom_type();
duke@435 195 if( (t1 == bot) || (t2 == bot) ||
duke@435 196 (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
duke@435 197 return bot;
duke@435 198
duke@435 199 // Divide the two numbers. We approximate.
duke@435 200 // If divisor is a constant and not zero
duke@435 201 const TypeInt *i1 = t1->is_int();
duke@435 202 const TypeInt *i2 = t2->is_int();
duke@435 203 int widen = MAX2(i1->_widen, i2->_widen);
duke@435 204
duke@435 205 if( i2->is_con() && i2->get_con() != 0 ) {
duke@435 206 int32 d = i2->get_con(); // Divisor
duke@435 207 jint lo, hi;
duke@435 208 if( d >= 0 ) {
duke@435 209 lo = i1->_lo/d;
duke@435 210 hi = i1->_hi/d;
duke@435 211 } else {
duke@435 212 if( d == -1 && i1->_lo == min_jint ) {
duke@435 213 // 'min_jint/-1' throws arithmetic exception during compilation
duke@435 214 lo = min_jint;
duke@435 215 // do not support holes, 'hi' must go to either min_jint or max_jint:
duke@435 216 // [min_jint, -10]/[-1,-1] ==> [min_jint] UNION [10,max_jint]
duke@435 217 hi = i1->_hi == min_jint ? min_jint : max_jint;
duke@435 218 } else {
duke@435 219 lo = i1->_hi/d;
duke@435 220 hi = i1->_lo/d;
duke@435 221 }
duke@435 222 }
duke@435 223 return TypeInt::make(lo, hi, widen);
duke@435 224 }
duke@435 225
duke@435 226 // If the dividend is a constant
duke@435 227 if( i1->is_con() ) {
duke@435 228 int32 d = i1->get_con();
duke@435 229 if( d < 0 ) {
duke@435 230 if( d == min_jint ) {
duke@435 231 // (-min_jint) == min_jint == (min_jint / -1)
duke@435 232 return TypeInt::make(min_jint, max_jint/2 + 1, widen);
duke@435 233 } else {
duke@435 234 return TypeInt::make(d, -d, widen);
duke@435 235 }
duke@435 236 }
duke@435 237 return TypeInt::make(-d, d, widen);
duke@435 238 }
duke@435 239
duke@435 240 // Otherwise we give up all hope
duke@435 241 return TypeInt::INT;
duke@435 242 }
duke@435 243
duke@435 244
duke@435 245 //=============================================================================
duke@435 246 //------------------------------Identity---------------------------------------
duke@435 247 // If the divisor is 1, we are an identity on the dividend.
duke@435 248 Node *DivLNode::Identity( PhaseTransform *phase ) {
duke@435 249 return (phase->type( in(2) )->higher_equal(TypeLong::ONE)) ? in(1) : this;
duke@435 250 }
duke@435 251
duke@435 252 //------------------------------Idealize---------------------------------------
duke@435 253 // Dividing by a power of 2 is a shift.
duke@435 254 Node *DivLNode::Ideal( PhaseGVN *phase, bool can_reshape) {
duke@435 255 if (in(0) && remove_dead_region(phase, can_reshape)) return this;
duke@435 256
duke@435 257 const Type *t = phase->type( in(2) );
duke@435 258 if( t == TypeLong::ONE ) // Identity?
duke@435 259 return NULL; // Skip it
duke@435 260
duke@435 261 const TypeLong *ti = t->isa_long();
duke@435 262 if( !ti ) return NULL;
duke@435 263 if( !ti->is_con() ) return NULL;
duke@435 264 jlong i = ti->get_con(); // Get divisor
duke@435 265 if( i ) set_req(0, NULL); // Dividing by a not-zero constant; no faulting
duke@435 266
duke@435 267 // Dividing by MININT does not optimize as a power-of-2 shift.
duke@435 268 if( i == min_jlong ) return NULL;
duke@435 269
duke@435 270 // Check for negative power of 2 divisor, if so, negate it and set a flag
duke@435 271 // to indicate result needs to be negated. Note that negating the dividend
duke@435 272 // here does not work when it has the value MININT
duke@435 273 Node *dividend = in(1);
duke@435 274 bool negate_res = false;
duke@435 275 if (is_power_of_2_long(-i)) {
duke@435 276 i = -i; // Flip divisor
duke@435 277 negate_res = true;
duke@435 278 }
duke@435 279
duke@435 280 // Check for power of 2
duke@435 281 if (!is_power_of_2_long(i)) // Is divisor a power of 2?
duke@435 282 return NULL; // Not a power of 2
duke@435 283
duke@435 284 // Compute number of bits to shift
duke@435 285 int log_i = log2_long(i);
duke@435 286
duke@435 287 // See if we can simply do a shift without rounding
duke@435 288 bool needs_rounding = true;
duke@435 289 const Type *dt = phase->type(dividend);
duke@435 290 const TypeLong *dtl = dt->isa_long();
duke@435 291
duke@435 292 if (dtl && dtl->_lo > 0) {
duke@435 293 // we don't need to round a positive dividend
duke@435 294 needs_rounding = false;
duke@435 295 } else if( dividend->Opcode() == Op_AndL ) {
duke@435 296 // An AND mask of sufficient size clears the low bits and
duke@435 297 // I can avoid rounding.
duke@435 298 const TypeLong *andconi = phase->type( dividend->in(2) )->isa_long();
duke@435 299 if( andconi &&
duke@435 300 andconi->is_con() &&
duke@435 301 andconi->get_con() == -i ) {
duke@435 302 dividend = dividend->in(1);
duke@435 303 needs_rounding = false;
duke@435 304 }
duke@435 305 }
duke@435 306
duke@435 307 if (!needs_rounding) {
duke@435 308 Node *result = new (phase->C, 3) RShiftLNode(dividend, phase->intcon(log_i));
duke@435 309 if (negate_res) {
duke@435 310 result = phase->transform(result);
duke@435 311 result = new (phase->C, 3) SubLNode(phase->longcon(0), result);
duke@435 312 }
duke@435 313 return result;
duke@435 314 }
duke@435 315
duke@435 316 // Divide-by-power-of-2 can be made into a shift, but you have to do
duke@435 317 // more math for the rounding. You need to add 0 for positive
duke@435 318 // numbers, and "i-1" for negative numbers. Example: i=4, so the
duke@435 319 // shift is by 2. You need to add 3 to negative dividends and 0 to
duke@435 320 // positive ones. So (-7+3)>>2 becomes -1, (-4+3)>>2 becomes -1,
duke@435 321 // (-2+3)>>2 becomes 0, etc.
duke@435 322
duke@435 323 // Compute 0 or -1, based on sign bit
duke@435 324 Node *sign = phase->transform(new (phase->C, 3) RShiftLNode(dividend,phase->intcon(63)));
duke@435 325 // Mask sign bit to the low sign bits
duke@435 326 Node *round = phase->transform(new (phase->C, 3) AndLNode(sign,phase->longcon(i-1)));
duke@435 327 // Round up before shifting
duke@435 328 Node *sum = phase->transform(new (phase->C, 3) AddLNode(dividend,round));
duke@435 329 // Shift for division
duke@435 330 Node *result = new (phase->C, 3) RShiftLNode(sum, phase->intcon(log_i));
duke@435 331 if (negate_res) {
duke@435 332 result = phase->transform(result);
duke@435 333 result = new (phase->C, 3) SubLNode(phase->longcon(0), result);
duke@435 334 }
duke@435 335
duke@435 336 return result;
duke@435 337 }
duke@435 338
duke@435 339 //------------------------------Value------------------------------------------
duke@435 340 // A DivLNode divides its inputs. The third input is a Control input, used to
duke@435 341 // prevent hoisting the divide above an unsafe test.
duke@435 342 const Type *DivLNode::Value( PhaseTransform *phase ) const {
duke@435 343 // Either input is TOP ==> the result is TOP
duke@435 344 const Type *t1 = phase->type( in(1) );
duke@435 345 const Type *t2 = phase->type( in(2) );
duke@435 346 if( t1 == Type::TOP ) return Type::TOP;
duke@435 347 if( t2 == Type::TOP ) return Type::TOP;
duke@435 348
duke@435 349 // x/x == 1 since we always generate the dynamic divisor check for 0.
duke@435 350 if( phase->eqv( in(1), in(2) ) )
duke@435 351 return TypeLong::ONE;
duke@435 352
duke@435 353 // Either input is BOTTOM ==> the result is the local BOTTOM
duke@435 354 const Type *bot = bottom_type();
duke@435 355 if( (t1 == bot) || (t2 == bot) ||
duke@435 356 (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
duke@435 357 return bot;
duke@435 358
duke@435 359 // Divide the two numbers. We approximate.
duke@435 360 // If divisor is a constant and not zero
duke@435 361 const TypeLong *i1 = t1->is_long();
duke@435 362 const TypeLong *i2 = t2->is_long();
duke@435 363 int widen = MAX2(i1->_widen, i2->_widen);
duke@435 364
duke@435 365 if( i2->is_con() && i2->get_con() != 0 ) {
duke@435 366 jlong d = i2->get_con(); // Divisor
duke@435 367 jlong lo, hi;
duke@435 368 if( d >= 0 ) {
duke@435 369 lo = i1->_lo/d;
duke@435 370 hi = i1->_hi/d;
duke@435 371 } else {
duke@435 372 if( d == CONST64(-1) && i1->_lo == min_jlong ) {
duke@435 373 // 'min_jlong/-1' throws arithmetic exception during compilation
duke@435 374 lo = min_jlong;
duke@435 375 // do not support holes, 'hi' must go to either min_jlong or max_jlong:
duke@435 376 // [min_jlong, -10]/[-1,-1] ==> [min_jlong] UNION [10,max_jlong]
duke@435 377 hi = i1->_hi == min_jlong ? min_jlong : max_jlong;
duke@435 378 } else {
duke@435 379 lo = i1->_hi/d;
duke@435 380 hi = i1->_lo/d;
duke@435 381 }
duke@435 382 }
duke@435 383 return TypeLong::make(lo, hi, widen);
duke@435 384 }
duke@435 385
duke@435 386 // If the dividend is a constant
duke@435 387 if( i1->is_con() ) {
duke@435 388 jlong d = i1->get_con();
duke@435 389 if( d < 0 ) {
duke@435 390 if( d == min_jlong ) {
duke@435 391 // (-min_jlong) == min_jlong == (min_jlong / -1)
duke@435 392 return TypeLong::make(min_jlong, max_jlong/2 + 1, widen);
duke@435 393 } else {
duke@435 394 return TypeLong::make(d, -d, widen);
duke@435 395 }
duke@435 396 }
duke@435 397 return TypeLong::make(-d, d, widen);
duke@435 398 }
duke@435 399
duke@435 400 // Otherwise we give up all hope
duke@435 401 return TypeLong::LONG;
duke@435 402 }
duke@435 403
duke@435 404
duke@435 405 //=============================================================================
duke@435 406 //------------------------------Value------------------------------------------
duke@435 407 // An DivFNode divides its inputs. The third input is a Control input, used to
duke@435 408 // prevent hoisting the divide above an unsafe test.
duke@435 409 const Type *DivFNode::Value( PhaseTransform *phase ) const {
duke@435 410 // Either input is TOP ==> the result is TOP
duke@435 411 const Type *t1 = phase->type( in(1) );
duke@435 412 const Type *t2 = phase->type( in(2) );
duke@435 413 if( t1 == Type::TOP ) return Type::TOP;
duke@435 414 if( t2 == Type::TOP ) return Type::TOP;
duke@435 415
duke@435 416 // Either input is BOTTOM ==> the result is the local BOTTOM
duke@435 417 const Type *bot = bottom_type();
duke@435 418 if( (t1 == bot) || (t2 == bot) ||
duke@435 419 (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
duke@435 420 return bot;
duke@435 421
duke@435 422 // x/x == 1, we ignore 0/0.
duke@435 423 // Note: if t1 and t2 are zero then result is NaN (JVMS page 213)
duke@435 424 // does not work for variables because of NaN's
duke@435 425 if( phase->eqv( in(1), in(2) ) && t1->base() == Type::FloatCon)
duke@435 426 if (!g_isnan(t1->getf()) && g_isfinite(t1->getf()) && t1->getf() != 0.0) // could be negative ZERO or NaN
duke@435 427 return TypeF::ONE;
duke@435 428
duke@435 429 if( t2 == TypeF::ONE )
duke@435 430 return t1;
duke@435 431
duke@435 432 // If divisor is a constant and not zero, divide them numbers
duke@435 433 if( t1->base() == Type::FloatCon &&
duke@435 434 t2->base() == Type::FloatCon &&
duke@435 435 t2->getf() != 0.0 ) // could be negative zero
duke@435 436 return TypeF::make( t1->getf()/t2->getf() );
duke@435 437
duke@435 438 // If the dividend is a constant zero
duke@435 439 // Note: if t1 and t2 are zero then result is NaN (JVMS page 213)
duke@435 440 // Test TypeF::ZERO is not sufficient as it could be negative zero
duke@435 441
duke@435 442 if( t1 == TypeF::ZERO && !g_isnan(t2->getf()) && t2->getf() != 0.0 )
duke@435 443 return TypeF::ZERO;
duke@435 444
duke@435 445 // Otherwise we give up all hope
duke@435 446 return Type::FLOAT;
duke@435 447 }
duke@435 448
duke@435 449 //------------------------------isA_Copy---------------------------------------
duke@435 450 // Dividing by self is 1.
duke@435 451 // If the divisor is 1, we are an identity on the dividend.
duke@435 452 Node *DivFNode::Identity( PhaseTransform *phase ) {
duke@435 453 return (phase->type( in(2) ) == TypeF::ONE) ? in(1) : this;
duke@435 454 }
duke@435 455
duke@435 456
duke@435 457 //------------------------------Idealize---------------------------------------
duke@435 458 Node *DivFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
duke@435 459 if (in(0) && remove_dead_region(phase, can_reshape)) return this;
duke@435 460
duke@435 461 const Type *t2 = phase->type( in(2) );
duke@435 462 if( t2 == TypeF::ONE ) // Identity?
duke@435 463 return NULL; // Skip it
duke@435 464
duke@435 465 const TypeF *tf = t2->isa_float_constant();
duke@435 466 if( !tf ) return NULL;
duke@435 467 if( tf->base() != Type::FloatCon ) return NULL;
duke@435 468
duke@435 469 // Check for out of range values
duke@435 470 if( tf->is_nan() || !tf->is_finite() ) return NULL;
duke@435 471
duke@435 472 // Get the value
duke@435 473 float f = tf->getf();
duke@435 474 int exp;
duke@435 475
duke@435 476 // Only for special case of dividing by a power of 2
duke@435 477 if( frexp((double)f, &exp) != 0.5 ) return NULL;
duke@435 478
duke@435 479 // Limit the range of acceptable exponents
duke@435 480 if( exp < -126 || exp > 126 ) return NULL;
duke@435 481
duke@435 482 // Compute the reciprocal
duke@435 483 float reciprocal = ((float)1.0) / f;
duke@435 484
duke@435 485 assert( frexp((double)reciprocal, &exp) == 0.5, "reciprocal should be power of 2" );
duke@435 486
duke@435 487 // return multiplication by the reciprocal
duke@435 488 return (new (phase->C, 3) MulFNode(in(1), phase->makecon(TypeF::make(reciprocal))));
duke@435 489 }
duke@435 490
duke@435 491 //=============================================================================
duke@435 492 //------------------------------Value------------------------------------------
duke@435 493 // An DivDNode divides its inputs. The third input is a Control input, used to
duke@435 494 // prvent hoisting the divide above an unsafe test.
duke@435 495 const Type *DivDNode::Value( PhaseTransform *phase ) const {
duke@435 496 // Either input is TOP ==> the result is TOP
duke@435 497 const Type *t1 = phase->type( in(1) );
duke@435 498 const Type *t2 = phase->type( in(2) );
duke@435 499 if( t1 == Type::TOP ) return Type::TOP;
duke@435 500 if( t2 == Type::TOP ) return Type::TOP;
duke@435 501
duke@435 502 // Either input is BOTTOM ==> the result is the local BOTTOM
duke@435 503 const Type *bot = bottom_type();
duke@435 504 if( (t1 == bot) || (t2 == bot) ||
duke@435 505 (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
duke@435 506 return bot;
duke@435 507
duke@435 508 // x/x == 1, we ignore 0/0.
duke@435 509 // Note: if t1 and t2 are zero then result is NaN (JVMS page 213)
duke@435 510 // Does not work for variables because of NaN's
duke@435 511 if( phase->eqv( in(1), in(2) ) && t1->base() == Type::DoubleCon)
duke@435 512 if (!g_isnan(t1->getd()) && g_isfinite(t1->getd()) && t1->getd() != 0.0) // could be negative ZERO or NaN
duke@435 513 return TypeD::ONE;
duke@435 514
duke@435 515 if( t2 == TypeD::ONE )
duke@435 516 return t1;
duke@435 517
duke@435 518 // If divisor is a constant and not zero, divide them numbers
duke@435 519 if( t1->base() == Type::DoubleCon &&
duke@435 520 t2->base() == Type::DoubleCon &&
duke@435 521 t2->getd() != 0.0 ) // could be negative zero
duke@435 522 return TypeD::make( t1->getd()/t2->getd() );
duke@435 523
duke@435 524 // If the dividend is a constant zero
duke@435 525 // Note: if t1 and t2 are zero then result is NaN (JVMS page 213)
duke@435 526 // Test TypeF::ZERO is not sufficient as it could be negative zero
duke@435 527 if( t1 == TypeD::ZERO && !g_isnan(t2->getd()) && t2->getd() != 0.0 )
duke@435 528 return TypeD::ZERO;
duke@435 529
duke@435 530 // Otherwise we give up all hope
duke@435 531 return Type::DOUBLE;
duke@435 532 }
duke@435 533
duke@435 534
duke@435 535 //------------------------------isA_Copy---------------------------------------
duke@435 536 // Dividing by self is 1.
duke@435 537 // If the divisor is 1, we are an identity on the dividend.
duke@435 538 Node *DivDNode::Identity( PhaseTransform *phase ) {
duke@435 539 return (phase->type( in(2) ) == TypeD::ONE) ? in(1) : this;
duke@435 540 }
duke@435 541
duke@435 542 //------------------------------Idealize---------------------------------------
duke@435 543 Node *DivDNode::Ideal(PhaseGVN *phase, bool can_reshape) {
duke@435 544 if (in(0) && remove_dead_region(phase, can_reshape)) return this;
duke@435 545
duke@435 546 const Type *t2 = phase->type( in(2) );
duke@435 547 if( t2 == TypeD::ONE ) // Identity?
duke@435 548 return NULL; // Skip it
duke@435 549
duke@435 550 const TypeD *td = t2->isa_double_constant();
duke@435 551 if( !td ) return NULL;
duke@435 552 if( td->base() != Type::DoubleCon ) return NULL;
duke@435 553
duke@435 554 // Check for out of range values
duke@435 555 if( td->is_nan() || !td->is_finite() ) return NULL;
duke@435 556
duke@435 557 // Get the value
duke@435 558 double d = td->getd();
duke@435 559 int exp;
duke@435 560
duke@435 561 // Only for special case of dividing by a power of 2
duke@435 562 if( frexp(d, &exp) != 0.5 ) return NULL;
duke@435 563
duke@435 564 // Limit the range of acceptable exponents
duke@435 565 if( exp < -1021 || exp > 1022 ) return NULL;
duke@435 566
duke@435 567 // Compute the reciprocal
duke@435 568 double reciprocal = 1.0 / d;
duke@435 569
duke@435 570 assert( frexp(reciprocal, &exp) == 0.5, "reciprocal should be power of 2" );
duke@435 571
duke@435 572 // return multiplication by the reciprocal
duke@435 573 return (new (phase->C, 3) MulDNode(in(1), phase->makecon(TypeD::make(reciprocal))));
duke@435 574 }
duke@435 575
duke@435 576 //=============================================================================
duke@435 577 //------------------------------Idealize---------------------------------------
duke@435 578 Node *ModINode::Ideal(PhaseGVN *phase, bool can_reshape) {
duke@435 579 // Check for dead control input
duke@435 580 if( remove_dead_region(phase, can_reshape) ) return this;
duke@435 581
duke@435 582 // Get the modulus
duke@435 583 const Type *t = phase->type( in(2) );
duke@435 584 if( t == Type::TOP ) return NULL;
duke@435 585 const TypeInt *ti = t->is_int();
duke@435 586
duke@435 587 // Check for useless control input
duke@435 588 // Check for excluding mod-zero case
duke@435 589 if( in(0) && (ti->_hi < 0 || ti->_lo > 0) ) {
duke@435 590 set_req(0, NULL); // Yank control input
duke@435 591 return this;
duke@435 592 }
duke@435 593
duke@435 594 // See if we are MOD'ing by 2^k or 2^k-1.
duke@435 595 if( !ti->is_con() ) return NULL;
duke@435 596 jint con = ti->get_con();
duke@435 597
duke@435 598 Node *hook = new (phase->C, 1) Node(1);
duke@435 599
duke@435 600 // First, special check for modulo 2^k-1
duke@435 601 if( con >= 0 && con < max_jint && is_power_of_2(con+1) ) {
duke@435 602 uint k = exact_log2(con+1); // Extract k
duke@435 603
duke@435 604 // Basic algorithm by David Detlefs. See fastmod_int.java for gory details.
duke@435 605 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*/};
duke@435 606 int trip_count = 1;
duke@435 607 if( k < ARRAY_SIZE(unroll_factor)) trip_count = unroll_factor[k];
duke@435 608
duke@435 609 // If the unroll factor is not too large, and if conditional moves are
duke@435 610 // ok, then use this case
duke@435 611 if( trip_count <= 5 && ConditionalMoveLimit != 0 ) {
duke@435 612 Node *x = in(1); // Value being mod'd
duke@435 613 Node *divisor = in(2); // Also is mask
duke@435 614
duke@435 615 hook->init_req(0, x); // Add a use to x to prevent him from dying
duke@435 616 // Generate code to reduce X rapidly to nearly 2^k-1.
duke@435 617 for( int i = 0; i < trip_count; i++ ) {
duke@435 618 Node *xl = phase->transform( new (phase->C, 3) AndINode(x,divisor) );
duke@435 619 Node *xh = phase->transform( new (phase->C, 3) RShiftINode(x,phase->intcon(k)) ); // Must be signed
duke@435 620 x = phase->transform( new (phase->C, 3) AddINode(xh,xl) );
duke@435 621 hook->set_req(0, x);
duke@435 622 }
duke@435 623
duke@435 624 // Generate sign-fixup code. Was original value positive?
duke@435 625 // int hack_res = (i >= 0) ? divisor : 1;
duke@435 626 Node *cmp1 = phase->transform( new (phase->C, 3) CmpINode( in(1), phase->intcon(0) ) );
duke@435 627 Node *bol1 = phase->transform( new (phase->C, 2) BoolNode( cmp1, BoolTest::ge ) );
duke@435 628 Node *cmov1= phase->transform( new (phase->C, 4) CMoveINode(bol1, phase->intcon(1), divisor, TypeInt::POS) );
duke@435 629 // if( x >= hack_res ) x -= divisor;
duke@435 630 Node *sub = phase->transform( new (phase->C, 3) SubINode( x, divisor ) );
duke@435 631 Node *cmp2 = phase->transform( new (phase->C, 3) CmpINode( x, cmov1 ) );
duke@435 632 Node *bol2 = phase->transform( new (phase->C, 2) BoolNode( cmp2, BoolTest::ge ) );
duke@435 633 // Convention is to not transform the return value of an Ideal
duke@435 634 // since Ideal is expected to return a modified 'this' or a new node.
duke@435 635 Node *cmov2= new (phase->C, 4) CMoveINode(bol2, x, sub, TypeInt::INT);
duke@435 636 // cmov2 is now the mod
duke@435 637
duke@435 638 // Now remove the bogus extra edges used to keep things alive
duke@435 639 if (can_reshape) {
duke@435 640 phase->is_IterGVN()->remove_dead_node(hook);
duke@435 641 } else {
duke@435 642 hook->set_req(0, NULL); // Just yank bogus edge during Parse phase
duke@435 643 }
duke@435 644 return cmov2;
duke@435 645 }
duke@435 646 }
duke@435 647
duke@435 648 // Fell thru, the unroll case is not appropriate. Transform the modulo
duke@435 649 // into a long multiply/int multiply/subtract case
duke@435 650
duke@435 651 // Cannot handle mod 0, and min_jint isn't handled by the transform
duke@435 652 if( con == 0 || con == min_jint ) return NULL;
duke@435 653
duke@435 654 // Get the absolute value of the constant; at this point, we can use this
duke@435 655 jint pos_con = (con >= 0) ? con : -con;
duke@435 656
duke@435 657 // integer Mod 1 is always 0
duke@435 658 if( pos_con == 1 ) return new (phase->C, 1) ConINode(TypeInt::ZERO);
duke@435 659
duke@435 660 int log2_con = -1;
duke@435 661
duke@435 662 // If this is a power of two, they maybe we can mask it
duke@435 663 if( is_power_of_2(pos_con) ) {
duke@435 664 log2_con = log2_intptr((intptr_t)pos_con);
duke@435 665
duke@435 666 const Type *dt = phase->type(in(1));
duke@435 667 const TypeInt *dti = dt->isa_int();
duke@435 668
duke@435 669 // See if this can be masked, if the dividend is non-negative
duke@435 670 if( dti && dti->_lo >= 0 )
duke@435 671 return ( new (phase->C, 3) AndINode( in(1), phase->intcon( pos_con-1 ) ) );
duke@435 672 }
duke@435 673
duke@435 674 // Save in(1) so that it cannot be changed or deleted
duke@435 675 hook->init_req(0, in(1));
duke@435 676
duke@435 677 // Divide using the transform from DivI to MulL
duke@435 678 Node *divide = phase->transform( transform_int_divide_to_long_multiply( phase, in(1), pos_con ) );
duke@435 679
duke@435 680 // Re-multiply, using a shift if this is a power of two
duke@435 681 Node *mult = NULL;
duke@435 682
duke@435 683 if( log2_con >= 0 )
duke@435 684 mult = phase->transform( new (phase->C, 3) LShiftINode( divide, phase->intcon( log2_con ) ) );
duke@435 685 else
duke@435 686 mult = phase->transform( new (phase->C, 3) MulINode( divide, phase->intcon( pos_con ) ) );
duke@435 687
duke@435 688 // Finally, subtract the multiplied divided value from the original
duke@435 689 Node *result = new (phase->C, 3) SubINode( in(1), mult );
duke@435 690
duke@435 691 // Now remove the bogus extra edges used to keep things alive
duke@435 692 if (can_reshape) {
duke@435 693 phase->is_IterGVN()->remove_dead_node(hook);
duke@435 694 } else {
duke@435 695 hook->set_req(0, NULL); // Just yank bogus edge during Parse phase
duke@435 696 }
duke@435 697
duke@435 698 // return the value
duke@435 699 return result;
duke@435 700 }
duke@435 701
duke@435 702 //------------------------------Value------------------------------------------
duke@435 703 const Type *ModINode::Value( PhaseTransform *phase ) const {
duke@435 704 // Either input is TOP ==> the result is TOP
duke@435 705 const Type *t1 = phase->type( in(1) );
duke@435 706 const Type *t2 = phase->type( in(2) );
duke@435 707 if( t1 == Type::TOP ) return Type::TOP;
duke@435 708 if( t2 == Type::TOP ) return Type::TOP;
duke@435 709
duke@435 710 // We always generate the dynamic check for 0.
duke@435 711 // 0 MOD X is 0
duke@435 712 if( t1 == TypeInt::ZERO ) return TypeInt::ZERO;
duke@435 713 // X MOD X is 0
duke@435 714 if( phase->eqv( in(1), in(2) ) ) return TypeInt::ZERO;
duke@435 715
duke@435 716 // Either input is BOTTOM ==> the result is the local BOTTOM
duke@435 717 const Type *bot = bottom_type();
duke@435 718 if( (t1 == bot) || (t2 == bot) ||
duke@435 719 (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
duke@435 720 return bot;
duke@435 721
duke@435 722 const TypeInt *i1 = t1->is_int();
duke@435 723 const TypeInt *i2 = t2->is_int();
duke@435 724 if( !i1->is_con() || !i2->is_con() ) {
duke@435 725 if( i1->_lo >= 0 && i2->_lo >= 0 )
duke@435 726 return TypeInt::POS;
duke@435 727 // If both numbers are not constants, we know little.
duke@435 728 return TypeInt::INT;
duke@435 729 }
duke@435 730 // Mod by zero? Throw exception at runtime!
duke@435 731 if( !i2->get_con() ) return TypeInt::POS;
duke@435 732
duke@435 733 // We must be modulo'ing 2 float constants.
duke@435 734 // Check for min_jint % '-1', result is defined to be '0'.
duke@435 735 if( i1->get_con() == min_jint && i2->get_con() == -1 )
duke@435 736 return TypeInt::ZERO;
duke@435 737
duke@435 738 return TypeInt::make( i1->get_con() % i2->get_con() );
duke@435 739 }
duke@435 740
duke@435 741
duke@435 742 //=============================================================================
duke@435 743 //------------------------------Idealize---------------------------------------
duke@435 744 Node *ModLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
duke@435 745 // Check for dead control input
duke@435 746 if( remove_dead_region(phase, can_reshape) ) return this;
duke@435 747
duke@435 748 // Get the modulus
duke@435 749 const Type *t = phase->type( in(2) );
duke@435 750 if( t == Type::TOP ) return NULL;
duke@435 751 const TypeLong *ti = t->is_long();
duke@435 752
duke@435 753 // Check for useless control input
duke@435 754 // Check for excluding mod-zero case
duke@435 755 if( in(0) && (ti->_hi < 0 || ti->_lo > 0) ) {
duke@435 756 set_req(0, NULL); // Yank control input
duke@435 757 return this;
duke@435 758 }
duke@435 759
duke@435 760 // See if we are MOD'ing by 2^k or 2^k-1.
duke@435 761 if( !ti->is_con() ) return NULL;
duke@435 762 jlong con = ti->get_con();
duke@435 763 bool m1 = false;
duke@435 764 if( !is_power_of_2_long(con) ) { // Not 2^k
duke@435 765 if( !is_power_of_2_long(con+1) ) // Not 2^k-1?
duke@435 766 return NULL; // No interesting mod hacks
duke@435 767 m1 = true; // Found 2^k-1
duke@435 768 con++; // Convert to 2^k form
duke@435 769 }
duke@435 770 uint k = log2_long(con); // Extract k
duke@435 771
duke@435 772 // Expand mod
duke@435 773 if( !m1 ) { // Case 2^k
duke@435 774 } else { // Case 2^k-1
duke@435 775 // Basic algorithm by David Detlefs. See fastmod_long.java for gory details.
duke@435 776 // Used to help a popular random number generator which does a long-mod
duke@435 777 // of 2^31-1 and shows up in SpecJBB and SciMark.
duke@435 778 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*/};
duke@435 779 int trip_count = 1;
duke@435 780 if( k < ARRAY_SIZE(unroll_factor)) trip_count = unroll_factor[k];
duke@435 781 if( trip_count > 4 ) return NULL; // Too much unrolling
duke@435 782 if (ConditionalMoveLimit == 0) return NULL; // cmov is required
duke@435 783
duke@435 784 Node *x = in(1); // Value being mod'd
duke@435 785 Node *divisor = in(2); // Also is mask
duke@435 786
duke@435 787 Node *hook = new (phase->C, 1) Node(x);
duke@435 788 // Generate code to reduce X rapidly to nearly 2^k-1.
duke@435 789 for( int i = 0; i < trip_count; i++ ) {
duke@435 790 Node *xl = phase->transform( new (phase->C, 3) AndLNode(x,divisor) );
duke@435 791 Node *xh = phase->transform( new (phase->C, 3) RShiftLNode(x,phase->intcon(k)) ); // Must be signed
duke@435 792 x = phase->transform( new (phase->C, 3) AddLNode(xh,xl) );
duke@435 793 hook->set_req(0, x); // Add a use to x to prevent him from dying
duke@435 794 }
duke@435 795 // Generate sign-fixup code. Was original value positive?
duke@435 796 // long hack_res = (i >= 0) ? divisor : CONST64(1);
duke@435 797 Node *cmp1 = phase->transform( new (phase->C, 3) CmpLNode( in(1), phase->longcon(0) ) );
duke@435 798 Node *bol1 = phase->transform( new (phase->C, 2) BoolNode( cmp1, BoolTest::ge ) );
duke@435 799 Node *cmov1= phase->transform( new (phase->C, 4) CMoveLNode(bol1, phase->longcon(1), divisor, TypeLong::LONG) );
duke@435 800 // if( x >= hack_res ) x -= divisor;
duke@435 801 Node *sub = phase->transform( new (phase->C, 3) SubLNode( x, divisor ) );
duke@435 802 Node *cmp2 = phase->transform( new (phase->C, 3) CmpLNode( x, cmov1 ) );
duke@435 803 Node *bol2 = phase->transform( new (phase->C, 2) BoolNode( cmp2, BoolTest::ge ) );
duke@435 804 // Convention is to not transform the return value of an Ideal
duke@435 805 // since Ideal is expected to return a modified 'this' or a new node.
duke@435 806 Node *cmov2= new (phase->C, 4) CMoveLNode(bol2, x, sub, TypeLong::LONG);
duke@435 807 // cmov2 is now the mod
duke@435 808
duke@435 809 // Now remove the bogus extra edges used to keep things alive
duke@435 810 if (can_reshape) {
duke@435 811 phase->is_IterGVN()->remove_dead_node(hook);
duke@435 812 } else {
duke@435 813 hook->set_req(0, NULL); // Just yank bogus edge during Parse phase
duke@435 814 }
duke@435 815 return cmov2;
duke@435 816 }
duke@435 817 return NULL;
duke@435 818 }
duke@435 819
duke@435 820 //------------------------------Value------------------------------------------
duke@435 821 const Type *ModLNode::Value( PhaseTransform *phase ) const {
duke@435 822 // Either input is TOP ==> the result is TOP
duke@435 823 const Type *t1 = phase->type( in(1) );
duke@435 824 const Type *t2 = phase->type( in(2) );
duke@435 825 if( t1 == Type::TOP ) return Type::TOP;
duke@435 826 if( t2 == Type::TOP ) return Type::TOP;
duke@435 827
duke@435 828 // We always generate the dynamic check for 0.
duke@435 829 // 0 MOD X is 0
duke@435 830 if( t1 == TypeLong::ZERO ) return TypeLong::ZERO;
duke@435 831 // X MOD X is 0
duke@435 832 if( phase->eqv( in(1), in(2) ) ) return TypeLong::ZERO;
duke@435 833
duke@435 834 // Either input is BOTTOM ==> the result is the local BOTTOM
duke@435 835 const Type *bot = bottom_type();
duke@435 836 if( (t1 == bot) || (t2 == bot) ||
duke@435 837 (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
duke@435 838 return bot;
duke@435 839
duke@435 840 const TypeLong *i1 = t1->is_long();
duke@435 841 const TypeLong *i2 = t2->is_long();
duke@435 842 if( !i1->is_con() || !i2->is_con() ) {
duke@435 843 if( i1->_lo >= CONST64(0) && i2->_lo >= CONST64(0) )
duke@435 844 return TypeLong::POS;
duke@435 845 // If both numbers are not constants, we know little.
duke@435 846 return TypeLong::LONG;
duke@435 847 }
duke@435 848 // Mod by zero? Throw exception at runtime!
duke@435 849 if( !i2->get_con() ) return TypeLong::POS;
duke@435 850
duke@435 851 // We must be modulo'ing 2 float constants.
duke@435 852 // Check for min_jint % '-1', result is defined to be '0'.
duke@435 853 if( i1->get_con() == min_jlong && i2->get_con() == -1 )
duke@435 854 return TypeLong::ZERO;
duke@435 855
duke@435 856 return TypeLong::make( i1->get_con() % i2->get_con() );
duke@435 857 }
duke@435 858
duke@435 859
duke@435 860 //=============================================================================
duke@435 861 //------------------------------Value------------------------------------------
duke@435 862 const Type *ModFNode::Value( PhaseTransform *phase ) const {
duke@435 863 // Either input is TOP ==> the result is TOP
duke@435 864 const Type *t1 = phase->type( in(1) );
duke@435 865 const Type *t2 = phase->type( in(2) );
duke@435 866 if( t1 == Type::TOP ) return Type::TOP;
duke@435 867 if( t2 == Type::TOP ) return Type::TOP;
duke@435 868
duke@435 869 // Either input is BOTTOM ==> the result is the local BOTTOM
duke@435 870 const Type *bot = bottom_type();
duke@435 871 if( (t1 == bot) || (t2 == bot) ||
duke@435 872 (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
duke@435 873 return bot;
duke@435 874
duke@435 875 // If either is a NaN, return an input NaN
duke@435 876 if( g_isnan(t1->getf()) ) return t1;
duke@435 877 if( g_isnan(t2->getf()) ) return t2;
duke@435 878
duke@435 879 // It is not worth trying to constant fold this stuff!
duke@435 880 return Type::FLOAT;
duke@435 881
duke@435 882 /*
duke@435 883 // If dividend is infinity or divisor is zero, or both, the result is NaN
duke@435 884 if( !g_isfinite(t1->getf()) || ((t2->getf() == 0.0) || (jint_cast(t2->getf()) == 0x80000000)) )
duke@435 885
duke@435 886 // X MOD infinity = X
duke@435 887 if( !g_isfinite(t2->getf()) && !g_isnan(t2->getf()) ) return t1;
duke@435 888 // 0 MOD finite = dividend (positive or negative zero)
duke@435 889 // Not valid for: NaN MOD any; any MOD nan; 0 MOD 0; or for 0 MOD NaN
duke@435 890 // NaNs are handled previously.
duke@435 891 if( !(t2->getf() == 0.0) && !((int)t2->getf() == 0x80000000)) {
duke@435 892 if (((t1->getf() == 0.0) || ((int)t1->getf() == 0x80000000)) && g_isfinite(t2->getf()) ) {
duke@435 893 return t1;
duke@435 894 }
duke@435 895 }
duke@435 896 // X MOD X is 0
duke@435 897 // Does not work for variables because of NaN's
duke@435 898 if( phase->eqv( in(1), in(2) ) && t1->base() == Type::FloatCon)
duke@435 899 if (!g_isnan(t1->getf()) && (t1->getf() != 0.0) && ((int)t1->getf() != 0x80000000)) {
duke@435 900 if(t1->getf() < 0.0) {
duke@435 901 float result = jfloat_cast(0x80000000);
duke@435 902 return TypeF::make( result );
duke@435 903 }
duke@435 904 else
duke@435 905 return TypeF::ZERO;
duke@435 906 }
duke@435 907
duke@435 908 // If both numbers are not constants, we know nothing.
duke@435 909 if( (t1->base() != Type::FloatCon) || (t2->base() != Type::FloatCon) )
duke@435 910 return Type::FLOAT;
duke@435 911
duke@435 912 // We must be modulo'ing 2 float constants.
duke@435 913 // Make sure that the sign of the fmod is equal to the sign of the dividend
duke@435 914 float result = (float)fmod( t1->getf(), t2->getf() );
duke@435 915 float dividend = t1->getf();
duke@435 916 if( (dividend < 0.0) || ((int)dividend == 0x80000000) ) {
duke@435 917 if( result > 0.0 )
duke@435 918 result = 0.0 - result;
duke@435 919 else if( result == 0.0 ) {
duke@435 920 result = jfloat_cast(0x80000000);
duke@435 921 }
duke@435 922 }
duke@435 923 return TypeF::make( result );
duke@435 924 */
duke@435 925 }
duke@435 926
duke@435 927
duke@435 928 //=============================================================================
duke@435 929 //------------------------------Value------------------------------------------
duke@435 930 const Type *ModDNode::Value( PhaseTransform *phase ) const {
duke@435 931 // Either input is TOP ==> the result is TOP
duke@435 932 const Type *t1 = phase->type( in(1) );
duke@435 933 const Type *t2 = phase->type( in(2) );
duke@435 934 if( t1 == Type::TOP ) return Type::TOP;
duke@435 935 if( t2 == Type::TOP ) return Type::TOP;
duke@435 936
duke@435 937 // Either input is BOTTOM ==> the result is the local BOTTOM
duke@435 938 const Type *bot = bottom_type();
duke@435 939 if( (t1 == bot) || (t2 == bot) ||
duke@435 940 (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
duke@435 941 return bot;
duke@435 942
duke@435 943 // If either is a NaN, return an input NaN
duke@435 944 if( g_isnan(t1->getd()) ) return t1;
duke@435 945 if( g_isnan(t2->getd()) ) return t2;
duke@435 946 // X MOD infinity = X
duke@435 947 if( !g_isfinite(t2->getd())) return t1;
duke@435 948 // 0 MOD finite = dividend (positive or negative zero)
duke@435 949 // Not valid for: NaN MOD any; any MOD nan; 0 MOD 0; or for 0 MOD NaN
duke@435 950 // NaNs are handled previously.
duke@435 951 if( !(t2->getd() == 0.0) ) {
duke@435 952 if( t1->getd() == 0.0 && g_isfinite(t2->getd()) ) {
duke@435 953 return t1;
duke@435 954 }
duke@435 955 }
duke@435 956
duke@435 957 // X MOD X is 0
duke@435 958 // does not work for variables because of NaN's
duke@435 959 if( phase->eqv( in(1), in(2) ) && t1->base() == Type::DoubleCon )
duke@435 960 if (!g_isnan(t1->getd()) && t1->getd() != 0.0)
duke@435 961 return TypeD::ZERO;
duke@435 962
duke@435 963
duke@435 964 // If both numbers are not constants, we know nothing.
duke@435 965 if( (t1->base() != Type::DoubleCon) || (t2->base() != Type::DoubleCon) )
duke@435 966 return Type::DOUBLE;
duke@435 967
duke@435 968 // We must be modulo'ing 2 double constants.
duke@435 969 return TypeD::make( fmod( t1->getd(), t2->getd() ) );
duke@435 970 }
duke@435 971
duke@435 972 //=============================================================================
duke@435 973
duke@435 974 DivModNode::DivModNode( Node *c, Node *dividend, Node *divisor ) : MultiNode(3) {
duke@435 975 init_req(0, c);
duke@435 976 init_req(1, dividend);
duke@435 977 init_req(2, divisor);
duke@435 978 }
duke@435 979
duke@435 980 //------------------------------make------------------------------------------
duke@435 981 DivModINode* DivModINode::make(Compile* C, Node* div_or_mod) {
duke@435 982 Node* n = div_or_mod;
duke@435 983 assert(n->Opcode() == Op_DivI || n->Opcode() == Op_ModI,
duke@435 984 "only div or mod input pattern accepted");
duke@435 985
duke@435 986 DivModINode* divmod = new (C, 3) DivModINode(n->in(0), n->in(1), n->in(2));
duke@435 987 Node* dproj = new (C, 1) ProjNode(divmod, DivModNode::div_proj_num);
duke@435 988 Node* mproj = new (C, 1) ProjNode(divmod, DivModNode::mod_proj_num);
duke@435 989 return divmod;
duke@435 990 }
duke@435 991
duke@435 992 //------------------------------make------------------------------------------
duke@435 993 DivModLNode* DivModLNode::make(Compile* C, Node* div_or_mod) {
duke@435 994 Node* n = div_or_mod;
duke@435 995 assert(n->Opcode() == Op_DivL || n->Opcode() == Op_ModL,
duke@435 996 "only div or mod input pattern accepted");
duke@435 997
duke@435 998 DivModLNode* divmod = new (C, 3) DivModLNode(n->in(0), n->in(1), n->in(2));
duke@435 999 Node* dproj = new (C, 1) ProjNode(divmod, DivModNode::div_proj_num);
duke@435 1000 Node* mproj = new (C, 1) ProjNode(divmod, DivModNode::mod_proj_num);
duke@435 1001 return divmod;
duke@435 1002 }
duke@435 1003
duke@435 1004 //------------------------------match------------------------------------------
duke@435 1005 // return result(s) along with their RegMask info
duke@435 1006 Node *DivModINode::match( const ProjNode *proj, const Matcher *match ) {
duke@435 1007 uint ideal_reg = proj->ideal_reg();
duke@435 1008 RegMask rm;
duke@435 1009 if (proj->_con == div_proj_num) {
duke@435 1010 rm = match->divI_proj_mask();
duke@435 1011 } else {
duke@435 1012 assert(proj->_con == mod_proj_num, "must be div or mod projection");
duke@435 1013 rm = match->modI_proj_mask();
duke@435 1014 }
duke@435 1015 return new (match->C, 1)MachProjNode(this, proj->_con, rm, ideal_reg);
duke@435 1016 }
duke@435 1017
duke@435 1018
duke@435 1019 //------------------------------match------------------------------------------
duke@435 1020 // return result(s) along with their RegMask info
duke@435 1021 Node *DivModLNode::match( const ProjNode *proj, const Matcher *match ) {
duke@435 1022 uint ideal_reg = proj->ideal_reg();
duke@435 1023 RegMask rm;
duke@435 1024 if (proj->_con == div_proj_num) {
duke@435 1025 rm = match->divL_proj_mask();
duke@435 1026 } else {
duke@435 1027 assert(proj->_con == mod_proj_num, "must be div or mod projection");
duke@435 1028 rm = match->modL_proj_mask();
duke@435 1029 }
duke@435 1030 return new (match->C, 1)MachProjNode(this, proj->_con, rm, ideal_reg);
duke@435 1031 }

mercurial