Add FIXME notes for spilling int/fp regs (need to calculate stack space).
[oota-llvm.git] / lib / Target / PowerPC / PowerPCRegisterInfo.cpp
1 //===- PowerPCRegisterInfo.cpp - PowerPC Register Information ---*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the PowerPC implementation of the MRegisterInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "reginfo"
15 #include "PowerPC.h"
16 #include "PowerPCRegisterInfo.h"
17 #include "PowerPCInstrBuilder.h"
18 #include "llvm/Constants.h"
19 #include "llvm/Type.h"
20 #include "llvm/CodeGen/ValueTypes.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/MachineFrameInfo.h"
24 #include "llvm/Target/TargetFrameInfo.h"
25 #include "llvm/Target/TargetMachine.h"
26 #include "llvm/Target/TargetMachineImpls.h"
27 #include "Support/CommandLine.h"
28 #include "Support/Debug.h"
29 #include "Support/STLExtras.h"
30 using namespace llvm;
31
32 PowerPCRegisterInfo::PowerPCRegisterInfo()
33   : PowerPCGenRegisterInfo(PPC32::ADJCALLSTACKDOWN,
34                            PPC32::ADJCALLSTACKUP) {}
35
36 static unsigned getIdx(const TargetRegisterClass *RC) {
37   if (RC == PowerPC::GPRCRegisterClass) {
38     switch (RC->getSize()) {
39       default: assert(0 && "Invalid data size!");
40       case 1:  return 0;
41       case 2:  return 1;
42       case 4:  return 2;
43     }
44   } else if (RC == PowerPC::FPRCRegisterClass) {
45     switch (RC->getSize()) {
46       default: assert(0 && "Invalid data size!");
47       case 4:  return 3;
48       case 8:  return 4;
49     }
50   }
51   std::cerr << "Invalid register class to getIdx()!\n";
52   abort();
53 }
54
55 int 
56 PowerPCRegisterInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
57                                          MachineBasicBlock::iterator MI,
58                                          unsigned SrcReg, int FrameIdx,
59                                          const TargetRegisterClass *RC) const {
60   static const unsigned Opcode[] = { 
61     PPC32::STB, PPC32::STH, PPC32::STW, PPC32::STFS, PPC32::STFD 
62   };
63   unsigned OC = Opcode[getIdx(RC)];
64   MBB.insert(MI, addFrameReference(BuildMI(OC, 3).addReg(SrcReg),FrameIdx));
65   return 1;
66 }
67
68 int PowerPCRegisterInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
69                                           MachineBasicBlock::iterator MI,
70                                           unsigned DestReg, int FrameIdx,
71                                           const TargetRegisterClass *RC) const{
72   static const unsigned Opcode[] = { 
73     PPC32::LBZ, PPC32::LHZ, PPC32::LWZ, PPC32::LFS, PPC32::LFD 
74   };
75   unsigned OC = Opcode[getIdx(RC)];
76   MBB.insert(MI, addFrameReference(BuildMI(OC, 2, DestReg), FrameIdx));
77   return 1;
78 }
79
80 int PowerPCRegisterInfo::copyRegToReg(MachineBasicBlock &MBB,
81                                       MachineBasicBlock::iterator MI,
82                                       unsigned DestReg, unsigned SrcReg,
83                                       const TargetRegisterClass *RC) const {
84   MachineInstr *I;
85
86   if (RC == PowerPC::GPRCRegisterClass) {
87     I = BuildMI(PPC32::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
88   } else if (RC == PowerPC::FPRCRegisterClass) {
89     I = BuildMI(PPC32::FMR, 1, DestReg).addReg(SrcReg);
90   } else { 
91     std::cerr << "Attempt to copy register that is not GPR or FPR";
92     abort();
93   }
94   MBB.insert(MI, I);
95   return 1;
96 }
97
98 //===----------------------------------------------------------------------===//
99 // Stack Frame Processing methods
100 //===----------------------------------------------------------------------===//
101
102 // hasFP - Return true if the specified function should have a dedicated frame
103 // pointer register.  This is true if the function has variable sized allocas or
104 // if frame pointer elimination is disabled.
105 //
106 static bool hasFP(MachineFunction &MF) {
107   return NoFramePointerElim || MF.getFrameInfo()->hasVarSizedObjects();
108 }
109
110 void PowerPCRegisterInfo::
111 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
112                               MachineBasicBlock::iterator I) const {
113   if (hasFP(MF)) {
114     // If we have a frame pointer, turn the adjcallstackdown instruction into a
115     // 'sub r1, r1, <amt>' and the adjcallstackup instruction into 
116     // 'add r1, r1, <amt>'
117     MachineInstr *Old = I;
118     int Amount = Old->getOperand(0).getImmedValue();
119     if (Amount != 0) {
120       // We need to keep the stack aligned properly.  To do this, we round the
121       // amount of space needed for the outgoing arguments up to the next
122       // alignment boundary.
123       unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
124       Amount = (Amount+Align-1)/Align*Align;
125       
126       MachineInstr *New;
127       if (Old->getOpcode() == PPC32::ADJCALLSTACKDOWN) {
128         New = BuildMI(PPC32::ADDI, 2, PPC32::R1).addReg(PPC32::R1)
129                 .addSImm(-Amount);
130       } else {
131         assert(Old->getOpcode() == PPC32::ADJCALLSTACKUP);
132         New = BuildMI(PPC32::ADDI, 2, PPC32::R1).addReg(PPC32::R1)
133                 .addSImm(Amount);
134       }
135       
136       // Replace the pseudo instruction with a new instruction...
137       MBB.insert(I, New);
138     }
139   }
140   
141   MBB.erase(I);
142 }
143
144 void
145 PowerPCRegisterInfo::eliminateFrameIndex(MachineFunction &MF,
146                                          MachineBasicBlock::iterator II) const {
147   unsigned i = 0;
148   MachineInstr &MI = *II;
149   while (!MI.getOperand(i).isFrameIndex()) {
150     ++i;
151     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
152   }
153
154   int FrameIndex = MI.getOperand(i).getFrameIndex();
155
156   // This must be part of a four operand memory reference.  Replace the
157   // FrameIndex with base register with GPR1.
158   MI.SetMachineOperandReg(i, PPC32::R1);
159
160   // Take into account whether its an add or mem instruction
161   if (i == 2) i--;
162
163   // Now add the frame object offset to the offset from r1.
164   int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex) +
165                MI.getOperand(i).getImmedValue()+4;
166
167   if (!hasFP(MF))
168     Offset += MF.getFrameInfo()->getStackSize();
169
170   MI.SetMachineOperandConst(i, MachineOperand::MO_SignExtendedImmed, Offset);
171   DEBUG(std::cerr << "offset = " << Offset << std::endl);
172 }
173
174
175 void
176 PowerPCRegisterInfo::processFunctionBeforeFrameFinalized(MachineFunction &MF)
177   const {
178   // Do Nothing
179 }
180
181 void PowerPCRegisterInfo::emitPrologue(MachineFunction &MF) const {
182   MachineBasicBlock &MBB = MF.front();   // Prolog goes in entry BB
183   MachineBasicBlock::iterator MBBI = MBB.begin();
184   MachineFrameInfo *MFI = MF.getFrameInfo();
185   MachineInstr *MI;
186   
187   // Get the number of bytes to allocate from the FrameInfo
188   unsigned NumBytes = MFI->getStackSize();
189
190   // FIXME: the assembly printer inserts "calls" aka branch-and-link to get the
191   // PC address. We may not know about those calls at this time, so be
192   // conservative.
193   if (MFI->hasCalls() || true) {
194     // When we have no frame pointer, we reserve argument space for call sites
195     // in the function immediately on entry to the current function.  This
196     // eliminates the need for add/sub brackets around call sites.
197     //
198     NumBytes += MFI->getMaxCallFrameSize() + 
199                 24   /* Predefined PowerPC link area */ + 
200                 // FIXME: must calculate #int regs actually spilled
201                 12*4 /* Spilled int regs */ +
202                 // FIXME: must calculate #fp regs actually spilled
203                 0*8  /* Spilled fp regs */;
204     
205     // Round the size to a multiple of the alignment (don't forget the 4 byte
206     // offset though).
207     unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
208     NumBytes = ((NumBytes+4)+Align-1)/Align*Align - 4;
209
210     // Store the incoming LR so it is preserved across calls
211     MI = BuildMI(PPC32::MFLR, 0, PPC32::R0);
212     MBB.insert(MBBI, MI);
213     MI = BuildMI(PPC32::STMW, 3).addReg(PPC32::R30).addSImm(-8)
214            .addReg(PPC32::R1);
215     MBB.insert(MBBI, MI);
216     MI = BuildMI(PPC32::STW, 3).addReg(PPC32::R0).addSImm(8).addReg(PPC32::R1);
217     MBB.insert(MBBI, MI);
218   }
219
220   // Update frame info to pretend that this is part of the stack...
221   MFI->setStackSize(NumBytes);
222   
223   // adjust stack pointer: r1 -= numbytes
224   if (NumBytes) {
225     MI = BuildMI(PPC32::STWU, 2, PPC32::R1).addImm(-NumBytes).addReg(PPC32::R1);
226     MBB.insert(MBBI, MI);
227   }
228 }
229
230 void PowerPCRegisterInfo::emitEpilogue(MachineFunction &MF,
231                                        MachineBasicBlock &MBB) const {
232   const MachineFrameInfo *MFI = MF.getFrameInfo();
233   MachineBasicBlock::iterator MBBI = prior(MBB.end());
234   MachineInstr *MI;
235   assert(MBBI->getOpcode() == PPC32::BLR &&
236          "Can only insert epilog into returning blocks");
237   
238   // Get the number of bytes allocated from the FrameInfo...
239   unsigned NumBytes = MFI->getStackSize();
240
241   // Adjust stack pointer back
242   MI = BuildMI(PPC32::LWZ, 2, PPC32::R1).addImm(0).addReg(PPC32::R1);
243   MBB.insert(MBBI, MI);
244
245   // If we have calls, restore the LR value before we branch to it
246   // FIXME: the assembly printer inserts "calls" aka branch-and-link to get the
247   // PC address. We may not know about those calls at this time, so be
248   // conservative.
249   if (MFI->hasCalls() || true) {
250     // Read old LR from stack into R0
251     MI = BuildMI(PPC32::LWZ, 2, PPC32::R0).addSImm(8).addReg(PPC32::R1);
252     MBB.insert(MBBI, MI);
253     MI = BuildMI(PPC32::MTLR, 1).addReg(PPC32::R0);
254     MBB.insert(MBBI, MI);
255     MI = BuildMI(PPC32::LMW, 2, PPC32::R30).addSImm(-8).addReg(PPC32::R1);
256     MBB.insert(MBBI, MI);
257   }
258 }
259
260 #include "PowerPCGenRegisterInfo.inc"
261
262 const TargetRegisterClass*
263 PowerPCRegisterInfo::getRegClassForType(const Type* Ty) const {
264   switch (Ty->getTypeID()) {
265     case Type::LongTyID:
266     case Type::ULongTyID: assert(0 && "Long values can't fit in registers!");
267     default:              assert(0 && "Invalid type to getClass!");
268     case Type::BoolTyID:
269     case Type::SByteTyID:
270     case Type::UByteTyID:
271     case Type::ShortTyID:
272     case Type::UShortTyID:
273     case Type::IntTyID:
274     case Type::UIntTyID:
275     case Type::PointerTyID: return &GPRCInstance;
276       
277     case Type::FloatTyID:
278     case Type::DoubleTyID: return &FPRCInstance;
279   }
280 }
281