[PATCH] D159060: [InstCombine] Make `getKnownSign` a member function of InstCombiner; NFC

Noah Goldstein via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 28 21:30:03 PDT 2023


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

This is prep for using `getKnownSign` outside of just InstCombineCalls.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D159060

Files:
  llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
  llvm/lib/Transforms/InstCombine/InstCombineInternal.h


Index: llvm/lib/Transforms/InstCombine/InstCombineInternal.h
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineInternal.h
+++ llvm/lib/Transforms/InstCombine/InstCombineInternal.h
@@ -430,6 +430,13 @@
       Instruction::BinaryOps BinaryOp, bool IsSigned,
       Value *LHS, Value *RHS, Instruction *CxtI) const;
 
+  // Return true if known negative, false if known positive, and nullopt if
+  // unknown.
+  std::optional<bool> getKnownSign(Value *Op, Instruction *CxtI) const;
+  // Return true if known negative or zero, false if known non-zero positive,
+  // and nullopt if unknown.
+  std::optional<bool> getKnownSignOrZero(Value *Op, Instruction *CxtI) const;
+
   /// Performs a few simplifications for operators which are associative
   /// or commutative.
   bool SimplifyAssociativeOrCommutative(BinaryOperator &I);
Index: llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -1001,10 +1001,9 @@
   return nullptr;
 }
 
-static std::optional<bool> getKnownSign(Value *Op, Instruction *CxtI,
-                                   const DataLayout &DL, AssumptionCache *AC,
-                                   DominatorTree *DT) {
-  KnownBits Known = computeKnownBits(Op, DL, 0, AC, CxtI, DT);
+std::optional<bool> InstCombinerImpl::getKnownSign(Value *Op,
+                                                   Instruction *CxtI) const {
+  KnownBits Known = llvm::computeKnownBits(Op, DL, /*Depth*/ 0, &AC, CxtI, &DT);
   if (Known.isNonNegative())
     return false;
   if (Known.isNegative())
@@ -1018,11 +1017,9 @@
       ICmpInst::ICMP_SLT, Op, Constant::getNullValue(Op->getType()), CxtI, DL);
 }
 
-static std::optional<bool> getKnownSignOrZero(Value *Op, Instruction *CxtI,
-                                              const DataLayout &DL,
-                                              AssumptionCache *AC,
-                                              DominatorTree *DT) {
-  if (std::optional<bool> Sign = getKnownSign(Op, CxtI, DL, AC, DT))
+std::optional<bool>
+InstCombinerImpl::getKnownSignOrZero(Value *Op, Instruction *CxtI) const {
+  if (std::optional<bool> Sign = getKnownSign(Op, CxtI))
     return Sign;
 
   Value *X, *Y;
@@ -1034,12 +1031,11 @@
 
 /// Return true if two values \p Op0 and \p Op1 are known to have the same sign.
 static bool signBitMustBeTheSame(Value *Op0, Value *Op1, Instruction *CxtI,
-                                 const DataLayout &DL, AssumptionCache *AC,
-                                 DominatorTree *DT) {
-  std::optional<bool> Known1 = getKnownSign(Op1, CxtI, DL, AC, DT);
+                                 InstCombinerImpl &IC) {
+  std::optional<bool> Known1 = IC.getKnownSign(Op1, CxtI);
   if (!Known1)
     return false;
-  std::optional<bool> Known0 = getKnownSign(Op0, CxtI, DL, AC, DT);
+  std::optional<bool> Known0 = IC.getKnownSign(Op0, CxtI);
   if (!Known0)
     return false;
   return *Known0 == *Known1;
@@ -1532,8 +1528,7 @@
     if (match(IIOperand, m_Select(m_Value(), m_Neg(m_Value(X)), m_Deferred(X))))
       return replaceOperand(*II, 0, X);
 
-    if (std::optional<bool> Known =
-            getKnownSignOrZero(IIOperand, II, DL, &AC, &DT)) {
+    if (std::optional<bool> Known = getKnownSignOrZero(IIOperand, II)) {
       // abs(x) -> x if x >= 0 (include abs(x-y) --> x - y where x >= y)
       // abs(x) -> x if x > 0 (include abs(x-y) --> x - y where x > y)
       if (!*Known)
@@ -1646,7 +1641,7 @@
       bool UseAndN = IID == Intrinsic::smin || IID == Intrinsic::umin;
 
       if (IID == Intrinsic::smax || IID == Intrinsic::smin) {
-        auto KnownSign = getKnownSign(X, II, DL, &AC, &DT);
+        auto KnownSign = getKnownSign(X, II);
         if (KnownSign == std::nullopt) {
           UseOr = false;
           UseAndN = false;
@@ -2419,7 +2414,7 @@
       FastMathFlags InnerFlags = cast<FPMathOperator>(Src)->getFastMathFlags();
 
       if ((FMF.allowReassoc() && InnerFlags.allowReassoc()) ||
-          signBitMustBeTheSame(Exp, InnerExp, II, DL, &AC, &DT)) {
+          signBitMustBeTheSame(Exp, InnerExp, II, *this)) {
         // TODO: Add nsw/nuw probably safe if integer type exceeds exponent
         // width.
         Value *NewExp = Builder.CreateAdd(InnerExp, Exp);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D159060.554167.patch
Type: text/x-patch
Size: 4443 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230829/6b521e68/attachment.bin>


More information about the llvm-commits mailing list