This is an archive of the discontinued LLVM Phabricator instance.

[LSR] Don't consider users of constant outside loop
ClosedPublic

Authored by nikic on Jul 12 2023, 5:15 AM.

Details

Summary

In CollectLoopInvariantFixupsAndFormulae(), LSR looks at users outside the loop. E.g. if we have an addrec based on %base, and %base is also used outside the loop, then we have to keep it in a register anyway, which may make it more profitable to use %base + %idx style addressing.

This reasoning doesn't hold up when the base is a constant, because the constant can be rematerialized. The lsr-memcpy.ll regressed when enabling opaque pointer, because inttoptr (i64 6442450944 to ptr) now also has a use outside the loop (previously it didn't due to a pointer type difference), and that extra "use" results in worse use of addressing modes in the loop. However, the use outside the loop actually gets rematerialized, so the alleged register saving does not occur.

This patch opts to skip users of constants entirely for this heuristic. I considered a narrower fix for only inttoptr expressions first, but as this doesn't impact any other tests, it seems better to just handle all constants.

Diff Detail

Event Timeline

nikic created this revision.Jul 12 2023, 5:15 AM
nikic requested review of this revision.Jul 12 2023, 5:15 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 12 2023, 5:15 AM
dmgreen accepted this revision.Jul 13 2023, 2:00 AM

Sounds OK to me. If you are changing all Constant's it might be worth adding a test for globals too, they are likely to be more common. I believe the same logic should apply to them too.

This revision is now accepted and ready to land.Jul 13 2023, 2:00 AM
nikic updated this revision to Diff 539921.Jul 13 2023, 3:20 AM

Add test variant with globals.

This revision was landed with ongoing or failed builds.Jul 13 2023, 3:22 AM
This revision was automatically updated to reflect the committed changes.