[llvm-commits] [llvm] r143028 - in /llvm/trunk: lib/Analysis/ValueTracking.cpp test/Transforms/InstSimplify/compare.ll

Bob Wilson bob.wilson at apple.com
Thu Oct 27 08:46:47 PDT 2011


A bisection blamed this change for a regression in 483.xalancbmk: http://llvm.org/perf/db_default/simple/nts/347/
I'm going to revert this for now.

On Oct 26, 2011, at 8:31 AM, Duncan Sands wrote:

> Author: baldrick
> Date: Wed Oct 26 10:31:51 2011
> New Revision: 143028
> 
> URL: http://llvm.org/viewvc/llvm-project?rev=143028&view=rev
> Log:
> My super-optimizer noticed that we weren't folding this expression to
> true: (x *nsw x) sgt 0, where x = (y | 1).  This occurs in 464.h264ref.
> 
> Modified:
>    llvm/trunk/lib/Analysis/ValueTracking.cpp
>    llvm/trunk/test/Transforms/InstSimplify/compare.ll
> 
> Modified: llvm/trunk/lib/Analysis/ValueTracking.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/ValueTracking.cpp?rev=143028&r1=143027&r2=143028&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Analysis/ValueTracking.cpp (original)
> +++ llvm/trunk/lib/Analysis/ValueTracking.cpp Wed Oct 26 10:31:51 2011
> @@ -201,9 +201,36 @@
>     ComputeMaskedBits(I->getOperand(1), Mask2, KnownZero, KnownOne, TD,Depth+1);
>     ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, TD,
>                       Depth+1);
> -    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
> -    assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 
> -    
> +    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
> +    assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
> +
> +    bool isKnownNegative = false;
> +    bool isKnownNonNegative = false;
> +    // If the multiplication is known not to overflow, compute the sign bit.
> +    if (Mask.isNegative() && cast<BinaryOperator>(I)->hasNoSignedWrap()) {
> +      Value *Op1 = I->getOperand(1), *Op2 = I->getOperand(0);
> +      if (Op1 == Op2) {
> +        // The product of a number with itself is non-negative.
> +        isKnownNonNegative = true;
> +      } else {
> +        bool isKnownNonNegative1 = KnownZero.isNegative();
> +        bool isKnownNonNegative2 = KnownZero2.isNegative();
> +        bool isKnownNegative1 = KnownOne.isNegative();
> +        bool isKnownNegative2 = KnownOne2.isNegative();
> +        // The product of two numbers with the same sign is non-negative.
> +        isKnownNonNegative = (isKnownNegative1 && isKnownNegative2) ||
> +          (isKnownNonNegative1 && isKnownNonNegative2);
> +        // The product of a negative number and a non-negative number is either
> +        // negative or zero.
> +        isKnownNegative = (isKnownNegative1 && isKnownNonNegative2 &&
> +                           isKnownNonZero(Op2, TD, Depth)) ||
> +                          (isKnownNegative2 && isKnownNonNegative1 &&
> +                           isKnownNonZero(Op1, TD, Depth));
> +        assert(!(isKnownNegative && isKnownNonNegative) &&
> +               "Sign bit both zero and one?");
> +      }
> +    }
> +
>     // If low bits are zero in either operand, output low known-0 bits.
>     // Also compute a conserative estimate for high known-0 bits.
>     // More trickiness is possible, but this is sufficient for the
> @@ -220,6 +247,12 @@
>     KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
>                 APInt::getHighBitsSet(BitWidth, LeadZ);
>     KnownZero &= Mask;
> +
> +    if (isKnownNonNegative)
> +      KnownZero.setBit(BitWidth - 1);
> +    else if (isKnownNegative)
> +      KnownOne.setBit(BitWidth - 1);
> +
>     return;
>   }
>   case Instruction::UDiv: {
> @@ -767,7 +800,7 @@
>   }
> 
>   // The remaining tests are all recursive, so bail out if we hit the limit.
> -  if (Depth++ == MaxDepth)
> +  if (Depth++ >= MaxDepth)
>     return false;
> 
>   unsigned BitWidth = getBitWidth(V->getType(), TD);
> @@ -851,6 +884,15 @@
>     if (YKnownNonNegative && isPowerOfTwo(X, TD, Depth))
>       return true;
>   }
> +  // X * Y.
> +  else if (match(V, m_Mul(m_Value(X), m_Value(Y)))) {
> +    BinaryOperator *BO = cast<BinaryOperator>(V);
> +    // If X and Y are non-zero then so is X * Y as long as the multiplication
> +    // does not overflow.
> +    if ((BO->hasNoSignedWrap() || BO->hasNoUnsignedWrap()) &&
> +        isKnownNonZero(X, TD, Depth) && isKnownNonZero(Y, TD, Depth))
> +      return true;
> +  }
>   // (C ? X : Y) != 0 if X != 0 and Y != 0.
>   else if (SelectInst *SI = dyn_cast<SelectInst>(V)) {
>     if (isKnownNonZero(SI->getTrueValue(), TD, Depth) &&
> 
> Modified: llvm/trunk/test/Transforms/InstSimplify/compare.ll
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstSimplify/compare.ll?rev=143028&r1=143027&r2=143028&view=diff
> ==============================================================================
> --- llvm/trunk/test/Transforms/InstSimplify/compare.ll (original)
> +++ llvm/trunk/test/Transforms/InstSimplify/compare.ll Wed Oct 26 10:31:51 2011
> @@ -323,3 +323,34 @@
>   ret i1 %B
> ; CHECK: ret i1 false
> }
> +
> +define i1 @mul1(i32 %X) {
> +; CHECK: @mul1
> +; Square of a non-zero number is non-zero if there is no overflow.
> +  %Y = or i32 %X, 1
> +  %M = mul nuw i32 %Y, %Y
> +  %C = icmp eq i32 %M, 0
> +  ret i1 %C
> +; CHECK: ret i1 false
> +}
> +
> +define i1 @mul2(i32 %X) {
> +; CHECK: @mul2
> +; Square of a non-zero number is positive if there is no signed overflow.
> +  %Y = or i32 %X, 1
> +  %M = mul nsw i32 %Y, %Y
> +  %C = icmp sgt i32 %M, 0
> +  ret i1 %C
> +; CHECK: ret i1 true
> +}
> +
> +define i1 @mul3(i32 %X, i32 %Y) {
> +; CHECK: @mul3
> +; Product of non-negative numbers is non-negative if there is no signed overflow.
> +  %XX = mul nsw i32 %X, %X
> +  %YY = mul nsw i32 %Y, %Y
> +  %M = mul nsw i32 %XX, %YY
> +  %C = icmp sge i32 %M, 0
> +  ret i1 %C
> +; CHECK: ret i1 true
> +}
> 
> 
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits




More information about the llvm-commits mailing list