[MLIR] Simplex::normalizeRow: early exit when gcd is one
authorArjun P <arjunpitchanathan@gmail.com>
Wed, 5 Jan 2022 17:51:45 +0000 (23:21 +0530)
committerArjun P <arjunpitchanathan@gmail.com>
Wed, 5 Jan 2022 17:56:29 +0000 (23:26 +0530)
Reviewed By: bondhugula

Differential Revision: https://reviews.llvm.org/D116672

mlir/lib/Analysis/Presburger/Simplex.cpp

index 1e881aa..3d63ebd 100644 (file)
@@ -124,10 +124,14 @@ unsigned SimplexBase::addRow(ArrayRef<int64_t> coeffs) {
 void SimplexBase::normalizeRow(unsigned row) {
   int64_t gcd = 0;
   for (unsigned col = 0; col < nCol; ++col) {
-    if (gcd == 1)
-      break;
     gcd = llvm::greatestCommonDivisor(gcd, std::abs(tableau(row, col)));
+    // If the gcd becomes 1 then the row is already normalized.
+    if (gcd == 1)
+      return;
   }
+
+  // Note that the gcd can never become zero since the first element of the row,
+  // the denominator, is non-zero.
   for (unsigned col = 0; col < nCol; ++col)
     tableau(row, col) /= gcd;
 }