[llvm] [LoopFusion] Simplify the logic of checking trip count equality (NFCI). (PR #201446)

Ehsan Amiri via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 5 08:47:12 PDT 2026


================
@@ -755,34 +742,20 @@ struct LoopFuser {
         FC0.verify();
         FC1.verify();
 
-        // Check if the candidates have identical tripcounts (first value of
-        // pair), and if not check the difference in the tripcounts between
-        // the loops (second value of pair). The difference is not equal to
-        // std::nullopt iff the loops iterate a constant number of times, and
-        // have a single exit.
-        std::pair<bool, std::optional<unsigned>> IdenticalTripCountRes =
-            haveIdenticalTripCounts(FC0, FC1);
-        bool SameTripCount = IdenticalTripCountRes.first;
-        std::optional<unsigned> TCDifference = IdenticalTripCountRes.second;
-
+        std::optional<int64_t> TCDifference = calculateTripCountDiff(FC0, FC1);
         // Here we are checking that FC0 (the first loop) can be peeled, and
-        // both loops have different tripcounts.
-        if (FC0.AbleToPeel && !SameTripCount && TCDifference) {
-          if (*TCDifference > FusionPeelMaxCount) {
-            LLVM_DEBUG(dbgs()
-                       << "Difference in loop trip counts: " << *TCDifference
-                       << " is greater than maximum peel count specificed: "
-                       << FusionPeelMaxCount << "\n");
-          } else {
-            // Dependent on peeling being performed on the first loop, and
-            // assuming all other conditions for fusion return true.
-            SameTripCount = true;
-          }
-        }
-
-        if (!SameTripCount) {
+        // the first loop has a larger trip count. In this case it is possible
+        // that the first loop is peeled to expose the fusion opportunity.
+        // Peeling the second loop is not currently supported.
+        bool WillPeel = false;
+        if (FC0.AbleToPeel && TCDifference && *TCDifference > 0 &&
+            *TCDifference <= static_cast<int64_t>(FusionPeelMaxCount))
+          WillPeel = true;
+
+        if (!WillPeel && (!TCDifference || *TCDifference != 0)) {
----------------
amehsan wrote:

You are right, but I was thinking to use on of the following two approaches consistently to make the code easier to read, and I ended up using (1) everywhere. I think it makes it easier for people to read the code.

1. always check the value exists and then read the value and compare
2.  Use overloaded comparison operators.



https://github.com/llvm/llvm-project/pull/201446


More information about the llvm-commits mailing list