[llvm-commits] [llvm] r147749 - in /llvm/trunk: lib/Transforms/InstCombine/InstCombineAndOrXor.cpp test/Transforms/InstCombine/sign-test-and-or.ll

Eli Friedman eli.friedman at gmail.com
Mon Jan 9 14:58:41 PST 2012


On Sun, Jan 8, 2012 at 10:32 AM, Benjamin Kramer
<benny.kra at googlemail.com> wrote:
> Author: d0k
> Date: Sun Jan  8 12:32:24 2012
> New Revision: 147749
>
> URL: http://llvm.org/viewvc/llvm-project?rev=147749&view=rev
> Log:
> InstCombine: If we have a bit test and a sign test anded/ored together, merge the sign bit into the bit test.
>
> This is common in bit field code, e.g. checking if the first or the last bit of a bit field is set.
>
> Modified:
>    llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
>    llvm/trunk/test/Transforms/InstCombine/sign-test-and-or.ll
>
> Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp?rev=147749&r1=147748&r2=147749&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp (original)
> +++ llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp Sun Jan  8 12:32:24 2012
> @@ -743,6 +743,22 @@
>       }
>     }
>   }
> +
> +  // (X & C) == 0 & X > -1  ->  (X & (C | SignBit)) == 0
> +  if (LHS->hasOneUse() && RHS->hasOneUse() &&
> +      ((LHSCC == ICmpInst::ICMP_EQ && LHSCst->isZero() &&
> +        RHSCC == ICmpInst::ICMP_SGT && RHSCst->isAllOnesValue()) ||
> +       (RHSCC == ICmpInst::ICMP_EQ && RHSCst->isZero() &&
> +        LHSCC == ICmpInst::ICMP_SGT && LHSCst->isAllOnesValue()))) {
> +    BinaryOperator *BO =
> +      dyn_cast<BinaryOperator>(LHSCC == ICmpInst::ICMP_EQ ? Val : Val2);
> +    ConstantInt *AndCst;
> +    if (BO && match(BO, m_OneUse(m_And(m_Value(), m_ConstantInt(AndCst))))) {
> +      APInt New = AndCst->getValue() | APInt::getSignBit(AndCst->getBitWidth());
> +      BO->setOperand(1, ConstantInt::get(AndCst->getContext(), New));
> +      return BO == Val ? LHS : RHS;
> +    }
> +  }

There's a rather nasty mistake in this transform as written: it will
transform "(X & C) == 0 & Y > -1  ->  (X & (C | SignBit)) == 0".  I
believe this is already fixed by r147777, but I figured I would note
it here anyway.

-Eli




More information about the llvm-commits mailing list