This is an archive of the discontinued LLVM Phabricator instance.

[InstSimplify] Fold icmp comparing GEPs with local allocation
ClosedPublic

Authored by ParkHanbum on Jun 26 2023, 3:46 AM.

Diff Detail

Event Timeline

ParkHanbum created this revision.Jun 26 2023, 3:46 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 26 2023, 3:46 AM
ParkHanbum requested review of this revision.Jun 26 2023, 3:46 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 26 2023, 3:46 AM
ParkHanbum edited the summary of this revision. (Show Details)Jun 26 2023, 3:50 AM
nikic requested changes to this revision.Jun 27 2023, 2:27 AM
nikic added inline comments.
llvm/lib/Analysis/InstructionSimplify.cpp
2797–2798

Variable names in LLVM start with an uppercase letter.

2800

This condition isn't wrong, but it's also not as accurate as it could be.

Let's say LHSSize = 4, LHSOffset = 0, RHSSize = 2, RHSOffset = 2 with LHSOffset - RHSOffset = -2. These can be equal.

Then consider LHSSize = 4, LHSOffset = 2, RHSSize = 2, RHSOffset = 0 with LHSOffset - RHSOffset = 2. These cannot be equal.

As you can see, the sign of the result matters and we should not just take the absolute value.

I believe the condition should be Dist.isNonNegative() ? Dist.ult(LHSSize) : (-Dist).ult(RHSSize).

This revision now requires changes to proceed.Jun 27 2023, 2:27 AM

updated the code according to nikic's comment.

This condition isn't wrong, but it's also not as accurate as it could be.

Let's say LHSSize = 4, LHSOffset = 0, RHSSize = 2, RHSOffset = 2 with LHSOffset - RHSOffset = -2. These can be equal.

Then consider LHSSize = 4, LHSOffset = 2, RHSSize = 2, RHSOffset = 0 with LHSOffset - RHSOffset = 2. These cannot be equal.

As you can see, the sign of the result matters and we should not just take the absolute value.

I believe the condition should be Dist.isNonNegative() ? Dist.ult(LHSSize) : (-Dist).ult(RHSSize).

nikic added a comment.Jun 27 2023, 6:22 AM

This mostly looks good to me.

As implemented, there was a potential issue here where you would get an assertion failure due to different bit widths if certain address space casts were involved. I've opted to make that impossible in https://github.com/llvm/llvm-project/commit/793eb0c0e4ffadc1cfebcda0c0cb1bcc2311def2 rather than have you introduce extra extensions here.

llvm/lib/Analysis/InstructionSimplify.cpp
2798

Combine declaration and initialization.

llvm/test/Transforms/InstSimplify/cmp-alloca-offsets.ll
219

We should have some additional test cases that work on differently-sized allocas, to show that the check is asymmetric. E.g. allocas with size 2 and 4 with effective diffs 2 and -2.

ParkHanbum marked 2 inline comments as done.Jun 28 2023, 4:05 AM

there was such a problem. I didn't know!! thanks for letting me.

llvm/lib/Analysis/InstructionSimplify.cpp
2797–2798

shoud I have to declare and assign Dist at the same time?

ParkHanbum marked an inline comment as done.
ParkHanbum marked an inline comment as done.

add testcases!

ParkHanbum marked an inline comment as done.
nikic accepted this revision.Jun 28 2023, 5:41 AM

LGTM. If you need me to commit this for you, can you please share the Name <email> to use for attribution?

This revision is now accepted and ready to land.Jun 28 2023, 5:41 AM

sure, I use email kese111@gmail.com and name is Hanbum Park!

llvm/test/Transforms/InstSimplify/cmp-alloca-offsets.ll
219

added test cases!

This revision was automatically updated to reflect the committed changes.

@nikic thanks you for your review and others!