[PATCH] D32785: [KnownBits] Add methods for determining if KnownBits is a constant value

Phabricator via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed May 3 16:25:37 PDT 2017


This revision was automatically updated to reflect the committed changes.
Closed by commit rL302091: [KnownBits] Add methods for determining if KnownBits is a constant value (authored by ctopper).

Changed prior to commit:
  https://reviews.llvm.org/D32785?vs=97647&id=97742#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D32785

Files:
  llvm/trunk/include/llvm/Support/KnownBits.h
  llvm/trunk/lib/Analysis/ConstantFolding.cpp
  llvm/trunk/lib/Analysis/InstructionSimplify.cpp
  llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp


Index: llvm/trunk/include/llvm/Support/KnownBits.h
===================================================================
--- llvm/trunk/include/llvm/Support/KnownBits.h
+++ llvm/trunk/include/llvm/Support/KnownBits.h
@@ -43,6 +43,22 @@
     return Zero.getBitWidth();
   }
 
+  /// Returns true if there is conflicting information.
+  bool hasConflict() const { return Zero.intersects(One); }
+
+  /// Returns true if we know the value of all bits.
+  bool isConstant() const {
+    assert(!hasConflict() && "KnownBits conflict!");
+    return Zero.countPopulation() + One.countPopulation() == getBitWidth();
+  }
+
+  /// Returns the value when all bits have a known value. This just returns One
+  /// with a protective assertion.
+  const APInt &getConstant() const {
+    assert(isConstant() && "Can only get value when all bits are known");
+    return One;
+  }
+
   /// Returns true if this value is known to be negative.
   bool isNegative() const { return One.isSignBitSet(); }
 
Index: llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp
===================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -2182,8 +2182,8 @@
   // determine the value. If so, constant fold it.
   KnownBits Known(VTy->getPrimitiveSizeInBits());
   computeKnownBits(ResultOp, Known, 0, &RI);
-  if ((Known.Zero|Known.One).isAllOnesValue())
-    RI.setOperand(0, Constant::getIntegerValue(VTy, Known.One));
+  if (Known.isConstant())
+    RI.setOperand(0, Constant::getIntegerValue(VTy, Known.getConstant()));
 
   return nullptr;
 }
@@ -2863,8 +2863,8 @@
       unsigned BitWidth = Ty->getScalarSizeInBits();
       KnownBits Known(BitWidth);
       computeKnownBits(I, Known, /*Depth*/0, I);
-      if ((Known.Zero | Known.One).isAllOnesValue()) {
-        Constant *C = ConstantInt::get(Ty, Known.One);
+      if (Known.isConstant()) {
+        Constant *C = ConstantInt::get(Ty, Known.getConstant());
         DEBUG(dbgs() << "IC: ConstFold (all bits known) to: " << *C <<
                         " from: " << *I << '\n');
 
Index: llvm/trunk/lib/Analysis/InstructionSimplify.cpp
===================================================================
--- llvm/trunk/lib/Analysis/InstructionSimplify.cpp
+++ llvm/trunk/lib/Analysis/InstructionSimplify.cpp
@@ -4595,8 +4595,8 @@
     unsigned BitWidth = I->getType()->getScalarSizeInBits();
     KnownBits Known(BitWidth);
     computeKnownBits(I, Known, Q.DL, /*Depth*/ 0, Q.AC, I, Q.DT, ORE);
-    if ((Known.Zero | Known.One).isAllOnesValue())
-      Result = ConstantInt::get(I->getType(), Known.One);
+    if (Known.isConstant())
+      Result = ConstantInt::get(I->getType(), Known.getConstant());
   }
 
   /// If called on unreachable code, the above logic may report that the
Index: llvm/trunk/lib/Analysis/ConstantFolding.cpp
===================================================================
--- llvm/trunk/lib/Analysis/ConstantFolding.cpp
+++ llvm/trunk/lib/Analysis/ConstantFolding.cpp
@@ -701,11 +701,10 @@
       return Op1;
     }
 
-    APInt KnownZero = Known0.Zero | Known1.Zero;
-    APInt KnownOne = Known0.One & Known1.One;
-    if ((KnownZero | KnownOne).isAllOnesValue()) {
-      return ConstantInt::get(Op0->getType(), KnownOne);
-    }
+    Known0.Zero |= Known1.Zero;
+    Known0.One &= Known1.One;
+    if (Known0.isConstant())
+      return ConstantInt::get(Op0->getType(), Known0.getConstant());
   }
 
   // If the constant expr is something like &A[123] - &A[4].f, fold this into a


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D32785.97742.patch
Type: text/x-patch
Size: 3603 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170503/03f90cb0/attachment.bin>


More information about the llvm-commits mailing list