This is an archive of the discontinued LLVM Phabricator instance.

[PatternMatch] Do not match constant expressions for binops
ClosedPublic

Authored by nikic on Jul 27 2023, 1:26 AM.

Details

Summary

Currently, m_Mul style matchers also match constant expressions. This is a regular source of assertion failures (usually by trying to do a match and then cast to Instruction or BinaryOperator) and infinite combine loops. At the same time, I don't think this provides useful optimization capabilities (all of the tests affected here are regression tests for crashes / infinite loops).

Long term, all of these constant expressions (apart from add/sub possibly) are slated for removal per https://discourse.llvm.org/t/rfc-remove-most-constant-expressions/63179 -- but doing those removals can itself expose new crashed and infinite loops due to the current PatternMatch behavior. I've been working on removing and expressions, and I've already run into 3 issues of that kind.

Diff Detail

Event Timeline

nikic created this revision.Jul 27 2023, 1:26 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 27 2023, 1:26 AM
Herald added a subscriber: StephenFan. · View Herald Transcript
nikic requested review of this revision.Jul 27 2023, 1:26 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 27 2023, 1:26 AM
ormris added a subscriber: ormris.Jul 27 2023, 9:00 AM

Is there a way to check/get a ConstantExpr as a ImmConstant? If so maybe this should come with an update to m_ImmConstant to also try to extract from a ConstantExpr match.

Is there a way to check/get a ConstantExpr as a ImmConstant? If so maybe this should come with an update to m_ImmConstant to also try to extract from a ConstantExpr match.

I don't really get what you mean here. Isn't m_ImmConstant specifically about not matching constant expressions? m_Constant() is the way to match arbitrary constants (including ConstantExpr).

goldstein.w.n accepted this revision.Jul 27 2023, 1:00 PM

Is there a way to check/get a ConstantExpr as a ImmConstant? If so maybe this should come with an update to m_ImmConstant to also try to extract from a ConstantExpr match.

I don't really get what you mean here. Isn't m_ImmConstant specifically about not matching constant expressions? m_Constant() is the way to match arbitrary constants (including ConstantExpr).

Err yeah nvm I misread some of the test changes.

Sorry.

Code change LGTM.

Regarding the regressions, as long as its temporary state its fine imo.
Might want to give others a day or so to comment as to that affect, however.

This revision is now accepted and ready to land.Jul 27 2023, 1:00 PM
This revision was automatically updated to reflect the committed changes.