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

via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 26 04:30:28 PDT 2024


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

>From f0adf7fd4d76a0ae0e4d08eead6e3c40ae37fc3a Mon Sep 17 00:00:00 2001
From: Anna Thomas <anna at azul.com>
Date: Thu, 25 Apr 2024 13:11:41 -0400
Subject: [PATCH] [UndefOrPoison] [CompileTime] Avoid IDom walk unless
 required. NFC

If the value is not boolean and we are checking for `Undef` or
`UndefOrPoison`, we can avoid the potentially expensive IDom walk.

This should improve compile time for isGuaranteedNotToBeUndefOrPoison
and isGuaranteedNotToBeUndef.
---
 llvm/lib/Analysis/ValueTracking.cpp | 46 ++++++++++++++++-------------
 1 file changed, 25 insertions(+), 21 deletions(-)

diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index de38eddaa98fef..b1aa940b844b46 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 can skip the IDom walk
+  // if what we are checking for includes undef and the value is not a boolean.
+  if (!includesUndef(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;



More information about the llvm-commits mailing list