This is an archive of the discontinued LLVM Phabricator instance.

[ConstantFold] Handle identity folds at top of ConstantFoldBinaryInst
ClosedPublic

Authored by fhahn on Nov 13 2019, 3:02 AM.

Details

Summary

Currently we miss folds with undef and identity values for binary ops
that do not fold to undef in general.

We can generalize the identity simplifications and do them before
checking for undef in particular.

Alive checks:

This will also allow us to remove some now redundant cases throughout
the function, but I would like to do this as follow-up. That should make
tracking down potential issues easier.

Diff Detail

Event Timeline

fhahn created this revision.Nov 13 2019, 3:02 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 13 2019, 3:02 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
spatel added inline comments.Nov 13 2019, 5:23 AM
llvm/lib/IR/ConstantFold.cpp
992

Add a code comment for future reference? An identity constant always allows returning the other operand including undef/poison.
There's some possibly related discussion about that in:
https://bugs.llvm.org/show_bug.cgi?id=43958

Also, we can handle several more identity patterns like:
// X << 0 = X
by passing the optional "AllowRHSConstant" parameter to getBinOpIdentity().

So that could be another TODO for a follow-up patch. Our test coverage seems very lacking for this function though...so that should be improved first to make sure we're not breaking any rules.

llvm/test/Transforms/InstCombine/binop-identity-undef.ll
2 ↗(On Diff #229051)

This file would be better placed under test/Analysis/ConstantFolding and RUN with -constprop. (Don't need full instcombine or even instsimplify to get these transforms.)

llvm/test/Transforms/InstCombine/vec_shuffle.ll
1037

Here and similarly below - remove and/or adjust the comment line since we handle this now.

fhahn updated this revision to Diff 229082.Nov 13 2019, 6:22 AM
fhahn marked 3 inline comments as done.

Add additional comment and FIXME to ConstantFold.cpp, move test to Analysis/ConstantFolding/, drop fixmes from vec_shuffle.ll

spatel accepted this revision.Nov 13 2019, 6:28 AM

LGTM

This revision is now accepted and ready to land.Nov 13 2019, 6:28 AM