[PATCH] D157529: [NFC][GuardUtils] Add util to extract widenable conditions

Aleksandr Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 9 10:55:53 PDT 2023


aleksandr.popov created this revision.
Herald added a subscriber: hiraditya.
Herald added a project: All.
aleksandr.popov requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

We've added parseWidenableGuard util which parses guard condition and
collects all checks existing in the expression tree: D157276 <https://reviews.llvm.org/D157276>

This patch adds util which walks similar way through the expression tree
but looks up for widenable condition without collecting the checks.

In the next patches llvm::parseWidenableBranch will be replaced by new  
llvm::extractWidenableCondition, which could parse  widenable branches 
with arbitrary position of widenable condition in the expression tree.


https://reviews.llvm.org/D157529

Files:
  llvm/include/llvm/Analysis/GuardUtils.h
  llvm/lib/Analysis/GuardUtils.cpp


Index: llvm/lib/Analysis/GuardUtils.cpp
===================================================================
--- llvm/lib/Analysis/GuardUtils.cpp
+++ llvm/lib/Analysis/GuardUtils.cpp
@@ -130,7 +130,8 @@
       Worklist.push_back(RHS);
       continue;
     }
-    Callback(Check);
+    if (!Callback(Check))
+      break;
   } while (!Worklist.empty());
 }
 
@@ -143,5 +144,26 @@
   parseCondition(Condition, [&](Value *Check) {
     if (!isWidenableCondition(Check))
       Checks.push_back(Check);
+    return true;
+  });
+}
+
+Value *llvm::extractWidenableCondition(const User *U) {
+  auto *BI = dyn_cast<BranchInst>(U);
+  if (!BI || !BI->isConditional())
+    return nullptr;
+
+  auto Condition = BI->getCondition();
+  if (!Condition->hasOneUse())
+    return nullptr;
+
+  Value *WidenableCondition = nullptr;
+  parseCondition(Condition, [&](Value *Check) {
+    if (isWidenableCondition(Check) && Check->hasOneUse()) {
+      WidenableCondition = Check;
+      return false;
+    }
+    return true;
   });
+  return WidenableCondition;
 }
Index: llvm/include/llvm/Analysis/GuardUtils.h
===================================================================
--- llvm/include/llvm/Analysis/GuardUtils.h
+++ llvm/include/llvm/Analysis/GuardUtils.h
@@ -60,6 +60,10 @@
 //   cond1 && cond2 && cond3 && widenable_contidion ...
 // Method collects the list of checks, but skips widenable_condition.
 void parseWidenableGuard(const User *U, llvm::SmallVectorImpl<Value *> &Checks);
+
+// Returns widenable_condition if it exists in the expression tree rooting from
+// \p U and has only one use.
+Value *extractWidenableCondition(const User *U);
 } // llvm
 
 #endif // LLVM_ANALYSIS_GUARDUTILS_H


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D157529.548681.patch
Type: text/x-patch
Size: 1698 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230809/94185db6/attachment.bin>


More information about the llvm-commits mailing list