[llvm] [LoopPeel] Peel iterations based on and, or conditions (PR #73413)

Joshua Cao via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 30 23:39:54 PST 2023


================
@@ -351,11 +351,19 @@ static unsigned countToEliminateCompares(Loop &L, unsigned MaxPeelCount,
     MaxPeelCount =
         std::min((unsigned)SC->getAPInt().getLimitedValue() - 1, MaxPeelCount);
 
-  auto ComputePeelCount = [&](Value *Condition) -> void {
+  std::function<void(Value *)> ComputePeelCount =
+      [&](Value *Condition) -> void {
     if (!Condition->getType()->isIntegerTy())
       return;
 
     Value *LeftVal, *RightVal;
+    if (match(Condition, m_And(m_Value(LeftVal), m_Value(RightVal))) ||
+        match(Condition, m_Or(m_Value(LeftVal), m_Value(RightVal)))) {
+      ComputePeelCount(LeftVal);
+      ComputePeelCount(RightVal);
----------------
caojoshua wrote:

What do you mean by safer? Are you concerned about infinite recursion?

I think there should never be infinite recursion if the IR is valid. If we want to be extra safe, we can maintain a `visited` set and bail if needed. I think this would be required weather we use recursion or a worklist.

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


More information about the llvm-commits mailing list