[PATCH] D141498: [LVI] Handle `x & 0xfff0 == 0x1230` comparison

Antonio Frighetto via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 11 07:14:57 PST 2023


antoniofrighetto created this revision.
antoniofrighetto added reviewers: nikic, reames.
Herald added subscribers: StephenFan, hiraditya.
Herald added a project: All.
antoniofrighetto requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

LazyValueInfo now handles the comparison of a value when masked in a certain way with a constant. For example, for a 16-bit integer, we might have `x & 0xfff0 == 0x1230`. This is a typical situation produced by InstCombine to efficiently check if x is between [0x1230, 0x1240).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D141498

Files:
  llvm/lib/Analysis/LazyValueInfo.cpp


Index: llvm/lib/Analysis/LazyValueInfo.cpp
===================================================================
--- llvm/lib/Analysis/LazyValueInfo.cpp
+++ llvm/lib/Analysis/LazyValueInfo.cpp
@@ -1085,13 +1085,38 @@
   CmpInst::Predicate EdgePred =
       isTrueDest ? ICI->getPredicate() : ICI->getInversePredicate();
 
-  if (isa<Constant>(RHS)) {
-    if (ICI->isEquality() && LHS == Val) {
+  if (isa<Constant>(RHS) && ICI->isEquality()) {
+    if (LHS == Val) {
+      // We know that V has the RHS constant if the edge predicate is equality.
       if (EdgePred == ICmpInst::ICMP_EQ)
         return ValueLatticeElement::get(cast<Constant>(RHS));
       else if (!isa<UndefValue>(RHS))
         return ValueLatticeElement::getNot(cast<Constant>(RHS));
     }
+
+    if (ConstantInt *CIRHS = dyn_cast<ConstantInt>(RHS)) {
+      ConstantInt *Mask;
+      if (match(LHS, m_And(m_Specific(Val), m_ConstantInt(Mask)))) {
+        const APInt &FixedBits = CIRHS->getValue();
+        APInt MaskValue = Mask->getValue();
+        MaskValue.flipAllBits();
+
+        if (MaskValue.isMask()) {
+          using VLE = ValueLatticeElement;
+          if ((MaskValue & FixedBits) == 0) {
+            if (EdgePred == ICmpInst::ICMP_EQ)
+              return VLE::getRange({FixedBits, FixedBits + MaskValue + 1});
+            else
+              return VLE::getRange({FixedBits + MaskValue + 1, FixedBits});
+          } else {
+            if (EdgePred == ICmpInst::ICMP_EQ)
+              return VLE();
+            else
+              return VLE::getOverdefined();
+          }
+        }
+      }
+    }
   }
 
   Type *Ty = Val->getType();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D141498.488201.patch
Type: text/x-patch
Size: 1639 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230111/ab725b61/attachment.bin>


More information about the llvm-commits mailing list