Correct the value of LowBits in srem and urem handling in
authorDan Gohman <gohman@apple.com>
Tue, 6 May 2008 00:51:48 +0000 (00:51 +0000)
committerDan Gohman <gohman@apple.com>
Tue, 6 May 2008 00:51:48 +0000 (00:51 +0000)
ComputeMaskedBits.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@50692 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/SelectionDAG/SelectionDAG.cpp
lib/Transforms/Scalar/InstructionCombining.cpp
test/Transforms/InstCombine/srem-simplify-bug.ll [new file with mode: 0644]

index 26d5548617455f69f01fb93e00ffb8ee6357f0e3..3e75f7b3056fb9206ce6b865cff48e55d4e3052b 100644 (file)
@@ -1560,7 +1560,7 @@ void SelectionDAG::ComputeMaskedBits(SDOperand Op, const APInt &Mask,
     if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
       APInt RA = Rem->getAPIntValue();
       if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
-        APInt LowBits = RA.isStrictlyPositive() ? ((RA - 1) | RA) : ~RA;
+        APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA;
         APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
         ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
 
@@ -1581,8 +1581,8 @@ void SelectionDAG::ComputeMaskedBits(SDOperand Op, const APInt &Mask,
   case ISD::UREM: {
     if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
       APInt RA = Rem->getAPIntValue();
-      if (RA.isStrictlyPositive() && RA.isPowerOf2()) {
-        APInt LowBits = (RA - 1) | RA;
+      if (RA.isPowerOf2()) {
+        APInt LowBits = (RA - 1);
         APInt Mask2 = LowBits & Mask;
         KnownZero |= ~LowBits & Mask;
         ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
index 6b1da0d25656ad071a4827516ce04b756f369f50..461d5fc7803b1573145f0c55152f67b0d6079a38 100644 (file)
@@ -965,7 +965,7 @@ void InstCombiner::ComputeMaskedBits(Value *V, const APInt &Mask,
     if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
       APInt RA = Rem->getValue();
       if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
-        APInt LowBits = RA.isStrictlyPositive() ? ((RA - 1) | RA) : ~RA;
+        APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA;
         APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
         ComputeMaskedBits(I->getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
 
@@ -986,8 +986,8 @@ void InstCombiner::ComputeMaskedBits(Value *V, const APInt &Mask,
   case Instruction::URem: {
     if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
       APInt RA = Rem->getValue();
-      if (RA.isStrictlyPositive() && RA.isPowerOf2()) {
-        APInt LowBits = (RA - 1) | RA;
+      if (RA.isPowerOf2()) {
+        APInt LowBits = (RA - 1);
         APInt Mask2 = LowBits & Mask;
         KnownZero |= ~LowBits & Mask;
         ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
@@ -1728,7 +1728,7 @@ bool InstCombiner::SimplifyDemandedBits(Value *V, APInt DemandedMask,
     if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
       APInt RA = Rem->getValue();
       if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
-        APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) | RA : ~RA;
+        APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA;
         APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
         if (SimplifyDemandedBits(I->getOperand(0), Mask2,
                                  LHSKnownZero, LHSKnownOne, Depth+1))
@@ -1749,8 +1749,8 @@ bool InstCombiner::SimplifyDemandedBits(Value *V, APInt DemandedMask,
   case Instruction::URem: {
     if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
       APInt RA = Rem->getValue();
-      if (RA.isStrictlyPositive() && RA.isPowerOf2()) {
-        APInt LowBits = (RA - 1) | RA;
+      if (RA.isPowerOf2()) {
+        APInt LowBits = (RA - 1);
         APInt Mask2 = LowBits & DemandedMask;
         KnownZero |= ~LowBits & DemandedMask;
         if (SimplifyDemandedBits(I->getOperand(0), Mask2,
diff --git a/test/Transforms/InstCombine/srem-simplify-bug.ll b/test/Transforms/InstCombine/srem-simplify-bug.ll
new file mode 100644 (file)
index 0000000..cdf5202
--- /dev/null
@@ -0,0 +1,9 @@
+; RUN: llvm-as < %s | opt -instcombine | llvm-dis | grep {ret i1 false}
+; PR2276
+
+define i1 @f(i32 %x) {
+  %A = or i32 %x, 1
+  %B = srem i32 %A, 1
+  %C = icmp ne i32 %B, 0
+  ret i1 %C
+}