This is an archive of the discontinued LLVM Phabricator instance.

[InstCombineCompares] Added shl optimization for the instruction - icmp eq/ne (shl Const2, A), Const1
ClosedPublic

Authored by ankur29.garg on Oct 17 2014, 3:00 AM.

Details

Summary

Hi,
The following patch implements the optimization for the instructions of the type:
icmp eq/ne (shl Const2, A), Const1

Such instructions can be converted to:
icmp eq/ne A, (TrailingZeros(Const1) - TrailingZeros(Const2))

This handles only the equality operators for now. Other operators need to be handled.

This patch is related to the http://reviews.llvm.org/D4068. The D4068 implements this optimization for ashr/lshr operators. The following patch implements the optimization for shl operator.

Please help in reviewing it.

Thanks.
Ankur

Diff Detail

Event Timeline

ankur29.garg retitled this revision from to [InstCombineCompares] Added shl optimization for the instruction - icmp eq/ne (shl Const2, A), Const1.
ankur29.garg updated this object.
ankur29.garg edited the test plan for this revision. (Show Details)
ankur29.garg set the repository for this revision to rL LLVM.
ankur29.garg added a subscriber: Unknown Object (MLST).

llvm-commits mailing list
llvm-commits@cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits

majnemer edited edge metadata.Oct 18 2014, 12:47 AM

All of the test cases you have here that return a constant are already handled by InstSimplify (which is run as part of InstCombine) so you aren't really testing them. Please do not complicate the code by handling impossible cases.

lib/Transforms/InstCombine/InstCombineCompares.cpp
1149

This should be unsigned. You should also save the result so it can be reused later on. This isn't always efficient to calculate.

1152

Run clang format here.

test/Transforms/InstCombine/icmp.ll
1424–1428

This is already handled by InstSimplify.

1432–1436

This is already handled by InstSimplify.

1449–1453

This is already handled by InstSimplify.

1466–1470

This is already handled by InstSimplify.

ankur29.garg edited edge metadata.

Hi David,
Thanks for the review.
I have made the changes as per your comments.

I have removed all those test cases, which were not being tested with this code. Also, I have removed the code for handling such cases and instead inserted an assertion.

Please review the updated revision.

Thanks.

majnemer added inline comments.Oct 18 2014, 1:37 AM
lib/Transforms/InstCombine/InstCombineCompares.cpp
1145

This should be named Zeros. Also, please reuse this for the calculation on line 1158.

1147–1150

I'd remove the braces around this.

1153–1164

These cases can be merged:

if (Shift >= 0 && AP2.shl(Shift) == AP1)
  return getICmp(I.ICMP_EQ, A, ConstantInt::get(A->getType(), Shift));

Hi David,
I have made the changes as per your comments.

Please review the updated revision.
Thanks.

majnemer accepted this revision.Oct 18 2014, 2:32 PM
majnemer edited edge metadata.

I sat down and proved it with Alive:

Name: 1
Pre: C2 != 0 && C1 == 0 && countTrailingZeros(C2) != 0
 %shl = shl i8 C2, %a
 %cmp = icmp eq i8 %shl, C1
=>
 %cmp = icmp uge i8 %a, width(%a) - countTrailingZeros(C2)

Name: 2
Pre: C2 != 0 && C1 == C2
 %shl = shl i8 C2, %a
 %cmp = icmp eq i8 %shl, C1
=>
 %cmp = icmp eq i8 %a, 0

Name: 3
Pre: C2 != 0 && C1 != C2 && C1 != 0 && countTrailingZeros(C1) > countTrailingZeros(C2) && (C2 << (countTrailingZeros(C1) - countTrailingZeros(C2)) == C1)
 %shl = shl i8 C2, %a
 %cmp = icmp eq i8 %shl, C1
=>
 %cmp = icmp eq i8 %a, countTrailingZeros(C1) - countTrailingZeros(C2)

Name: 4
Pre: C2 != 0 && C1 != C2 && C1 != 0 && !(countTrailingZeros(C1) > countTrailingZeros(C2) && (C2 << (countTrailingZeros(C1) - countTrailingZeros(C2)) == C1))
 %shl = shl i8 C2, %a
 %cmp = icmp eq i8 %shl, C1
=>
 %cmp = i1 false

LGTM is conditional on addressing the variable name change.

lib/Transforms/InstCombine/InstCombineCompares.cpp
1145

Can we name this something like AP2TrailingZeros?

This revision is now accepted and ready to land.Oct 18 2014, 2:32 PM
ankur29.garg edited edge metadata.

Hi David,
Thanks for reviewing the patch.

I have renamed the variable 'Zeros' to 'AP2TrailingZeros' as per your comments.

Also, Can you please commit this patch, as I don't have commit access.

Thanks.

majnemer closed this revision.Oct 19 2014, 1:37 AM

Commited as rL220162.