don't include CR bit subregs in callee-saved list
[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 is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the PowerPC implementation of the TargetRegisterInfo
11 // class.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #define DEBUG_TYPE "reginfo"
16 #include "PPC.h"
17 #include "PPCInstrBuilder.h"
18 #include "PPCMachineFunctionInfo.h"
19 #include "PPCRegisterInfo.h"
20 #include "PPCFrameLowering.h"
21 #include "PPCSubtarget.h"
22 #include "llvm/CallingConv.h"
23 #include "llvm/Constants.h"
24 #include "llvm/Function.h"
25 #include "llvm/Type.h"
26 #include "llvm/CodeGen/ValueTypes.h"
27 #include "llvm/CodeGen/MachineInstrBuilder.h"
28 #include "llvm/CodeGen/MachineModuleInfo.h"
29 #include "llvm/CodeGen/MachineFunction.h"
30 #include "llvm/CodeGen/MachineFrameInfo.h"
31 #include "llvm/CodeGen/MachineRegisterInfo.h"
32 #include "llvm/CodeGen/RegisterScavenging.h"
33 #include "llvm/Target/TargetFrameLowering.h"
34 #include "llvm/Target/TargetInstrInfo.h"
35 #include "llvm/Target/TargetMachine.h"
36 #include "llvm/Target/TargetOptions.h"
37 #include "llvm/Support/CommandLine.h"
38 #include "llvm/Support/Debug.h"
39 #include "llvm/Support/ErrorHandling.h"
40 #include "llvm/Support/MathExtras.h"
41 #include "llvm/Support/raw_ostream.h"
42 #include "llvm/ADT/BitVector.h"
43 #include "llvm/ADT/STLExtras.h"
44 #include <cstdlib>
45
46 #define GET_REGINFO_TARGET_DESC
47 #include "PPCGenRegisterInfo.inc"
48
49 // FIXME (64-bit): Eventually enable by default.
50 namespace llvm {
51 cl::opt<bool> EnablePPC32RS("enable-ppc32-regscavenger",
52                                    cl::init(false),
53                                    cl::desc("Enable PPC32 register scavenger"),
54                                    cl::Hidden);
55 cl::opt<bool> EnablePPC64RS("enable-ppc64-regscavenger",
56                                    cl::init(false),
57                                    cl::desc("Enable PPC64 register scavenger"),
58                                    cl::Hidden);
59 }
60
61 using namespace llvm;
62
63 // FIXME (64-bit): Should be inlined.
64 bool
65 PPCRegisterInfo::requiresRegisterScavenging(const MachineFunction &) const {
66   return ((EnablePPC32RS && !Subtarget.isPPC64()) ||
67           (EnablePPC64RS && Subtarget.isPPC64()));
68 }
69
70 PPCRegisterInfo::PPCRegisterInfo(const PPCSubtarget &ST,
71                                  const TargetInstrInfo &tii)
72   : PPCGenRegisterInfo(ST.isPPC64() ? PPC::LR8 : PPC::LR,
73                        ST.isPPC64() ? 0 : 1,
74                        ST.isPPC64() ? 0 : 1),
75     Subtarget(ST), TII(tii) {
76   ImmToIdxMap[PPC::LD]   = PPC::LDX;    ImmToIdxMap[PPC::STD]  = PPC::STDX;
77   ImmToIdxMap[PPC::LBZ]  = PPC::LBZX;   ImmToIdxMap[PPC::STB]  = PPC::STBX;
78   ImmToIdxMap[PPC::LHZ]  = PPC::LHZX;   ImmToIdxMap[PPC::LHA]  = PPC::LHAX;
79   ImmToIdxMap[PPC::LWZ]  = PPC::LWZX;   ImmToIdxMap[PPC::LWA]  = PPC::LWAX;
80   ImmToIdxMap[PPC::LFS]  = PPC::LFSX;   ImmToIdxMap[PPC::LFD]  = PPC::LFDX;
81   ImmToIdxMap[PPC::STH]  = PPC::STHX;   ImmToIdxMap[PPC::STW]  = PPC::STWX;
82   ImmToIdxMap[PPC::STFS] = PPC::STFSX;  ImmToIdxMap[PPC::STFD] = PPC::STFDX;
83   ImmToIdxMap[PPC::ADDI] = PPC::ADD4;
84
85   // 64-bit
86   ImmToIdxMap[PPC::LHA8] = PPC::LHAX8; ImmToIdxMap[PPC::LBZ8] = PPC::LBZX8;
87   ImmToIdxMap[PPC::LHZ8] = PPC::LHZX8; ImmToIdxMap[PPC::LWZ8] = PPC::LWZX8;
88   ImmToIdxMap[PPC::STB8] = PPC::STBX8; ImmToIdxMap[PPC::STH8] = PPC::STHX8;
89   ImmToIdxMap[PPC::STW8] = PPC::STWX8; ImmToIdxMap[PPC::STDU] = PPC::STDUX;
90   ImmToIdxMap[PPC::ADDI8] = PPC::ADD8; ImmToIdxMap[PPC::STD_32] = PPC::STDX_32;
91 }
92
93 /// getPointerRegClass - Return the register class to use to hold pointers.
94 /// This is used for addressing modes.
95 const TargetRegisterClass *
96 PPCRegisterInfo::getPointerRegClass(unsigned Kind) const {
97   if (Subtarget.isPPC64())
98     return &PPC::G8RCRegClass;
99   return &PPC::GPRCRegClass;
100 }
101
102 const unsigned*
103 PPCRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
104   // 32-bit Darwin calling convention. 
105   static const unsigned Darwin32_CalleeSavedRegs[] = {
106               PPC::R13, PPC::R14, PPC::R15,
107     PPC::R16, PPC::R17, PPC::R18, PPC::R19,
108     PPC::R20, PPC::R21, PPC::R22, PPC::R23,
109     PPC::R24, PPC::R25, PPC::R26, PPC::R27,
110     PPC::R28, PPC::R29, PPC::R30, PPC::R31,
111
112     PPC::F14, PPC::F15, PPC::F16, PPC::F17,
113     PPC::F18, PPC::F19, PPC::F20, PPC::F21,
114     PPC::F22, PPC::F23, PPC::F24, PPC::F25,
115     PPC::F26, PPC::F27, PPC::F28, PPC::F29,
116     PPC::F30, PPC::F31,
117     
118     PPC::CR2, PPC::CR3, PPC::CR4,
119     PPC::V20, PPC::V21, PPC::V22, PPC::V23,
120     PPC::V24, PPC::V25, PPC::V26, PPC::V27,
121     PPC::V28, PPC::V29, PPC::V30, PPC::V31,
122     
123     PPC::LR,  0
124   };
125
126   // 32-bit SVR4 calling convention.
127   static const unsigned SVR4_CalleeSavedRegs[] = {
128                         PPC::R14, PPC::R15,
129     PPC::R16, PPC::R17, PPC::R18, PPC::R19,
130     PPC::R20, PPC::R21, PPC::R22, PPC::R23,
131     PPC::R24, PPC::R25, PPC::R26, PPC::R27,
132     PPC::R28, PPC::R29, PPC::R30, PPC::R31,
133
134     PPC::F14, PPC::F15, PPC::F16, PPC::F17,
135     PPC::F18, PPC::F19, PPC::F20, PPC::F21,
136     PPC::F22, PPC::F23, PPC::F24, PPC::F25,
137     PPC::F26, PPC::F27, PPC::F28, PPC::F29,
138     PPC::F30, PPC::F31,
139     
140     PPC::CR2, PPC::CR3, PPC::CR4,
141     
142     PPC::VRSAVE,
143     
144     PPC::V20, PPC::V21, PPC::V22, PPC::V23,
145     PPC::V24, PPC::V25, PPC::V26, PPC::V27,
146     PPC::V28, PPC::V29, PPC::V30, PPC::V31,
147     
148     0
149   };
150   // 64-bit Darwin calling convention. 
151   static const unsigned Darwin64_CalleeSavedRegs[] = {
152     PPC::X14, PPC::X15,
153     PPC::X16, PPC::X17, PPC::X18, PPC::X19,
154     PPC::X20, PPC::X21, PPC::X22, PPC::X23,
155     PPC::X24, PPC::X25, PPC::X26, PPC::X27,
156     PPC::X28, PPC::X29, PPC::X30, PPC::X31,
157     
158     PPC::F14, PPC::F15, PPC::F16, PPC::F17,
159     PPC::F18, PPC::F19, PPC::F20, PPC::F21,
160     PPC::F22, PPC::F23, PPC::F24, PPC::F25,
161     PPC::F26, PPC::F27, PPC::F28, PPC::F29,
162     PPC::F30, PPC::F31,
163     
164     PPC::CR2, PPC::CR3, PPC::CR4,
165     PPC::V20, PPC::V21, PPC::V22, PPC::V23,
166     PPC::V24, PPC::V25, PPC::V26, PPC::V27,
167     PPC::V28, PPC::V29, PPC::V30, PPC::V31,
168     
169     PPC::LR8,  0
170   };
171
172   // 64-bit SVR4 calling convention.
173   static const unsigned SVR4_64_CalleeSavedRegs[] = {
174     PPC::X14, PPC::X15,
175     PPC::X16, PPC::X17, PPC::X18, PPC::X19,
176     PPC::X20, PPC::X21, PPC::X22, PPC::X23,
177     PPC::X24, PPC::X25, PPC::X26, PPC::X27,
178     PPC::X28, PPC::X29, PPC::X30, PPC::X31,
179
180     PPC::F14, PPC::F15, PPC::F16, PPC::F17,
181     PPC::F18, PPC::F19, PPC::F20, PPC::F21,
182     PPC::F22, PPC::F23, PPC::F24, PPC::F25,
183     PPC::F26, PPC::F27, PPC::F28, PPC::F29,
184     PPC::F30, PPC::F31,
185
186     PPC::CR2, PPC::CR3, PPC::CR4,
187
188     PPC::VRSAVE,
189
190     PPC::V20, PPC::V21, PPC::V22, PPC::V23,
191     PPC::V24, PPC::V25, PPC::V26, PPC::V27,
192     PPC::V28, PPC::V29, PPC::V30, PPC::V31,
193
194     0
195   };
196   
197   if (Subtarget.isDarwinABI())
198     return Subtarget.isPPC64() ? Darwin64_CalleeSavedRegs :
199                                  Darwin32_CalleeSavedRegs;
200
201   return Subtarget.isPPC64() ? SVR4_64_CalleeSavedRegs : SVR4_CalleeSavedRegs;
202 }
203
204 BitVector PPCRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
205   BitVector Reserved(getNumRegs());
206   const PPCFrameLowering *PPCFI =
207     static_cast<const PPCFrameLowering*>(MF.getTarget().getFrameLowering());
208
209   Reserved.set(PPC::R0);
210   Reserved.set(PPC::R1);
211   Reserved.set(PPC::LR);
212   Reserved.set(PPC::LR8);
213   Reserved.set(PPC::RM);
214
215   // The SVR4 ABI reserves r2 and r13
216   if (Subtarget.isSVR4ABI()) {
217     Reserved.set(PPC::R2);  // System-reserved register
218     Reserved.set(PPC::R13); // Small Data Area pointer register
219   }
220   // Reserve R2 on Darwin to hack around the problem of save/restore of CR
221   // when the stack frame is too big to address directly; we need two regs.
222   // This is a hack.
223   if (Subtarget.isDarwinABI()) {
224     Reserved.set(PPC::R2);
225   }
226   
227   // On PPC64, r13 is the thread pointer. Never allocate this register.
228   // Note that this is over conservative, as it also prevents allocation of R31
229   // when the FP is not needed.
230   if (Subtarget.isPPC64()) {
231     Reserved.set(PPC::R13);
232     Reserved.set(PPC::R31);
233
234     if (!requiresRegisterScavenging(MF))
235       Reserved.set(PPC::R0);    // FIXME (64-bit): Remove
236
237     Reserved.set(PPC::X0);
238     Reserved.set(PPC::X1);
239     Reserved.set(PPC::X13);
240     Reserved.set(PPC::X31);
241
242     // The 64-bit SVR4 ABI reserves r2 for the TOC pointer.
243     if (Subtarget.isSVR4ABI()) {
244       Reserved.set(PPC::X2);
245     }
246     // Reserve R2 on Darwin to hack around the problem of save/restore of CR
247     // when the stack frame is too big to address directly; we need two regs.
248     // This is a hack.
249     if (Subtarget.isDarwinABI()) {
250       Reserved.set(PPC::X2);
251     }
252   }
253
254   if (PPCFI->needsFP(MF))
255     Reserved.set(PPC::R31);
256
257   return Reserved;
258 }
259
260 unsigned
261 PPCRegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC,
262                                          MachineFunction &MF) const {
263   const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
264   const unsigned DefaultSafety = 1;
265
266   switch (RC->getID()) {
267   default:
268     return 0;
269   case PPC::G8RCRegClassID:
270   case PPC::GPRCRegClassID: {
271     unsigned FP = TFI->hasFP(MF) ? 1 : 0;
272     return 32 - FP - DefaultSafety;
273   }
274   case PPC::F8RCRegClassID:
275   case PPC::F4RCRegClassID:
276   case PPC::VRRCRegClassID:
277     return 32 - DefaultSafety;
278   case PPC::CRRCRegClassID:
279     return 8 - DefaultSafety;
280   }
281 }
282
283 //===----------------------------------------------------------------------===//
284 // Stack Frame Processing methods
285 //===----------------------------------------------------------------------===//
286
287 void PPCRegisterInfo::
288 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
289                               MachineBasicBlock::iterator I) const {
290   if (MF.getTarget().Options.GuaranteedTailCallOpt &&
291       I->getOpcode() == PPC::ADJCALLSTACKUP) {
292     // Add (actually subtract) back the amount the callee popped on return.
293     if (int CalleeAmt =  I->getOperand(1).getImm()) {
294       bool is64Bit = Subtarget.isPPC64();
295       CalleeAmt *= -1;
296       unsigned StackReg = is64Bit ? PPC::X1 : PPC::R1;
297       unsigned TmpReg = is64Bit ? PPC::X0 : PPC::R0;
298       unsigned ADDIInstr = is64Bit ? PPC::ADDI8 : PPC::ADDI;
299       unsigned ADDInstr = is64Bit ? PPC::ADD8 : PPC::ADD4;
300       unsigned LISInstr = is64Bit ? PPC::LIS8 : PPC::LIS;
301       unsigned ORIInstr = is64Bit ? PPC::ORI8 : PPC::ORI;
302       MachineInstr *MI = I;
303       DebugLoc dl = MI->getDebugLoc();
304
305       if (isInt<16>(CalleeAmt)) {
306         BuildMI(MBB, I, dl, TII.get(ADDIInstr), StackReg).addReg(StackReg).
307           addImm(CalleeAmt);
308       } else {
309         MachineBasicBlock::iterator MBBI = I;
310         BuildMI(MBB, MBBI, dl, TII.get(LISInstr), TmpReg)
311           .addImm(CalleeAmt >> 16);
312         BuildMI(MBB, MBBI, dl, TII.get(ORIInstr), TmpReg)
313           .addReg(TmpReg, RegState::Kill)
314           .addImm(CalleeAmt & 0xFFFF);
315         BuildMI(MBB, MBBI, dl, TII.get(ADDInstr))
316           .addReg(StackReg)
317           .addReg(StackReg)
318           .addReg(TmpReg);
319       }
320     }
321   }
322   // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions.
323   MBB.erase(I);
324 }
325
326 /// findScratchRegister - Find a 'free' PPC register. Try for a call-clobbered
327 /// register first and then a spilled callee-saved register if that fails.
328 static
329 unsigned findScratchRegister(MachineBasicBlock::iterator II, RegScavenger *RS,
330                              const TargetRegisterClass *RC, int SPAdj) {
331   assert(RS && "Register scavenging must be on");
332   unsigned Reg = RS->FindUnusedReg(RC);
333   // FIXME: move ARM callee-saved reg scan to target independent code, then 
334   // search for already spilled CS register here.
335   if (Reg == 0)
336     Reg = RS->scavengeRegister(RC, II, SPAdj);
337   return Reg;
338 }
339
340 /// lowerDynamicAlloc - Generate the code for allocating an object in the
341 /// current frame.  The sequence of code with be in the general form
342 ///
343 ///   addi   R0, SP, \#frameSize ; get the address of the previous frame
344 ///   stwxu  R0, SP, Rnegsize   ; add and update the SP with the negated size
345 ///   addi   Rnew, SP, \#maxCalFrameSize ; get the top of the allocation
346 ///
347 void PPCRegisterInfo::lowerDynamicAlloc(MachineBasicBlock::iterator II,
348                                         int SPAdj, RegScavenger *RS) const {
349   // Get the instruction.
350   MachineInstr &MI = *II;
351   // Get the instruction's basic block.
352   MachineBasicBlock &MBB = *MI.getParent();
353   // Get the basic block's function.
354   MachineFunction &MF = *MBB.getParent();
355   // Get the frame info.
356   MachineFrameInfo *MFI = MF.getFrameInfo();
357   // Determine whether 64-bit pointers are used.
358   bool LP64 = Subtarget.isPPC64();
359   DebugLoc dl = MI.getDebugLoc();
360
361   // Get the maximum call stack size.
362   unsigned maxCallFrameSize = MFI->getMaxCallFrameSize();
363   // Get the total frame size.
364   unsigned FrameSize = MFI->getStackSize();
365   
366   // Get stack alignments.
367   unsigned TargetAlign = MF.getTarget().getFrameLowering()->getStackAlignment();
368   unsigned MaxAlign = MFI->getMaxAlignment();
369   if (MaxAlign > TargetAlign)
370     report_fatal_error("Dynamic alloca with large aligns not supported");
371
372   // Determine the previous frame's address.  If FrameSize can't be
373   // represented as 16 bits or we need special alignment, then we load the
374   // previous frame's address from 0(SP).  Why not do an addis of the hi? 
375   // Because R0 is our only safe tmp register and addi/addis treat R0 as zero. 
376   // Constructing the constant and adding would take 3 instructions. 
377   // Fortunately, a frame greater than 32K is rare.
378   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
379   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
380   const TargetRegisterClass *RC = LP64 ? G8RC : GPRC;
381
382   // FIXME (64-bit): Use "findScratchRegister"
383   unsigned Reg;
384   if (requiresRegisterScavenging(MF))
385     Reg = findScratchRegister(II, RS, RC, SPAdj);
386   else
387     Reg = PPC::R0;
388   
389   if (MaxAlign < TargetAlign && isInt<16>(FrameSize)) {
390     BuildMI(MBB, II, dl, TII.get(PPC::ADDI), Reg)
391       .addReg(PPC::R31)
392       .addImm(FrameSize);
393   } else if (LP64) {
394     if (requiresRegisterScavenging(MF)) // FIXME (64-bit): Use "true" part.
395       BuildMI(MBB, II, dl, TII.get(PPC::LD), Reg)
396         .addImm(0)
397         .addReg(PPC::X1);
398     else
399       BuildMI(MBB, II, dl, TII.get(PPC::LD), PPC::X0)
400         .addImm(0)
401         .addReg(PPC::X1);
402   } else {
403     BuildMI(MBB, II, dl, TII.get(PPC::LWZ), Reg)
404       .addImm(0)
405       .addReg(PPC::R1);
406   }
407   
408   // Grow the stack and update the stack pointer link, then determine the
409   // address of new allocated space.
410   if (LP64) {
411     if (requiresRegisterScavenging(MF)) // FIXME (64-bit): Use "true" part.
412       BuildMI(MBB, II, dl, TII.get(PPC::STDUX))
413         .addReg(Reg, RegState::Kill)
414         .addReg(PPC::X1)
415         .addReg(MI.getOperand(1).getReg());
416     else
417       BuildMI(MBB, II, dl, TII.get(PPC::STDUX))
418         .addReg(PPC::X0, RegState::Kill)
419         .addReg(PPC::X1)
420         .addReg(MI.getOperand(1).getReg());
421
422     if (!MI.getOperand(1).isKill())
423       BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), MI.getOperand(0).getReg())
424         .addReg(PPC::X1)
425         .addImm(maxCallFrameSize);
426     else
427       // Implicitly kill the register.
428       BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), MI.getOperand(0).getReg())
429         .addReg(PPC::X1)
430         .addImm(maxCallFrameSize)
431         .addReg(MI.getOperand(1).getReg(), RegState::ImplicitKill);
432   } else {
433     BuildMI(MBB, II, dl, TII.get(PPC::STWUX))
434       .addReg(Reg, RegState::Kill)
435       .addReg(PPC::R1)
436       .addReg(MI.getOperand(1).getReg());
437
438     if (!MI.getOperand(1).isKill())
439       BuildMI(MBB, II, dl, TII.get(PPC::ADDI), MI.getOperand(0).getReg())
440         .addReg(PPC::R1)
441         .addImm(maxCallFrameSize);
442     else
443       // Implicitly kill the register.
444       BuildMI(MBB, II, dl, TII.get(PPC::ADDI), MI.getOperand(0).getReg())
445         .addReg(PPC::R1)
446         .addImm(maxCallFrameSize)
447         .addReg(MI.getOperand(1).getReg(), RegState::ImplicitKill);
448   }
449   
450   // Discard the DYNALLOC instruction.
451   MBB.erase(II);
452 }
453
454 /// lowerCRSpilling - Generate the code for spilling a CR register. Instead of
455 /// reserving a whole register (R0), we scrounge for one here. This generates
456 /// code like this:
457 ///
458 ///   mfcr rA                  ; Move the conditional register into GPR rA.
459 ///   rlwinm rA, rA, SB, 0, 31 ; Shift the bits left so they are in CR0's slot.
460 ///   stw rA, FI               ; Store rA to the frame.
461 ///
462 void PPCRegisterInfo::lowerCRSpilling(MachineBasicBlock::iterator II,
463                                       unsigned FrameIndex, int SPAdj,
464                                       RegScavenger *RS) const {
465   // Get the instruction.
466   MachineInstr &MI = *II;       // ; SPILL_CR <SrcReg>, <offset>, <FI>
467   // Get the instruction's basic block.
468   MachineBasicBlock &MBB = *MI.getParent();
469   DebugLoc dl = MI.getDebugLoc();
470
471   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
472   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
473   const TargetRegisterClass *RC = Subtarget.isPPC64() ? G8RC : GPRC;
474   unsigned Reg = findScratchRegister(II, RS, RC, SPAdj);
475   unsigned SrcReg = MI.getOperand(0).getReg();
476   bool LP64 = Subtarget.isPPC64();
477
478   // We need to store the CR in the low 4-bits of the saved value. First, issue
479   // an MFCRpsued to save all of the CRBits and, if needed, kill the SrcReg.
480   BuildMI(MBB, II, dl, TII.get(PPC::MFCRpseud), Reg)
481           .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill()));
482     
483   // If the saved register wasn't CR0, shift the bits left so that they are in
484   // CR0's slot.
485   if (SrcReg != PPC::CR0)
486     // rlwinm rA, rA, ShiftBits, 0, 31.
487     BuildMI(MBB, II, dl, TII.get(PPC::RLWINM), Reg)
488       .addReg(Reg, RegState::Kill)
489       .addImm(getPPCRegisterNumbering(SrcReg) * 4)
490       .addImm(0)
491       .addImm(31);
492
493   addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW))
494                     .addReg(Reg, getKillRegState(MI.getOperand(1).getImm())),
495                     FrameIndex);
496
497   // Discard the pseudo instruction.
498   MBB.erase(II);
499 }
500
501 void
502 PPCRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
503                                      int SPAdj, RegScavenger *RS) const {
504   assert(SPAdj == 0 && "Unexpected");
505
506   // Get the instruction.
507   MachineInstr &MI = *II;
508   // Get the instruction's basic block.
509   MachineBasicBlock &MBB = *MI.getParent();
510   // Get the basic block's function.
511   MachineFunction &MF = *MBB.getParent();
512   // Get the frame info.
513   MachineFrameInfo *MFI = MF.getFrameInfo();
514   const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
515   DebugLoc dl = MI.getDebugLoc();
516
517   // Find out which operand is the frame index.
518   unsigned FIOperandNo = 0;
519   while (!MI.getOperand(FIOperandNo).isFI()) {
520     ++FIOperandNo;
521     assert(FIOperandNo != MI.getNumOperands() &&
522            "Instr doesn't have FrameIndex operand!");
523   }
524   // Take into account whether it's an add or mem instruction
525   unsigned OffsetOperandNo = (FIOperandNo == 2) ? 1 : 2;
526   if (MI.isInlineAsm())
527     OffsetOperandNo = FIOperandNo-1;
528
529   // Get the frame index.
530   int FrameIndex = MI.getOperand(FIOperandNo).getIndex();
531
532   // Get the frame pointer save index.  Users of this index are primarily
533   // DYNALLOC instructions.
534   PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
535   int FPSI = FI->getFramePointerSaveIndex();
536   // Get the instruction opcode.
537   unsigned OpC = MI.getOpcode();
538   
539   // Special case for dynamic alloca.
540   if (FPSI && FrameIndex == FPSI &&
541       (OpC == PPC::DYNALLOC || OpC == PPC::DYNALLOC8)) {
542     lowerDynamicAlloc(II, SPAdj, RS);
543     return;
544   }
545
546   // Special case for pseudo-op SPILL_CR.
547   if (requiresRegisterScavenging(MF)) // FIXME (64-bit): Enable by default.
548     if (OpC == PPC::SPILL_CR) {
549       lowerCRSpilling(II, FrameIndex, SPAdj, RS);
550       return;
551     }
552
553   // Replace the FrameIndex with base register with GPR1 (SP) or GPR31 (FP).
554   MI.getOperand(FIOperandNo).ChangeToRegister(TFI->hasFP(MF) ?
555                                               PPC::R31 : PPC::R1,
556                                               false);
557
558   // Figure out if the offset in the instruction is shifted right two bits. This
559   // is true for instructions like "STD", which the machine implicitly adds two
560   // low zeros to.
561   bool isIXAddr = false;
562   switch (OpC) {
563   case PPC::LWA:
564   case PPC::LD:
565   case PPC::STD:
566   case PPC::STD_32:
567     isIXAddr = true;
568     break;
569   }
570   
571   // Now add the frame object offset to the offset from r1.
572   int Offset = MFI->getObjectOffset(FrameIndex);
573   if (!isIXAddr)
574     Offset += MI.getOperand(OffsetOperandNo).getImm();
575   else
576     Offset += MI.getOperand(OffsetOperandNo).getImm() << 2;
577
578   // If we're not using a Frame Pointer that has been set to the value of the
579   // SP before having the stack size subtracted from it, then add the stack size
580   // to Offset to get the correct offset.
581   // Naked functions have stack size 0, although getStackSize may not reflect that
582   // because we didn't call all the pieces that compute it for naked functions.
583   if (!MF.getFunction()->hasFnAttr(Attribute::Naked))
584     Offset += MFI->getStackSize();
585
586   // If we can, encode the offset directly into the instruction.  If this is a
587   // normal PPC "ri" instruction, any 16-bit value can be safely encoded.  If
588   // this is a PPC64 "ix" instruction, only a 16-bit value with the low two bits
589   // clear can be encoded.  This is extremely uncommon, because normally you
590   // only "std" to a stack slot that is at least 4-byte aligned, but it can
591   // happen in invalid code.
592   if (isInt<16>(Offset) && (!isIXAddr || (Offset & 3) == 0)) {
593     if (isIXAddr)
594       Offset >>= 2;    // The actual encoded value has the low two bits zero.
595     MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset);
596     return;
597   }
598
599   // The offset doesn't fit into a single register, scavenge one to build the
600   // offset in.
601   // FIXME: figure out what SPAdj is doing here.
602
603   // FIXME (64-bit): Use "findScratchRegister".
604   unsigned SReg;
605   if (requiresRegisterScavenging(MF))
606     SReg = findScratchRegister(II, RS, &PPC::GPRCRegClass, SPAdj);
607   else
608     SReg = PPC::R0;
609
610   // Insert a set of rA with the full offset value before the ld, st, or add
611   BuildMI(MBB, II, dl, TII.get(PPC::LIS), SReg)
612     .addImm(Offset >> 16);
613   BuildMI(MBB, II, dl, TII.get(PPC::ORI), SReg)
614     .addReg(SReg, RegState::Kill)
615     .addImm(Offset);
616
617   // Convert into indexed form of the instruction:
618   // 
619   //   sth 0:rA, 1:imm 2:(rB) ==> sthx 0:rA, 2:rB, 1:r0
620   //   addi 0:rA 1:rB, 2, imm ==> add 0:rA, 1:rB, 2:r0
621   unsigned OperandBase;
622
623   if (OpC != TargetOpcode::INLINEASM) {
624     assert(ImmToIdxMap.count(OpC) &&
625            "No indexed form of load or store available!");
626     unsigned NewOpcode = ImmToIdxMap.find(OpC)->second;
627     MI.setDesc(TII.get(NewOpcode));
628     OperandBase = 1;
629   } else {
630     OperandBase = OffsetOperandNo;
631   }
632
633   unsigned StackReg = MI.getOperand(FIOperandNo).getReg();
634   MI.getOperand(OperandBase).ChangeToRegister(StackReg, false);
635   MI.getOperand(OperandBase + 1).ChangeToRegister(SReg, false);
636 }
637
638 unsigned PPCRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
639   const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
640
641   if (!Subtarget.isPPC64())
642     return TFI->hasFP(MF) ? PPC::R31 : PPC::R1;
643   else
644     return TFI->hasFP(MF) ? PPC::X31 : PPC::X1;
645 }
646
647 unsigned PPCRegisterInfo::getEHExceptionRegister() const {
648   return !Subtarget.isPPC64() ? PPC::R3 : PPC::X3;
649 }
650
651 unsigned PPCRegisterInfo::getEHHandlerRegister() const {
652   return !Subtarget.isPPC64() ? PPC::R4 : PPC::X4;
653 }