[llvm] r224217 - APInt: udivrem should use machine instructions for single-word APInts

David Majnemer david.majnemer at gmail.com
Sun Dec 14 01:41:56 PST 2014


Author: majnemer
Date: Sun Dec 14 03:41:56 2014
New Revision: 224217

URL: http://llvm.org/viewvc/llvm-project?rev=224217&view=rev
Log:
APInt: udivrem should use machine instructions for single-word APInts

This mirrors the behavior of APInt::udiv and APInt::urem.  Some
architectures, like X86, have a single instruction which can compute
both division and remainder.

Modified:
    llvm/trunk/lib/Support/APInt.cpp

Modified: llvm/trunk/lib/Support/APInt.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/APInt.cpp?rev=224217&r1=224216&r2=224217&view=diff
==============================================================================
--- llvm/trunk/lib/Support/APInt.cpp (original)
+++ llvm/trunk/lib/Support/APInt.cpp Sun Dec 14 03:41:56 2014
@@ -1956,6 +1956,18 @@ APInt APInt::srem(const APInt &RHS) cons
 
 void APInt::udivrem(const APInt &LHS, const APInt &RHS,
                     APInt &Quotient, APInt &Remainder) {
+  assert(LHS.BitWidth == RHS.BitWidth && "Bit widths must be the same");
+
+  // First, deal with the easy case
+  if (LHS.isSingleWord()) {
+    assert(RHS.VAL != 0 && "Divide by zero?");
+    uint64_t QuotVal = LHS.VAL / RHS.VAL;
+    uint64_t RemVal = LHS.VAL % RHS.VAL;
+    Quotient = APInt(LHS.BitWidth, QuotVal);
+    Remainder = APInt(LHS.BitWidth, RemVal);
+    return;
+  }
+
   // Get some size facts about the dividend and divisor
   unsigned lhsBits  = LHS.getActiveBits();
   unsigned lhsWords = !lhsBits ? 0 : (APInt::whichWord(lhsBits - 1) + 1);





More information about the llvm-commits mailing list