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