[llvm] r178409 - Implement XOR reassociation. It is based on following rules:

Shuxin Yang shuxin.llvm at gmail.com
Mon Apr 1 09:53:32 PDT 2013


The problem is caused by a miscommunication between a caller & callee:

   - the caller guard the callsite with "if xor operand is constantint",
   - however, the callee assert the "input operand is constant".

  In this case %0 is not "constantint", but is "constant".

Sorry for the inconvenience. Testing is in progress.

On 4/1/13 1:06 AM, Evgeniy Stepanov wrote:
> With this,  bin/opt -reassociate is crashing with the following input:
>
> @a = external global <{}>, align 4
>
> define void @_Z3fn1v() {
> entry:
>    %0 = ptrtoint i32* undef to i64
>    %1 = xor i64 %0, ptrtoint (<{}>* @a to i64)
>    %2 = and i64 undef, %1
>    ret void
> }
>
> and output:
>
> opt: ../lib/Transforms/Scalar/Reassociate.cpp:202: <anonymous
> namespace>::XorOpnd::XorOpnd(llvm::Value *): Assertion
> `!isa<Constant>(V) && "No constant"' failed.
>
> Full input attached.
>
>
> On Sat, Mar 30, 2013 at 9:25 PM, Stephen Canon <scanon at apple.com> wrote:
>> Many processors can do a & ~b with a single instruction (BIC on arm, ANDN in
>> the BMI extension on x86, PANDN for sse, ANDC on ppc).  Hence my claim that
>> it is a preferable form.
>>
>> - Steve
>>
>> On Mar 30, 2013, at 12:39 PM, Shuxin Yang <shuxin.llvm at gmail.com> wrote:
>>
>> This is truly wonderful tool. I need to spent some time today to digest this
>> elegant approach.
>>
>>> We can do better for rule 3.  It simplifies to (~x & c3).
>> I don't think (~x & c3) is better; it still need two instructions: one for
>> ~x, the other one for & c3.
>> On the other hand, the c3 is bound to & expression, instead of a xor
>> operand, which means the
>> c3 is not able to directly contribute to the xor simplication.
>>
>> In contrast, "(x & c3) ^ c3" may need only one instruction, in case c3 is
>> "canceled" by XORing
>> with other constant.
>>
>>
>> On 3/30/13 7:56 AM, Stephen Canon wrote:
>>
>> On Mar 30, 2013, at 10:11 AM, Stephen Canon <scanon at apple.com> wrote:
>>
>> On Mar 29, 2013, at 10:15 PM, Shuxin Yang <shuxin.llvm at gmail.com> wrote:
>>
>> Author: shuxin_yang
>> Date: Fri Mar 29 21:15:01 2013
>> New Revision: 178409
>>
>> URL: http://llvm.org/viewvc/llvm-project?rev=178409&view=rev
>> Log:
>> Implement XOR reassociation. It is based on following rules:
>>
>>   rule 1: (x | c1) ^ c2 => (x & ~c1) ^ (c1^c2),
>>      only useful when c1=c2
>>   rule 2: (x & c1) ^ (x & c2) = (x & (c1^c2))
>>   rule 3: (x | c1) ^ (x | c2) = (x & c3) ^ c3 where c3 = c1 ^ c2
>>   rule 4: (x | c1) ^ (x & c2) => (x & c3) ^ c1, where c3 = ~c1 ^ c2
>>
>>
>> We can do better for rule 3.  It simplifies to (~x & c3).
>>
>>
>> I should also point out that all of these have one-line proofs using only
>> middle-school algebra (no theorem-prover required, though I encourage
>> everyone to use one) if you re-cast them as statements about boolean rings.
>>
>> A refresher:
>>
>> Any expression in boolean algebra can be transformed into a statement in
>> boolean rings as follows:
>>
>> (a XOR b) ==> a + b
>> (a OR b) ==> a + ab + b
>> (a AND b) ==> ab
>> (NOT a) ==> a + 1
>>
>> This may not seem like an improvement at first glance, but boolean rings are
>> rings, meaning that your favorite properties of multiplication and addition
>> hold, and you can now work with expressions in the same way that you have
>> since you were 10.  Boolean rings also have two additional properties that
>> let you do some simplifications:
>>
>> aa = a
>> a + a = 0
>>
>> As examples, I’ll give quick proofs of the rules in question:
>>
>> Rule 1: (x | c) ^ d ==> x + xc + c + d ==> x(1 + c) + (c + d) ==> (x & ~c) ^
>> (c ^ d).
>> Rule 2: (x & c) ^ (x & d) ==> xc + xd ==> x(c + d) ==> x & (c ^ d).
>> Rule 3: (x | c) ^ (x | d) ==> x + xc + c + x + xd + d ==> xc + c + xd + d
>> ==> (x + 1)(c + d) ==> ~x & (c ^ d).
>> Rule 4: (x | c) ^ (x & d) ==> x + xc + c + xd ==> x(c + d + 1) + c ==> (x &
>> ~(c ^ d)) ^ c.
>>
>> Much cleaner than the usual mess of truth tables or case analysis.
>>
>> - Steve
>>
>>
>>
>>
>>
>> _______________________________________________
>> 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