[llvm] [Analysis][NFC] Clean-up in ScalarEvolution when copying predicates (PR #108851)

via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 16 09:12:44 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-analysis

Author: David Sherwood (david-arm)

<details>
<summary>Changes</summary>

There are a few places in ScalarEvolution.cpp where we copy predicates
from one list to another and they have a similar pattern:

```
  for (const auto *P : ENT.Predicates)
    Predicates->push_back(P);
```

We can avoid the loop by writing them like this:

```
  Predicates->append(ENT.Predicates.begin(), ENT.Predicates.end());
```

which may end up being more efficient since we only have to try
reserving more space once.

---
Full diff: https://github.com/llvm/llvm-project/pull/108851.diff


1 Files Affected:

- (modified) llvm/lib/Analysis/ScalarEvolution.cpp (+4-8) 


``````````diff
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 57e03f667ba6ff..98c4956159449b 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -8594,8 +8594,7 @@ const SCEV *ScalarEvolution::BackedgeTakenInfo::getExact(
     Ops.push_back(BECount);
 
     if (Preds)
-      for (const auto *P : ENT.Predicates)
-        Preds->push_back(P);
+      Preds->append(ENT.Predicates.begin(), ENT.Predicates.end());
 
     assert((Preds || ENT.hasAlwaysTruePredicate()) &&
            "Predicate should be always true!");
@@ -8616,8 +8615,7 @@ ScalarEvolution::BackedgeTakenInfo::getExitNotTaken(
       if (ENT.hasAlwaysTruePredicate())
         return &ENT;
       else if (Predicates) {
-        for (const auto *P : ENT.Predicates)
-          Predicates->push_back(P);
+        Predicates->append(ENT.Predicates.begin(), ENT.Predicates.end());
         return &ENT;
       }
     }
@@ -8659,8 +8657,7 @@ const SCEV *ScalarEvolution::BackedgeTakenInfo::getSymbolicMax(
                "dominate latch!");
         ExitCounts.push_back(ExitCount);
         if (Predicates)
-          for (const auto *P : ENT.Predicates)
-            Predicates->push_back(P);
+          Predicates->append(ENT.Predicates.begin(), ENT.Predicates.end());
 
         assert((Predicates || ENT.hasAlwaysTruePredicate()) &&
                "Predicate should be always true!");
@@ -14804,8 +14801,7 @@ const SCEVAddRecExpr *ScalarEvolution::convertSCEVToAddRecWithPredicates(
 
   // Since the transformation was successful, we can now transfer the SCEV
   // predicates.
-  for (const auto *P : TransformPreds)
-    Preds.insert(P);
+  Preds.insert(TransformPreds.begin(), TransformPreds.end());
 
   return AddRec;
 }

``````````

</details>


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


More information about the llvm-commits mailing list