[Mlir-commits] [mlir] a1e62aa - Minor reflow of FloorDivSIOp/CeilDivSIOp folder to limit the number of APInt API calls (NFC)

Mehdi Amini llvmlistbot at llvm.org
Sun Jan 2 21:55:32 PST 2022


Author: Mehdi Amini
Date: 2022-01-03T05:55:23Z
New Revision: a1e62aa75b66e2b25e85fd98f41a8d6134192783

URL: https://github.com/llvm/llvm-project/commit/a1e62aa75b66e2b25e85fd98f41a8d6134192783
DIFF: https://github.com/llvm/llvm-project/commit/a1e62aa75b66e2b25e85fd98f41a8d6134192783.diff

LOG: Minor reflow of FloorDivSIOp/CeilDivSIOp folder to limit the number of APInt API calls (NFC)

Cache the result of the comparison in boolean, and check early for 0 to
leverage `(a < 0) == !(a > 0)`.

Added: 
    

Modified: 
    mlir/lib/Dialect/Arithmetic/IR/ArithmeticOps.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Dialect/Arithmetic/IR/ArithmeticOps.cpp b/mlir/lib/Dialect/Arithmetic/IR/ArithmeticOps.cpp
index aa485d39e0db..2fe32597b03f 100644
--- a/mlir/lib/Dialect/Arithmetic/IR/ArithmeticOps.cpp
+++ b/mlir/lib/Dialect/Arithmetic/IR/ArithmeticOps.cpp
@@ -357,25 +357,30 @@ OpFoldResult arith::CeilDivSIOp::fold(ArrayRef<Attribute> operands) {
           overflowOrDiv0 = true;
           return a;
         }
+        if (!a)
+          return a;
+        // After this point we know that neither a or b are zero.
         unsigned bits = a.getBitWidth();
         APInt zero = APInt::getZero(bits);
-        if (a.sgt(zero) && b.sgt(zero)) {
+        bool aGtZero = a.sgt(zero);
+        bool bGtZero = b.sgt(zero);
+        if (aGtZero && bGtZero) {
           // Both positive, return ceil(a, b).
           return signedCeilNonnegInputs(a, b, overflowOrDiv0);
         }
-        if (a.slt(zero) && b.slt(zero)) {
+        if (!aGtZero && !bGtZero) {
           // Both negative, return ceil(-a, -b).
           APInt posA = zero.ssub_ov(a, overflowOrDiv0);
           APInt posB = zero.ssub_ov(b, overflowOrDiv0);
           return signedCeilNonnegInputs(posA, posB, overflowOrDiv0);
         }
-        if (a.slt(zero) && b.sgt(zero)) {
+        if (!aGtZero && bGtZero) {
           // A is negative, b is positive, return - ( -a / b).
           APInt posA = zero.ssub_ov(a, overflowOrDiv0);
           APInt div = posA.sdiv_ov(b, overflowOrDiv0);
           return zero.ssub_ov(div, overflowOrDiv0);
         }
-        // A is positive (or zero), b is negative, return - (a / -b).
+        // A is positive, b is negative, return - (a / -b).
         APInt posB = zero.ssub_ov(b, overflowOrDiv0);
         APInt div = a.sdiv_ov(posB, overflowOrDiv0);
         return zero.ssub_ov(div, overflowOrDiv0);
@@ -407,19 +412,24 @@ OpFoldResult arith::FloorDivSIOp::fold(ArrayRef<Attribute> operands) {
           overflowOrDiv0 = true;
           return a;
         }
+        if (!a)
+          return a;
+        // After this point we know that neither a or b are zero.
         unsigned bits = a.getBitWidth();
         APInt zero = APInt::getZero(bits);
-        if (a.sge(zero) && b.sgt(zero)) {
-          // Both positive (or a is zero), return a / b.
+        bool aGtZero = a.sgt(zero);
+        bool bGtZero = b.sgt(zero);
+        if (aGtZero && bGtZero) {
+          // Both positive, return a / b.
           return a.sdiv_ov(b, overflowOrDiv0);
         }
-        if (a.sle(zero) && b.slt(zero)) {
-          // Both negative (or a is zero), return -a / -b.
+        if (!aGtZero && !bGtZero) {
+          // Both negative, return -a / -b.
           APInt posA = zero.ssub_ov(a, overflowOrDiv0);
           APInt posB = zero.ssub_ov(b, overflowOrDiv0);
           return posA.sdiv_ov(posB, overflowOrDiv0);
         }
-        if (a.slt(zero) && b.sgt(zero)) {
+        if (!aGtZero && bGtZero) {
           // A is negative, b is positive, return - ceil(-a, b).
           APInt posA = zero.ssub_ov(a, overflowOrDiv0);
           APInt ceil = signedCeilNonnegInputs(posA, b, overflowOrDiv0);


        


More information about the Mlir-commits mailing list