This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] mempcpy(d,s,n) to memcpy(d,s,n) + n
ClosedPublic

Authored by xbolva00 on Aug 5 2019, 3:08 AM.

Details

Summary

Back-end currently expands mempcpy, but middle-end should work with memcpy instead of mempcpy to enable more memcpy-optimization.

GCC backend emits mempcpy, so LLVM backend could form it too, if we know mempcpy libcall is better than memcpy + n.
https://godbolt.org/z/dOCG96

Diff Detail

Repository
rL LLVM

Event Timeline

xbolva00 created this revision.Aug 5 2019, 3:08 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 5 2019, 3:08 AM
xbolva00 edited the summary of this revision. (Show Details)Aug 5 2019, 3:35 AM
xbolva00 edited the summary of this revision. (Show Details)Aug 5 2019, 4:01 AM
xbolva00 added a reviewer: RKSimon.

Shouldn't this wait until we have confirmed the backend can create mempcpy ?

xbolva00 added a comment.EditedAug 13 2019, 8:14 AM

I dont think so.

Nowdays middle end does nothing with mempcpy, backend expands it to memcpy + n and then possibly does some memcpy opts. Middle end loses opportunities to do something better.

To sum up, with this patch nothing should regress, only improve due to middle-end opt.

I think this makes sense, do you have verified that it doesn't regress anything?

xbolva00 added a comment.EditedAug 14 2019, 2:47 PM

mempcpy is quite rare, so hard to measure it on codebases.

I wrote small C “benchmarks” and no perf changes (backend produced memcpy/inlined anyway).

Another motivation is our recent attribute annotation work so we would get all good things from memcpy. Otherwise, I would have to annotate it individually.

It seems (some gcc discussion) only x86-64 has fast mempcpy libc impl. - so I really think backend should create mempcpy under target hook.

Not sure, maybe @craig.topper has more info about mempcpy perf on x86-64.

I like that we get the attributes, let's wait and see if anyone objects.

@hjl.tools can you speak to mempcpy perf?

xbolva00 added a comment.EditedAug 17 2019, 3:53 AM

As side note, there are a few cases in real word code
https://codesearch.isocpp.org/cgi-bin/cgi_ppsearch?q=mempcpy&search=Search

where mempcpy's return value is not used.. so it is pessiimizaton to use mempcpy instead of memcpy..

This revision is now accepted and ready to land.Aug 30 2019, 2:15 PM
xbolva00 updated this revision to Diff 218227.Aug 31 2019, 11:15 AM

Rebased + more tests.

This revision was automatically updated to reflect the committed changes.