[PATCH] D156631: [InstSimplify] Hoist some basic simplifications from `simplifyAndInst` to helper; NFC

Noah Goldstein via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 30 16:00:28 PDT 2023


goldstein.w.n created this revision.
goldstein.w.n added reviewers: nikic, RKSimon.
Herald added subscribers: StephenFan, hiraditya.
Herald added a project: All.
goldstein.w.n requested review of this revision.
Herald added subscribers: llvm-commits, wangpc.
Herald added a project: LLVM.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D156631

Files:
  llvm/lib/Analysis/InstructionSimplify.cpp


Index: llvm/lib/Analysis/InstructionSimplify.cpp
===================================================================
--- llvm/lib/Analysis/InstructionSimplify.cpp
+++ llvm/lib/Analysis/InstructionSimplify.cpp
@@ -2048,6 +2048,23 @@
   return nullptr;
 }
 
+static Value *
+trySimplifyingAndLikeUsingOp(Value *Op, const SimplifyQuery &Q, Type *ResTy) {
+  // X & poison -> poison
+  if (isa<PoisonValue>(Op))
+    return PoisonValue::get(ResTy);
+
+  // X & undef -> 0
+  if (Q.isUndefValue(Op))
+    return Constant::getNullValue(ResTy);
+
+  // X & 0 = 0
+  if (match(Op, m_Zero()))
+    return Constant::getNullValue(ResTy);
+
+  return nullptr;
+}
+
 /// Given operands for an And, see if we can fold the result.
 /// If not, this returns null.
 static Value *simplifyAndInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
@@ -2055,22 +2072,13 @@
   if (Constant *C = foldOrCommuteConstant(Instruction::And, Op0, Op1, Q))
     return C;
 
-  // X & poison -> poison
-  if (isa<PoisonValue>(Op1))
-    return Op1;
-
-  // X & undef -> 0
-  if (Q.isUndefValue(Op1))
-    return Constant::getNullValue(Op0->getType());
+  if (auto R = trySimplifyingAndLikeUsingOp(Op1, Q, Op0->getType()))
+    return R;
 
   // X & X = X
   if (Op0 == Op1)
     return Op0;
 
-  // X & 0 = 0
-  if (match(Op1, m_Zero()))
-    return Constant::getNullValue(Op0->getType());
-
   // X & -1 = X
   if (match(Op1, m_AllOnes()))
     return Op0;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156631.545475.patch
Type: text/x-patch
Size: 1426 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230730/1d39c806/attachment.bin>


More information about the llvm-commits mailing list