This is an archive of the discontinued LLVM Phabricator instance.

[InstSimplify] Constant fold icmp of gep
ClosedPublic

Authored by nikic on Feb 29 2020, 9:38 AM.

Details

Summary

InstSimplify can fold icmps of gep where the base pointers are the same and the offsets are constant. It does so by constructing a constant expression icmp and assumes that it gets folded -- but this doesn't actually happen, because GEP expressions can usually only be folded by the target-dependent constant folding layer. As such, we need to explicitly invoke it here.

PS: Does someone know why we constant folding is separated in this way? I understand that we don't want to have ConstantExpr depend on TLI, but why can't it depend on DL?

Diff Detail

Event Timeline

nikic created this revision.Feb 29 2020, 9:38 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 29 2020, 9:38 AM

PS: Does someone know why we constant folding is separated in this way? I understand that we don't want to have ConstantExpr depend on TLI, but why can't it depend on DL?

This is sort of historical; deep in LLVM's history, we allowed generating/optimizing modules without a datalayout. This turned out to be mostly useless in practice, so we abandoned it many years ago. The two separate constant folders is one of the leftover traces of that.

It would be non-trivial to change at this point: in ConstantExpr::getICmp, there currently isn't any way to get a DataLayout.

lib/Analysis/InstructionSimplify.cpp
3487 ↗(On Diff #247449)

If we need to repeat this pattern in multiple places, probably makes sense to factor this out into a helper.

nikic marked an inline comment as done.Mar 2 2020, 1:33 PM

PS: Does someone know why we constant folding is separated in this way? I understand that we don't want to have ConstantExpr depend on TLI, but why can't it depend on DL?

This is sort of historical; deep in LLVM's history, we allowed generating/optimizing modules without a datalayout. This turned out to be mostly useless in practice, so we abandoned it many years ago. The two separate constant folders is one of the leftover traces of that.

It would be non-trivial to change at this point: in ConstantExpr::getICmp, there currently isn't any way to get a DataLayout.

Thanks for the historical context, that makes sense! Threading DL through 1.4k uses of ConstantExpr::get* indeed doesn't seem realistic at this point.

lib/Analysis/InstructionSimplify.cpp
3487 ↗(On Diff #247449)

I was actually wondering whether we can make ConstantFoldConstant() simply always return non-null (either the original constant or a new one).

I initially thought that ConstantFoldConstant() follows the usual pattern where it returns non-null if and only if it actually folded something. However, this isn't actually the case: It returns null for non constexpr/constvector, but otherwise returns the original constant even if it didn't fold anything.

This seems like a not particularly useful in-between state and we could save this boilerplate (which is indeed repeated many times in the codebase) if we made ConstantFoldConstant() always return a constant.

nikic updated this revision to Diff 248223.Mar 4 2020, 9:44 AM

Rebase and simplify.

efriedma accepted this revision.Mar 4 2020, 1:18 PM

LGTM

This revision is now accepted and ready to land.Mar 4 2020, 1:18 PM
This revision was automatically updated to reflect the committed changes.