[llvm-commits] patch: go crazy, compute bits for an entire add instruction

Nick Lewycky nicholas at mxc.ca
Thu Jul 14 01:59:29 PDT 2011


Jay Foad wrote:
>> Also, it seems that there should be some tricky hacker's delight sort of tricks to compute what you want without explicit loops.
>
> Does this help:
>
> http://gcc.gnu.org/ml/gcc-patches/2010-08/msg00254.html
>
> ?

Mmm, clever! I'd been trying to compute the carry distinctly from the 
sum (given that add is 'xor with carry', something like a+b - a^b might 
but doesn't work) but hadn't gotten it to work. Based on your technique, 
here's what I've written:

   // Calculate the sum as if the unknown bits were all zeros, and
   // another as if the unknown bits were all ones.
   APInt SumWithZeros = LHSKnownOne + RHSKnownOne;
   APInt SumWithOnes = ~LHSKnownZero + ~RHSKnownZero;

   // At a bit position where SumWithZeros and SumWithOne agree, the
   // carry value is the same regardless of the unknown bits.
   APInt CarryMask = ~(SumWithZeros ^ SumWithOnes);

   // We can only know the result of the sum when we know the values for
   // the left, right and carry inputs.
   APInt KnownMask = (LHSKnownZero | LHSKnownOne) &
                     (RHSKnownZero | RHSKnownOne) &
                     CarryMask &
                     Mask;

   KnownZero = ~SumWithZeros & KnownMask;
   KnownOne = SumWithOnes & KnownMask;

I'll roll this into an updated patch shortly (addressing Chris's 
concerns about the InstSimplify change over IRC). In general though, 
will I be allowed to use an O(n) loop in ComputeMaskedBits if needed? I 
don't think we'll be able to solve multiply without it--though we can try!

Nick



More information about the llvm-commits mailing list