[llvm] [CodeGen] Avoid repeated hash lookups (NFC) (PR #113414)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 22 20:52:04 PDT 2024


https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/113414

None

>From 1566d47dd9b23cc34bfbc6c6d6cacb954daefd5c Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Tue, 22 Oct 2024 20:44:58 -0700
Subject: [PATCH] [CodeGen] Avoid repeated hash lookups (NFC)

---
 llvm/lib/CodeGen/SelectOptimize.cpp | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)

diff --git a/llvm/lib/CodeGen/SelectOptimize.cpp b/llvm/lib/CodeGen/SelectOptimize.cpp
index 61341e1f2d04ce..fe4feae46ad8d5 100644
--- a/llvm/lib/CodeGen/SelectOptimize.cpp
+++ b/llvm/lib/CodeGen/SelectOptimize.cpp
@@ -244,22 +244,25 @@ class SelectOptimizeImpl {
     Scaled64 getTrueOpCost(DenseMap<const Instruction *, CostInfo> &InstCostMap,
                            const TargetTransformInfo *TTI) {
       if (isa<SelectInst>(I))
-        if (auto *I = dyn_cast<Instruction>(getTrueValue()))
-          return InstCostMap.contains(I) ? InstCostMap[I].NonPredCost
+        if (auto *I = dyn_cast<Instruction>(getTrueValue())) {
+          auto It = InstCostMap.find(I);
+          return It != InstCostMap.end() ? It->second.NonPredCost
                                          : Scaled64::getZero();
+        }
 
       // Or case - add the cost of an extra Or to the cost of the False case.
       if (isa<BinaryOperator>(I))
-        if (auto I = dyn_cast<Instruction>(getFalseValue()))
-          if (InstCostMap.contains(I)) {
+        if (auto I = dyn_cast<Instruction>(getFalseValue())) {
+          auto It = InstCostMap.find(I);
+          if (It != InstCostMap.end()) {
             InstructionCost OrCost = TTI->getArithmeticInstrCost(
                 Instruction::Or, I->getType(), TargetTransformInfo::TCK_Latency,
                 {TargetTransformInfo::OK_AnyValue,
                  TargetTransformInfo::OP_None},
                 {TTI::OK_UniformConstantValue, TTI::OP_PowerOf2});
-            return InstCostMap[I].NonPredCost +
-                   Scaled64::get(*OrCost.getValue());
+            return It->second.NonPredCost + Scaled64::get(*OrCost.getValue());
           }
+        }
 
       return Scaled64::getZero();
     }
@@ -270,15 +273,17 @@ class SelectOptimizeImpl {
     getFalseOpCost(DenseMap<const Instruction *, CostInfo> &InstCostMap,
                    const TargetTransformInfo *TTI) {
       if (isa<SelectInst>(I))
-        if (auto *I = dyn_cast<Instruction>(getFalseValue()))
-          return InstCostMap.contains(I) ? InstCostMap[I].NonPredCost
+        if (auto *I = dyn_cast<Instruction>(getFalseValue())) {
+          auto It = InstCostMap.find(I);
+          return It != InstCostMap.end() ? It->second.NonPredCost
                                          : Scaled64::getZero();
+        }
 
       // Or case - return the cost of the false case
       if (isa<BinaryOperator>(I))
         if (auto I = dyn_cast<Instruction>(getFalseValue()))
-          if (InstCostMap.contains(I))
-            return InstCostMap[I].NonPredCost;
+          if (auto It = InstCostMap.find(I); It != InstCostMap.end())
+            return It->second.NonPredCost;
 
       return Scaled64::getZero();
     }



More information about the llvm-commits mailing list