This is an archive of the discontinued LLVM Phabricator instance.

[PowerPC][Future] Remove redundant r2 save and restore for indirect call
ClosedPublic

Authored by NeHuang on Apr 8 2020, 12:19 PM.

Details

Summary

Currently an indirect call produces the following sequence on PCRelative mode:

extern void function( );
extern void (*ptrfunc) ( );

void g() {
  ptrfunc=function;
}

void f() {
  (*ptrfunc) ( );
}

Produce

	paddi 3, 0, .LC0@PCREL, 1
	ld 3, 0(3)
	std 2, 24(1)
	ld 12, 0(3)
	mtctr 12
	bctrl
	ld 2, 24(1)

Though the caller does not use or preserve r2, it is still saved and restored across a function call.
This patch is added to remove these redundant save and restores for indirect call.

Diff Detail

Event Timeline

NeHuang created this revision.Apr 8 2020, 12:19 PM

There are 3 places that are concerned with whether or not we need to save/restore the TOC. There is certainly a possibility of these conditions diverging. Can you please write a small function that checks this and then use that in all 3 places?
Maybe something like

static inline bool isTOCSaveRequired(const PPCSubtarget &Subtarget) {
  return Subtarget.isAIXABI() ||
         (Subtarget.is64BitELFABI() && !Subtarget.isUsingPCRelativeCalls());
}

I suppose we don't really have to use it in LowerCall_64SVR4() since there we have already checked that we are on a 64-bit ELF, but I think it doesn't hurt to just use the same query everywhere.

Also, I think it would be useful to add a test case where the function pointer is passed as a parameter.

llvm/lib/Target/PowerPC/PPCISelLowering.cpp
5426–5428

I don't think we should say "disable the code". Maybe:
// For ELFv2 ABI with PCRel, do not restore the TOC as it is not saved or used.

NeHuang updated this revision to Diff 257526.Apr 14 2020, 3:32 PM

Addressed review comment.

NeHuang marked an inline comment as done.Apr 14 2020, 3:33 PM
amyk added a subscriber: amyk.Apr 14 2020, 8:23 PM
amyk added inline comments.
llvm/lib/Target/PowerPC/PPCISelLowering.cpp
5141

Is it worth mentioning AIX in the comment too, since the check also considers AIX?

6499

nit: End with period.

llvm/test/CodeGen/PowerPC/pcrel-indirect-call.ll
4

I don't believe the FileCheck prefix is needed if you wanted to prefix to say CHECK.
CHECK should be the default if no prefix is supplied.

NeHuang updated this revision to Diff 257790.Apr 15 2020, 11:59 AM

Address review comments.

NeHuang marked 3 inline comments as done.Apr 15 2020, 11:59 AM
nemanjai accepted this revision.Apr 15 2020, 3:23 PM

Minor change to the comment can be made on the commit. LGTM.

llvm/lib/Target/PowerPC/PPCISelLowering.cpp
5141

// AIX and 64-bit ELF ABIs w/o PCRel require a TOC save/restore around calls.

This revision is now accepted and ready to land.Apr 15 2020, 3:23 PM
NeHuang updated this revision to Diff 258066.Apr 16 2020, 8:47 AM

Address review comment.

NeHuang marked an inline comment as done.Apr 16 2020, 8:47 AM
NeHuang updated this revision to Diff 258071.Apr 16 2020, 9:00 AM
NeHuang updated this revision to Diff 258832.Apr 20 2020, 1:42 PM

Update "pcrel-call-linkage-with-calls.ll" after re-basing the branch.

This revision was automatically updated to reflect the committed changes.