remove a bogus pattern, which had the same pattern as STDU
[oota-llvm.git] / lib / Target / PowerPC / PPCInstrInfo.cpp
1 //===- PPCInstrInfo.cpp - PowerPC32 Instruction 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 TargetInstrInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "PPCInstrInfo.h"
15 #include "PPCInstrBuilder.h"
16 #include "PPCMachineFunctionInfo.h"
17 #include "PPCPredicates.h"
18 #include "PPCGenInstrInfo.inc"
19 #include "PPCTargetMachine.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/CodeGen/MachineRegisterInfo.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/MC/MCAsmInfo.h"
27 using namespace llvm;
28
29 extern cl::opt<bool> EnablePPC32RS;  // FIXME (64-bit): See PPCRegisterInfo.cpp.
30 extern cl::opt<bool> EnablePPC64RS;  // FIXME (64-bit): See PPCRegisterInfo.cpp.
31
32 PPCInstrInfo::PPCInstrInfo(PPCTargetMachine &tm)
33   : TargetInstrInfoImpl(PPCInsts, array_lengthof(PPCInsts)), TM(tm),
34     RI(*TM.getSubtargetImpl(), *this) {}
35
36 bool PPCInstrInfo::isMoveInstr(const MachineInstr& MI,
37                                unsigned& sourceReg,
38                                unsigned& destReg,
39                                unsigned& sourceSubIdx,
40                                unsigned& destSubIdx) const {
41   sourceSubIdx = destSubIdx = 0; // No sub-registers.
42
43   unsigned oc = MI.getOpcode();
44   if (oc == PPC::OR || oc == PPC::OR8 || oc == PPC::VOR ||
45       oc == PPC::OR4To8 || oc == PPC::OR8To4) {                // or r1, r2, r2
46     assert(MI.getNumOperands() >= 3 &&
47            MI.getOperand(0).isReg() &&
48            MI.getOperand(1).isReg() &&
49            MI.getOperand(2).isReg() &&
50            "invalid PPC OR instruction!");
51     if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
52       sourceReg = MI.getOperand(1).getReg();
53       destReg = MI.getOperand(0).getReg();
54       return true;
55     }
56   } else if (oc == PPC::ADDI) {             // addi r1, r2, 0
57     assert(MI.getNumOperands() >= 3 &&
58            MI.getOperand(0).isReg() &&
59            MI.getOperand(2).isImm() &&
60            "invalid PPC ADDI instruction!");
61     if (MI.getOperand(1).isReg() && MI.getOperand(2).getImm() == 0) {
62       sourceReg = MI.getOperand(1).getReg();
63       destReg = MI.getOperand(0).getReg();
64       return true;
65     }
66   } else if (oc == PPC::ORI) {             // ori r1, r2, 0
67     assert(MI.getNumOperands() >= 3 &&
68            MI.getOperand(0).isReg() &&
69            MI.getOperand(1).isReg() &&
70            MI.getOperand(2).isImm() &&
71            "invalid PPC ORI instruction!");
72     if (MI.getOperand(2).getImm() == 0) {
73       sourceReg = MI.getOperand(1).getReg();
74       destReg = MI.getOperand(0).getReg();
75       return true;
76     }
77   } else if (oc == PPC::FMR || oc == PPC::FMRSD) { // fmr r1, r2
78     assert(MI.getNumOperands() >= 2 &&
79            MI.getOperand(0).isReg() &&
80            MI.getOperand(1).isReg() &&
81            "invalid PPC FMR instruction");
82     sourceReg = MI.getOperand(1).getReg();
83     destReg = MI.getOperand(0).getReg();
84     return true;
85   } else if (oc == PPC::MCRF) {             // mcrf cr1, cr2
86     assert(MI.getNumOperands() >= 2 &&
87            MI.getOperand(0).isReg() &&
88            MI.getOperand(1).isReg() &&
89            "invalid PPC MCRF instruction");
90     sourceReg = MI.getOperand(1).getReg();
91     destReg = MI.getOperand(0).getReg();
92     return true;
93   }
94   return false;
95 }
96
97 unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr *MI, 
98                                            int &FrameIndex) const {
99   switch (MI->getOpcode()) {
100   default: break;
101   case PPC::LD:
102   case PPC::LWZ:
103   case PPC::LFS:
104   case PPC::LFD:
105     if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
106         MI->getOperand(2).isFI()) {
107       FrameIndex = MI->getOperand(2).getIndex();
108       return MI->getOperand(0).getReg();
109     }
110     break;
111   }
112   return 0;
113 }
114
115 unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr *MI, 
116                                           int &FrameIndex) const {
117   switch (MI->getOpcode()) {
118   default: break;
119   case PPC::STD:
120   case PPC::STW:
121   case PPC::STFS:
122   case PPC::STFD:
123     if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
124         MI->getOperand(2).isFI()) {
125       FrameIndex = MI->getOperand(2).getIndex();
126       return MI->getOperand(0).getReg();
127     }
128     break;
129   }
130   return 0;
131 }
132
133 // commuteInstruction - We can commute rlwimi instructions, but only if the
134 // rotate amt is zero.  We also have to munge the immediates a bit.
135 MachineInstr *
136 PPCInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
137   MachineFunction &MF = *MI->getParent()->getParent();
138
139   // Normal instructions can be commuted the obvious way.
140   if (MI->getOpcode() != PPC::RLWIMI)
141     return TargetInstrInfoImpl::commuteInstruction(MI, NewMI);
142   
143   // Cannot commute if it has a non-zero rotate count.
144   if (MI->getOperand(3).getImm() != 0)
145     return 0;
146   
147   // If we have a zero rotate count, we have:
148   //   M = mask(MB,ME)
149   //   Op0 = (Op1 & ~M) | (Op2 & M)
150   // Change this to:
151   //   M = mask((ME+1)&31, (MB-1)&31)
152   //   Op0 = (Op2 & ~M) | (Op1 & M)
153
154   // Swap op1/op2
155   unsigned Reg0 = MI->getOperand(0).getReg();
156   unsigned Reg1 = MI->getOperand(1).getReg();
157   unsigned Reg2 = MI->getOperand(2).getReg();
158   bool Reg1IsKill = MI->getOperand(1).isKill();
159   bool Reg2IsKill = MI->getOperand(2).isKill();
160   bool ChangeReg0 = false;
161   // If machine instrs are no longer in two-address forms, update
162   // destination register as well.
163   if (Reg0 == Reg1) {
164     // Must be two address instruction!
165     assert(MI->getDesc().getOperandConstraint(0, TOI::TIED_TO) &&
166            "Expecting a two-address instruction!");
167     Reg2IsKill = false;
168     ChangeReg0 = true;
169   }
170
171   // Masks.
172   unsigned MB = MI->getOperand(4).getImm();
173   unsigned ME = MI->getOperand(5).getImm();
174
175   if (NewMI) {
176     // Create a new instruction.
177     unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg();
178     bool Reg0IsDead = MI->getOperand(0).isDead();
179     return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
180       .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
181       .addReg(Reg2, getKillRegState(Reg2IsKill))
182       .addReg(Reg1, getKillRegState(Reg1IsKill))
183       .addImm((ME+1) & 31)
184       .addImm((MB-1) & 31);
185   }
186
187   if (ChangeReg0)
188     MI->getOperand(0).setReg(Reg2);
189   MI->getOperand(2).setReg(Reg1);
190   MI->getOperand(1).setReg(Reg2);
191   MI->getOperand(2).setIsKill(Reg1IsKill);
192   MI->getOperand(1).setIsKill(Reg2IsKill);
193   
194   // Swap the mask around.
195   MI->getOperand(4).setImm((ME+1) & 31);
196   MI->getOperand(5).setImm((MB-1) & 31);
197   return MI;
198 }
199
200 void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB, 
201                               MachineBasicBlock::iterator MI) const {
202   DebugLoc DL = DebugLoc::getUnknownLoc();
203   if (MI != MBB.end()) DL = MI->getDebugLoc();
204
205   BuildMI(MBB, MI, DL, get(PPC::NOP));
206 }
207
208
209 // Branch analysis.
210 bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
211                                  MachineBasicBlock *&FBB,
212                                  SmallVectorImpl<MachineOperand> &Cond,
213                                  bool AllowModify) const {
214   // If the block has no terminators, it just falls into the block after it.
215   MachineBasicBlock::iterator I = MBB.end();
216   if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
217     return false;
218
219   // Get the last instruction in the block.
220   MachineInstr *LastInst = I;
221   
222   // If there is only one terminator instruction, process it.
223   if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
224     if (LastInst->getOpcode() == PPC::B) {
225       if (!LastInst->getOperand(0).isMBB())
226         return true;
227       TBB = LastInst->getOperand(0).getMBB();
228       return false;
229     } else if (LastInst->getOpcode() == PPC::BCC) {
230       if (!LastInst->getOperand(2).isMBB())
231         return true;
232       // Block ends with fall-through condbranch.
233       TBB = LastInst->getOperand(2).getMBB();
234       Cond.push_back(LastInst->getOperand(0));
235       Cond.push_back(LastInst->getOperand(1));
236       return false;
237     }
238     // Otherwise, don't know what this is.
239     return true;
240   }
241   
242   // Get the instruction before it if it's a terminator.
243   MachineInstr *SecondLastInst = I;
244
245   // If there are three terminators, we don't know what sort of block this is.
246   if (SecondLastInst && I != MBB.begin() &&
247       isUnpredicatedTerminator(--I))
248     return true;
249   
250   // If the block ends with PPC::B and PPC:BCC, handle it.
251   if (SecondLastInst->getOpcode() == PPC::BCC && 
252       LastInst->getOpcode() == PPC::B) {
253     if (!SecondLastInst->getOperand(2).isMBB() ||
254         !LastInst->getOperand(0).isMBB())
255       return true;
256     TBB =  SecondLastInst->getOperand(2).getMBB();
257     Cond.push_back(SecondLastInst->getOperand(0));
258     Cond.push_back(SecondLastInst->getOperand(1));
259     FBB = LastInst->getOperand(0).getMBB();
260     return false;
261   }
262   
263   // If the block ends with two PPC:Bs, handle it.  The second one is not
264   // executed, so remove it.
265   if (SecondLastInst->getOpcode() == PPC::B && 
266       LastInst->getOpcode() == PPC::B) {
267     if (!SecondLastInst->getOperand(0).isMBB())
268       return true;
269     TBB = SecondLastInst->getOperand(0).getMBB();
270     I = LastInst;
271     if (AllowModify)
272       I->eraseFromParent();
273     return false;
274   }
275
276   // Otherwise, can't handle this.
277   return true;
278 }
279
280 unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
281   MachineBasicBlock::iterator I = MBB.end();
282   if (I == MBB.begin()) return 0;
283   --I;
284   if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC)
285     return 0;
286   
287   // Remove the branch.
288   I->eraseFromParent();
289   
290   I = MBB.end();
291
292   if (I == MBB.begin()) return 1;
293   --I;
294   if (I->getOpcode() != PPC::BCC)
295     return 1;
296   
297   // Remove the branch.
298   I->eraseFromParent();
299   return 2;
300 }
301
302 unsigned
303 PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
304                            MachineBasicBlock *FBB,
305                            const SmallVectorImpl<MachineOperand> &Cond) const {
306   // FIXME this should probably have a DebugLoc argument
307   DebugLoc dl = DebugLoc::getUnknownLoc();
308   // Shouldn't be a fall through.
309   assert(TBB && "InsertBranch must not be told to insert a fallthrough");
310   assert((Cond.size() == 2 || Cond.size() == 0) && 
311          "PPC branch conditions have two components!");
312   
313   // One-way branch.
314   if (FBB == 0) {
315     if (Cond.empty())   // Unconditional branch
316       BuildMI(&MBB, dl, get(PPC::B)).addMBB(TBB);
317     else                // Conditional branch
318       BuildMI(&MBB, dl, get(PPC::BCC))
319         .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
320     return 1;
321   }
322   
323   // Two-way Conditional Branch.
324   BuildMI(&MBB, dl, get(PPC::BCC))
325     .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
326   BuildMI(&MBB, dl, get(PPC::B)).addMBB(FBB);
327   return 2;
328 }
329
330 bool PPCInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
331                                    MachineBasicBlock::iterator MI,
332                                    unsigned DestReg, unsigned SrcReg,
333                                    const TargetRegisterClass *DestRC,
334                                    const TargetRegisterClass *SrcRC) const {
335   if (DestRC != SrcRC) {
336     // Not yet supported!
337     return false;
338   }
339
340   DebugLoc DL = DebugLoc::getUnknownLoc();
341   if (MI != MBB.end()) DL = MI->getDebugLoc();
342
343   if (DestRC == PPC::GPRCRegisterClass) {
344     BuildMI(MBB, MI, DL, get(PPC::OR), DestReg).addReg(SrcReg).addReg(SrcReg);
345   } else if (DestRC == PPC::G8RCRegisterClass) {
346     BuildMI(MBB, MI, DL, get(PPC::OR8), DestReg).addReg(SrcReg).addReg(SrcReg);
347   } else if (DestRC == PPC::F4RCRegisterClass ||
348              DestRC == PPC::F8RCRegisterClass) {
349     BuildMI(MBB, MI, DL, get(PPC::FMR), DestReg).addReg(SrcReg);
350   } else if (DestRC == PPC::CRRCRegisterClass) {
351     BuildMI(MBB, MI, DL, get(PPC::MCRF), DestReg).addReg(SrcReg);
352   } else if (DestRC == PPC::VRRCRegisterClass) {
353     BuildMI(MBB, MI, DL, get(PPC::VOR), DestReg).addReg(SrcReg).addReg(SrcReg);
354   } else if (DestRC == PPC::CRBITRCRegisterClass) {
355     BuildMI(MBB, MI, DL, get(PPC::CROR), DestReg).addReg(SrcReg).addReg(SrcReg);
356   } else {
357     // Attempt to copy register that is not GPR or FPR
358     return false;
359   }
360   
361   return true;
362 }
363
364 bool
365 PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
366                                   unsigned SrcReg, bool isKill,
367                                   int FrameIdx,
368                                   const TargetRegisterClass *RC,
369                                   SmallVectorImpl<MachineInstr*> &NewMIs) const{
370   DebugLoc DL = DebugLoc::getUnknownLoc();
371   if (RC == PPC::GPRCRegisterClass) {
372     if (SrcReg != PPC::LR) {
373       NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
374                                          .addReg(SrcReg,
375                                                  getKillRegState(isKill)),
376                                          FrameIdx));
377     } else {
378       // FIXME: this spills LR immediately to memory in one step.  To do this,
379       // we use R11, which we know cannot be used in the prolog/epilog.  This is
380       // a hack.
381       NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFLR), PPC::R11));
382       NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
383                                          .addReg(PPC::R11,
384                                                  getKillRegState(isKill)),
385                                          FrameIdx));
386     }
387   } else if (RC == PPC::G8RCRegisterClass) {
388     if (SrcReg != PPC::LR8) {
389       NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
390                                          .addReg(SrcReg,
391                                                  getKillRegState(isKill)),
392                                          FrameIdx));
393     } else {
394       // FIXME: this spills LR immediately to memory in one step.  To do this,
395       // we use R11, which we know cannot be used in the prolog/epilog.  This is
396       // a hack.
397       NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFLR8), PPC::X11));
398       NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
399                                          .addReg(PPC::X11,
400                                                  getKillRegState(isKill)),
401                                          FrameIdx));
402     }
403   } else if (RC == PPC::F8RCRegisterClass) {
404     NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD))
405                                        .addReg(SrcReg,
406                                                getKillRegState(isKill)),
407                                        FrameIdx));
408   } else if (RC == PPC::F4RCRegisterClass) {
409     NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS))
410                                        .addReg(SrcReg,
411                                                getKillRegState(isKill)),
412                                        FrameIdx));
413   } else if (RC == PPC::CRRCRegisterClass) {
414     if ((EnablePPC32RS && !TM.getSubtargetImpl()->isPPC64()) ||
415         (EnablePPC64RS && TM.getSubtargetImpl()->isPPC64())) {
416       // FIXME (64-bit): Enable
417       NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR))
418                                          .addReg(SrcReg,
419                                                  getKillRegState(isKill)),
420                                          FrameIdx));
421       return true;
422     } else {
423       // FIXME: We need a scatch reg here.  The trouble with using R0 is that
424       // it's possible for the stack frame to be so big the save location is
425       // out of range of immediate offsets, necessitating another register.
426       // We hack this on Darwin by reserving R2.  It's probably broken on Linux
427       // at the moment.
428
429       // We need to store the CR in the low 4-bits of the saved value.  First,
430       // issue a MFCR to save all of the CRBits.
431       unsigned ScratchReg = TM.getSubtargetImpl()->isDarwinABI() ? 
432                                                            PPC::R2 : PPC::R0;
433       NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFCR), ScratchReg));
434     
435       // If the saved register wasn't CR0, shift the bits left so that they are
436       // in CR0's slot.
437       if (SrcReg != PPC::CR0) {
438         unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(SrcReg)*4;
439         // rlwinm scratch, scratch, ShiftBits, 0, 31.
440         NewMIs.push_back(BuildMI(MF, DL, get(PPC::RLWINM), ScratchReg)
441                        .addReg(ScratchReg).addImm(ShiftBits)
442                        .addImm(0).addImm(31));
443       }
444     
445       NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
446                                          .addReg(ScratchReg,
447                                                  getKillRegState(isKill)),
448                                          FrameIdx));
449     }
450   } else if (RC == PPC::CRBITRCRegisterClass) {
451     // FIXME: We use CRi here because there is no mtcrf on a bit. Since the
452     // backend currently only uses CR1EQ as an individual bit, this should
453     // not cause any bug. If we need other uses of CR bits, the following
454     // code may be invalid.
455     unsigned Reg = 0;
456     if (SrcReg == PPC::CR0LT || SrcReg == PPC::CR0GT ||
457         SrcReg == PPC::CR0EQ || SrcReg == PPC::CR0UN)
458       Reg = PPC::CR0;
459     else if (SrcReg == PPC::CR1LT || SrcReg == PPC::CR1GT ||
460              SrcReg == PPC::CR1EQ || SrcReg == PPC::CR1UN)
461       Reg = PPC::CR1;
462     else if (SrcReg == PPC::CR2LT || SrcReg == PPC::CR2GT ||
463              SrcReg == PPC::CR2EQ || SrcReg == PPC::CR2UN)
464       Reg = PPC::CR2;
465     else if (SrcReg == PPC::CR3LT || SrcReg == PPC::CR3GT ||
466              SrcReg == PPC::CR3EQ || SrcReg == PPC::CR3UN)
467       Reg = PPC::CR3;
468     else if (SrcReg == PPC::CR4LT || SrcReg == PPC::CR4GT ||
469              SrcReg == PPC::CR4EQ || SrcReg == PPC::CR4UN)
470       Reg = PPC::CR4;
471     else if (SrcReg == PPC::CR5LT || SrcReg == PPC::CR5GT ||
472              SrcReg == PPC::CR5EQ || SrcReg == PPC::CR5UN)
473       Reg = PPC::CR5;
474     else if (SrcReg == PPC::CR6LT || SrcReg == PPC::CR6GT ||
475              SrcReg == PPC::CR6EQ || SrcReg == PPC::CR6UN)
476       Reg = PPC::CR6;
477     else if (SrcReg == PPC::CR7LT || SrcReg == PPC::CR7GT ||
478              SrcReg == PPC::CR7EQ || SrcReg == PPC::CR7UN)
479       Reg = PPC::CR7;
480
481     return StoreRegToStackSlot(MF, Reg, isKill, FrameIdx, 
482                                PPC::CRRCRegisterClass, NewMIs);
483
484   } else if (RC == PPC::VRRCRegisterClass) {
485     // We don't have indexed addressing for vector loads.  Emit:
486     // R0 = ADDI FI#
487     // STVX VAL, 0, R0
488     // 
489     // FIXME: We use R0 here, because it isn't available for RA.
490     NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::ADDI), PPC::R0),
491                                        FrameIdx, 0, 0));
492     NewMIs.push_back(BuildMI(MF, DL, get(PPC::STVX))
493                      .addReg(SrcReg, getKillRegState(isKill))
494                      .addReg(PPC::R0)
495                      .addReg(PPC::R0));
496   } else {
497     llvm_unreachable("Unknown regclass!");
498   }
499
500   return false;
501 }
502
503 void
504 PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
505                                   MachineBasicBlock::iterator MI,
506                                   unsigned SrcReg, bool isKill, int FrameIdx,
507                                   const TargetRegisterClass *RC) const {
508   MachineFunction &MF = *MBB.getParent();
509   SmallVector<MachineInstr*, 4> NewMIs;
510
511   if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs)) {
512     PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
513     FuncInfo->setSpillsCR();
514   }
515
516   for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
517     MBB.insert(MI, NewMIs[i]);
518 }
519
520 void
521 PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL,
522                                    unsigned DestReg, int FrameIdx,
523                                    const TargetRegisterClass *RC,
524                                    SmallVectorImpl<MachineInstr*> &NewMIs)const{
525   if (RC == PPC::GPRCRegisterClass) {
526     if (DestReg != PPC::LR) {
527       NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
528                                                  DestReg), FrameIdx));
529     } else {
530       NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
531                                                  PPC::R11), FrameIdx));
532       NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTLR)).addReg(PPC::R11));
533     }
534   } else if (RC == PPC::G8RCRegisterClass) {
535     if (DestReg != PPC::LR8) {
536       NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg),
537                                          FrameIdx));
538     } else {
539       NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD),
540                                                  PPC::R11), FrameIdx));
541       NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTLR8)).addReg(PPC::R11));
542     }
543   } else if (RC == PPC::F8RCRegisterClass) {
544     NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg),
545                                        FrameIdx));
546   } else if (RC == PPC::F4RCRegisterClass) {
547     NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg),
548                                        FrameIdx));
549   } else if (RC == PPC::CRRCRegisterClass) {
550     // FIXME: We need a scatch reg here.  The trouble with using R0 is that
551     // it's possible for the stack frame to be so big the save location is
552     // out of range of immediate offsets, necessitating another register.
553     // We hack this on Darwin by reserving R2.  It's probably broken on Linux
554     // at the moment.
555     unsigned ScratchReg = TM.getSubtargetImpl()->isDarwinABI() ?
556                                                           PPC::R2 : PPC::R0;
557     NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ), 
558                                        ScratchReg), FrameIdx));
559     
560     // If the reloaded register isn't CR0, shift the bits right so that they are
561     // in the right CR's slot.
562     if (DestReg != PPC::CR0) {
563       unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(DestReg)*4;
564       // rlwinm r11, r11, 32-ShiftBits, 0, 31.
565       NewMIs.push_back(BuildMI(MF, DL, get(PPC::RLWINM), ScratchReg)
566                     .addReg(ScratchReg).addImm(32-ShiftBits).addImm(0)
567                     .addImm(31));
568     }
569     
570     NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTCRF), DestReg)
571                      .addReg(ScratchReg));
572   } else if (RC == PPC::CRBITRCRegisterClass) {
573    
574     unsigned Reg = 0;
575     if (DestReg == PPC::CR0LT || DestReg == PPC::CR0GT ||
576         DestReg == PPC::CR0EQ || DestReg == PPC::CR0UN)
577       Reg = PPC::CR0;
578     else if (DestReg == PPC::CR1LT || DestReg == PPC::CR1GT ||
579              DestReg == PPC::CR1EQ || DestReg == PPC::CR1UN)
580       Reg = PPC::CR1;
581     else if (DestReg == PPC::CR2LT || DestReg == PPC::CR2GT ||
582              DestReg == PPC::CR2EQ || DestReg == PPC::CR2UN)
583       Reg = PPC::CR2;
584     else if (DestReg == PPC::CR3LT || DestReg == PPC::CR3GT ||
585              DestReg == PPC::CR3EQ || DestReg == PPC::CR3UN)
586       Reg = PPC::CR3;
587     else if (DestReg == PPC::CR4LT || DestReg == PPC::CR4GT ||
588              DestReg == PPC::CR4EQ || DestReg == PPC::CR4UN)
589       Reg = PPC::CR4;
590     else if (DestReg == PPC::CR5LT || DestReg == PPC::CR5GT ||
591              DestReg == PPC::CR5EQ || DestReg == PPC::CR5UN)
592       Reg = PPC::CR5;
593     else if (DestReg == PPC::CR6LT || DestReg == PPC::CR6GT ||
594              DestReg == PPC::CR6EQ || DestReg == PPC::CR6UN)
595       Reg = PPC::CR6;
596     else if (DestReg == PPC::CR7LT || DestReg == PPC::CR7GT ||
597              DestReg == PPC::CR7EQ || DestReg == PPC::CR7UN)
598       Reg = PPC::CR7;
599
600     return LoadRegFromStackSlot(MF, DL, Reg, FrameIdx, 
601                                 PPC::CRRCRegisterClass, NewMIs);
602
603   } else if (RC == PPC::VRRCRegisterClass) {
604     // We don't have indexed addressing for vector loads.  Emit:
605     // R0 = ADDI FI#
606     // Dest = LVX 0, R0
607     // 
608     // FIXME: We use R0 here, because it isn't available for RA.
609     NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::ADDI), PPC::R0),
610                                        FrameIdx, 0, 0));
611     NewMIs.push_back(BuildMI(MF, DL, get(PPC::LVX),DestReg).addReg(PPC::R0)
612                      .addReg(PPC::R0));
613   } else {
614     llvm_unreachable("Unknown regclass!");
615   }
616 }
617
618 void
619 PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
620                                    MachineBasicBlock::iterator MI,
621                                    unsigned DestReg, int FrameIdx,
622                                    const TargetRegisterClass *RC) const {
623   MachineFunction &MF = *MBB.getParent();
624   SmallVector<MachineInstr*, 4> NewMIs;
625   DebugLoc DL = DebugLoc::getUnknownLoc();
626   if (MI != MBB.end()) DL = MI->getDebugLoc();
627   LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs);
628   for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
629     MBB.insert(MI, NewMIs[i]);
630 }
631
632 /// foldMemoryOperand - PowerPC (like most RISC's) can only fold spills into
633 /// copy instructions, turning them into load/store instructions.
634 MachineInstr *PPCInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
635                                                   MachineInstr *MI,
636                                            const SmallVectorImpl<unsigned> &Ops,
637                                                   int FrameIndex) const {
638   if (Ops.size() != 1) return NULL;
639
640   // Make sure this is a reg-reg copy.  Note that we can't handle MCRF, because
641   // it takes more than one instruction to store it.
642   unsigned Opc = MI->getOpcode();
643   unsigned OpNum = Ops[0];
644
645   MachineInstr *NewMI = NULL;
646   if ((Opc == PPC::OR &&
647        MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
648     if (OpNum == 0) {  // move -> store
649       unsigned InReg = MI->getOperand(1).getReg();
650       bool isKill = MI->getOperand(1).isKill();
651       bool isUndef = MI->getOperand(1).isUndef();
652       NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::STW))
653                                 .addReg(InReg,
654                                         getKillRegState(isKill) |
655                                         getUndefRegState(isUndef)),
656                                 FrameIndex);
657     } else {           // move -> load
658       unsigned OutReg = MI->getOperand(0).getReg();
659       bool isDead = MI->getOperand(0).isDead();
660       bool isUndef = MI->getOperand(0).isUndef();
661       NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::LWZ))
662                                 .addReg(OutReg,
663                                         RegState::Define |
664                                         getDeadRegState(isDead) |
665                                         getUndefRegState(isUndef)),
666                                 FrameIndex);
667     }
668   } else if ((Opc == PPC::OR8 &&
669               MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
670     if (OpNum == 0) {  // move -> store
671       unsigned InReg = MI->getOperand(1).getReg();
672       bool isKill = MI->getOperand(1).isKill();
673       bool isUndef = MI->getOperand(1).isUndef();
674       NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::STD))
675                                 .addReg(InReg,
676                                         getKillRegState(isKill) |
677                                         getUndefRegState(isUndef)),
678                                 FrameIndex);
679     } else {           // move -> load
680       unsigned OutReg = MI->getOperand(0).getReg();
681       bool isDead = MI->getOperand(0).isDead();
682       bool isUndef = MI->getOperand(0).isUndef();
683       NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::LD))
684                                 .addReg(OutReg,
685                                         RegState::Define |
686                                         getDeadRegState(isDead) |
687                                         getUndefRegState(isUndef)),
688                                 FrameIndex);
689     }
690   } else if (Opc == PPC::FMR || Opc == PPC::FMRSD) {
691     // The register may be F4RC or F8RC, and that determines the memory op.
692     unsigned OrigReg = MI->getOperand(OpNum).getReg();
693     // We cannot tell the register class from a physreg alone.
694     if (TargetRegisterInfo::isPhysicalRegister(OrigReg))
695       return NULL;
696     const TargetRegisterClass *RC = MF.getRegInfo().getRegClass(OrigReg);
697     const bool is64 = RC == PPC::F8RCRegisterClass;
698
699     if (OpNum == 0) {  // move -> store
700       unsigned InReg = MI->getOperand(1).getReg();
701       bool isKill = MI->getOperand(1).isKill();
702       bool isUndef = MI->getOperand(1).isUndef();
703       NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(),
704                                         get(is64 ? PPC::STFD : PPC::STFS))
705                                 .addReg(InReg,
706                                         getKillRegState(isKill) |
707                                         getUndefRegState(isUndef)),
708                                 FrameIndex);
709     } else {           // move -> load
710       unsigned OutReg = MI->getOperand(0).getReg();
711       bool isDead = MI->getOperand(0).isDead();
712       bool isUndef = MI->getOperand(0).isUndef();
713       NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(),
714                                         get(is64 ? PPC::LFD : PPC::LFS))
715                                 .addReg(OutReg,
716                                         RegState::Define |
717                                         getDeadRegState(isDead) |
718                                         getUndefRegState(isUndef)),
719                                 FrameIndex);
720     }
721   }
722
723   return NewMI;
724 }
725
726 bool PPCInstrInfo::canFoldMemoryOperand(const MachineInstr *MI,
727                                   const SmallVectorImpl<unsigned> &Ops) const {
728   if (Ops.size() != 1) return false;
729
730   // Make sure this is a reg-reg copy.  Note that we can't handle MCRF, because
731   // it takes more than one instruction to store it.
732   unsigned Opc = MI->getOpcode();
733
734   if ((Opc == PPC::OR &&
735        MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
736     return true;
737   else if ((Opc == PPC::OR8 &&
738               MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
739     return true;
740   else if (Opc == PPC::FMR || Opc == PPC::FMRSD)
741     return true;
742
743   return false;
744 }
745
746
747 bool PPCInstrInfo::
748 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
749   assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
750   // Leave the CR# the same, but invert the condition.
751   Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
752   return false;
753 }
754
755 /// GetInstSize - Return the number of bytes of code the specified
756 /// instruction may be.  This returns the maximum number of bytes.
757 ///
758 unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
759   switch (MI->getOpcode()) {
760   case PPC::INLINEASM: {       // Inline Asm: Variable size.
761     const MachineFunction *MF = MI->getParent()->getParent();
762     const char *AsmStr = MI->getOperand(0).getSymbolName();
763     return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
764   }
765   case PPC::DBG_LABEL:
766   case PPC::EH_LABEL:
767   case PPC::GC_LABEL:
768     return 0;
769   default:
770     return 4; // PowerPC instructions are all 4 bytes
771   }
772 }