[llvm] r306980 - [InstCombine] look through bswap/bitreverse for equality comparisons

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 2 13:01:10 PDT 2017


The other bit manipulation intrinsics (cttz, ctlz, ctpop) don't have this
property.

I think some FP intrinsics will work here, but we would need to be careful
with NANs, signed zero, overflow, underflow, exactness, etc.

sin(x) == sin(y) does not work though: y = x + 2*PI --> sin(x) == sin(x +
2*PI) is probably true, but x == y is probably false?

For f(x) == f(x), early-cse should catch those? But again, we might need
relaxed FP for FP intrinsics.


On Sun, Jul 2, 2017 at 12:54 PM, Philip Reames <listmail at philipreames.com>
wrote:

> Nothing wrong with this change, but any chance we could generalize here?
> Are there other intrinsics whose input/output preserve equality?  I think
> any intrinsic which is a 1-to-1 total function preserves this property
> right?  What about sin/cost/tan, sqrt, etc..?
>
> There's also a larger category where equality of inputs implies equality
> of outputs.  That is: f(a) == f(a) is always true for certain functions f.
> Do we exploit that anywhere?  log, signbit, fabs, etc... all have that
> property.  (I suspect we get this one, but have not checked.)
>
> Philip
>
>
> On 07/02/2017 07:34 AM, Sanjay Patel via llvm-commits wrote:
>
>> Author: spatel
>> Date: Sun Jul  2 07:34:50 2017
>> New Revision: 306980
>>
>> URL: http://llvm.org/viewvc/llvm-project?rev=306980&view=rev
>> Log:
>> [InstCombine] look through bswap/bitreverse for equality comparisons
>>
>> I noticed this missed bswap optimization in the CGP memcmp() expansion,
>> and then I saw that we don't have the fold in InstCombine.
>>
>> Differential Revision: https://reviews.llvm.org/D34763
>>
>> Modified:
>>      llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
>>      llvm/trunk/test/Transforms/InstCombine/icmp.ll
>>
>> Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
>> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transform
>> s/InstCombine/InstCombineCompares.cpp?rev=306980&r1=306979&
>> r2=306980&view=diff
>> ============================================================
>> ==================
>> --- llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
>> (original)
>> +++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp Sun
>> Jul  2 07:34:50 2017
>> @@ -3438,6 +3438,15 @@ Instruction *InstCombiner::foldICmpEqual
>>       }
>>     }
>>   +  // If both operands are byte-swapped or bit-reversed, just compare
>> the
>> +  // original values.
>> +  // TODO: Move this to a function similar to
>> foldICmpIntrinsicWithConstant()
>> +  // and handle more intrinsics.
>> +  if ((match(Op0, m_BSwap(m_Value(A))) && match(Op1,
>> m_BSwap(m_Value(B)))) ||
>> +      (match(Op0, m_Intrinsic<Intrinsic::bitreverse>(m_Value(A))) &&
>> +       match(Op1, m_Intrinsic<Intrinsic::bitreverse>(m_Value(B)))))
>> +    return new ICmpInst(Pred, A, B);
>> +
>>     return nullptr;
>>   }
>>
>> Modified: llvm/trunk/test/Transforms/InstCombine/icmp.ll
>> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transfor
>> ms/InstCombine/icmp.ll?rev=306980&r1=306979&r2=306980&view=diff
>> ============================================================
>> ==================
>> --- llvm/trunk/test/Transforms/InstCombine/icmp.ll (original)
>> +++ llvm/trunk/test/Transforms/InstCombine/icmp.ll Sun Jul  2 07:34:50
>> 2017
>> @@ -2979,9 +2979,7 @@ declare i32 @llvm.bswap.i32(i32)
>>     define i1 @bswap_ne(i32 %x, i32 %y) {
>>   ; CHECK-LABEL: @bswap_ne(
>> -; CHECK-NEXT:    [[SWAPX:%.*]] = call i32 @llvm.bswap.i32(i32 %x)
>> -; CHECK-NEXT:    [[SWAPY:%.*]] = call i32 @llvm.bswap.i32(i32 %y)
>> -; CHECK-NEXT:    [[CMP:%.*]] = icmp ne i32 [[SWAPX]], [[SWAPY]]
>> +; CHECK-NEXT:    [[CMP:%.*]] = icmp ne i32 %x, %y
>>   ; CHECK-NEXT:    ret i1 [[CMP]]
>>   ;
>>     %swapx = call i32 @llvm.bswap.i32(i32 %x)
>> @@ -2994,9 +2992,7 @@ declare <8 x i16> @llvm.bswap.v8i16(<8 x
>>     define <8 x i1> @bswap_vec_eq(<8 x i16> %x, <8 x i16> %y) {
>>   ; CHECK-LABEL: @bswap_vec_eq(
>> -; CHECK-NEXT:    [[SWAPX:%.*]] = call <8 x i16> @llvm.bswap.v8i16(<8 x
>> i16> %x)
>> -; CHECK-NEXT:    [[SWAPY:%.*]] = call <8 x i16> @llvm.bswap.v8i16(<8 x
>> i16> %y)
>> -; CHECK-NEXT:    [[CMP:%.*]] = icmp eq <8 x i16> [[SWAPX]], [[SWAPY]]
>> +; CHECK-NEXT:    [[CMP:%.*]] = icmp eq <8 x i16> %x, %y
>>   ; CHECK-NEXT:    ret <8 x i1> [[CMP]]
>>   ;
>>     %swapx = call <8 x i16> @llvm.bswap.v8i16(<8 x i16> %x)
>> @@ -3009,9 +3005,7 @@ declare i64 @llvm.bitreverse.i64(i64)
>>     define i1 @bitreverse_eq(i64 %x, i64 %y) {
>>   ; CHECK-LABEL: @bitreverse_eq(
>> -; CHECK-NEXT:    [[REVX:%.*]] = call i64 @llvm.bitreverse.i64(i64 %x)
>> -; CHECK-NEXT:    [[REVY:%.*]] = call i64 @llvm.bitreverse.i64(i64 %y)
>> -; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i64 [[REVX]], [[REVY]]
>> +; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i64 %x, %y
>>   ; CHECK-NEXT:    ret i1 [[CMP]]
>>   ;
>>     %revx = call i64 @llvm.bitreverse.i64(i64 %x)
>> @@ -3024,9 +3018,7 @@ declare <8 x i16> @llvm.bitreverse.v8i16
>>     define <8 x i1> @bitreverse_vec_ne(<8 x i16> %x, <8 x i16> %y) {
>>   ; CHECK-LABEL: @bitreverse_vec_ne(
>> -; CHECK-NEXT:    [[REVX:%.*]] = call <8 x i16> @llvm.bitreverse.v8i16(<8
>> x i16> %x)
>> -; CHECK-NEXT:    [[REVY:%.*]] = call <8 x i16> @llvm.bitreverse.v8i16(<8
>> x i16> %y)
>> -; CHECK-NEXT:    [[CMP:%.*]] = icmp ne <8 x i16> [[REVX]], [[REVY]]
>> +; CHECK-NEXT:    [[CMP:%.*]] = icmp ne <8 x i16> %x, %y
>>   ; CHECK-NEXT:    ret <8 x i1> [[CMP]]
>>   ;
>>     %revx = call <8 x i16> @llvm.bitreverse.v8i16(<8 x i16> %x)
>>
>>
>> _______________________________________________
>> llvm-commits mailing list
>> llvm-commits at lists.llvm.org
>> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
>>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170702/ff050975/attachment.html>


More information about the llvm-commits mailing list