For PR786:
[oota-llvm.git] / lib / Target / PowerPC / PPCRegisterInfo.cpp
1 //===- PPCRegisterInfo.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 "PPC.h"
16 #include "PPCInstrBuilder.h"
17 #include "PPCRegisterInfo.h"
18 #include "PPCSubtarget.h"
19 #include "llvm/Constants.h"
20 #include "llvm/Type.h"
21 #include "llvm/CodeGen/ValueTypes.h"
22 #include "llvm/CodeGen/MachineInstrBuilder.h"
23 #include "llvm/CodeGen/MachineDebugInfo.h"
24 #include "llvm/CodeGen/MachineFunction.h"
25 #include "llvm/CodeGen/MachineFrameInfo.h"
26 #include "llvm/CodeGen/MachineLocation.h"
27 #include "llvm/CodeGen/SelectionDAGNodes.h"
28 #include "llvm/Target/TargetFrameInfo.h"
29 #include "llvm/Target/TargetInstrInfo.h"
30 #include "llvm/Target/TargetMachine.h"
31 #include "llvm/Target/TargetOptions.h"
32 #include "llvm/Support/CommandLine.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/Support/MathExtras.h"
35 #include "llvm/ADT/STLExtras.h"
36 #include <cstdlib>
37 #include <iostream>
38 using namespace llvm;
39
40 /// getRegisterNumbering - Given the enum value for some register, e.g.
41 /// PPC::F14, return the number that it corresponds to (e.g. 14).
42 unsigned PPCRegisterInfo::getRegisterNumbering(unsigned RegEnum) {
43   using namespace PPC;
44   switch (RegEnum) {
45   case R0 :  case X0 :  case F0 :  case V0 : case CR0:  return  0;
46   case R1 :  case X1 :  case F1 :  case V1 : case CR1:  return  1;
47   case R2 :  case X2 :  case F2 :  case V2 : case CR2:  return  2;
48   case R3 :  case X3 :  case F3 :  case V3 : case CR3:  return  3;
49   case R4 :  case X4 :  case F4 :  case V4 : case CR4:  return  4;
50   case R5 :  case X5 :  case F5 :  case V5 : case CR5:  return  5;
51   case R6 :  case X6 :  case F6 :  case V6 : case CR6:  return  6;
52   case R7 :  case X7 :  case F7 :  case V7 : case CR7:  return  7;
53   case R8 :  case X8 :  case F8 :  case V8 : return  8;
54   case R9 :  case X9 :  case F9 :  case V9 : return  9;
55   case R10:  case X10:  case F10:  case V10: return 10;
56   case R11:  case X11:  case F11:  case V11: return 11;
57   case R12:  case X12:  case F12:  case V12: return 12;
58   case R13:  case X13:  case F13:  case V13: return 13;
59   case R14:  case X14:  case F14:  case V14: return 14;
60   case R15:  case X15:  case F15:  case V15: return 15;
61   case R16:  case X16:  case F16:  case V16: return 16;
62   case R17:  case X17:  case F17:  case V17: return 17;
63   case R18:  case X18:  case F18:  case V18: return 18;
64   case R19:  case X19:  case F19:  case V19: return 19;
65   case R20:  case X20:  case F20:  case V20: return 20;
66   case R21:  case X21:  case F21:  case V21: return 21;
67   case R22:  case X22:  case F22:  case V22: return 22;
68   case R23:  case X23:  case F23:  case V23: return 23;
69   case R24:  case X24:  case F24:  case V24: return 24;
70   case R25:  case X25:  case F25:  case V25: return 25;
71   case R26:  case X26:  case F26:  case V26: return 26;
72   case R27:  case X27:  case F27:  case V27: return 27;
73   case R28:  case X28:  case F28:  case V28: return 28;
74   case R29:  case X29:  case F29:  case V29: return 29;
75   case R30:  case X30:  case F30:  case V30: return 30;
76   case R31:  case X31:  case F31:  case V31: return 31;
77   default:
78     std::cerr << "Unhandled reg in PPCRegisterInfo::getRegisterNumbering!\n";
79     abort();
80   }
81 }
82
83 PPCRegisterInfo::PPCRegisterInfo(const PPCSubtarget &ST)
84   : PPCGenRegisterInfo(PPC::ADJCALLSTACKDOWN, PPC::ADJCALLSTACKUP),
85     Subtarget(ST) {
86   ImmToIdxMap[PPC::LD]   = PPC::LDX;    ImmToIdxMap[PPC::STD]  = PPC::STDX;
87   ImmToIdxMap[PPC::LBZ]  = PPC::LBZX;   ImmToIdxMap[PPC::STB]  = PPC::STBX;
88   ImmToIdxMap[PPC::LHZ]  = PPC::LHZX;   ImmToIdxMap[PPC::LHA]  = PPC::LHAX;
89   ImmToIdxMap[PPC::LWZ]  = PPC::LWZX;   ImmToIdxMap[PPC::LWA]  = PPC::LWAX;
90   ImmToIdxMap[PPC::LFS]  = PPC::LFSX;   ImmToIdxMap[PPC::LFD]  = PPC::LFDX;
91   ImmToIdxMap[PPC::STH]  = PPC::STHX;   ImmToIdxMap[PPC::STW]  = PPC::STWX;
92   ImmToIdxMap[PPC::STFS] = PPC::STFSX;  ImmToIdxMap[PPC::STFD] = PPC::STFDX;
93   ImmToIdxMap[PPC::ADDI] = PPC::ADD4;
94 }
95
96 void
97 PPCRegisterInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
98                                      MachineBasicBlock::iterator MI,
99                                      unsigned SrcReg, int FrameIdx,
100                                      const TargetRegisterClass *RC) const {
101   if (SrcReg == PPC::LR) {
102     // FIXME: this spills LR immediately to memory in one step.  To do this, we
103     // use R11, which we know cannot be used in the prolog/epilog.  This is a
104     // hack.
105     BuildMI(MBB, MI, PPC::MFLR, 1, PPC::R11);
106     addFrameReference(BuildMI(MBB, MI, PPC::STW, 3).addReg(PPC::R11), FrameIdx);
107   } else if (RC == PPC::CRRCRegisterClass) {
108     // FIXME: We use R0 here, because it isn't available for RA.
109     // We need to store the CR in the low 4-bits of the saved value.  First,
110     // issue a MFCR to save all of the CRBits.
111     BuildMI(MBB, MI, PPC::MFCR, 0, PPC::R0);
112     
113     // If the saved register wasn't CR0, shift the bits left so that they are in
114     // CR0's slot.
115     if (SrcReg != PPC::CR0) {
116       unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(SrcReg)*4;
117       // rlwinm r0, r0, ShiftBits, 0, 31.
118       BuildMI(MBB, MI, PPC::RLWINM, 4, PPC::R0)
119         .addReg(PPC::R0).addImm(ShiftBits).addImm(0).addImm(31);
120     }
121     
122     addFrameReference(BuildMI(MBB, MI, PPC::STW, 3).addReg(PPC::R0), FrameIdx);
123   } else if (RC == PPC::GPRCRegisterClass) {
124     addFrameReference(BuildMI(MBB, MI, PPC::STW, 3).addReg(SrcReg),FrameIdx);
125   } else if (RC == PPC::G8RCRegisterClass) {
126     addFrameReference(BuildMI(MBB, MI, PPC::STD, 3).addReg(SrcReg),FrameIdx);
127   } else if (RC == PPC::F8RCRegisterClass) {
128     addFrameReference(BuildMI(MBB, MI, PPC::STFD, 3).addReg(SrcReg),FrameIdx);
129   } else if (RC == PPC::F4RCRegisterClass) {
130     addFrameReference(BuildMI(MBB, MI, PPC::STFS, 3).addReg(SrcReg),FrameIdx);
131   } else if (RC == PPC::VRRCRegisterClass) {
132     // We don't have indexed addressing for vector loads.  Emit:
133     // R11 = ADDI FI#
134     // Dest = LVX R0, R11
135     // 
136     // FIXME: We use R0 here, because it isn't available for RA.
137     addFrameReference(BuildMI(MBB, MI, PPC::ADDI, 1, PPC::R0), FrameIdx, 0, 0);
138     BuildMI(MBB, MI, PPC::STVX, 3)
139       .addReg(SrcReg).addReg(PPC::R0).addReg(PPC::R0);
140   } else {
141     assert(0 && "Unknown regclass!");
142     abort();
143   }
144 }
145
146 void
147 PPCRegisterInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
148                                         MachineBasicBlock::iterator MI,
149                                         unsigned DestReg, int FrameIdx,
150                                         const TargetRegisterClass *RC) const {
151   if (DestReg == PPC::LR) {
152     addFrameReference(BuildMI(MBB, MI, PPC::LWZ, 2, PPC::R11), FrameIdx);
153     BuildMI(MBB, MI, PPC::MTLR, 1).addReg(PPC::R11);
154   } else if (RC == PPC::CRRCRegisterClass) {
155     // FIXME: We use R0 here, because it isn't available for RA.
156     addFrameReference(BuildMI(MBB, MI, PPC::LWZ, 2, PPC::R0), FrameIdx);
157     
158     // If the reloaded register isn't CR0, shift the bits right so that they are
159     // in the right CR's slot.
160     if (DestReg != PPC::CR0) {
161       unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(DestReg)*4;
162       // rlwinm r11, r11, 32-ShiftBits, 0, 31.
163       BuildMI(MBB, MI, PPC::RLWINM, 4, PPC::R0)
164         .addReg(PPC::R0).addImm(32-ShiftBits).addImm(0).addImm(31);
165     }
166     
167     BuildMI(MBB, MI, PPC::MTCRF, 1, DestReg).addReg(PPC::R0);
168   } else if (RC == PPC::GPRCRegisterClass) {
169     addFrameReference(BuildMI(MBB, MI, PPC::LWZ, 2, DestReg), FrameIdx);
170   } else if (RC == PPC::G8RCRegisterClass) {
171     addFrameReference(BuildMI(MBB, MI, PPC::LD, 2, DestReg), FrameIdx);
172   } else if (RC == PPC::F8RCRegisterClass) {
173     addFrameReference(BuildMI(MBB, MI, PPC::LFD, 2, DestReg), FrameIdx);
174   } else if (RC == PPC::F4RCRegisterClass) {
175     addFrameReference(BuildMI(MBB, MI, PPC::LFS, 2, DestReg), FrameIdx);
176   } else if (RC == PPC::VRRCRegisterClass) {
177     // We don't have indexed addressing for vector loads.  Emit:
178     // R11 = ADDI FI#
179     // Dest = LVX R0, R11
180     // 
181     // FIXME: We use R0 here, because it isn't available for RA.
182     addFrameReference(BuildMI(MBB, MI, PPC::ADDI, 1, PPC::R0), FrameIdx, 0, 0);
183     BuildMI(MBB, MI, PPC::LVX, 2, DestReg).addReg(PPC::R0).addReg(PPC::R0);
184   } else {
185     assert(0 && "Unknown regclass!");
186     abort();
187   }
188 }
189
190 void PPCRegisterInfo::copyRegToReg(MachineBasicBlock &MBB,
191                                    MachineBasicBlock::iterator MI,
192                                    unsigned DestReg, unsigned SrcReg,
193                                    const TargetRegisterClass *RC) const {
194   if (RC == PPC::GPRCRegisterClass) {
195     BuildMI(MBB, MI, PPC::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
196   } else if (RC == PPC::G8RCRegisterClass) {
197     BuildMI(MBB, MI, PPC::OR8, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
198   } else if (RC == PPC::F4RCRegisterClass) {
199     BuildMI(MBB, MI, PPC::FMRS, 1, DestReg).addReg(SrcReg);
200   } else if (RC == PPC::F8RCRegisterClass) {
201     BuildMI(MBB, MI, PPC::FMRD, 1, DestReg).addReg(SrcReg);
202   } else if (RC == PPC::CRRCRegisterClass) {
203     BuildMI(MBB, MI, PPC::MCRF, 1, DestReg).addReg(SrcReg);
204   } else if (RC == PPC::VRRCRegisterClass) {
205     BuildMI(MBB, MI, PPC::VOR, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
206   } else {
207     std::cerr << "Attempt to copy register that is not GPR or FPR";
208     abort();
209   }
210 }
211
212 const unsigned* PPCRegisterInfo::getCalleeSaveRegs() const {
213   // 32-bit Darwin calling convention. 
214   static const unsigned Darwin32_CalleeSaveRegs[] = {
215     PPC::R1 , PPC::R13, PPC::R14, PPC::R15,
216     PPC::R16, PPC::R17, PPC::R18, PPC::R19,
217     PPC::R20, PPC::R21, PPC::R22, PPC::R23,
218     PPC::R24, PPC::R25, PPC::R26, PPC::R27,
219     PPC::R28, PPC::R29, PPC::R30, PPC::R31,
220
221     PPC::F14, PPC::F15, PPC::F16, PPC::F17,
222     PPC::F18, PPC::F19, PPC::F20, PPC::F21,
223     PPC::F22, PPC::F23, PPC::F24, PPC::F25,
224     PPC::F26, PPC::F27, PPC::F28, PPC::F29,
225     PPC::F30, PPC::F31,
226     
227     PPC::CR2, PPC::CR3, PPC::CR4,
228     PPC::V20, PPC::V21, PPC::V22, PPC::V23,
229     PPC::V24, PPC::V25, PPC::V26, PPC::V27,
230     PPC::V28, PPC::V29, PPC::V30, PPC::V31,
231     
232     PPC::LR,  0
233   };
234   // 64-bit Darwin calling convention. 
235   static const unsigned Darwin64_CalleeSaveRegs[] = {
236     PPC::X1 , PPC::X13, PPC::X14, PPC::X15,
237     PPC::X16, PPC::X17, PPC::X18, PPC::X19,
238     PPC::X20, PPC::X21, PPC::X22, PPC::X23,
239     PPC::X24, PPC::X25, PPC::X26, PPC::X27,
240     PPC::X28, PPC::X29, PPC::X30, PPC::X31,
241     
242     PPC::F14, PPC::F15, PPC::F16, PPC::F17,
243     PPC::F18, PPC::F19, PPC::F20, PPC::F21,
244     PPC::F22, PPC::F23, PPC::F24, PPC::F25,
245     PPC::F26, PPC::F27, PPC::F28, PPC::F29,
246     PPC::F30, PPC::F31,
247     
248     PPC::CR2, PPC::CR3, PPC::CR4,
249     PPC::V20, PPC::V21, PPC::V22, PPC::V23,
250     PPC::V24, PPC::V25, PPC::V26, PPC::V27,
251     PPC::V28, PPC::V29, PPC::V30, PPC::V31,
252     
253     PPC::LR,  0
254   };
255   
256   return Subtarget.isPPC64() ? Darwin64_CalleeSaveRegs :
257                                Darwin32_CalleeSaveRegs;
258 }
259
260 const TargetRegisterClass* const*
261 PPCRegisterInfo::getCalleeSaveRegClasses() const {
262   // 32-bit Darwin calling convention. 
263   static const TargetRegisterClass * const Darwin32_CalleeSaveRegClasses[] = {
264     &PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,
265     &PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,
266     &PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,
267     &PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,
268     &PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,
269
270     &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
271     &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
272     &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
273     &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
274     &PPC::F8RCRegClass,&PPC::F8RCRegClass,
275     
276     &PPC::CRRCRegClass,&PPC::CRRCRegClass,&PPC::CRRCRegClass,
277     
278     &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
279     &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
280     &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
281     
282     &PPC::GPRCRegClass, 0
283   };
284   
285   // 64-bit Darwin calling convention. 
286   static const TargetRegisterClass * const Darwin64_CalleeSaveRegClasses[] = {
287     &PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,
288     &PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,
289     &PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,
290     &PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,
291     &PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,
292     
293     &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
294     &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
295     &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
296     &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
297     &PPC::F8RCRegClass,&PPC::F8RCRegClass,
298     
299     &PPC::CRRCRegClass,&PPC::CRRCRegClass,&PPC::CRRCRegClass,
300     
301     &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
302     &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
303     &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
304     
305     &PPC::GPRCRegClass, 0
306   };
307  
308   return Subtarget.isPPC64() ? Darwin64_CalleeSaveRegClasses :
309                                Darwin32_CalleeSaveRegClasses;
310 }
311
312 /// foldMemoryOperand - PowerPC (like most RISC's) can only fold spills into
313 /// copy instructions, turning them into load/store instructions.
314 MachineInstr *PPCRegisterInfo::foldMemoryOperand(MachineInstr *MI,
315                                                  unsigned OpNum,
316                                                  int FrameIndex) const {
317   // Make sure this is a reg-reg copy.  Note that we can't handle MCRF, because
318   // it takes more than one instruction to store it.
319   unsigned Opc = MI->getOpcode();
320   
321   if ((Opc == PPC::OR &&
322        MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
323     if (OpNum == 0) {  // move -> store
324       unsigned InReg = MI->getOperand(1).getReg();
325       return addFrameReference(BuildMI(PPC::STW,
326                                        3).addReg(InReg), FrameIndex);
327     } else {           // move -> load
328       unsigned OutReg = MI->getOperand(0).getReg();
329       return addFrameReference(BuildMI(PPC::LWZ, 2, OutReg), FrameIndex);
330     }
331   } else if ((Opc == PPC::OR8 &&
332               MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
333     if (OpNum == 0) {  // move -> store
334       unsigned InReg = MI->getOperand(1).getReg();
335       return addFrameReference(BuildMI(PPC::STD,
336                                        3).addReg(InReg), FrameIndex);
337     } else {           // move -> load
338       unsigned OutReg = MI->getOperand(0).getReg();
339       return addFrameReference(BuildMI(PPC::LD, 2, OutReg), FrameIndex);
340     }
341   } else if (Opc == PPC::FMRD) {
342     if (OpNum == 0) {  // move -> store
343       unsigned InReg = MI->getOperand(1).getReg();
344       return addFrameReference(BuildMI(PPC::STFD,
345                                        3).addReg(InReg), FrameIndex);
346     } else {           // move -> load
347       unsigned OutReg = MI->getOperand(0).getReg();
348       return addFrameReference(BuildMI(PPC::LFD, 2, OutReg), FrameIndex);
349     }
350   } else if (Opc == PPC::FMRS) {
351     if (OpNum == 0) {  // move -> store
352       unsigned InReg = MI->getOperand(1).getReg();
353       return addFrameReference(BuildMI(PPC::STFS,
354                                        3).addReg(InReg), FrameIndex);
355     } else {           // move -> load
356       unsigned OutReg = MI->getOperand(0).getReg();
357       return addFrameReference(BuildMI(PPC::LFS, 2, OutReg), FrameIndex);
358     }
359   }
360   return 0;
361 }
362
363 //===----------------------------------------------------------------------===//
364 // Stack Frame Processing methods
365 //===----------------------------------------------------------------------===//
366
367 // hasFP - Return true if the specified function should have a dedicated frame
368 // pointer register.  This is true if the function has variable sized allocas or
369 // if frame pointer elimination is disabled.
370 //
371 static bool hasFP(const MachineFunction &MF) {
372   const MachineFrameInfo *MFI = MF.getFrameInfo();
373
374   // If frame pointers are forced, or if there are variable sized stack objects,
375   // use a frame pointer.
376   // 
377   return NoFramePointerElim || MFI->hasVarSizedObjects();
378 }
379
380 void PPCRegisterInfo::
381 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
382                               MachineBasicBlock::iterator I) const {
383   if (hasFP(MF)) {
384     // If we have a frame pointer, convert as follows:
385     // ADJCALLSTACKDOWN -> addi, r1, r1, -amount
386     // ADJCALLSTACKUP   -> addi, r1, r1, amount
387     MachineInstr *Old = I;
388     unsigned Amount = Old->getOperand(0).getImmedValue();
389     if (Amount != 0) {
390       // We need to keep the stack aligned properly.  To do this, we round the
391       // amount of space needed for the outgoing arguments up to the next
392       // alignment boundary.
393       unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
394       Amount = (Amount+Align-1)/Align*Align;
395
396       // Replace the pseudo instruction with a new instruction...
397       if (Old->getOpcode() == PPC::ADJCALLSTACKDOWN) {
398         BuildMI(MBB, I, PPC::ADDI, 2, PPC::R1).addReg(PPC::R1).addImm(-Amount);
399       } else {
400         assert(Old->getOpcode() == PPC::ADJCALLSTACKUP);
401         BuildMI(MBB, I, PPC::ADDI, 2, PPC::R1).addReg(PPC::R1).addImm(Amount);
402       }
403     }
404   }
405   MBB.erase(I);
406 }
407
408 void
409 PPCRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II) const {
410   unsigned i = 0;
411   MachineInstr &MI = *II;
412   MachineBasicBlock &MBB = *MI.getParent();
413   MachineFunction &MF = *MBB.getParent();
414
415   while (!MI.getOperand(i).isFrameIndex()) {
416     ++i;
417     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
418   }
419
420   int FrameIndex = MI.getOperand(i).getFrameIndex();
421
422   // Replace the FrameIndex with base register with GPR1 (SP) or GPR31 (FP).
423   MI.getOperand(i).ChangeToRegister(hasFP(MF) ? PPC::R31 : PPC::R1, false);
424
425   // Take into account whether it's an add or mem instruction
426   unsigned OffIdx = (i == 2) ? 1 : 2;
427
428   // Figure out if the offset in the instruction is shifted right two bits. This
429   // is true for instructions like "STD", which the machine implicitly adds two
430   // low zeros to.
431   bool isIXAddr = false;
432   switch (MI.getOpcode()) {
433   case PPC::LWA:
434   case PPC::LD:
435   case PPC::STD:
436   case PPC::STD_32:
437     isIXAddr = true;
438     break;
439   }
440   
441   
442   // Now add the frame object offset to the offset from r1.
443   int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex);
444   
445   if (!isIXAddr)
446     Offset += MI.getOperand(OffIdx).getImmedValue();
447   else
448     Offset += MI.getOperand(OffIdx).getImmedValue() << 2;
449
450   // If we're not using a Frame Pointer that has been set to the value of the
451   // SP before having the stack size subtracted from it, then add the stack size
452   // to Offset to get the correct offset.
453   Offset += MF.getFrameInfo()->getStackSize();
454
455   if (Offset > 32767 || Offset < -32768) {
456     // Insert a set of r0 with the full offset value before the ld, st, or add
457     MachineBasicBlock *MBB = MI.getParent();
458     BuildMI(*MBB, II, PPC::LIS, 1, PPC::R0).addImm(Offset >> 16);
459     BuildMI(*MBB, II, PPC::ORI, 2, PPC::R0).addReg(PPC::R0).addImm(Offset);
460     
461     // convert into indexed form of the instruction
462     // sth 0:rA, 1:imm 2:(rB) ==> sthx 0:rA, 2:rB, 1:r0
463     // addi 0:rA 1:rB, 2, imm ==> add 0:rA, 1:rB, 2:r0
464     assert(ImmToIdxMap.count(MI.getOpcode()) &&
465            "No indexed form of load or store available!");
466     unsigned NewOpcode = ImmToIdxMap.find(MI.getOpcode())->second;
467     MI.setOpcode(NewOpcode);
468     MI.getOperand(1).ChangeToRegister(MI.getOperand(i).getReg(), false);
469     MI.getOperand(2).ChangeToRegister(PPC::R0, false);
470   } else {
471     if (isIXAddr) {
472       assert((Offset & 3) == 0 && "Invalid frame offset!");
473       Offset >>= 2;    // The actual encoded value has the low two bits zero.
474     }
475     MI.getOperand(OffIdx).ChangeToImmediate(Offset);
476   }
477 }
478
479 /// VRRegNo - Map from a numbered VR register to its enum value.
480 ///
481 static const unsigned short VRRegNo[] = {
482  PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 , PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
483  PPC::V8 , PPC::V9 , PPC::V10, PPC::V11, PPC::V12, PPC::V13, PPC::V14, PPC::V15,
484  PPC::V16, PPC::V17, PPC::V18, PPC::V19, PPC::V20, PPC::V21, PPC::V22, PPC::V23,
485  PPC::V24, PPC::V25, PPC::V26, PPC::V27, PPC::V28, PPC::V29, PPC::V30, PPC::V31
486 };
487
488 /// RemoveVRSaveCode - We have found that this function does not need any code
489 /// to manipulate the VRSAVE register, even though it uses vector registers.
490 /// This can happen when the only registers used are known to be live in or out
491 /// of the function.  Remove all of the VRSAVE related code from the function.
492 static void RemoveVRSaveCode(MachineInstr *MI) {
493   MachineBasicBlock *Entry = MI->getParent();
494   MachineFunction *MF = Entry->getParent();
495
496   // We know that the MTVRSAVE instruction immediately follows MI.  Remove it.
497   MachineBasicBlock::iterator MBBI = MI;
498   ++MBBI;
499   assert(MBBI != Entry->end() && MBBI->getOpcode() == PPC::MTVRSAVE);
500   MBBI->eraseFromParent();
501   
502   bool RemovedAllMTVRSAVEs = true;
503   // See if we can find and remove the MTVRSAVE instruction from all of the
504   // epilog blocks.
505   const TargetInstrInfo &TII = *MF->getTarget().getInstrInfo();
506   for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E; ++I) {
507     // If last instruction is a return instruction, add an epilogue
508     if (!I->empty() && TII.isReturn(I->back().getOpcode())) {
509       bool FoundIt = false;
510       for (MBBI = I->end(); MBBI != I->begin(); ) {
511         --MBBI;
512         if (MBBI->getOpcode() == PPC::MTVRSAVE) {
513           MBBI->eraseFromParent();  // remove it.
514           FoundIt = true;
515           break;
516         }
517       }
518       RemovedAllMTVRSAVEs &= FoundIt;
519     }
520   }
521
522   // If we found and removed all MTVRSAVE instructions, remove the read of
523   // VRSAVE as well.
524   if (RemovedAllMTVRSAVEs) {
525     MBBI = MI;
526     assert(MBBI != Entry->begin() && "UPDATE_VRSAVE is first instr in block?");
527     --MBBI;
528     assert(MBBI->getOpcode() == PPC::MFVRSAVE && "VRSAVE instrs wandered?");
529     MBBI->eraseFromParent();
530   }
531   
532   // Finally, nuke the UPDATE_VRSAVE.
533   MI->eraseFromParent();
534 }
535
536 // HandleVRSaveUpdate - MI is the UPDATE_VRSAVE instruction introduced by the
537 // instruction selector.  Based on the vector registers that have been used,
538 // transform this into the appropriate ORI instruction.
539 static void HandleVRSaveUpdate(MachineInstr *MI, const bool *UsedRegs) {
540   unsigned UsedRegMask = 0;
541   for (unsigned i = 0; i != 32; ++i)
542     if (UsedRegs[VRRegNo[i]])
543       UsedRegMask |= 1 << (31-i);
544   
545   // Live in and live out values already must be in the mask, so don't bother
546   // marking them.
547   MachineFunction *MF = MI->getParent()->getParent();
548   for (MachineFunction::livein_iterator I = 
549        MF->livein_begin(), E = MF->livein_end(); I != E; ++I) {
550     unsigned RegNo = PPCRegisterInfo::getRegisterNumbering(I->first);
551     if (VRRegNo[RegNo] == I->first)        // If this really is a vector reg.
552       UsedRegMask &= ~(1 << (31-RegNo));   // Doesn't need to be marked.
553   }
554   for (MachineFunction::liveout_iterator I = 
555        MF->liveout_begin(), E = MF->liveout_end(); I != E; ++I) {
556     unsigned RegNo = PPCRegisterInfo::getRegisterNumbering(*I);
557     if (VRRegNo[RegNo] == *I)              // If this really is a vector reg.
558       UsedRegMask &= ~(1 << (31-RegNo));   // Doesn't need to be marked.
559   }
560   
561   unsigned SrcReg = MI->getOperand(1).getReg();
562   unsigned DstReg = MI->getOperand(0).getReg();
563   // If no registers are used, turn this into a copy.
564   if (UsedRegMask == 0) {
565     // Remove all VRSAVE code.
566     RemoveVRSaveCode(MI);
567     return;
568   } else if ((UsedRegMask & 0xFFFF) == UsedRegMask) {
569     BuildMI(*MI->getParent(), MI, PPC::ORI, 2, DstReg)
570         .addReg(SrcReg).addImm(UsedRegMask);
571   } else if ((UsedRegMask & 0xFFFF0000) == UsedRegMask) {
572     BuildMI(*MI->getParent(), MI, PPC::ORIS, 2, DstReg)
573         .addReg(SrcReg).addImm(UsedRegMask >> 16);
574   } else {
575     BuildMI(*MI->getParent(), MI, PPC::ORIS, 2, DstReg)
576        .addReg(SrcReg).addImm(UsedRegMask >> 16);
577     BuildMI(*MI->getParent(), MI, PPC::ORI, 2, DstReg)
578       .addReg(DstReg).addImm(UsedRegMask & 0xFFFF);
579   }
580   
581   // Remove the old UPDATE_VRSAVE instruction.
582   MI->eraseFromParent();
583 }
584
585
586 void PPCRegisterInfo::emitPrologue(MachineFunction &MF) const {
587   MachineBasicBlock &MBB = MF.front();   // Prolog goes in entry BB
588   MachineBasicBlock::iterator MBBI = MBB.begin();
589   MachineFrameInfo *MFI = MF.getFrameInfo();
590   MachineDebugInfo *DebugInfo = MFI->getMachineDebugInfo();
591   
592   // Do we have a frame pointer for this function?
593   bool HasFP = hasFP(MF);
594
595   // Scan the prolog, looking for an UPDATE_VRSAVE instruction.  If we find it,
596   // process it.
597   for (unsigned i = 0; MBBI != MBB.end(); ++i, ++MBBI) {
598     if (MBBI->getOpcode() == PPC::UPDATE_VRSAVE) {
599       HandleVRSaveUpdate(MBBI, MF.getUsedPhysregs());
600       break;
601     }
602   }
603   
604   // Move MBBI back to the beginning of the function.
605   MBBI = MBB.begin();
606   
607   // Get the number of bytes to allocate from the FrameInfo
608   unsigned NumBytes = MFI->getStackSize();
609   
610   // Get the alignments provided by the target, and the maximum alignment
611   // (if any) of the fixed frame objects.
612   unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
613   unsigned MaxAlign = MFI->getMaxAlignment();
614
615   // If we have calls, we cannot use the red zone to store callee save registers
616   // and we must set up a stack frame, so calculate the necessary size here.
617   if (MFI->hasCalls()) {
618     // We reserve argument space for call sites in the function immediately on
619     // entry to the current function.  This eliminates the need for add/sub
620     // brackets around call sites.
621     NumBytes += MFI->getMaxCallFrameSize();
622   }
623
624   // If we are a leaf function, and use up to 224 bytes of stack space,
625   // and don't have a frame pointer, then we do not need to adjust the stack
626   // pointer (we fit in the Red Zone).
627   if ((NumBytes == 0) || (NumBytes <= 224 && !HasFP && !MFI->hasCalls() &&
628                           MaxAlign <= TargetAlign)) {
629     MFI->setStackSize(0);
630     return;
631   }
632
633   // Add the size of R1 to  NumBytes size for the store of R1 to the bottom
634   // of the stack and round the size to a multiple of the alignment.
635   unsigned Align = std::max(TargetAlign, MaxAlign);
636   unsigned GPRSize = 4;
637   unsigned Size = HasFP ? GPRSize + GPRSize : GPRSize;
638   NumBytes = (NumBytes+Size+Align-1)/Align*Align;
639
640   // Update frame info to pretend that this is part of the stack...
641   MFI->setStackSize(NumBytes);
642   int NegNumbytes = -NumBytes;
643
644   // Adjust stack pointer: r1 -= numbytes.
645   // If there is a preferred stack alignment, align R1 now
646   if (MaxAlign > TargetAlign) {
647     assert(isPowerOf2_32(MaxAlign) && MaxAlign < 32767 && "Invalid alignment!");
648     assert(isInt16(0-NumBytes) && "Unhandled stack size and alignment!");
649     BuildMI(MBB, MBBI, PPC::RLWINM, 4, PPC::R0)
650       .addReg(PPC::R1).addImm(0).addImm(32-Log2_32(MaxAlign)).addImm(31);
651     BuildMI(MBB, MBBI, PPC::SUBFIC,2,PPC::R0).addReg(PPC::R0)
652       .addImm(0-NumBytes);
653     BuildMI(MBB, MBBI, PPC::STWUX, 3)
654       .addReg(PPC::R1).addReg(PPC::R1).addReg(PPC::R0);
655   } else if (NumBytes <= 32768) {
656     BuildMI(MBB, MBBI, PPC::STWU, 3).addReg(PPC::R1).addImm(NegNumbytes)
657       .addReg(PPC::R1);
658   } else {
659     BuildMI(MBB, MBBI, PPC::LIS, 1, PPC::R0).addImm(NegNumbytes >> 16);
660     BuildMI(MBB, MBBI, PPC::ORI, 2, PPC::R0).addReg(PPC::R0)
661       .addImm(NegNumbytes & 0xFFFF);
662     BuildMI(MBB, MBBI, PPC::STWUX, 3).addReg(PPC::R1).addReg(PPC::R1)
663       .addReg(PPC::R0);
664   }
665   
666   if (DebugInfo && DebugInfo->hasInfo()) {
667     std::vector<MachineMove *> &Moves = DebugInfo->getFrameMoves();
668     unsigned LabelID = DebugInfo->NextLabelID();
669     
670     // Mark effective beginning of when frame pointer becomes valid.
671     BuildMI(MBB, MBBI, PPC::DWARF_LABEL, 1).addImm(LabelID);
672     
673     // Show update of SP.
674     MachineLocation SPDst(MachineLocation::VirtualFP);
675     MachineLocation SPSrc(MachineLocation::VirtualFP, NegNumbytes);
676     Moves.push_back(new MachineMove(LabelID, SPDst, SPSrc));
677
678     // Add callee saved registers to move list.
679     const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
680     for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
681       MachineLocation CSDst(MachineLocation::VirtualFP,
682                             MFI->getObjectOffset(CSI[I].getFrameIdx()));
683       MachineLocation CSSrc(CSI[I].getReg());
684       Moves.push_back(new MachineMove(LabelID, CSDst, CSSrc));
685     }
686   }
687   
688   // If there is a frame pointer, copy R1 (SP) into R31 (FP)
689   if (HasFP) {
690     BuildMI(MBB, MBBI, PPC::STW, 3)
691       .addReg(PPC::R31).addImm(GPRSize).addReg(PPC::R1);
692     BuildMI(MBB, MBBI, PPC::OR, 2, PPC::R31).addReg(PPC::R1).addReg(PPC::R1);
693   }
694 }
695
696 void PPCRegisterInfo::emitEpilogue(MachineFunction &MF,
697                                    MachineBasicBlock &MBB) const {
698   MachineBasicBlock::iterator MBBI = prior(MBB.end());
699   assert(MBBI->getOpcode() == PPC::BLR &&
700          "Can only insert epilog into returning blocks");
701
702   // Get alignment info so we know how to restore r1
703   const MachineFrameInfo *MFI = MF.getFrameInfo();
704   unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
705
706   // Get the number of bytes allocated from the FrameInfo.
707   unsigned NumBytes = MFI->getStackSize();
708   unsigned GPRSize = 4; 
709
710   if (NumBytes != 0) {
711     // If this function has a frame pointer, load the saved stack pointer from
712     // its stack slot.
713     if (hasFP(MF)) {
714       BuildMI(MBB, MBBI, PPC::LWZ, 2, PPC::R31)
715           .addImm(GPRSize).addReg(PPC::R31);
716     }
717     
718     // The loaded (or persistent) stack pointer value is offseted by the 'stwu'
719     // on entry to the function.  Add this offset back now.
720     if (NumBytes < 32768 && TargetAlign >= MFI->getMaxAlignment()) {
721       BuildMI(MBB, MBBI, PPC::ADDI, 2, PPC::R1)
722           .addReg(PPC::R1).addImm(NumBytes);
723     } else {
724       BuildMI(MBB, MBBI, PPC::LWZ, 2, PPC::R1).addImm(0).addReg(PPC::R1);
725     }
726   }
727 }
728
729 unsigned PPCRegisterInfo::getRARegister() const {
730   return PPC::LR;
731 }
732
733 unsigned PPCRegisterInfo::getFrameRegister(MachineFunction &MF) const {
734   return hasFP(MF) ? PPC::R31 : PPC::R1;
735 }
736
737 void PPCRegisterInfo::getInitialFrameState(std::vector<MachineMove *> &Moves)
738                                                                          const {
739   // Initial state of the frame pointer is R1.
740   MachineLocation Dst(MachineLocation::VirtualFP);
741   MachineLocation Src(PPC::R1, 0);
742   Moves.push_back(new MachineMove(0, Dst, Src));
743 }
744
745 #include "PPCGenRegisterInfo.inc"
746