1 //===- X86RegisterInfo.cpp - X86 Register Information -----------*- C++ -*-===//
3 // This file contains the X86 implementation of the MRegisterInfo class. This
4 // file is responsible for the frame pointer elimination optimization on X86.
6 //===----------------------------------------------------------------------===//
9 #include "X86RegisterInfo.h"
10 #include "X86InstrBuilder.h"
11 #include "llvm/Constants.h"
12 #include "llvm/Type.h"
13 #include "llvm/CodeGen/MachineInstrBuilder.h"
14 #include "llvm/CodeGen/MachineFunction.h"
15 #include "llvm/CodeGen/MachineFrameInfo.h"
16 #include "Support/CommandLine.h"
20 NoFPElim("no-fp-elim",
21 cl::desc("Disable frame pointer elimination optimization"));
24 static unsigned getIdx(const TargetRegisterClass *RC) {
25 switch (RC->getSize()) {
26 default: assert(0 && "Invalid data size!");
34 void X86RegisterInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
35 MachineBasicBlock::iterator &MBBI,
36 unsigned SrcReg, int FrameIdx,
37 const TargetRegisterClass *RC) const {
38 static const unsigned Opcode[] =
39 { X86::MOVrm8, X86::MOVrm16, X86::MOVrm32, X86::FSTPr80 };
40 MachineInstr *MI = addFrameReference(BuildMI(Opcode[getIdx(RC)], 5),
41 FrameIdx).addReg(SrcReg);
42 MBBI = MBB.insert(MBBI, MI)+1;
45 void X86RegisterInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
46 MachineBasicBlock::iterator &MBBI,
47 unsigned DestReg, int FrameIdx,
48 const TargetRegisterClass *RC) const{
49 static const unsigned Opcode[] =
50 { X86::MOVmr8, X86::MOVmr16, X86::MOVmr32, X86::FLDr80 };
51 MachineInstr *MI = addFrameReference(BuildMI(Opcode[getIdx(RC)], 4, DestReg),
53 MBBI = MBB.insert(MBBI, MI)+1;
56 void X86RegisterInfo::copyRegToReg(MachineBasicBlock &MBB,
57 MachineBasicBlock::iterator &MBBI,
58 unsigned DestReg, unsigned SrcReg,
59 const TargetRegisterClass *RC) const {
60 static const unsigned Opcode[] =
61 { X86::MOVrr8, X86::MOVrr16, X86::MOVrr32, X86::FpMOV };
62 MachineInstr *MI = BuildMI(Opcode[getIdx(RC)],1,DestReg).addReg(SrcReg);
63 MBBI = MBB.insert(MBBI, MI)+1;
66 const unsigned* X86RegisterInfo::getCalleeSaveRegs() const {
67 static const unsigned CalleeSaveRegs[] = {
68 X86::ESI, X86::EDI, X86::EBX, X86::EBP, 0
70 return CalleeSaveRegs;
74 //===----------------------------------------------------------------------===//
75 // Stack Frame Processing methods
76 //===----------------------------------------------------------------------===//
78 // hasFP - Return true if the specified function should have a dedicated frame
79 // pointer register. This is true if the function has variable sized allocas or
80 // if frame pointer elimination is disabled.
82 static bool hasFP(MachineFunction &MF) {
83 return NoFPElim || MF.getFrameInfo()->hasVarSizedObjects();
86 // hasSPAdjust - Return true if this function has ESP adjustment instructions in
87 // the prolog and epilog which allocate local stack space. This is neccesary
88 // because we elide these instructions if there are no function calls in the
89 // current function (ie, this is a leaf function). In this case, we can refer
90 // beyond the stack pointer because we know that nothing will trample on that
93 static bool hasSPAdjust(MachineFunction &MF) {
94 assert(!hasFP(MF) && "Can only eliminate SP adjustment if no frame-pointer!");
95 return MF.getFrameInfo()->hasCalls();
98 void X86RegisterInfo::eliminateCallFramePseudoInstr(MachineFunction &MF,
99 MachineBasicBlock &MBB,
100 MachineBasicBlock::iterator &I) const {
101 MachineInstr *New = 0, *Old = *I;;
103 // If we have a frame pointer, turn the adjcallstackup instruction into a
104 // 'sub ESP, <amt>' and the adjcallstackdown instruction into 'add ESP,
106 unsigned Amount = Old->getOperand(0).getImmedValue();
108 if (Old->getOpcode() == X86::ADJCALLSTACKDOWN) {
109 New=BuildMI(X86::SUBri32, 2, X86::ESP).addReg(X86::ESP).addZImm(Amount);
111 assert(Old->getOpcode() == X86::ADJCALLSTACKUP);
112 New=BuildMI(X86::ADDri32, 2, X86::ESP).addReg(X86::ESP).addZImm(Amount);
118 *I = New; // Replace the pseudo instruction with a new instruction...
120 I = MBB.erase(I);// Just delete the pseudo instruction...
124 void X86RegisterInfo::eliminateFrameIndex(MachineFunction &MF,
125 MachineBasicBlock::iterator &II) const {
127 MachineInstr &MI = **II;
128 while (!MI.getOperand(i).isFrameIndex()) {
130 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
133 // This must be part of a four operand memory reference. Replace the
134 // FrameIndex with the offset and the base register with EBP.
135 MI.SetMachineOperandReg(i-3, hasFP(MF) ? X86::EBP : X86::ESP);
137 // Now replace the frame index itself with the offset from EBP.
138 int FrameIndex = MI.getOperand(i).getFrameIndex();
139 int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex);
141 if (!hasFP(MF) && hasSPAdjust(MF)) {
142 const MachineFrameInfo *MFI = MF.getFrameInfo();
143 Offset += MFI->getStackSize() + MFI->getMaxCallFrameSize();
146 MI.SetMachineOperandConst(i, MachineOperand::MO_SignExtendedImmed, Offset);
149 void X86RegisterInfo::processFunctionBeforeFrameFinalized(MachineFunction &MF)
152 // Create a frame entry for the EBP register that must be saved.
153 int FrameIdx = MF.getFrameInfo()->CreateStackObject(4, 4);
154 assert(FrameIdx == MF.getFrameInfo()->getObjectIndexEnd()-1 &&
155 "Slot for EBP register must be last in order to be found!");
159 void X86RegisterInfo::emitPrologue(MachineFunction &MF) const {
160 MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB
161 MachineBasicBlock::iterator MBBI = MBB.begin();
162 const MachineFrameInfo *MFI = MF.getFrameInfo();
165 // Get the number of bytes to allocate from the FrameInfo
166 unsigned NumBytes = MFI->getStackSize();
168 // Get the offset of the stack slot for the EBP register... which is
169 // guaranteed to be the last slot by processFunctionBeforeFrameFinalized.
170 int EBPOffset = MFI->getObjectOffset(MFI->getObjectIndexEnd()-1);
172 MI = addRegOffset(BuildMI(X86::MOVrm32, 5), // mov [ESP-<offset>], EBP
173 X86::ESP, EBPOffset).addReg(X86::EBP);
174 MBBI = MBB.insert(MBBI, MI)+1;
176 MI = BuildMI(X86::MOVrr32, 2, X86::EBP).addReg(X86::ESP);
177 MBBI = MBB.insert(MBBI, MI)+1;
179 // If we don't have a frame pointer, and the function contains no call sites
180 // (it's a leaf function), we don't have to emit ANY stack adjustment
181 // instructions at all, we can just refer to the area beyond the stack
182 // pointer. This can be important for small functions.
184 if (!hasSPAdjust(MF)) return;
186 // When we have no frame pointer, we reserve argument space for call sites
187 // in the function immediately on entry to the current function. This
188 // eliminates the need for add/sub ESP brackets around call sites.
190 NumBytes += MFI->getMaxCallFrameSize();
194 // adjust stack pointer: ESP -= numbytes
195 MI = BuildMI(X86::SUBri32, 2, X86::ESP).addReg(X86::ESP).addZImm(NumBytes);
196 MBBI = 1+MBB.insert(MBBI, MI);
200 void X86RegisterInfo::emitEpilogue(MachineFunction &MF,
201 MachineBasicBlock &MBB) const {
202 const MachineFrameInfo *MFI = MF.getFrameInfo();
203 MachineBasicBlock::iterator MBBI = MBB.end()-1;
205 assert((*MBBI)->getOpcode() == X86::RET &&
206 "Can only insert epilog into returning blocks");
209 // Get the offset of the stack slot for the EBP register... which is
210 // guaranteed to be the last slot by processFunctionBeforeFrameFinalized.
211 int EBPOffset = MFI->getObjectOffset(MFI->getObjectIndexEnd()-1);
214 MI = BuildMI(X86::MOVrr32, 1,X86::ESP).addReg(X86::EBP);
215 MBBI = 1+MBB.insert(MBBI, MI);
217 // mov EBP, [ESP-<offset>]
218 MI = addRegOffset(BuildMI(X86::MOVmr32, 5, X86::EBP), X86::ESP, EBPOffset);
219 MBBI = 1+MBB.insert(MBBI, MI);
221 if (!hasSPAdjust(MF)) return;
223 // Get the number of bytes allocated from the FrameInfo...
224 unsigned NumBytes = MFI->getStackSize();
225 NumBytes += MFI->getMaxCallFrameSize();
227 if (NumBytes) { // adjust stack pointer back: ESP += numbytes
228 MI =BuildMI(X86::ADDri32, 2, X86::ESP).addReg(X86::ESP).addZImm(NumBytes);
229 MBBI = 1+MBB.insert(MBBI, MI);
235 //===----------------------------------------------------------------------===//
236 // Register Class Implementation Code
237 //===----------------------------------------------------------------------===//
239 //===----------------------------------------------------------------------===//
240 // 8 Bit Integer Registers
243 const unsigned ByteRegClassRegs[] = {
244 X86::AL, X86::CL, X86::DL, X86::BL, X86::AH, X86::CH, X86::DH, X86::BH,
247 TargetRegisterClass X86ByteRegisterClassInstance(1, 1, ByteRegClassRegs,
248 ByteRegClassRegs+sizeof(ByteRegClassRegs)/sizeof(ByteRegClassRegs[0]));
250 //===----------------------------------------------------------------------===//
251 // 16 Bit Integer Registers
253 const unsigned ShortRegClassRegs[] = {
254 X86::AX, X86::CX, X86::DX, X86::BX, X86::SI, X86::DI, X86::BP, X86::SP
257 struct R16CL : public TargetRegisterClass {
258 R16CL():TargetRegisterClass(2, 2, ShortRegClassRegs, ShortRegClassRegs+8) {}
259 iterator allocation_order_end(MachineFunction &MF) const {
260 if (hasFP(MF)) // Does the function dedicate EBP to being a frame ptr?
261 return end()-2; // Don't allocate SP or BP
263 return end()-1; // Don't allocate SP
265 } X86ShortRegisterClassInstance;
267 //===----------------------------------------------------------------------===//
268 // 32 Bit Integer Registers
270 const unsigned IntRegClassRegs[] = {
271 X86::EAX, X86::ECX, X86::EDX, X86::EBX,
272 X86::ESI, X86::EDI, X86::EBP, X86::ESP
275 struct R32CL : public TargetRegisterClass {
276 R32CL() : TargetRegisterClass(4, 4, IntRegClassRegs, IntRegClassRegs+8) {}
277 iterator allocation_order_end(MachineFunction &MF) const {
278 if (hasFP(MF)) // Does the function dedicate EBP to being a frame ptr?
279 return end()-2; // Don't allocate ESP or EBP
281 return end()-1; // Don't allocate ESP
283 } X86IntRegisterClassInstance;
285 //===----------------------------------------------------------------------===//
286 // Pseudo Floating Point Registers
288 const unsigned PFPRegClassRegs[] = {
289 #define PFP(ENUM, NAME, FLAGS, TSFLAGS, ALIAS_SET) X86::ENUM,
290 #include "X86RegisterInfo.def"
293 TargetRegisterClass X86FPRegisterClassInstance(10, 4, PFPRegClassRegs,
294 PFPRegClassRegs+sizeof(PFPRegClassRegs)/sizeof(PFPRegClassRegs[0]));
296 //===----------------------------------------------------------------------===//
297 // Register class array...
299 const TargetRegisterClass * const X86RegClasses[] = {
300 &X86ByteRegisterClassInstance,
301 &X86ShortRegisterClassInstance,
302 &X86IntRegisterClassInstance,
303 &X86FPRegisterClassInstance,
308 // Create static lists to contain register alias sets...
309 #define ALIASLIST(NAME, ...) \
310 static const unsigned NAME[] = { __VA_ARGS__ };
311 #include "X86RegisterInfo.def"
314 // X86Regs - Turn the X86RegisterInfo.def file into a bunch of register
317 static const MRegisterDesc X86Regs[] = {
318 #define R(ENUM, NAME, FLAGS, TSFLAGS, ALIAS_SET) \
319 { NAME, ALIAS_SET, FLAGS, TSFLAGS },
320 #include "X86RegisterInfo.def"
323 X86RegisterInfo::X86RegisterInfo()
324 : MRegisterInfo(X86Regs, sizeof(X86Regs)/sizeof(X86Regs[0]),
326 X86RegClasses+sizeof(X86RegClasses)/sizeof(X86RegClasses[0]),
327 X86::ADJCALLSTACKDOWN, X86::ADJCALLSTACKUP) {
332 const TargetRegisterClass*
333 X86RegisterInfo::getRegClassForType(const Type* Ty) const {
334 switch (Ty->getPrimitiveID()) {
335 default: assert(0 && "Invalid type to getClass!");
337 case Type::SByteTyID:
338 case Type::UByteTyID: return &X86ByteRegisterClassInstance;
339 case Type::ShortTyID:
340 case Type::UShortTyID: return &X86ShortRegisterClassInstance;
341 case Type::LongTyID: // FIXME: Longs are not handled yet!
342 case Type::ULongTyID: // FIXME: Treat these like ints, this is bogus!
346 case Type::PointerTyID: return &X86IntRegisterClassInstance;
348 case Type::FloatTyID:
349 case Type::DoubleTyID: return &X86FPRegisterClassInstance;