This is an archive of the discontinued LLVM Phabricator instance.

[Reassociate][NFC] Use an appropriate `dyn_cast` for `BinaryOperator`
ClosedPublic

Authored by wristow on Jul 24 2022, 6:42 PM.

Details

Summary

In D129523, it was noted by @spatel that there is are some questionable naked
casts from Instruction to BinaryOperator. Specifically, Sanjay noted
regarding isReassociableOp:

Existing issue: this is awkward - we're dyn_casting to Instruction, but then we nakedly
cast to BinaryOperator on the return. Could this cast to BO directly? There might be
some subtlety with binop constant expressions that needs to be addressed.

Looking into this, it seems to me it's a guaranteed-safe change, and a small
improvement. The result of the dyn_cast is checked for non-null, and when that
passes more elaborate aspects are checked before returning the desired
BinaryOperator on success (and nullptr is returned if any of the checks
fail). Changing the cast directly to BO results in the dyn_cast returning
null in more cases, and so we skip the more elaborate checking which is
guaranteed to have either been pointless, or exposing a bug. (The bug would
only manifest if an inappropriate Opcode were passed (which presumably doesn't
happen). So I'd say the existing code isn't a problem, but this way is better.)

Diff Detail

Event Timeline

wristow created this revision.Jul 24 2022, 6:42 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 24 2022, 6:42 PM
Herald added a subscriber: hiraditya. · View Herald Transcript
wristow requested review of this revision.Jul 24 2022, 6:42 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 24 2022, 6:42 PM
nikic accepted this revision.Jul 25 2022, 3:35 AM
nikic added a subscriber: nikic.

LG. This should not affect constexpr behavior, because BinaryOperator is, despite its wonderful name, not an Operator (unlike OverflowingBinaryOperator -- go figure!)

This revision is now accepted and ready to land.Jul 25 2022, 3:35 AM
RKSimon added inline comments.Jul 25 2022, 3:44 AM
llvm/lib/Transforms/Scalar/Reassociate.cpp
157–160

(style) use auto *

166–170

(style) use auto *

spatel accepted this revision.Jul 25 2022, 6:00 AM

LG. This should not affect constexpr behavior, because BinaryOperator is, despite its wonderful name, not an Operator (unlike OverflowingBinaryOperator -- go figure!)

Wow...I didn't notice that in all my time working on LLVM. :)

This revision was landed with ongoing or failed builds.Jul 25 2022, 10:25 AM
This revision was automatically updated to reflect the committed changes.

Thanks for the quick reviews!