[llvm] [InstCombine] Generalize `icmp (shl nuw C2, Y), C -> icmp Y, C3` (PR #104696)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 18 03:22:07 PDT 2024


================
@@ -2227,18 +2227,24 @@ Instruction *InstCombinerImpl::foldICmpMulConstant(ICmpInst &Cmp,
   return NewC ? new ICmpInst(Pred, X, NewC) : nullptr;
 }
 
-/// Fold icmp (shl 1, Y), C.
-static Instruction *foldICmpShlOne(ICmpInst &Cmp, Instruction *Shl,
-                                   const APInt &C) {
+/// Fold icmp (shl nuw C2, Y), C.
+static Instruction *foldICmpShlLHSC(ICmpInst &Cmp, Instruction *Shl,
+                                    const APInt &C) {
   Value *Y;
-  if (!match(Shl, m_Shl(m_One(), m_Value(Y))))
+  const APInt *C2;
+  if (!match(Shl, m_NUWShl(m_APInt(C2), m_Value(Y))))
     return nullptr;
 
   Type *ShiftType = Shl->getType();
   unsigned TypeBits = C.getBitWidth();
-  bool CIsPowerOf2 = C.isPowerOf2();
   ICmpInst::Predicate Pred = Cmp.getPredicate();
   if (Cmp.isUnsigned()) {
+    APInt Div, Rem;
+    APInt::udivrem(C, *C2, Div, Rem);
+    if (!Rem.isZero())
+      return nullptr;
----------------
dtcxzyw wrote:

All non-rem-0 cases:
```
Input files: 37900
Progress: 3183icmp ugt (shl nuw 12, X), 72057594037927935
icmp ugt (shl nuw 12, X), 72057594037927935
Progress: 3189icmp ugt (shl nuw 12, X), 72057594037927935
Progress: 3279icmp ugt (shl nuw 12, X), 72057594037927935
Progress: 3308icmp ugt (shl nuw 12, X), 72057594037927935
icmp ugt (shl nuw 12, X), 72057594037927935
Progress: 6875icmp ugt (shl nuw 4, X), 31
Progress: 16038icmp ugt (shl nuw 2, X), 134217727
icmp ugt (shl nuw 2, X), 134217727
icmp ugt (shl nuw 2, X), 134217727
icmp ugt (shl nuw 2, X), 134217727
icmp ugt (shl nuw 2, X), 134217727
icmp ugt (shl nuw 2, X), 134217727
icmp ugt (shl nuw 2, X), 134217727
icmp ugt (shl nuw 2, X), 134217727
Progress: 18015icmp ugt (shl nuw 16, X), 63
Progress: 24392icmp ugt (shl nuw 2, X), 3
Progress: 37893
8
c3c/optimized/target.c.ll
folly/optimized/DynamicParser.cpp.ll
folly/optimized/LogConfigParser.cpp.ll
folly/optimized/dynamic.cpp.ll
folly/optimized/json.cpp.ll
linux/optimized/xhci-pci.ll
regex-rs/optimized/11vfjke4utuj478u.ll
wasmtime-rs/optimized/2ly4gzztxx8hlwxv.ll
```


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


More information about the llvm-commits mailing list