[llvm-commits] [llvm] r123203 - in /llvm/trunk: lib/Target/README.txt lib/Transforms/InstCombine/InstCombineCompares.cpp test/Transforms/InstCombine/icmp.ll

Chris Lattner clattner at apple.com
Tue Jan 11 08:52:29 PST 2011


On Jan 10, 2011, at 4:36 PM, Owen Anderson wrote:

> Author: resistor
> Date: Mon Jan 10 18:36:45 2011
> New Revision: 123203
> 
> URL: http://llvm.org/viewvc/llvm-project?rev=123203&view=rev
> Log:
> Fix a random missed optimization by making InstCombine more aggressive when determining which bits are demanded by
> a comparison against a constant.

Very nice, thanks Owen!  This is a great generalization.

> +  APInt RHS = CI->getValue();

I think RHS can be a const&.

> +  APInt Mask(BitWidth, 0);

Mask is dead, please remove it and the code after the switch.

-Chris

>  
> +  switch (I.getPredicate()) {
> +  // For a UGT comparison, we don't care about any bits that 
> +  // correspond to the trailing ones of the comparand.  The value of these
> +  // bits doesn't impact the outcome of the comparison, because any value
> +  // greater than the RHS must differ in a bit higher than these due to carry.
> +  case ICmpInst::ICMP_UGT: {
> +    unsigned trailingOnes = RHS.countTrailingOnes();
> +    APInt lowBitsSet = APInt::getLowBitsSet(BitWidth, trailingOnes);
> +    return ~lowBitsSet;
> +  }
> +  
> +  // Similarly, for a ULT comparison, we don't care about the trailing zeros.
> +  // Any value less than the RHS must differ in a higher bit because of carries.
> +  case ICmpInst::ICMP_ULT: {
> +    unsigned trailingZeros = RHS.countTrailingZeros();
> +    APInt lowBitsSet = APInt::getLowBitsSet(BitWidth, trailingZeros);
> +    return ~lowBitsSet;
> +  }
> +  
> +  default:
> +    return APInt::getAllOnesValue(BitWidth);
> +  }
> +  
> +  return Mask;
> +}
> 
> Instruction *InstCombiner::visitICmpInst(ICmpInst &I) {
>   bool Changed = false;
> @@ -1830,8 +1869,7 @@
>     APInt Op1KnownZero(BitWidth, 0), Op1KnownOne(BitWidth, 0);
> 
>     if (SimplifyDemandedBits(I.getOperandUse(0),
> -                             isSignBit ? APInt::getSignBit(BitWidth)
> -                                       : APInt::getAllOnesValue(BitWidth),
> +                             DemandedBitsLHSMask(I, BitWidth, isSignBit),
>                              Op0KnownZero, Op0KnownOne, 0))
>       return &I;
>     if (SimplifyDemandedBits(I.getOperandUse(1),
> 
> Modified: llvm/trunk/test/Transforms/InstCombine/icmp.ll
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/icmp.ll?rev=123203&r1=123202&r2=123203&view=diff
> ==============================================================================
> --- llvm/trunk/test/Transforms/InstCombine/icmp.ll (original)
> +++ llvm/trunk/test/Transforms/InstCombine/icmp.ll Mon Jan 10 18:36:45 2011
> @@ -192,3 +192,20 @@
> ; CHECK-NEXT: %cmp = icmp eq i32 %x, 3
> }
> 
> +define i1 @test21(i8 %x, i8 %y) {
> +; CHECK: @test21
> +; CHECK-NOT: or i8
> +; CHECK: icmp ugt
> +  %A = or i8 %x, 1
> +  %B = icmp ugt i8 %A, 3
> +  ret i1 %B
> +}
> +
> +define i1 @test22(i8 %x, i8 %y) {
> +; CHECK: @test22
> +; CHECK-NOT: or i8
> +; CHECK: icmp ult
> +  %A = or i8 %x, 1
> +  %B = icmp ult i8 %A, 4
> +  ret i1 %B
> +}
> 
> 
> _______________________________________________
> 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