[Mlir-commits] [mlir] 267f21a - Use std::gcd (NFC)

Kazu Hirata llvmlistbot at llvm.org
Sun Aug 28 10:42:08 PDT 2022


Author: Kazu Hirata
Date: 2022-08-28T10:41:51-07:00
New Revision: 267f21a21bee4029d4f5bda8b192abdcc8c3db36

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

LOG: Use std::gcd (NFC)

This patch replaces calls to greatestCommonDivisor with std::gcd where
two arguments are of the same type.  This means that
std::common_type_t of the argument type is the same as the argument
type.

We could drop calls to std::abs in some cases, but that's left for
another patch.

Added: 
    

Modified: 
    llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
    mlir/include/mlir/Analysis/Presburger/MPInt.h
    mlir/lib/Analysis/Presburger/Utils.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp b/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
index 424041cef7290..a7d74bf07adc4 100644
--- a/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
@@ -33,6 +33,7 @@
 #include "llvm/Support/MathExtras.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/Target/TargetMachine.h"
+#include <numeric>
 
 #define DEBUG_TYPE "legalizer"
 
@@ -1568,7 +1569,7 @@ LegalizerHelper::widenScalarMergeValues(MachineInstr &MI, unsigned TypeIdx,
   // %9:_(s6) = G_MERGE_VALUES %6, %7, %7
   // %10:_(s12) = G_MERGE_VALUES %8, %9
 
-  const int GCD = greatestCommonDivisor(SrcSize, WideSize);
+  const int GCD = std::gcd(SrcSize, WideSize);
   LLT GCDTy = LLT::scalar(GCD);
 
   SmallVector<Register, 8> Parts;

diff  --git a/mlir/include/mlir/Analysis/Presburger/MPInt.h b/mlir/include/mlir/Analysis/Presburger/MPInt.h
index ab21d32fe9018..12ab0598d10d9 100644
--- a/mlir/include/mlir/Analysis/Presburger/MPInt.h
+++ b/mlir/include/mlir/Analysis/Presburger/MPInt.h
@@ -19,6 +19,7 @@
 #include "mlir/Analysis/Presburger/SlowMPInt.h"
 #include "mlir/Support/MathExtras.h"
 #include "llvm/Support/raw_ostream.h"
+#include <numeric>
 
 namespace mlir {
 namespace presburger {
@@ -398,7 +399,7 @@ LLVM_ATTRIBUTE_ALWAYS_INLINE MPInt mod(const MPInt &lhs, const MPInt &rhs) {
 LLVM_ATTRIBUTE_ALWAYS_INLINE MPInt gcd(const MPInt &a, const MPInt &b) {
   assert(a >= 0 && b >= 0 && "operands must be non-negative!");
   if (LLVM_LIKELY(a.isSmall() && b.isSmall()))
-    return MPInt(llvm::greatestCommonDivisor(a.getSmall(), b.getSmall()));
+    return MPInt(std::gcd(a.getSmall(), b.getSmall()));
   return MPInt(gcd(detail::SlowMPInt(a), detail::SlowMPInt(b)));
 }
 

diff  --git a/mlir/lib/Analysis/Presburger/Utils.cpp b/mlir/lib/Analysis/Presburger/Utils.cpp
index 54dcfad166032..ccb9ef71fa3dd 100644
--- a/mlir/lib/Analysis/Presburger/Utils.cpp
+++ b/mlir/lib/Analysis/Presburger/Utils.cpp
@@ -14,6 +14,7 @@
 #include "mlir/Analysis/Presburger/IntegerRelation.h"
 #include "mlir/Support/LogicalResult.h"
 #include "mlir/Support/MathExtras.h"
+#include <numeric>
 
 using namespace mlir;
 using namespace presburger;
@@ -27,8 +28,7 @@ static void normalizeDivisionByGCD(MutableArrayRef<int64_t> dividend,
     return;
   // We take the absolute value of dividend's coefficients to make sure that
   // `gcd` is positive.
-  int64_t gcd =
-      llvm::greatestCommonDivisor(std::abs(dividend.front()), int64_t(divisor));
+  int64_t gcd = std::gcd(std::abs(dividend.front()), int64_t(divisor));
 
   // The reason for ignoring the constant term is as follows.
   // For a division:
@@ -38,7 +38,7 @@ static void normalizeDivisionByGCD(MutableArrayRef<int64_t> dividend,
   // Since `{a/m}/d` in the dividend satisfies 0 <= {a/m}/d < 1/d, it will not
   // influence the result of the floor division and thus, can be ignored.
   for (size_t i = 1, m = dividend.size() - 1; i < m; i++) {
-    gcd = llvm::greatestCommonDivisor(std::abs(dividend[i]), gcd);
+    gcd = std::gcd(std::abs(dividend[i]), gcd);
     if (gcd == 1)
       return;
   }
@@ -334,7 +334,7 @@ int64_t presburger::normalizeRange(MutableArrayRef<int64_t> range) {
 
 void presburger::normalizeDiv(MutableArrayRef<int64_t> num, int64_t &denom) {
   assert(denom > 0 && "denom must be positive!");
-  int64_t gcd = llvm::greatestCommonDivisor(gcdRange(num), denom);
+  int64_t gcd = std::gcd(gcdRange(num), denom);
   for (int64_t &coeff : num)
     coeff /= gcd;
   denom /= gcd;


        


More information about the Mlir-commits mailing list