Disable the leaf function optimization, which is apparently not legal on
[oota-llvm.git] / lib / Target / X86 / X86RegisterInfo.cpp
1 //===- X86RegisterInfo.cpp - X86 Register Information -----------*- C++ -*-===//
2 //
3 // This file contains the X86 implementation of the MRegisterInfo class.  This
4 // file is responsible for the frame pointer elimination optimization on X86.
5 //
6 //===----------------------------------------------------------------------===//
7
8 #include "X86.h"
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"
20
21 namespace {
22   cl::opt<bool>
23   NoFPElim("disable-fp-elim",
24            cl::desc("Disable frame pointer elimination optimization"));
25 }
26
27 X86RegisterInfo::X86RegisterInfo()
28   : X86GenRegisterInfo(X86::ADJCALLSTACKDOWN, X86::ADJCALLSTACKUP) {}
29
30 static unsigned getIdx(const TargetRegisterClass *RC) {
31   switch (RC->getSize()) {
32   default: assert(0 && "Invalid data size!");
33   case 1:  return 0;
34   case 2:  return 1;
35   case 4:  return 2;
36   case 10: return 3;
37   }
38 }
39
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;
49 }
50
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),
58                                        FrameIdx);
59   MBBI = MBB.insert(MBBI, MI)+1;
60 }
61
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;
70 }
71
72 //===----------------------------------------------------------------------===//
73 // Stack Frame Processing methods
74 //===----------------------------------------------------------------------===//
75
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.
79 //
80 static bool hasFP(MachineFunction &MF) {
81   return NoFPElim || MF.getFrameInfo()->hasVarSizedObjects();
82 }
83
84 void X86RegisterInfo::eliminateCallFramePseudoInstr(MachineFunction &MF,
85                                                     MachineBasicBlock &MBB,
86                                          MachineBasicBlock::iterator &I) const {
87   MachineInstr *New = 0, *Old = *I;;
88   if (hasFP(MF)) {
89     // If we have a frame pointer, turn the adjcallstackup instruction into a
90     // 'sub ESP, <amt>' and the adjcallstackdown instruction into 'add ESP,
91     // <amt>'
92     unsigned Amount = Old->getOperand(0).getImmedValue();
93     if (Amount != 0) {
94       // We need to keep the stack aligned properly.  To do this, we round the
95       // amount of space needed for the outgoing arguments up to the next
96       // alignment boundary.
97       unsigned Align = MF.getTarget().getFrameInfo().getStackAlignment();
98       Amount = (Amount+Align-1)/Align*Align;
99
100       if (Old->getOpcode() == X86::ADJCALLSTACKDOWN) {
101         New=BuildMI(X86::SUBri32, 2, X86::ESP).addReg(X86::ESP).addZImm(Amount);
102       } else {
103         assert(Old->getOpcode() == X86::ADJCALLSTACKUP);
104         New=BuildMI(X86::ADDri32, 2, X86::ESP).addReg(X86::ESP).addZImm(Amount);
105       }
106     }
107   }
108
109   if (New)
110     *I = New;        // Replace the pseudo instruction with a new instruction...
111   else
112     I = MBB.erase(I);// Just delete the pseudo instruction...
113   delete Old;
114 }
115
116 void X86RegisterInfo::eliminateFrameIndex(MachineFunction &MF,
117                                         MachineBasicBlock::iterator &II) const {
118   unsigned i = 0;
119   MachineInstr &MI = **II;
120   while (!MI.getOperand(i).isFrameIndex()) {
121     ++i;
122     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
123   }
124
125   int FrameIndex = MI.getOperand(i).getFrameIndex();
126
127   // This must be part of a four operand memory reference.  Replace the
128   // FrameIndex with base register with EBP.  Add add an offset to the offset.
129   MI.SetMachineOperandReg(i, hasFP(MF) ? X86::EBP : X86::ESP);
130
131   // Now add the frame object offset to the offset from EBP.
132   int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex) +
133                MI.getOperand(i+3).getImmedValue()+4;
134
135   if (!hasFP(MF))
136     Offset += MF.getFrameInfo()->getStackSize();
137
138   MI.SetMachineOperandConst(i+3, MachineOperand::MO_SignExtendedImmed, Offset);
139 }
140
141 void X86RegisterInfo::processFunctionBeforeFrameFinalized(MachineFunction &MF)
142   const {
143   if (hasFP(MF)) {
144     // Create a frame entry for the EBP register that must be saved.
145     int FrameIdx = MF.getFrameInfo()->CreateStackObject(4, 4);
146     assert(FrameIdx == MF.getFrameInfo()->getObjectIndexEnd()-1 &&
147            "Slot for EBP register must be last in order to be found!");
148   }
149 }
150
151 void X86RegisterInfo::emitPrologue(MachineFunction &MF) const {
152   MachineBasicBlock &MBB = MF.front();   // Prolog goes in entry BB
153   MachineBasicBlock::iterator MBBI = MBB.begin();
154   MachineFrameInfo *MFI = MF.getFrameInfo();
155   MachineInstr *MI;
156
157   // Get the number of bytes to allocate from the FrameInfo
158   unsigned NumBytes = MFI->getStackSize();
159   if (hasFP(MF)) {
160     // Get the offset of the stack slot for the EBP register... which is
161     // guaranteed to be the last slot by processFunctionBeforeFrameFinalized.
162     int EBPOffset = MFI->getObjectOffset(MFI->getObjectIndexEnd()-1)+4;
163
164     MI = addRegOffset(BuildMI(X86::MOVrm32, 5),    // mov [ESP-<offset>], EBP
165                       X86::ESP, EBPOffset).addReg(X86::EBP);
166     MBBI = MBB.insert(MBBI, MI)+1;
167     
168     MI = BuildMI(X86::MOVrr32, 2, X86::EBP).addReg(X86::ESP);
169     MBBI = MBB.insert(MBBI, MI)+1;
170   } else {
171     // When we have no frame pointer, we reserve argument space for call sites
172     // in the function immediately on entry to the current function.  This
173     // eliminates the need for add/sub ESP brackets around call sites.
174     //
175     NumBytes += MFI->getMaxCallFrameSize();
176
177     // Round the size to a multiple of the alignment (don't forget the 4 byte
178     // offset though).
179     unsigned Align = MF.getTarget().getFrameInfo().getStackAlignment();
180     NumBytes = ((NumBytes+4)+Align-1)/Align*Align - 4;
181
182     // Update frame info to pretend that this is part of the stack...
183     MFI->setStackSize(NumBytes);
184   }
185
186   if (NumBytes) {
187     // adjust stack pointer: ESP -= numbytes
188     MI  = BuildMI(X86::SUBri32, 2, X86::ESP).addReg(X86::ESP).addZImm(NumBytes);
189     MBBI = 1+MBB.insert(MBBI, MI);
190   }
191 }
192
193 void X86RegisterInfo::emitEpilogue(MachineFunction &MF,
194                                    MachineBasicBlock &MBB) const {
195   const MachineFrameInfo *MFI = MF.getFrameInfo();
196   MachineBasicBlock::iterator MBBI = MBB.end()-1;
197   MachineInstr *MI;
198   assert((*MBBI)->getOpcode() == X86::RET &&
199          "Can only insert epilog into returning blocks");
200
201   if (hasFP(MF)) {
202     // Get the offset of the stack slot for the EBP register... which is
203     // guaranteed to be the last slot by processFunctionBeforeFrameFinalized.
204     int EBPOffset = MFI->getObjectOffset(MFI->getObjectIndexEnd()-1)+4;
205     
206     // mov ESP, EBP
207     MI = BuildMI(X86::MOVrr32, 1,X86::ESP).addReg(X86::EBP);
208     MBBI = 1+MBB.insert(MBBI, MI);
209
210     // mov EBP, [ESP-<offset>]
211     MI = addRegOffset(BuildMI(X86::MOVmr32, 5, X86::EBP), X86::ESP, EBPOffset);
212     MBBI = 1+MBB.insert(MBBI, MI);
213   } else {
214     // Get the number of bytes allocated from the FrameInfo...
215     unsigned NumBytes = MFI->getStackSize();
216
217     if (NumBytes) {    // adjust stack pointer back: ESP += numbytes
218       MI =BuildMI(X86::ADDri32, 2, X86::ESP).addReg(X86::ESP).addZImm(NumBytes);
219       MBBI = 1+MBB.insert(MBBI, MI);
220     }
221   }
222 }
223
224 #include "X86GenRegisterInfo.inc"
225
226 const TargetRegisterClass*
227 X86RegisterInfo::getRegClassForType(const Type* Ty) const {
228   switch (Ty->getPrimitiveID()) {
229   case Type::LongTyID:
230   case Type::ULongTyID: assert(0 && "Long values can't fit in registers!");
231   default:              assert(0 && "Invalid type to getClass!");
232   case Type::BoolTyID:
233   case Type::SByteTyID:
234   case Type::UByteTyID:   return &R8Instance;
235   case Type::ShortTyID:
236   case Type::UShortTyID:  return &R16Instance;
237   case Type::IntTyID:
238   case Type::UIntTyID:
239   case Type::PointerTyID: return &R32Instance;
240     
241   case Type::FloatTyID:
242   case Type::DoubleTyID: return &RFPInstance;
243   }
244 }