This is an archive of the discontinued LLVM Phabricator instance.

[LICM] Reassociate GEPs to allow hoisting
ClosedPublic

Authored by nikic on Mar 24 2023, 7:47 AM.

Details

Summary

Reassociate gep (gep ptr, idx1), idx2 to gep (gep ptr, idx2), idx1 if this would make the inner GEP loop invariant and thus hoistable.

This is intended to replace an InstCombine fold that does this (in https://github.com/llvm/llvm-project/blob/04f61fb73dc6a994ab267d431f2fdaedc67430ff/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp#L2006). The problem with the InstCombine fold is that LoopInfo is an optional dependency, so it is not performed reliably.

Diff Detail

Event Timeline

nikic created this revision.Mar 24 2023, 7:47 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 24 2023, 7:47 AM
nikic requested review of this revision.Mar 24 2023, 7:47 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 24 2023, 7:47 AM
mkazantsev added inline comments.Mar 27 2023, 7:55 PM
llvm/lib/Transforms/Scalar/LICM.cpp
2535

Can you please add a test where GEP and Src's indices have different dimensions? I'm a bit worried about type compatibility here.

mkazantsev added inline comments.Mar 27 2023, 8:00 PM
llvm/lib/Transforms/Scalar/LICM.cpp
2539

Give them some name maybe?

nikic updated this revision to Diff 509667.Mar 30 2023, 7:17 AM

Name instructions, add extra test.

nikic marked an inline comment as done.Mar 30 2023, 7:18 AM
nikic added inline comments.
llvm/lib/Transforms/Scalar/LICM.cpp
2535

There is a test for different source element types in @different_elem_types and I've added @different_index_types with different index types. Not sure whether these are what you had in mind...

nikic updated this revision to Diff 509940.Mar 31 2023, 1:24 AM

Add another test with different number of indices.

mkazantsev accepted this revision.Apr 6 2023, 10:24 PM
mkazantsev added inline comments.
llvm/lib/Transforms/Scalar/LICM.cpp
2535

I meant something like:

Src = gep ptr %src_p, i32 %i1, i32 %i2

GEP = gep ptr %gep_p, i32 %j1

different_index_count will do, thanks!

This revision is now accepted and ready to land.Apr 6 2023, 10:24 PM
This revision was automatically updated to reflect the committed changes.