Skip to content

Commit 67cd3ed

Browse files
scottconstablenikic
authored andcommitted
[X86] Move findDeadCallerSavedReg() into X86RegisterInfo
The findDeadCallerSavedReg() function has utility outside of X86FrameLowering.cpp Differential Revision: https://reviews.llvm.org/D88924
1 parent e8b556b commit 67cd3ed

File tree

3 files changed

+58
-56
lines changed

3 files changed

+58
-56
lines changed

llvm/lib/Target/X86/X86FrameLowering.cpp

Lines changed: 2 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -148,60 +148,6 @@ static unsigned getLEArOpcode(bool IsLP64) {
148148
return IsLP64 ? X86::LEA64r : X86::LEA32r;
149149
}
150150

151-
/// findDeadCallerSavedReg - Return a caller-saved register that isn't live
152-
/// when it reaches the "return" instruction. We can then pop a stack object
153-
/// to this register without worry about clobbering it.
154-
static unsigned findDeadCallerSavedReg(MachineBasicBlock &MBB,
155-
MachineBasicBlock::iterator &MBBI,
156-
const X86RegisterInfo *TRI,
157-
bool Is64Bit) {
158-
const MachineFunction *MF = MBB.getParent();
159-
if (MF->callsEHReturn())
160-
return 0;
161-
162-
const TargetRegisterClass &AvailableRegs = *TRI->getGPRsForTailCall(*MF);
163-
164-
if (MBBI == MBB.end())
165-
return 0;
166-
167-
switch (MBBI->getOpcode()) {
168-
default: return 0;
169-
case TargetOpcode::PATCHABLE_RET:
170-
case X86::RET:
171-
case X86::RETL:
172-
case X86::RETQ:
173-
case X86::RETIL:
174-
case X86::RETIQ:
175-
case X86::TCRETURNdi:
176-
case X86::TCRETURNri:
177-
case X86::TCRETURNmi:
178-
case X86::TCRETURNdi64:
179-
case X86::TCRETURNri64:
180-
case X86::TCRETURNmi64:
181-
case X86::EH_RETURN:
182-
case X86::EH_RETURN64: {
183-
SmallSet<uint16_t, 8> Uses;
184-
for (unsigned i = 0, e = MBBI->getNumOperands(); i != e; ++i) {
185-
MachineOperand &MO = MBBI->getOperand(i);
186-
if (!MO.isReg() || MO.isDef())
187-
continue;
188-
Register Reg = MO.getReg();
189-
if (!Reg)
190-
continue;
191-
for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
192-
Uses.insert(*AI);
193-
}
194-
195-
for (auto CS : AvailableRegs)
196-
if (!Uses.count(CS) && CS != X86::RIP && CS != X86::RSP &&
197-
CS != X86::ESP)
198-
return CS;
199-
}
200-
}
201-
202-
return 0;
203-
}
204-
205151
static bool isEAXLiveIn(MachineBasicBlock &MBB) {
206152
for (MachineBasicBlock::RegisterMaskPair RegMask : MBB.liveins()) {
207153
unsigned Reg = RegMask.PhysReg;
@@ -288,7 +234,7 @@ void X86FrameLowering::emitSPUpdate(MachineBasicBlock &MBB,
288234
if (isSub && !isEAXLiveIn(MBB))
289235
Reg = Rax;
290236
else
291-
Reg = findDeadCallerSavedReg(MBB, MBBI, TRI, Is64Bit);
237+
Reg = TRI->findDeadCallerSavedReg(MBB, MBBI);
292238

293239
unsigned MovRIOpc = Is64Bit ? X86::MOV64ri : X86::MOV32ri;
294240
unsigned AddSubRROpc =
@@ -345,7 +291,7 @@ void X86FrameLowering::emitSPUpdate(MachineBasicBlock &MBB,
345291
// need to find a dead register when using pop.
346292
unsigned Reg = isSub
347293
? (unsigned)(Is64Bit ? X86::RAX : X86::EAX)
348-
: findDeadCallerSavedReg(MBB, MBBI, TRI, Is64Bit);
294+
: TRI->findDeadCallerSavedReg(MBB, MBBI);
349295
if (Reg) {
350296
unsigned Opc = isSub
351297
? (Is64Bit ? X86::PUSH64r : X86::PUSH32r)

llvm/lib/Target/X86/X86RegisterInfo.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "X86Subtarget.h"
1919
#include "llvm/ADT/BitVector.h"
2020
#include "llvm/ADT/STLExtras.h"
21+
#include "llvm/ADT/SmallSet.h"
2122
#include "llvm/CodeGen/MachineFrameInfo.h"
2223
#include "llvm/CodeGen/MachineFunction.h"
2324
#include "llvm/CodeGen/MachineFunctionPass.h"
@@ -790,6 +791,55 @@ X86RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
790791
}
791792
}
792793

794+
unsigned X86RegisterInfo::findDeadCallerSavedReg(
795+
MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI) const {
796+
const MachineFunction *MF = MBB.getParent();
797+
if (MF->callsEHReturn())
798+
return 0;
799+
800+
const TargetRegisterClass &AvailableRegs = *getGPRsForTailCall(*MF);
801+
802+
if (MBBI == MBB.end())
803+
return 0;
804+
805+
switch (MBBI->getOpcode()) {
806+
default:
807+
return 0;
808+
case TargetOpcode::PATCHABLE_RET:
809+
case X86::RET:
810+
case X86::RETL:
811+
case X86::RETQ:
812+
case X86::RETIL:
813+
case X86::RETIQ:
814+
case X86::TCRETURNdi:
815+
case X86::TCRETURNri:
816+
case X86::TCRETURNmi:
817+
case X86::TCRETURNdi64:
818+
case X86::TCRETURNri64:
819+
case X86::TCRETURNmi64:
820+
case X86::EH_RETURN:
821+
case X86::EH_RETURN64: {
822+
SmallSet<uint16_t, 8> Uses;
823+
for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) {
824+
MachineOperand &MO = MBBI->getOperand(I);
825+
if (!MO.isReg() || MO.isDef())
826+
continue;
827+
Register Reg = MO.getReg();
828+
if (!Reg)
829+
continue;
830+
for (MCRegAliasIterator AI(Reg, this, true); AI.isValid(); ++AI)
831+
Uses.insert(*AI);
832+
}
833+
834+
for (auto CS : AvailableRegs)
835+
if (!Uses.count(CS) && CS != X86::RIP && CS != X86::RSP && CS != X86::ESP)
836+
return CS;
837+
}
838+
}
839+
840+
return 0;
841+
}
842+
793843
Register X86RegisterInfo::getFrameRegister(const MachineFunction &MF) const {
794844
const X86FrameLowering *TFI = getFrameLowering(MF);
795845
return TFI->hasFP(MF) ? FramePtr : StackPtr;

llvm/lib/Target/X86/X86RegisterInfo.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ class X86RegisterInfo final : public X86GenRegisterInfo {
128128
int SPAdj, unsigned FIOperandNum,
129129
RegScavenger *RS = nullptr) const override;
130130

131+
/// findDeadCallerSavedReg - Return a caller-saved register that isn't live
132+
/// when it reaches the "return" instruction. We can then pop a stack object
133+
/// to this register without worry about clobbering it.
134+
unsigned findDeadCallerSavedReg(MachineBasicBlock &MBB,
135+
MachineBasicBlock::iterator &MBBI) const;
136+
131137
// Debug information queries.
132138
Register getFrameRegister(const MachineFunction &MF) const override;
133139
unsigned getPtrSizedFrameRegister(const MachineFunction &MF) const;

0 commit comments

Comments
 (0)