This is an archive of the discontinued LLVM Phabricator instance.

[ConstantFold] Make icmp of gep fold offset based
ClosedPublic

Authored by nikic on Dec 28 2021, 5:41 AM.

Details

Reviewers
spatel
lebedev.ri
Group Reviewers
Restricted Project
Commits
rG5afbfe33e7d6: [ConstantFold] Make icmp of gep fold offset based
Summary

We can fold an equality or unsigned icmp between base+offset1 and base+offset2 with inbounds offsets by comparison the offsets directly.

This replaces a pair of specialized folds that tried to reason based on the GEP structure instead. One of those folds was plain wrong (because it does not account for negative offsets), while the other is unnecessarily complicated and limited (e.g. it will fail with bitcasts involved).

The disadvantage of this change is that it requires data layout, so the fold is no longer performed by datalayout-independent constant folding. I don't think this is a loss in practice, but it does regress the ConstantExprFold.ll test, which checks folding without running any passes.

Diff Detail

Event Timeline

nikic created this revision.Dec 28 2021, 5:41 AM
nikic requested review of this revision.Dec 28 2021, 5:41 AM
Herald added a project: Restricted Project. · View Herald TranscriptDec 28 2021, 5:41 AM
spatel added inline comments.Dec 28 2021, 8:17 AM
llvm/lib/Analysis/ConstantFolding.cpp
1258

Similar to your comment in D116168 - I'm not sure why we specify this parameter as unsigned instead of the actual enum. I see that we are not including any headers via ConstantFolding.h, so maybe there's some compile-time justification for it?

1260

!ICmpInst::isSigned(CmpPred) ?

llvm/test/Transforms/InstSimplify/ConstProp/icmp-global.ll
202

Add a comment for this test to explain that a 'true' result would be a miscompile (unlike the 'inbounds' variation above here)?

nikic updated this revision to Diff 396402.Dec 28 2021, 9:09 AM

Use !isSigned, add comment

nikic marked 2 inline comments as done.Dec 28 2021, 9:11 AM
nikic added inline comments.
llvm/lib/Analysis/ConstantFolding.cpp
1258

Yeah, I believe the reason for the unsigned is that it's impossible to forward-declare a nested type in C++.

What do you think about the new variant that does a cast right at the start of the function?

spatel accepted this revision.Dec 28 2021, 9:41 AM

LGTM

llvm/lib/Analysis/ConstantFolding.cpp
1258

Sure, that's better. That means we could replace the checks for EQ/NE around line 1242 with ICmpInst::isEquality(Predicate) as another small cleanup.

This revision is now accepted and ready to land.Dec 28 2021, 9:41 AM
This revision was landed with ongoing or failed builds.Jan 3 2022, 12:45 AM
This revision was automatically updated to reflect the committed changes.