[llvm] [UndefOrPoison] [CompileTime] Avoid IDom walk unless required. NFC (PR #90092)

via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 25 10:21:26 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-analysis

Author: None (annamthomas)

<details>
<summary>Changes</summary>

When we are only checking for UndefKind and if the value we're checking for is not a boolean, we can avoid the potentially expensive IDom walk.

isGuaranteedNotToBeUndef() is used in CVP and LVI.

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


1 Files Affected:

- (modified) llvm/lib/Analysis/ValueTracking.cpp (+25-21) 


``````````diff
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index de38eddaa98fef..95968368a7d719 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -7283,31 +7283,35 @@ static bool isGuaranteedNotToBeUndefOrPoison(
   // BB1:
   //   CtxI ; V cannot be undef or poison here
   auto *Dominator = DNode->getIDom();
-  while (Dominator) {
-    auto *TI = Dominator->getBlock()->getTerminator();
-
-    Value *Cond = nullptr;
-    if (auto BI = dyn_cast_or_null<BranchInst>(TI)) {
-      if (BI->isConditional())
-        Cond = BI->getCondition();
-    } else if (auto SI = dyn_cast_or_null<SwitchInst>(TI)) {
-      Cond = SI->getCondition();
-    }
+  // This check is purely for compile time reasons: we do not need to do the
+  // IDom walk if we are checking for only undef and the value is not a boolean.
+  if (includesPoison(Kind) || V->getType()->isIntOrIntVectorTy(1))
+    while (Dominator) {
+      auto *TI = Dominator->getBlock()->getTerminator();
+
+      Value *Cond = nullptr;
+      if (auto BI = dyn_cast_or_null<BranchInst>(TI)) {
+        if (BI->isConditional())
+          Cond = BI->getCondition();
+      } else if (auto SI = dyn_cast_or_null<SwitchInst>(TI)) {
+        Cond = SI->getCondition();
+      }
 
-    if (Cond) {
-      if (Cond == V)
-        return true;
-      else if (!includesUndef(Kind) && isa<Operator>(Cond)) {
-        // For poison, we can analyze further
-        auto *Opr = cast<Operator>(Cond);
-        if (any_of(Opr->operands(),
-                   [V](const Use &U) { return V == U && propagatesPoison(U); }))
+      if (Cond) {
+        if (Cond == V)
           return true;
+        else if (!includesUndef(Kind) && isa<Operator>(Cond)) {
+          // For poison, we can analyze further
+          auto *Opr = cast<Operator>(Cond);
+          if (any_of(Opr->operands(), [V](const Use &U) {
+                return V == U && propagatesPoison(U);
+              }))
+            return true;
+        }
       }
-    }
 
-    Dominator = Dominator->getIDom();
-  }
+      Dominator = Dominator->getIDom();
+    }
 
   if (getKnowledgeValidInContext(V, {Attribute::NoUndef}, CtxI, DT, AC))
     return true;

``````````

</details>


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


More information about the llvm-commits mailing list