[llvm] d3366ef - [LV] Simplify register usage code and avoid double map lookups. NFC

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 14 11:57:49 PDT 2022


Author: Craig Topper
Date: 2022-10-14T11:55:48-07:00
New Revision: d3366efd436dfc121d9d2f021c51832a800a8cd9

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

LOG: [LV] Simplify register usage code and avoid double map lookups. NFC

Instead of checking whether a map entry exists to decide if we should
initialize it or add to it, we can rely on the map entry being constructed
and initialized to 0 before the addition happens.

For the std::max case, I've made a reference to the map entry to
avoid looking it up twice.

Reviewed By: reames

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

Added: 
    

Modified: 
    llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index e5273662e70ec..bdeae6fdc33b1 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -6026,10 +6026,9 @@ LoopVectorizationCostModel::calculateRegisterUsage(ArrayRef<ElementCount> VFs) {
       if (VFs[j].isScalar()) {
         for (auto *Inst : OpenIntervals) {
           unsigned ClassID = TTI.getRegisterClassForType(false, Inst->getType());
-          if (RegUsage.find(ClassID) == RegUsage.end())
-            RegUsage[ClassID] = 1;
-          else
-            RegUsage[ClassID] += 1;
+          // If RegUsage[ClassID] doesn't exist, it will be default
+          // constructed as 0 before the addition
+          RegUsage[ClassID] += 1;
         }
       } else {
         collectUniformsAndScalars(VFs[j]);
@@ -6039,25 +6038,21 @@ LoopVectorizationCostModel::calculateRegisterUsage(ArrayRef<ElementCount> VFs) {
             continue;
           if (isScalarAfterVectorization(Inst, VFs[j])) {
             unsigned ClassID = TTI.getRegisterClassForType(false, Inst->getType());
-            if (RegUsage.find(ClassID) == RegUsage.end())
-              RegUsage[ClassID] = 1;
-            else
-              RegUsage[ClassID] += 1;
+            // If RegUsage[ClassID] doesn't exist, it will be default
+            // constructed as 0 before the addition
+            RegUsage[ClassID] += 1;
           } else {
             unsigned ClassID = TTI.getRegisterClassForType(true, Inst->getType());
-            if (RegUsage.find(ClassID) == RegUsage.end())
-              RegUsage[ClassID] = GetRegUsage(Inst->getType(), VFs[j]);
-            else
-              RegUsage[ClassID] += GetRegUsage(Inst->getType(), VFs[j]);
+            // If RegUsage[ClassID] doesn't exist, it will be default
+            // constructed as 0 before the addition
+            RegUsage[ClassID] += GetRegUsage(Inst->getType(), VFs[j]);
           }
         }
       }
 
       for (auto& pair : RegUsage) {
-        if (MaxUsages[j].find(pair.first) != MaxUsages[j].end())
-          MaxUsages[j][pair.first] = std::max(MaxUsages[j][pair.first], pair.second);
-        else
-          MaxUsages[j][pair.first] = pair.second;
+        auto &Entry = MaxUsages[j][pair.first];
+        Entry = std::max(Entry, pair.second);
       }
     }
 


        


More information about the llvm-commits mailing list