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/ValueTypes.h"
14 #include "llvm/CodeGen/MachineInstrBuilder.h"
15 #include "llvm/CodeGen/MachineFunction.h"
16 #include "llvm/CodeGen/MachineFrameInfo.h"
17 #include "llvm/Target/TargetMachine.h"
18 #include "llvm/Target/TargetFrameInfo.h"
19 #include "Support/CommandLine.h"
23 NoFPElim("disable-fp-elim",
24 cl::desc("Disable frame pointer elimination optimization"));
27 X86RegisterInfo::X86RegisterInfo()
28 : X86GenRegisterInfo(X86::ADJCALLSTACKDOWN, X86::ADJCALLSTACKUP) {}
30 static unsigned getIdx(const TargetRegisterClass *RC) {
31 switch (RC->getSize()) {
32 default: assert(0 && "Invalid data size!");
40 void X86RegisterInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
41 MachineBasicBlock::iterator &MBBI,
42 unsigned SrcReg, int FrameIdx,
43 const TargetRegisterClass *RC) const {
44 static const unsigned Opcode[] =
45 { X86::MOVrm8, X86::MOVrm16, X86::MOVrm32, X86::FSTPr80 };
46 MachineInstr *MI = addFrameReference(BuildMI(Opcode[getIdx(RC)], 5),
47 FrameIdx).addReg(SrcReg);
48 MBBI = MBB.insert(MBBI, MI)+1;
51 void X86RegisterInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
52 MachineBasicBlock::iterator &MBBI,
53 unsigned DestReg, int FrameIdx,
54 const TargetRegisterClass *RC) const{
55 static const unsigned Opcode[] =
56 { X86::MOVmr8, X86::MOVmr16, X86::MOVmr32, X86::FLDr80 };
57 MachineInstr *MI = addFrameReference(BuildMI(Opcode[getIdx(RC)], 4, DestReg),
59 MBBI = MBB.insert(MBBI, MI)+1;
62 void X86RegisterInfo::copyRegToReg(MachineBasicBlock &MBB,
63 MachineBasicBlock::iterator &MBBI,
64 unsigned DestReg, unsigned SrcReg,
65 const TargetRegisterClass *RC) const {
66 static const unsigned Opcode[] =
67 { X86::MOVrr8, X86::MOVrr16, X86::MOVrr32, X86::FpMOV };
68 MachineInstr *MI = BuildMI(Opcode[getIdx(RC)],1,DestReg).addReg(SrcReg);
69 MBBI = MBB.insert(MBBI, MI)+1;
72 //===----------------------------------------------------------------------===//
73 // Stack Frame Processing methods
74 //===----------------------------------------------------------------------===//
76 // hasFP - Return true if the specified function should have a dedicated frame
77 // pointer register. This is true if the function has variable sized allocas or
78 // if frame pointer elimination is disabled.
80 static bool hasFP(MachineFunction &MF) {
81 return NoFPElim || MF.getFrameInfo()->hasVarSizedObjects();
84 // hasSPAdjust - Return true if this function has ESP adjustment instructions in
85 // the prolog and epilog which allocate local stack space. This is neccesary
86 // because we elide these instructions if there are no function calls in the
87 // current function (ie, this is a leaf function). In this case, we can refer
88 // beyond the stack pointer because we know that nothing will trample on that
91 static bool hasSPAdjust(MachineFunction &MF) {
92 assert(!hasFP(MF) && "Can only eliminate SP adjustment if no frame-pointer!");
93 return MF.getFrameInfo()->hasCalls();
96 void X86RegisterInfo::eliminateCallFramePseudoInstr(MachineFunction &MF,
97 MachineBasicBlock &MBB,
98 MachineBasicBlock::iterator &I) const {
99 MachineInstr *New = 0, *Old = *I;;
101 // If we have a frame pointer, turn the adjcallstackup instruction into a
102 // 'sub ESP, <amt>' and the adjcallstackdown instruction into 'add ESP,
104 unsigned Amount = Old->getOperand(0).getImmedValue();
106 // We need to keep the stack aligned properly. To do this, we round the
107 // amount of space needed for the outgoing arguments up to the next
108 // alignment boundary.
109 unsigned Align = MF.getTarget().getFrameInfo().getStackAlignment();
110 Amount = (Amount+Align-1)/Align*Align;
112 if (Old->getOpcode() == X86::ADJCALLSTACKDOWN) {
113 New=BuildMI(X86::SUBri32, 2, X86::ESP).addReg(X86::ESP).addZImm(Amount);
115 assert(Old->getOpcode() == X86::ADJCALLSTACKUP);
116 New=BuildMI(X86::ADDri32, 2, X86::ESP).addReg(X86::ESP).addZImm(Amount);
122 *I = New; // Replace the pseudo instruction with a new instruction...
124 I = MBB.erase(I);// Just delete the pseudo instruction...
128 void X86RegisterInfo::eliminateFrameIndex(MachineFunction &MF,
129 MachineBasicBlock::iterator &II) const {
131 MachineInstr &MI = **II;
132 while (!MI.getOperand(i).isFrameIndex()) {
134 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
137 int FrameIndex = MI.getOperand(i).getFrameIndex();
139 // This must be part of a four operand memory reference. Replace the
140 // FrameIndex with base register with EBP. Add add an offset to the offset.
141 MI.SetMachineOperandReg(i, hasFP(MF) ? X86::EBP : X86::ESP);
143 // Now add the frame object offset to the offset from EBP.
144 int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex) +
145 MI.getOperand(i+3).getImmedValue()+4;
147 if (!hasFP(MF) && hasSPAdjust(MF)) {
148 const MachineFrameInfo *MFI = MF.getFrameInfo();
149 Offset += MFI->getStackSize();
152 MI.SetMachineOperandConst(i+3, MachineOperand::MO_SignExtendedImmed, Offset);
155 void X86RegisterInfo::processFunctionBeforeFrameFinalized(MachineFunction &MF)
158 // Create a frame entry for the EBP register that must be saved.
159 int FrameIdx = MF.getFrameInfo()->CreateStackObject(4, 4);
160 assert(FrameIdx == MF.getFrameInfo()->getObjectIndexEnd()-1 &&
161 "Slot for EBP register must be last in order to be found!");
165 void X86RegisterInfo::emitPrologue(MachineFunction &MF) const {
166 MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB
167 MachineBasicBlock::iterator MBBI = MBB.begin();
168 MachineFrameInfo *MFI = MF.getFrameInfo();
171 // Get the number of bytes to allocate from the FrameInfo
172 unsigned NumBytes = MFI->getStackSize();
174 // Get the offset of the stack slot for the EBP register... which is
175 // guaranteed to be the last slot by processFunctionBeforeFrameFinalized.
176 int EBPOffset = MFI->getObjectOffset(MFI->getObjectIndexEnd()-1)+4;
178 MI = addRegOffset(BuildMI(X86::MOVrm32, 5), // mov [ESP-<offset>], EBP
179 X86::ESP, EBPOffset).addReg(X86::EBP);
180 MBBI = MBB.insert(MBBI, MI)+1;
182 MI = BuildMI(X86::MOVrr32, 2, X86::EBP).addReg(X86::ESP);
183 MBBI = MBB.insert(MBBI, MI)+1;
185 // If we don't have a frame pointer, and the function contains no call sites
186 // (it's a leaf function), we don't have to emit ANY stack adjustment
187 // instructions at all, we can just refer to the area beyond the stack
188 // pointer. This can be important for small functions.
190 if (!hasSPAdjust(MF)) return;
192 // When we have no frame pointer, we reserve argument space for call sites
193 // in the function immediately on entry to the current function. This
194 // eliminates the need for add/sub ESP brackets around call sites.
196 NumBytes += MFI->getMaxCallFrameSize();
198 // Round the size to a multiple of the alignment (don't forget the 4 byte
200 unsigned Align = MF.getTarget().getFrameInfo().getStackAlignment();
201 NumBytes = ((NumBytes+4)+Align-1)/Align*Align - 4;
203 // Update frame info to pretend that this is part of the stack...
204 MFI->setStackSize(NumBytes);
208 // adjust stack pointer: ESP -= numbytes
209 MI = BuildMI(X86::SUBri32, 2, X86::ESP).addReg(X86::ESP).addZImm(NumBytes);
210 MBBI = 1+MBB.insert(MBBI, MI);
214 void X86RegisterInfo::emitEpilogue(MachineFunction &MF,
215 MachineBasicBlock &MBB) const {
216 const MachineFrameInfo *MFI = MF.getFrameInfo();
217 MachineBasicBlock::iterator MBBI = MBB.end()-1;
219 assert((*MBBI)->getOpcode() == X86::RET &&
220 "Can only insert epilog into returning blocks");
223 // Get the offset of the stack slot for the EBP register... which is
224 // guaranteed to be the last slot by processFunctionBeforeFrameFinalized.
225 int EBPOffset = MFI->getObjectOffset(MFI->getObjectIndexEnd()-1)+4;
228 MI = BuildMI(X86::MOVrr32, 1,X86::ESP).addReg(X86::EBP);
229 MBBI = 1+MBB.insert(MBBI, MI);
231 // mov EBP, [ESP-<offset>]
232 MI = addRegOffset(BuildMI(X86::MOVmr32, 5, X86::EBP), X86::ESP, EBPOffset);
233 MBBI = 1+MBB.insert(MBBI, MI);
235 if (!hasSPAdjust(MF)) return;
237 // Get the number of bytes allocated from the FrameInfo...
238 unsigned NumBytes = MFI->getStackSize();
240 if (NumBytes) { // adjust stack pointer back: ESP += numbytes
241 MI =BuildMI(X86::ADDri32, 2, X86::ESP).addReg(X86::ESP).addZImm(NumBytes);
242 MBBI = 1+MBB.insert(MBBI, MI);
247 #include "X86GenRegisterInfo.inc"
249 const TargetRegisterClass*
250 X86RegisterInfo::getRegClassForType(const Type* Ty) const {
251 switch (Ty->getPrimitiveID()) {
253 case Type::ULongTyID: assert(0 && "Long values can't fit in registers!");
254 default: assert(0 && "Invalid type to getClass!");
256 case Type::SByteTyID:
257 case Type::UByteTyID: return &R8Instance;
258 case Type::ShortTyID:
259 case Type::UShortTyID: return &R16Instance;
262 case Type::PointerTyID: return &R32Instance;
264 case Type::FloatTyID:
265 case Type::DoubleTyID: return &RFPInstance;