[llvm] 8996742 - [KnownBits] Add KnownBits::makeConstant helper. NFCI.

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 12 08:19:27 PST 2020


Author: Simon Pilgrim
Date: 2020-11-12T16:16:04Z
New Revision: 89967427412489e10b3625bf1563a804fe5f2be2

URL: https://github.com/llvm/llvm-project/commit/89967427412489e10b3625bf1563a804fe5f2be2
DIFF: https://github.com/llvm/llvm-project/commit/89967427412489e10b3625bf1563a804fe5f2be2.diff

LOG: [KnownBits] Add KnownBits::makeConstant helper. NFCI.

Helper for cases where we need to create a KnownBits from a (fully known) constant value.

Added: 
    

Modified: 
    llvm/include/llvm/Support/KnownBits.h
    llvm/lib/Analysis/ValueTracking.cpp
    llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Support/KnownBits.h b/llvm/include/llvm/Support/KnownBits.h
index 5686279068e2..b43fde0085db 100644
--- a/llvm/include/llvm/Support/KnownBits.h
+++ b/llvm/include/llvm/Support/KnownBits.h
@@ -250,6 +250,11 @@ struct KnownBits {
     return getBitWidth() - Zero.countPopulation();
   }
 
+  /// Create known bits from a known constant.
+  static KnownBits makeConstant(const APInt &C) {
+    return KnownBits(~C, C);
+  }
+
   /// Compute known bits common to LHS and RHS.
   static KnownBits commonBits(const KnownBits &LHS, const KnownBits &RHS) {
     return KnownBits(LHS.Zero & RHS.Zero, LHS.One & RHS.One);

diff  --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index a7aee656a290..8568a23f2f9a 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -1223,10 +1223,7 @@ static void computeKnownBitsFromOperator(const Operator *I,
   case Instruction::Shl: {
     bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
     auto KF = [NSW](const KnownBits &KnownShiftVal, unsigned ShiftAmt) {
-      APInt ShiftAmtV(KnownShiftVal.getBitWidth(), ShiftAmt);
-      KnownBits KnownShiftAmt;
-      KnownShiftAmt.One = ShiftAmtV;
-      KnownShiftAmt.Zero = ~ShiftAmtV;
+      KnownBits KnownShiftAmt = KnownBits::makeConstant(APInt(32, ShiftAmt));
       KnownBits Result = KnownBits::shl(KnownShiftVal, KnownShiftAmt);
       // If this shift has "nsw" keyword, then the result is either a poison
       // value or has the same sign bit as the first operand.
@@ -1244,10 +1241,7 @@ static void computeKnownBitsFromOperator(const Operator *I,
   }
   case Instruction::LShr: {
     auto KF = [](const KnownBits &KnownShiftVal, unsigned ShiftAmt) {
-      APInt ShiftAmtV(KnownShiftVal.getBitWidth(), ShiftAmt);
-      KnownBits KnownShiftAmt;
-      KnownShiftAmt.One = ShiftAmtV;
-      KnownShiftAmt.Zero = ~ShiftAmtV;
+      KnownBits KnownShiftAmt = KnownBits::makeConstant(APInt(32, ShiftAmt));
       return KnownBits::lshr(KnownShiftVal, KnownShiftAmt);
     };
     computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Depth, Q,
@@ -1256,10 +1250,7 @@ static void computeKnownBitsFromOperator(const Operator *I,
   }
   case Instruction::AShr: {
     auto KF = [](const KnownBits &KnownShiftVal, unsigned ShiftAmt) {
-      APInt ShiftAmtV(KnownShiftVal.getBitWidth(), ShiftAmt);
-      KnownBits KnownShiftAmt;
-      KnownShiftAmt.One = ShiftAmtV;
-      KnownShiftAmt.Zero = ~ShiftAmtV;
+      KnownBits KnownShiftAmt = KnownBits::makeConstant(APInt(32, ShiftAmt));
       return KnownBits::ashr(KnownShiftVal, KnownShiftAmt);
     };
     computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Depth, Q,

diff  --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
index 44c23d804bd8..7057d4e61fc6 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -2624,15 +2624,11 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
 
   if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
     // We know all of the bits for a constant!
-    Known.One = C->getAPIntValue();
-    Known.Zero = ~Known.One;
-    return Known;
+    return KnownBits::makeConstant(C->getAPIntValue());
   }
   if (auto *C = dyn_cast<ConstantFPSDNode>(Op)) {
     // We know all of the bits for a constant fp!
-    Known.One = C->getValueAPF().bitcastToAPInt();
-    Known.Zero = ~Known.One;
-    return Known;
+    return KnownBits::makeConstant(C->getValueAPF().bitcastToAPInt());
   }
 
   if (Depth >= MaxRecursionDepth)


        


More information about the llvm-commits mailing list