Changes For Bug 352
[oota-llvm.git] / lib / Target / PowerPC / PPCRegisterInfo.cpp
1 //===- PPC32RegisterInfo.cpp - PowerPC32 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 PowerPC32 implementation of the MRegisterInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "reginfo"
15 #include "PowerPC.h"
16 #include "PowerPCInstrBuilder.h"
17 #include "PPC32RegisterInfo.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/TargetOptions.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/ADT/STLExtras.h"
30 #include <cstdlib>
31 #include <iostream>
32 using namespace llvm;
33
34 namespace llvm {
35   // Switch toggling compilation for AIX
36   extern cl::opt<bool> AIX;
37 }
38
39 PPC32RegisterInfo::PPC32RegisterInfo()
40   : PPC32GenRegisterInfo(PPC::ADJCALLSTACKDOWN, PPC::ADJCALLSTACKUP) {
41   ImmToIdxMap[PPC::LD]   = PPC::LDX;    ImmToIdxMap[PPC::STD]  = PPC::STDX;    
42   ImmToIdxMap[PPC::LBZ]  = PPC::LBZX;   ImmToIdxMap[PPC::STB]  = PPC::STBX;
43   ImmToIdxMap[PPC::LHZ]  = PPC::LHZX;   ImmToIdxMap[PPC::LHA]  = PPC::LHAX;
44   ImmToIdxMap[PPC::LWZ]  = PPC::LWZX;   ImmToIdxMap[PPC::LWA]  = PPC::LWAX;
45   ImmToIdxMap[PPC::LFS]  = PPC::LFSX;   ImmToIdxMap[PPC::LFD]  = PPC::LFDX;
46   ImmToIdxMap[PPC::STH]  = PPC::STHX;   ImmToIdxMap[PPC::STW]  = PPC::STWX;
47   ImmToIdxMap[PPC::STFS] = PPC::STFSX;  ImmToIdxMap[PPC::STFD] = PPC::STFDX;
48   ImmToIdxMap[PPC::ADDI] = PPC::ADD;
49 }
50
51 static unsigned getIdx(const TargetRegisterClass *RC) {
52   if (RC == PPC32::GPRCRegisterClass) {
53     switch (RC->getSize()) {
54       default: assert(0 && "Invalid data size!");
55       case 1:  return 0;
56       case 2:  return 1;
57       case 4:  return 2;
58     }
59   } else if (RC == PPC32::FPRCRegisterClass) {
60     switch (RC->getSize()) {
61       default: assert(0 && "Invalid data size!");
62       case 4:  return 3;
63       case 8:  return 4;
64     }
65   }
66   std::cerr << "Invalid register class to getIdx()!\n";
67   abort();
68 }
69
70 void 
71 PPC32RegisterInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
72                                        MachineBasicBlock::iterator MI,
73                                        unsigned SrcReg, int FrameIdx) const {
74   const TargetRegisterClass *RC = getRegClass(SrcReg);
75   static const unsigned Opcode[] = { 
76     PPC::STB, PPC::STH, PPC::STW, PPC::STFS, PPC::STFD 
77   };
78
79   unsigned OC = Opcode[getIdx(RC)];
80   if (SrcReg == PPC::LR) {
81     BuildMI(MBB, MI, PPC::MFLR, 0, PPC::R11);
82     BuildMI(MBB, MI, PPC::IMPLICIT_DEF, 0, PPC::R0);
83     addFrameReference(BuildMI(MBB, MI, OC, 3).addReg(PPC::R11),FrameIdx);
84   } else {
85     BuildMI(MBB, MI, PPC::IMPLICIT_DEF, 0, PPC::R0);
86     addFrameReference(BuildMI(MBB, MI, OC, 3).addReg(SrcReg),FrameIdx);
87   }
88 }
89
90 void
91 PPC32RegisterInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
92                                         MachineBasicBlock::iterator MI,
93                                         unsigned DestReg, int FrameIdx) const{
94   static const unsigned Opcode[] = { 
95     PPC::LBZ, PPC::LHZ, PPC::LWZ, PPC::LFS, PPC::LFD 
96   };
97   const TargetRegisterClass *RC = getRegClass(DestReg);
98   unsigned OC = Opcode[getIdx(RC)];
99   if (DestReg == PPC::LR) {
100     BuildMI(MBB, MI, PPC::IMPLICIT_DEF, 0, PPC::R0);
101     addFrameReference(BuildMI(MBB, MI, OC, 2, PPC::R11), FrameIdx);
102     BuildMI(MBB, MI, PPC::MTLR, 1).addReg(PPC::R11);
103   } else {
104     BuildMI(MBB, MI, PPC::IMPLICIT_DEF, 0, PPC::R0);
105     addFrameReference(BuildMI(MBB, MI, OC, 2, DestReg), FrameIdx);
106   }
107 }
108
109 void PPC32RegisterInfo::copyRegToReg(MachineBasicBlock &MBB,
110                                      MachineBasicBlock::iterator MI,
111                                      unsigned DestReg, unsigned SrcReg,
112                                      const TargetRegisterClass *RC) const {
113   MachineInstr *I;
114
115   if (RC == PPC32::GPRCRegisterClass) {
116     BuildMI(MBB, MI, PPC::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
117   } else if (RC == PPC32::FPRCRegisterClass) {
118     BuildMI(MBB, MI, PPC::FMR, 1, DestReg).addReg(SrcReg);
119   } else { 
120     std::cerr << "Attempt to copy register that is not GPR or FPR";
121     abort();
122   }
123 }
124
125 //===----------------------------------------------------------------------===//
126 // Stack Frame Processing methods
127 //===----------------------------------------------------------------------===//
128
129 // hasFP - Return true if the specified function should have a dedicated frame
130 // pointer register.  This is true if the function has variable sized allocas or
131 // if frame pointer elimination is disabled.
132 //
133 static bool hasFP(MachineFunction &MF) {
134   MachineFrameInfo *MFI = MF.getFrameInfo();
135   return MFI->hasVarSizedObjects();
136 }
137
138 void PPC32RegisterInfo::
139 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
140                               MachineBasicBlock::iterator I) const {
141   if (hasFP(MF)) {
142     // If we have a frame pointer, convert as follows:
143     // ADJCALLSTACKDOWN -> addi, r1, r1, -amount
144     // ADJCALLSTACKUP   -> addi, r1, r1, amount
145     MachineInstr *Old = I;
146     unsigned Amount = Old->getOperand(0).getImmedValue();
147     if (Amount != 0) {
148       // We need to keep the stack aligned properly.  To do this, we round the
149       // amount of space needed for the outgoing arguments up to the next
150       // alignment boundary.
151       unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
152       Amount = (Amount+Align-1)/Align*Align;
153       
154       // Replace the pseudo instruction with a new instruction...
155       if (Old->getOpcode() == PPC::ADJCALLSTACKDOWN) {
156         MBB.insert(I, BuildMI(PPC::ADDI, 2, PPC::R1).addReg(PPC::R1)
157                 .addSImm(-Amount));
158       } else {
159         assert(Old->getOpcode() == PPC::ADJCALLSTACKUP);
160         MBB.insert(I, BuildMI(PPC::ADDI, 2, PPC::R1).addReg(PPC::R1)
161                 .addSImm(Amount));
162       }
163     }
164   }
165   MBB.erase(I);
166 }
167
168 void
169 PPC32RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II) const {
170   unsigned i = 0;
171   MachineInstr &MI = *II;
172   MachineBasicBlock &MBB = *MI.getParent();
173   MachineFunction &MF = *MBB.getParent();
174   
175   while (!MI.getOperand(i).isFrameIndex()) {
176     ++i;
177     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
178   }
179
180   int FrameIndex = MI.getOperand(i).getFrameIndex();
181
182   // Replace the FrameIndex with base register with GPR1 (SP) or GPR31 (FP).
183   MI.SetMachineOperandReg(i, hasFP(MF) ? PPC::R31 : PPC::R1);
184
185   // Take into account whether it's an add or mem instruction
186   unsigned OffIdx = (i == 2) ? 1 : 2;
187
188   // Now add the frame object offset to the offset from r1.
189   int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex) +
190                MI.getOperand(OffIdx).getImmedValue();
191
192   // If we're not using a Frame Pointer that has been set to the value of the
193   // SP before having the stack size subtracted from it, then add the stack size
194   // to Offset to get the correct offset.
195   Offset += MF.getFrameInfo()->getStackSize();
196   
197   if (Offset > 32767 || Offset < -32768) {
198     // Insert a set of r0 with the full offset value before the ld, st, or add
199     MachineBasicBlock *MBB = MI.getParent();
200     MBB->insert(II, BuildMI(PPC::LIS, 1, PPC::R0).addSImm(Offset >> 16));
201     MBB->insert(II, BuildMI(PPC::ORI, 2, PPC::R0).addReg(PPC::R0)
202       .addImm(Offset));
203     // convert into indexed form of the instruction
204     // sth 0:rA, 1:imm 2:(rB) ==> sthx 0:rA, 2:rB, 1:r0
205     // addi 0:rA 1:rB, 2, imm ==> add 0:rA, 1:rB, 2:r0
206     unsigned NewOpcode = const_cast<std::map<unsigned, unsigned>& >(ImmToIdxMap)[MI.getOpcode()];
207     assert(NewOpcode && "No indexed form of load or store available!");
208     MI.setOpcode(NewOpcode);
209     MI.SetMachineOperandReg(1, MI.getOperand(i).getReg());
210     MI.SetMachineOperandReg(2, PPC::R0);
211   } else {
212     MI.SetMachineOperandConst(OffIdx,MachineOperand::MO_SignExtendedImmed,Offset);
213   }
214 }
215
216
217 void PPC32RegisterInfo::emitPrologue(MachineFunction &MF) const {
218   MachineBasicBlock &MBB = MF.front();   // Prolog goes in entry BB
219   MachineBasicBlock::iterator MBBI = MBB.begin();
220   MachineFrameInfo *MFI = MF.getFrameInfo();
221   MachineInstr *MI;
222   
223   // Get the number of bytes to allocate from the FrameInfo
224   unsigned NumBytes = MFI->getStackSize();
225
226   // If we have calls, we cannot use the red zone to store callee save registers
227   // and we must set up a stack frame, so calculate the necessary size here.
228   if (MFI->hasCalls()) {
229     // We reserve argument space for call sites in the function immediately on 
230     // entry to the current function.  This eliminates the need for add/sub 
231     // brackets around call sites.
232     NumBytes += MFI->getMaxCallFrameSize();
233   }
234
235   // Do we need to allocate space on the stack?
236   if (NumBytes == 0) return;
237
238   // Add the size of R1 to  NumBytes size for the store of R1 to the bottom 
239   // of the stack and round the size to a multiple of the alignment.
240   unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
241   unsigned GPRSize = getSpillSize(PPC::R1)/8;
242   unsigned Size = hasFP(MF) ? GPRSize + GPRSize : GPRSize;
243   NumBytes = (NumBytes+Size+Align-1)/Align*Align;
244
245   // Update frame info to pretend that this is part of the stack...
246   MFI->setStackSize(NumBytes);
247
248   // adjust stack pointer: r1 -= numbytes
249   if (NumBytes <= 32768) {
250     MI=BuildMI(PPC::STWU,3).addReg(PPC::R1).addSImm(-NumBytes).addReg(PPC::R1);
251     MBB.insert(MBBI, MI);
252   } else {
253     int NegNumbytes = -NumBytes;
254     MI = BuildMI(PPC::LIS, 1, PPC::R0).addSImm(NegNumbytes >> 16);
255     MBB.insert(MBBI, MI);
256     MI = BuildMI(PPC::ORI, 2, PPC::R0).addReg(PPC::R0)
257       .addImm(NegNumbytes & 0xFFFF);
258     MBB.insert(MBBI, MI);
259     MI = BuildMI(PPC::STWUX, 3).addReg(PPC::R1).addReg(PPC::R1).addReg(PPC::R0);
260     MBB.insert(MBBI, MI);
261   }
262   
263   if (hasFP(MF)) {
264     MI = BuildMI(PPC::STW, 3).addReg(PPC::R31).addSImm(GPRSize).addReg(PPC::R1);
265     MBB.insert(MBBI, MI);
266     MI = BuildMI(PPC::OR, 2, PPC::R31).addReg(PPC::R1).addReg(PPC::R1);
267     MBB.insert(MBBI, MI);
268   }
269 }
270
271 void PPC32RegisterInfo::emitEpilogue(MachineFunction &MF,
272                                      MachineBasicBlock &MBB) const {
273   const MachineFrameInfo *MFI = MF.getFrameInfo();
274   MachineBasicBlock::iterator MBBI = prior(MBB.end());
275   MachineInstr *MI;
276   assert(MBBI->getOpcode() == PPC::BLR &&
277          "Can only insert epilog into returning blocks");
278   
279   // Get the number of bytes allocated from the FrameInfo...
280   unsigned NumBytes = MFI->getStackSize();
281   unsigned GPRSize = getSpillSize(PPC::R31)/8;
282
283   if (NumBytes != 0) {
284     if (hasFP(MF)) {
285       MI = BuildMI(PPC::OR, 2, PPC::R1).addReg(PPC::R31).addReg(PPC::R31);
286       MBB.insert(MBBI, MI);
287       MI = BuildMI(PPC::LWZ, 2, PPC::R31).addSImm(GPRSize).addReg(PPC::R31);
288       MBB.insert(MBBI, MI);
289     }
290     MI = BuildMI(PPC::LWZ, 2, PPC::R1).addSImm(0).addReg(PPC::R1);
291     MBB.insert(MBBI, MI);
292   }
293 }
294
295 #include "PPC32GenRegisterInfo.inc"
296
297 const TargetRegisterClass*
298 PPC32RegisterInfo::getRegClassForType(const Type* Ty) const {
299   switch (Ty->getTypeID()) {
300     default:              assert(0 && "Invalid type to getClass!");
301     case Type::LongTyID:
302     case Type::ULongTyID: assert(0 && "Long values can't fit in registers!");
303     case Type::BoolTyID:
304     case Type::SByteTyID:
305     case Type::UByteTyID:
306     case Type::ShortTyID:
307     case Type::UShortTyID:
308     case Type::IntTyID:
309     case Type::UIntTyID:
310     case Type::PointerTyID: return &GPRCInstance;
311      
312     case Type::FloatTyID:
313     case Type::DoubleTyID: return &FPRCInstance;
314   }
315 }
316