Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[UndefOrPoison] [CompileTime] Avoid IDom walk unless required. NFC #90092

Merged
merged 1 commit into from
May 1, 2024

Conversation

annamthomas
Copy link
Contributor

@annamthomas annamthomas commented Apr 25, 2024

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.

@annamthomas annamthomas self-assigned this Apr 25, 2024
@annamthomas annamthomas requested a review from nikic as a code owner April 25, 2024 17:20
@llvmbot
Copy link
Collaborator

llvmbot commented Apr 25, 2024

@llvm/pr-subscribers-llvm-analysis

Author: None (annamthomas)

Changes

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 --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;

If the value is not integer 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 with non-integer values.
@annamthomas
Copy link
Contributor Author

ping @nikic?

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@annamthomas annamthomas merged commit 78270cb into llvm:main May 1, 2024
3 of 4 checks passed
Michael137 pushed a commit to Michael137/llvm-project that referenced this pull request May 3, 2024
…lvm#90092)

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants