llvm.org GIT mirror llvm / 4e5ea55
Move helper functions for optimizing division by constant into the APInt class. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@70488 91177308-0d34-0410-b5e6-96231b3b80d8 Jay Foad 11 years ago
3 changed file(s) with 119 addition(s) and 101 deletion(s). Raw diff Collapse all Expand all
12571257 APInt multiplicativeInverse(const APInt& modulo) const;
12581258
12591259 /// @}
1260 /// @name Support for division by constant
1261 /// @{
1262
1263 /// Calculate the magic number for signed division by a constant.
1264 struct ms;
1265 ms magic() const;
1266
1267 /// Calculate the magic number for unsigned division by a constant.
1268 struct mu;
1269 mu magicu() const;
1270
1271 /// @}
12601272 /// @name Building-block Operations for APInt and APFloat
12611273 /// @{
12621274
13871399 /// @}
13881400 };
13891401
1402 /// Magic data for optimising signed division by a constant.
1403 struct APInt::ms {
1404 APInt m; ///< magic number
1405 unsigned s; ///< shift amount
1406 };
1407
1408 /// Magic data for optimising unsigned division by a constant.
1409 struct APInt::mu {
1410 APInt m; ///< magic number
1411 bool a; ///< add indicator
1412 unsigned s; ///< shift amount
1413 };
1414
13901415 inline bool operator==(uint64_t V1, const APInt& V2) {
13911416 return V2 == V1;
13921417 }
24332433 return true;
24342434 }
24352435
2436 struct mu {
2437 APInt m; // magic number
2438 bool a; // add indicator
2439 unsigned s; // shift amount
2440 };
2441
2442 /// magicu - calculate the magic numbers required to codegen an integer udiv as
2443 /// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
2444 static mu magicu(const APInt& d) {
2445 unsigned p;
2446 APInt nc, delta, q1, r1, q2, r2;
2447 struct mu magu;
2448 magu.a = 0; // initialize "add" indicator
2449 APInt allOnes = APInt::getAllOnesValue(d.getBitWidth());
2450 APInt signedMin = APInt::getSignedMinValue(d.getBitWidth());
2451 APInt signedMax = APInt::getSignedMaxValue(d.getBitWidth());
2452
2453 nc = allOnes - (-d).urem(d);
2454 p = d.getBitWidth() - 1; // initialize p
2455 q1 = signedMin.udiv(nc); // initialize q1 = 2p/nc
2456 r1 = signedMin - q1*nc; // initialize r1 = rem(2p,nc)
2457 q2 = signedMax.udiv(d); // initialize q2 = (2p-1)/d
2458 r2 = signedMax - q2*d; // initialize r2 = rem((2p-1),d)
2459 do {
2460 p = p + 1;
2461 if (r1.uge(nc - r1)) {
2462 q1 = q1 + q1 + 1; // update q1
2463 r1 = r1 + r1 - nc; // update r1
2464 }
2465 else {
2466 q1 = q1+q1; // update q1
2467 r1 = r1+r1; // update r1
2468 }
2469 if ((r2 + 1).uge(d - r2)) {
2470 if (q2.uge(signedMax)) magu.a = 1;
2471 q2 = q2+q2 + 1; // update q2
2472 r2 = r2+r2 + 1 - d; // update r2
2473 }
2474 else {
2475 if (q2.uge(signedMin)) magu.a = 1;
2476 q2 = q2+q2; // update q2
2477 r2 = r2+r2 + 1; // update r2
2478 }
2479 delta = d - 1 - r2;
2480 } while (p < d.getBitWidth()*2 &&
2481 (q1.ult(delta) || (q1 == delta && r1 == 0)));
2482 magu.m = q2 + 1; // resulting magic number
2483 magu.s = p - d.getBitWidth(); // resulting shift
2484 return magu;
2485 }
2486
2487 // Magic for divide replacement
2488 struct ms {
2489 APInt m; // magic number
2490 unsigned s; // shift amount
2491 };
2492
2493 /// magic - calculate the magic numbers required to codegen an integer sdiv as
2494 /// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
2495 /// or -1.
2496 static ms magic(const APInt& d) {
2497 unsigned p;
2498 APInt ad, anc, delta, q1, r1, q2, r2, t;
2499 APInt allOnes = APInt::getAllOnesValue(d.getBitWidth());
2500 APInt signedMin = APInt::getSignedMinValue(d.getBitWidth());
2501 APInt signedMax = APInt::getSignedMaxValue(d.getBitWidth());
2502 struct ms mag;
2503
2504 ad = d.abs();
2505 t = signedMin + (d.lshr(d.getBitWidth() - 1));
2506 anc = t - 1 - t.urem(ad); // absolute value of nc
2507 p = d.getBitWidth() - 1; // initialize p
2508 q1 = signedMin.udiv(anc); // initialize q1 = 2p/abs(nc)
2509 r1 = signedMin - q1*anc; // initialize r1 = rem(2p,abs(nc))
2510 q2 = signedMin.udiv(ad); // initialize q2 = 2p/abs(d)
2511 r2 = signedMin - q2*ad; // initialize r2 = rem(2p,abs(d))
2512 do {
2513 p = p + 1;
2514 q1 = q1<<1; // update q1 = 2p/abs(nc)
2515 r1 = r1<<1; // update r1 = rem(2p/abs(nc))
2516 if (r1.uge(anc)) { // must be unsigned comparison
2517 q1 = q1 + 1;
2518 r1 = r1 - anc;
2519 }
2520 q2 = q2<<1; // update q2 = 2p/abs(d)
2521 r2 = r2<<1; // update r2 = rem(2p/abs(d))
2522 if (r2.uge(ad)) { // must be unsigned comparison
2523 q2 = q2 + 1;
2524 r2 = r2 - ad;
2525 }
2526 delta = ad - r2;
2527 } while (q1.ule(delta) || (q1 == delta && r1 == 0));
2528
2529 mag.m = q2 + 1;
2530 if (d.isNegative()) mag.m = -mag.m; // resulting magic number
2531 mag.s = p - d.getBitWidth(); // resulting shift
2532 return mag;
2533 }
2534
25352436 /// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
25362437 /// return a DAG expression to select that will generate the same value by
25372438 /// multiplying by a magic number. See:
25472448 return SDValue();
25482449
25492450 APInt d = cast(N->getOperand(1))->getAPIntValue();
2550 ms magics = magic(d);
2451 APInt::ms magics = d.magic();
25512452
25522453 // Multiply the numerator (operand 0) by the magic value
25532454 // FIXME: We should support doing a MUL in a wider type
26062507 // FIXME: We should use a narrower constant when the upper
26072508 // bits are known to be zero.
26082509 ConstantSDNode *N1C = cast(N->getOperand(1));
2609 mu magics = magicu(N1C->getAPIntValue());
2510 APInt::mu magics = N1C->getAPIntValue().magicu();
26102511
26112512 // Multiply the numerator (operand 0) by the magic value
26122513 // FIXME: We should support doing a MUL in a wider type
14321432 // one if necessary. A simple addition of the modulo suffices because
14331433 // abs(t[i]) is known to be less than *this/2 (see the link above).
14341434 return t[i].isNegative() ? t[i] + modulo : t[i];
1435 }
1436
1437 /// Calculate the magic numbers required to implement a signed integer division
1438 /// by a constant as a sequence of multiplies, adds and shifts. Requires that
1439 /// the divisor not be 0, 1, or -1. Taken from "Hacker's Delight", Henry S.
1440 /// Warren, Jr., chapter 10.
1441 APInt::ms APInt::magic() const {
1442 const APInt& d = *this;
1443 unsigned p;
1444 APInt ad, anc, delta, q1, r1, q2, r2, t;
1445 APInt allOnes = APInt::getAllOnesValue(d.getBitWidth());
1446 APInt signedMin = APInt::getSignedMinValue(d.getBitWidth());
1447 APInt signedMax = APInt::getSignedMaxValue(d.getBitWidth());
1448 struct ms mag;
1449
1450 ad = d.abs();
1451 t = signedMin + (d.lshr(d.getBitWidth() - 1));
1452 anc = t - 1 - t.urem(ad); // absolute value of nc
1453 p = d.getBitWidth() - 1; // initialize p
1454 q1 = signedMin.udiv(anc); // initialize q1 = 2p/abs(nc)
1455 r1 = signedMin - q1*anc; // initialize r1 = rem(2p,abs(nc))
1456 q2 = signedMin.udiv(ad); // initialize q2 = 2p/abs(d)
1457 r2 = signedMin - q2*ad; // initialize r2 = rem(2p,abs(d))
1458 do {
1459 p = p + 1;
1460 q1 = q1<<1; // update q1 = 2p/abs(nc)
1461 r1 = r1<<1; // update r1 = rem(2p/abs(nc))
1462 if (r1.uge(anc)) { // must be unsigned comparison
1463 q1 = q1 + 1;
1464 r1 = r1 - anc;
1465 }
1466 q2 = q2<<1; // update q2 = 2p/abs(d)
1467 r2 = r2<<1; // update r2 = rem(2p/abs(d))
1468 if (r2.uge(ad)) { // must be unsigned comparison
1469 q2 = q2 + 1;
1470 r2 = r2 - ad;
1471 }
1472 delta = ad - r2;
1473 } while (q1.ule(delta) || (q1 == delta && r1 == 0));
1474
1475 mag.m = q2 + 1;
1476 if (d.isNegative()) mag.m = -mag.m; // resulting magic number
1477 mag.s = p - d.getBitWidth(); // resulting shift
1478 return mag;
1479 }
1480
1481 /// Calculate the magic numbers required to implement an unsigned integer
1482 /// division by a constant as a sequence of multiplies, adds and shifts.
1483 /// Requires that the divisor not be 0. Taken from "Hacker's Delight", Henry
1484 /// S. Warren, Jr., chapter 10.
1485 APInt::mu APInt::magicu() const {
1486 const APInt& d = *this;
1487 unsigned p;
1488 APInt nc, delta, q1, r1, q2, r2;
1489 struct mu magu;
1490 magu.a = 0; // initialize "add" indicator
1491 APInt allOnes = APInt::getAllOnesValue(d.getBitWidth());
1492 APInt signedMin = APInt::getSignedMinValue(d.getBitWidth());
1493 APInt signedMax = APInt::getSignedMaxValue(d.getBitWidth());
1494
1495 nc = allOnes - (-d).urem(d);
1496 p = d.getBitWidth() - 1; // initialize p
1497 q1 = signedMin.udiv(nc); // initialize q1 = 2p/nc
1498 r1 = signedMin - q1*nc; // initialize r1 = rem(2p,nc)
1499 q2 = signedMax.udiv(d); // initialize q2 = (2p-1)/d
1500 r2 = signedMax - q2*d; // initialize r2 = rem((2p-1),d)
1501 do {
1502 p = p + 1;
1503 if (r1.uge(nc - r1)) {
1504 q1 = q1 + q1 + 1; // update q1
1505 r1 = r1 + r1 - nc; // update r1
1506 }
1507 else {
1508 q1 = q1+q1; // update q1
1509 r1 = r1+r1; // update r1
1510 }
1511 if ((r2 + 1).uge(d - r2)) {
1512 if (q2.uge(signedMax)) magu.a = 1;
1513 q2 = q2+q2 + 1; // update q2
1514 r2 = r2+r2 + 1 - d; // update r2
1515 }
1516 else {
1517 if (q2.uge(signedMin)) magu.a = 1;
1518 q2 = q2+q2; // update q2
1519 r2 = r2+r2 + 1; // update r2
1520 }
1521 delta = d - 1 - r2;
1522 } while (p < d.getBitWidth()*2 &&
1523 (q1.ult(delta) || (q1 == delta && r1 == 0)));
1524 magu.m = q2 + 1; // resulting magic number
1525 magu.s = p - d.getBitWidth(); // resulting shift
1526 return magu;
14351527 }
14361528
14371529 /// Implementation of Knuth's Algorithm D (Division of nonnegative integers)