llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Redundant save and restore the GPR and SPE registers .

Open ukalappa-mips opened this issue 4 years ago • 12 comments

The following code has the redundant save and restore of r29 and r30 GPR's in PPC32.

https://godbolt.org/z/dxE36MG99

ukalappa-mips avatar Aug 23 '22 14:08 ukalappa-mips

@llvm/issue-subscribers-backend-powerpc

llvmbot avatar Aug 23 '22 14:08 llvmbot

@chmeeedalf It certainly does seem odd that we have the evstdd/evldd for these registers in 32-bit mode. And it is certainly odd that we spill/restore them as 32-bit registers as well as 64-bit registers.

nemanjai avatar Aug 24 '22 10:08 nemanjai

@nemanjai you're right, S29 and S30 should be removed from the CalleeSavedRegs block in PPCCallingConv.td R30 looks to always be in use for something, and R29 is only in use when PIC, otherwise it's free for general use. We could either have a PIC block, or always reserve R29 from SPE.

chmeeedalf avatar Aug 24 '22 13:08 chmeeedalf

@chmeeedalf <<always reserve R29 from SPE

that's the case for R29 ,but what about the other callee (non-volatile) save regs …its insane to save in 32 bit mode and 64 bit mode too .

ukalappa-mips avatar Aug 24 '22 13:08 ukalappa-mips

Someone (forget who) mentioned a couple years ago making it save only the super registers, not the subregs. It's still not done this way yet? Saving 64-bit only seems excessive in many cases when only 32-bit is used, but I don't know how to make it determine that only the subreg is used.

chmeeedalf avatar Aug 24 '22 14:08 chmeeedalf

@chmeeedalf
<<making it save only the super registers Does this super spe registers are non volatile across the call ?

ukalappa-mips avatar Aug 24 '22 14:08 ukalappa-mips

@UmeshKalappa0 the super registers have the same volatility as the subregs, and integer operations should only touch the subregs, per the reference:

According to the E500CORERM.pdf page 2-1 (2.1 Overview)

General-purpose registers (GPRs)—Used as source and destination operands for most operations. The e500 implements 64-bit GPRs. Book E–defined instructions access only the lower word; SPE vector instructions and embedded vector single-precision and double-precision floating-point APUs (e500v2 only) use all 64 bits. See Section 2.3.1, “General-Purpose Registers (GPRs).”

Section 2.3.1 shows:

Each GPR is a 64-bit register and can be used to contain address and integer data, although all instructions except SPE APU instructions, double-precision embedded floating-point instructions (e500v2 only), and single-precision embedded vector floating-point instructions use and return 32-bit values in GPR bits 32–63.

So ideally we should be able to just save the subregs when we only need the subregs, and the super regs when we need the full super regs.

chmeeedalf avatar Aug 24 '22 15:08 chmeeedalf

@chmeeedalf and @nemanjai ,https://godbolt.org/z/bbM44fcvM ,still we can use stw and ld ,instead of evstdd etc ,that reduce the stack space .

ukalappa-mips avatar Nov 23 '22 09:11 ukalappa-mips

@UmeshKalappa0 I agree. I thought your work in 975f4f2ddb would have addressed at least part of that.

Can it be reduced even further, by looking at the usage of the registers to decide whether to save upper or just lower?

chmeeedalf avatar Nov 23 '22 19:11 chmeeedalf

Can it be reduced even further, by looking at the usage of the registers to decide whether to save upper or just lower? @chmeeedalf ,does PPC clang has like sub-registers class for these SPE registers ,if not we need to implement the same ,so that we know its high or low reg of the given SPE register ?

ukalappa-mips avatar May 15 '23 08:05 ukalappa-mips

Someone (forget who) mentioned a couple years ago making it save only the super registers, not the subregs. It's still not done this way yet? Saving 64-bit only seems excessive in many cases when only 32-bit is used, but I don't know how to make it determine that only the subreg is used.

That was me, committed in 2021 in time for LLVM 14 https://github.com/llvm/llvm-project/commit/95ef464ac9d1972953709c57449ac178771cd221

jrtc27 avatar May 15 '23 16:05 jrtc27

Can it be reduced even further, by looking at the usage of the registers to decide whether to save upper or just lower? @chmeeedalf ,does PPC clang has like sub-registers class for these SPE registers ,if not we need to implement the same ,so that we know its high or low reg of the given SPE register ?

The SPE registers have S* and R*, with R* being sub-registers of S*. @jrtc27 mentioned in the review for 95ef464 that there's no way to track subreg and superreg separately. SPE would need to be a special case, as it's the only target I'm aware of that does treat the subreg as distinct from the superreg.

If there's a way to mark in the instruction what registers get clobbered, and trace the usage, such that if only Rn gets clobbered only save Rn, not Sn, then we'd be in good shape. Is there such a capability? Some kind of liveness marker for the function?

chmeeedalf avatar May 15 '23 17:05 chmeeedalf