This is an archive of the discontinued LLVM Phabricator instance.

[LoopSimplify] Restoring the normalization of br instructions.
Needs RevisionPublic

Authored by dbakunevich on Dec 12 2022, 12:48 PM.

Details

Summary

As part of a particular optimization (InstCombine for example),
the situations may occur when we exit the loop on the true branch.
This transformation breaks a number of optimizations.
Therefore, it was decided to restore such instructions.

Diff Detail

Event Timeline

dbakunevich created this revision.Dec 12 2022, 12:48 PM
Herald added a project: Restricted Project. · View Herald TranscriptDec 12 2022, 12:48 PM
Herald added a subscriber: hiraditya. · View Herald Transcript
dbakunevich requested review of this revision.Dec 12 2022, 12:48 PM
Herald added a project: Restricted Project. · View Herald TranscriptDec 12 2022, 12:48 PM
lebedev.ri requested changes to this revision.Dec 12 2022, 12:52 PM
lebedev.ri added a subscriber: lebedev.ri.

You did not invert the branch condition.

This revision now requires changes to proceed.Dec 12 2022, 12:52 PM

You did not invert the branch condition.

Fixed it

lebedev.ri requested changes to this revision.Dec 12 2022, 1:52 PM

And what if the condition had other users?
And what if the condition was not relational?

Please just insert xor %cond, -1.
This is missing tests (please precommit) that now are getting optimized.

This revision now requires changes to proceed.Dec 12 2022, 1:52 PM
nikic added a subscriber: nikic.Dec 12 2022, 2:06 PM

This is almost certainly not the correct way to fix whatever problem you're trying to fix. If an optimization doesn't work with commuted branch targets, then that optimization needs to be fixed.

mkazantsev requested changes to this revision.Dec 14 2022, 12:22 AM

This is almost certainly not the correct way to fix whatever problem you're trying to fix. If an optimization doesn't work with commuted branch targets, then that optimization needs to be fixed.

There is always a tradeoff between having a canonical form and expecting this canonical form in transforms, and trying to support everything. I've seen enough cases where exit by false isn't [fully] supported, so saying that "fallthrough stays in loop is our canonical form" isn't a that bad idea in general.

Besides, even if it's not about the optimizations, codegen will still do something similar when it tries to lay out the blocks. So generally I like the idea of this patch (though I think the place for doing it is wrong).

llvm/lib/Transforms/Utils/LoopSimplify.cpp
543

How is that even possible?
UB here.

545

m_match(BI, m_Br(m_ICmp (...) )

548

Changed = true somewhere?

lebedev.ri resigned from this revision.Jan 12 2023, 4:59 PM

This review may be stuck/dead, consider abandoning if no longer relevant.
Removing myself as reviewer in attempt to clean dashboard.