This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Fold cmpeq/ne(and(~X,Y),0) --> cmpeq/ne(or(freeze(X),Y),freeze(X)) (PR44136)
AbandonedPublic

Authored by RKSimon on Apr 9 2021, 8:58 AM.

Details

Summary

Reverse of the backend fold from D100177 (PR44136), this is only valid if we freeze the not source value.

If we require the freeze we're not actually reducing the instruction count - should we limit this to cases where 'X' isGuaranteedNotToBeUndefOrPoison?

https://alive2.llvm.org/ce/z/PgAUx5
https://alive2.llvm.org/ce/z/8uRSz6

https://bugs.llvm.org/show_bug.cgi?id=44136

Diff Detail

Event Timeline

RKSimon created this revision.Apr 9 2021, 8:58 AM
RKSimon requested review of this revision.Apr 9 2021, 8:58 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 9 2021, 8:58 AM

I'm somewhat doubtful that this fold will be beneficial if it requires inserting freeze. Is there some broader context here for which this is intended?

I'm somewhat doubtful that this fold will be beneficial if it requires inserting freeze. Is there some broader context here for which this is intended?

+1. So for now, use isGuaranteedNotToBeUndefOrPoison?

The missing middle-end fold was noted on the ticket (PR44136) - this patch is to show what would be necessary.

Limiting this to isGuaranteedNotToBeUndefOrPoison would be safer, although I'm not sure how common the case is tbh. Happy to abandon and close the ticket if this is pointless extra work.

@lebedev.ri any thoughts?

lebedev.ri retitled this revision from [InstCombine] Fold cmpeq/ne(and(~X,Y),0) --> cmpeq/ne(or(freeze(X),Y),X) to [InstCombine] Fold cmpeq/ne(and(~X,Y),0) --> cmpeq/ne(or(freeze(X),Y),X) (PR44136).Apr 11 2021, 10:43 AM
lebedev.ri edited the summary of this revision. (Show Details)

The missing middle-end fold was noted on the ticket (PR44136) - this patch is to show what would be necessary.

Limiting this to isGuaranteedNotToBeUndefOrPoison would be safer, although I'm not sure how common the case is tbh. Happy to abandon and close the ticket if this is pointless extra work.

@lebedev.ri any thoughts?

I'm not sure, which is why i haven't posted any patches after originally replying to the bug.

Right now i don't quite understand why we are so afraid of using freeze.
Can we not teach instcombine to hoist it?

Ignoring the freeze question, i think this is an improvement.

RKSimon retitled this revision from [InstCombine] Fold cmpeq/ne(and(~X,Y),0) --> cmpeq/ne(or(freeze(X),Y),X) (PR44136) to [InstCombine] Fold cmpeq/ne(and(~X,Y),0) --> cmpeq/ne(or(freeze(X),Y),freeze(X)) (PR44136).Apr 11 2021, 10:52 AM

The missing middle-end fold was noted on the ticket (PR44136) - this patch is to show what would be necessary.

Limiting this to isGuaranteedNotToBeUndefOrPoison would be safer, although I'm not sure how common the case is tbh. Happy to abandon and close the ticket if this is pointless extra work.

Using isGuaranteedNotTobeUndefOrPoison() will just make the transform apply inconsistently. It seems to me like there is little motivation for doing this change (the report was about the reverse transform in the backend after all), so I'd drop the patch for now. Can be reconsidered when undef goes away or we establish policy to ignore undef optimization bugs.

If freeze is ignored, this canonicalization helps optimizations like GVN to treat cmpeq (and(~X,Y),0) and cmpeq (or(X,Y),X) as equivalent ones, right?

If we're going to use freeze here, let's leave a comment saying e.g. TODO: if undef is removed, freeze is not needed: https://alive2.llvm.org/ce/z/V2Z4Lz
This will help finding out introduction of redundant freezes after undef is removed.

Regarding when to use freeze, I don't have a very good policy TBH. But, I agree that we cannot infinitely postpone usage of freeze.
There has been progress in SimplifyCFG and JumpThreading to successfully deal with frozen branch conditions.
If InstCombine is also updated to deal with freeze, it will be great.
A question is what kind of rules should we introduce first. It depends on which bug to fix; e.g. if we fix some fcmp bug using freeze, we'll need to update fp related optimizations to recognize freeze.

RKSimon abandoned this revision.Apr 12 2021, 3:25 AM

Abandoning for now, we can revisit this if/when people get more relaxed about using freeze()

xbolva00 added a comment.EditedApr 12 2021, 4:11 AM

Abandoning for now, we can revisit this if/when people get more relaxed about using freeze()

Can you create some umbrella PR for “transformations with freeze” (or something like that) on bugzilla ?
And add this there.

Abandoning for now, we can revisit this if/when people get more relaxed about using freeze()

Can you create some umbrella PR for “transformations with freeze” (or something like that) on bugzilla ?
And add this there.

https://bugs.llvm.org/show_bug.cgi?id=49930