Use enumeration for preffered EH dwarf encoding reason
[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 "PPCPredicates.h"
17 #include "PPCGenInstrInfo.inc"
18 #include "PPCTargetMachine.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
21 using namespace llvm;
22
23 PPCInstrInfo::PPCInstrInfo(PPCTargetMachine &tm)
24   : TargetInstrInfoImpl(PPCInsts, array_lengthof(PPCInsts)), TM(tm),
25     RI(*TM.getSubtargetImpl(), *this) {}
26
27 /// getPointerRegClass - Return the register class to use to hold pointers.
28 /// This is used for addressing modes.
29 const TargetRegisterClass *PPCInstrInfo::getPointerRegClass() const {
30   if (TM.getSubtargetImpl()->isPPC64())
31     return &PPC::G8RCRegClass;
32   else
33     return &PPC::GPRCRegClass;
34 }
35
36
37 bool PPCInstrInfo::isMoveInstr(const MachineInstr& MI,
38                                unsigned& sourceReg,
39                                unsigned& destReg) const {
40   unsigned oc = MI.getOpcode();
41   if (oc == PPC::OR || oc == PPC::OR8 || oc == PPC::VOR ||
42       oc == PPC::OR4To8 || oc == PPC::OR8To4) {                // or r1, r2, r2
43     assert(MI.getNumOperands() >= 3 &&
44            MI.getOperand(0).isRegister() &&
45            MI.getOperand(1).isRegister() &&
46            MI.getOperand(2).isRegister() &&
47            "invalid PPC OR instruction!");
48     if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
49       sourceReg = MI.getOperand(1).getReg();
50       destReg = MI.getOperand(0).getReg();
51       return true;
52     }
53   } else if (oc == PPC::ADDI) {             // addi r1, r2, 0
54     assert(MI.getNumOperands() >= 3 &&
55            MI.getOperand(0).isRegister() &&
56            MI.getOperand(2).isImmediate() &&
57            "invalid PPC ADDI instruction!");
58     if (MI.getOperand(1).isRegister() && MI.getOperand(2).getImm() == 0) {
59       sourceReg = MI.getOperand(1).getReg();
60       destReg = MI.getOperand(0).getReg();
61       return true;
62     }
63   } else if (oc == PPC::ORI) {             // ori r1, r2, 0
64     assert(MI.getNumOperands() >= 3 &&
65            MI.getOperand(0).isRegister() &&
66            MI.getOperand(1).isRegister() &&
67            MI.getOperand(2).isImmediate() &&
68            "invalid PPC ORI instruction!");
69     if (MI.getOperand(2).getImm() == 0) {
70       sourceReg = MI.getOperand(1).getReg();
71       destReg = MI.getOperand(0).getReg();
72       return true;
73     }
74   } else if (oc == PPC::FMRS || oc == PPC::FMRD ||
75              oc == PPC::FMRSD) {      // fmr r1, r2
76     assert(MI.getNumOperands() >= 2 &&
77            MI.getOperand(0).isRegister() &&
78            MI.getOperand(1).isRegister() &&
79            "invalid PPC FMR instruction");
80     sourceReg = MI.getOperand(1).getReg();
81     destReg = MI.getOperand(0).getReg();
82     return true;
83   } else if (oc == PPC::MCRF) {             // mcrf cr1, cr2
84     assert(MI.getNumOperands() >= 2 &&
85            MI.getOperand(0).isRegister() &&
86            MI.getOperand(1).isRegister() &&
87            "invalid PPC MCRF instruction");
88     sourceReg = MI.getOperand(1).getReg();
89     destReg = MI.getOperand(0).getReg();
90     return true;
91   }
92   return false;
93 }
94
95 unsigned PPCInstrInfo::isLoadFromStackSlot(MachineInstr *MI, 
96                                            int &FrameIndex) const {
97   switch (MI->getOpcode()) {
98   default: break;
99   case PPC::LD:
100   case PPC::LWZ:
101   case PPC::LFS:
102   case PPC::LFD:
103     if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
104         MI->getOperand(2).isFI()) {
105       FrameIndex = MI->getOperand(2).getIndex();
106       return MI->getOperand(0).getReg();
107     }
108     break;
109   }
110   return 0;
111 }
112
113 unsigned PPCInstrInfo::isStoreToStackSlot(MachineInstr *MI, 
114                                           int &FrameIndex) const {
115   switch (MI->getOpcode()) {
116   default: break;
117   case PPC::STD:
118   case PPC::STW:
119   case PPC::STFS:
120   case PPC::STFD:
121     if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
122         MI->getOperand(2).isFI()) {
123       FrameIndex = MI->getOperand(2).getIndex();
124       return MI->getOperand(0).getReg();
125     }
126     break;
127   }
128   return 0;
129 }
130
131 // commuteInstruction - We can commute rlwimi instructions, but only if the
132 // rotate amt is zero.  We also have to munge the immediates a bit.
133 MachineInstr *PPCInstrInfo::commuteInstruction(MachineInstr *MI) const {
134   // Normal instructions can be commuted the obvious way.
135   if (MI->getOpcode() != PPC::RLWIMI)
136     return TargetInstrInfoImpl::commuteInstruction(MI);
137   
138   // Cannot commute if it has a non-zero rotate count.
139   if (MI->getOperand(3).getImm() != 0)
140     return 0;
141   
142   // If we have a zero rotate count, we have:
143   //   M = mask(MB,ME)
144   //   Op0 = (Op1 & ~M) | (Op2 & M)
145   // Change this to:
146   //   M = mask((ME+1)&31, (MB-1)&31)
147   //   Op0 = (Op2 & ~M) | (Op1 & M)
148
149   // Swap op1/op2
150   unsigned Reg0 = MI->getOperand(0).getReg();
151   unsigned Reg1 = MI->getOperand(1).getReg();
152   unsigned Reg2 = MI->getOperand(2).getReg();
153   bool Reg1IsKill = MI->getOperand(1).isKill();
154   bool Reg2IsKill = MI->getOperand(2).isKill();
155   // If machine instrs are no longer in two-address forms, update
156   // destination register as well.
157   if (Reg0 == Reg1) {
158     // Must be two address instruction!
159     assert(MI->getDesc().getOperandConstraint(0, TOI::TIED_TO) &&
160            "Expecting a two-address instruction!");
161     MI->getOperand(0).setReg(Reg2);
162     Reg2IsKill = false;
163   }
164   MI->getOperand(2).setReg(Reg1);
165   MI->getOperand(1).setReg(Reg2);
166   MI->getOperand(2).setIsKill(Reg1IsKill);
167   MI->getOperand(1).setIsKill(Reg2IsKill);
168   
169   // Swap the mask around.
170   unsigned MB = MI->getOperand(4).getImm();
171   unsigned ME = MI->getOperand(5).getImm();
172   MI->getOperand(4).setImm((ME+1) & 31);
173   MI->getOperand(5).setImm((MB-1) & 31);
174   return MI;
175 }
176
177 void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB, 
178                               MachineBasicBlock::iterator MI) const {
179   BuildMI(MBB, MI, get(PPC::NOP));
180 }
181
182
183 // Branch analysis.
184 bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
185                                  MachineBasicBlock *&FBB,
186                                  std::vector<MachineOperand> &Cond) const {
187   // If the block has no terminators, it just falls into the block after it.
188   MachineBasicBlock::iterator I = MBB.end();
189   if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
190     return false;
191
192   // Get the last instruction in the block.
193   MachineInstr *LastInst = I;
194   
195   // If there is only one terminator instruction, process it.
196   if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
197     if (LastInst->getOpcode() == PPC::B) {
198       TBB = LastInst->getOperand(0).getMBB();
199       return false;
200     } else if (LastInst->getOpcode() == PPC::BCC) {
201       // Block ends with fall-through condbranch.
202       TBB = LastInst->getOperand(2).getMBB();
203       Cond.push_back(LastInst->getOperand(0));
204       Cond.push_back(LastInst->getOperand(1));
205       return false;
206     }
207     // Otherwise, don't know what this is.
208     return true;
209   }
210   
211   // Get the instruction before it if it's a terminator.
212   MachineInstr *SecondLastInst = I;
213
214   // If there are three terminators, we don't know what sort of block this is.
215   if (SecondLastInst && I != MBB.begin() &&
216       isUnpredicatedTerminator(--I))
217     return true;
218   
219   // If the block ends with PPC::B and PPC:BCC, handle it.
220   if (SecondLastInst->getOpcode() == PPC::BCC && 
221       LastInst->getOpcode() == PPC::B) {
222     TBB =  SecondLastInst->getOperand(2).getMBB();
223     Cond.push_back(SecondLastInst->getOperand(0));
224     Cond.push_back(SecondLastInst->getOperand(1));
225     FBB = LastInst->getOperand(0).getMBB();
226     return false;
227   }
228   
229   // If the block ends with two PPC:Bs, handle it.  The second one is not
230   // executed, so remove it.
231   if (SecondLastInst->getOpcode() == PPC::B && 
232       LastInst->getOpcode() == PPC::B) {
233     TBB = SecondLastInst->getOperand(0).getMBB();
234     I = LastInst;
235     I->eraseFromParent();
236     return false;
237   }
238
239   // Otherwise, can't handle this.
240   return true;
241 }
242
243 unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
244   MachineBasicBlock::iterator I = MBB.end();
245   if (I == MBB.begin()) return 0;
246   --I;
247   if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC)
248     return 0;
249   
250   // Remove the branch.
251   I->eraseFromParent();
252   
253   I = MBB.end();
254
255   if (I == MBB.begin()) return 1;
256   --I;
257   if (I->getOpcode() != PPC::BCC)
258     return 1;
259   
260   // Remove the branch.
261   I->eraseFromParent();
262   return 2;
263 }
264
265 unsigned
266 PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
267                            MachineBasicBlock *FBB,
268                            const std::vector<MachineOperand> &Cond) const {
269   // Shouldn't be a fall through.
270   assert(TBB && "InsertBranch must not be told to insert a fallthrough");
271   assert((Cond.size() == 2 || Cond.size() == 0) && 
272          "PPC branch conditions have two components!");
273   
274   // One-way branch.
275   if (FBB == 0) {
276     if (Cond.empty())   // Unconditional branch
277       BuildMI(&MBB, get(PPC::B)).addMBB(TBB);
278     else                // Conditional branch
279       BuildMI(&MBB, get(PPC::BCC))
280         .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
281     return 1;
282   }
283   
284   // Two-way Conditional Branch.
285   BuildMI(&MBB, get(PPC::BCC))
286     .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
287   BuildMI(&MBB, get(PPC::B)).addMBB(FBB);
288   return 2;
289 }
290
291 void PPCInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
292                                    MachineBasicBlock::iterator MI,
293                                    unsigned DestReg, unsigned SrcReg,
294                                    const TargetRegisterClass *DestRC,
295                                    const TargetRegisterClass *SrcRC) const {
296   if (DestRC != SrcRC) {
297     cerr << "Not yet supported!";
298     abort();
299   }
300
301   if (DestRC == PPC::GPRCRegisterClass) {
302     BuildMI(MBB, MI, get(PPC::OR), DestReg).addReg(SrcReg).addReg(SrcReg);
303   } else if (DestRC == PPC::G8RCRegisterClass) {
304     BuildMI(MBB, MI, get(PPC::OR8), DestReg).addReg(SrcReg).addReg(SrcReg);
305   } else if (DestRC == PPC::F4RCRegisterClass) {
306     BuildMI(MBB, MI, get(PPC::FMRS), DestReg).addReg(SrcReg);
307   } else if (DestRC == PPC::F8RCRegisterClass) {
308     BuildMI(MBB, MI, get(PPC::FMRD), DestReg).addReg(SrcReg);
309   } else if (DestRC == PPC::CRRCRegisterClass) {
310     BuildMI(MBB, MI, get(PPC::MCRF), DestReg).addReg(SrcReg);
311   } else if (DestRC == PPC::VRRCRegisterClass) {
312     BuildMI(MBB, MI, get(PPC::VOR), DestReg).addReg(SrcReg).addReg(SrcReg);
313   } else {
314     cerr << "Attempt to copy register that is not GPR or FPR";
315     abort();
316   }
317 }
318
319 static void StoreRegToStackSlot(const TargetInstrInfo &TII,
320                                 unsigned SrcReg, bool isKill, int FrameIdx,
321                                 const TargetRegisterClass *RC,
322                                 SmallVectorImpl<MachineInstr*> &NewMIs) {
323   if (RC == PPC::GPRCRegisterClass) {
324     if (SrcReg != PPC::LR) {
325       NewMIs.push_back(addFrameReference(BuildMI(TII.get(PPC::STW))
326                                 .addReg(SrcReg, false, false, isKill), FrameIdx));
327     } else {
328       // FIXME: this spills LR immediately to memory in one step.  To do this,
329       // we use R11, which we know cannot be used in the prolog/epilog.  This is
330       // a hack.
331       NewMIs.push_back(BuildMI(TII.get(PPC::MFLR), PPC::R11));
332       NewMIs.push_back(addFrameReference(BuildMI(TII.get(PPC::STW))
333                               .addReg(PPC::R11, false, false, isKill), FrameIdx));
334     }
335   } else if (RC == PPC::G8RCRegisterClass) {
336     if (SrcReg != PPC::LR8) {
337       NewMIs.push_back(addFrameReference(BuildMI(TII.get(PPC::STD))
338                                 .addReg(SrcReg, false, false, isKill), FrameIdx));
339     } else {
340       // FIXME: this spills LR immediately to memory in one step.  To do this,
341       // we use R11, which we know cannot be used in the prolog/epilog.  This is
342       // a hack.
343       NewMIs.push_back(BuildMI(TII.get(PPC::MFLR8), PPC::X11));
344       NewMIs.push_back(addFrameReference(BuildMI(TII.get(PPC::STD))
345                               .addReg(PPC::X11, false, false, isKill), FrameIdx));
346     }
347   } else if (RC == PPC::F8RCRegisterClass) {
348     NewMIs.push_back(addFrameReference(BuildMI(TII.get(PPC::STFD))
349                                 .addReg(SrcReg, false, false, isKill), FrameIdx));
350   } else if (RC == PPC::F4RCRegisterClass) {
351     NewMIs.push_back(addFrameReference(BuildMI(TII.get(PPC::STFS))
352                                 .addReg(SrcReg, false, false, isKill), FrameIdx));
353   } else if (RC == PPC::CRRCRegisterClass) {
354     // FIXME: We use R0 here, because it isn't available for RA.
355     // We need to store the CR in the low 4-bits of the saved value.  First,
356     // issue a MFCR to save all of the CRBits.
357     NewMIs.push_back(BuildMI(TII.get(PPC::MFCR), PPC::R0));
358     
359     // If the saved register wasn't CR0, shift the bits left so that they are in
360     // CR0's slot.
361     if (SrcReg != PPC::CR0) {
362       unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(SrcReg)*4;
363       // rlwinm r0, r0, ShiftBits, 0, 31.
364       NewMIs.push_back(BuildMI(TII.get(PPC::RLWINM), PPC::R0)
365                        .addReg(PPC::R0).addImm(ShiftBits).addImm(0).addImm(31));
366     }
367     
368     NewMIs.push_back(addFrameReference(BuildMI(TII.get(PPC::STW))
369                                .addReg(PPC::R0, false, false, isKill), FrameIdx));
370   } else if (RC == PPC::VRRCRegisterClass) {
371     // We don't have indexed addressing for vector loads.  Emit:
372     // R0 = ADDI FI#
373     // STVX VAL, 0, R0
374     // 
375     // FIXME: We use R0 here, because it isn't available for RA.
376     NewMIs.push_back(addFrameReference(BuildMI(TII.get(PPC::ADDI), PPC::R0),
377                                        FrameIdx, 0, 0));
378     NewMIs.push_back(BuildMI(TII.get(PPC::STVX))
379            .addReg(SrcReg, false, false, isKill).addReg(PPC::R0).addReg(PPC::R0));
380   } else {
381     assert(0 && "Unknown regclass!");
382     abort();
383   }
384 }
385
386 void
387 PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
388                                      MachineBasicBlock::iterator MI,
389                                      unsigned SrcReg, bool isKill, int FrameIdx,
390                                      const TargetRegisterClass *RC) const {
391   SmallVector<MachineInstr*, 4> NewMIs;
392   StoreRegToStackSlot(*this, SrcReg, isKill, FrameIdx, RC, NewMIs);
393   for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
394     MBB.insert(MI, NewMIs[i]);
395 }
396
397 void PPCInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
398                                      bool isKill,
399                                      SmallVectorImpl<MachineOperand> &Addr,
400                                      const TargetRegisterClass *RC,
401                                  SmallVectorImpl<MachineInstr*> &NewMIs) const {
402   if (Addr[0].isFrameIndex()) {
403     StoreRegToStackSlot(*this, SrcReg, isKill, Addr[0].getIndex(), RC, NewMIs);
404     return;
405   }
406
407   unsigned Opc = 0;
408   if (RC == PPC::GPRCRegisterClass) {
409     Opc = PPC::STW;
410   } else if (RC == PPC::G8RCRegisterClass) {
411     Opc = PPC::STD;
412   } else if (RC == PPC::F8RCRegisterClass) {
413     Opc = PPC::STFD;
414   } else if (RC == PPC::F4RCRegisterClass) {
415     Opc = PPC::STFS;
416   } else if (RC == PPC::VRRCRegisterClass) {
417     Opc = PPC::STVX;
418   } else {
419     assert(0 && "Unknown regclass!");
420     abort();
421   }
422   MachineInstrBuilder MIB = BuildMI(get(Opc))
423     .addReg(SrcReg, false, false, isKill);
424   for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
425     MachineOperand &MO = Addr[i];
426     if (MO.isRegister())
427       MIB.addReg(MO.getReg());
428     else if (MO.isImmediate())
429       MIB.addImm(MO.getImm());
430     else
431       MIB.addFrameIndex(MO.getIndex());
432   }
433   NewMIs.push_back(MIB);
434   return;
435 }
436
437 static void LoadRegFromStackSlot(const TargetInstrInfo &TII,
438                                  unsigned DestReg, int FrameIdx,
439                                  const TargetRegisterClass *RC,
440                                  SmallVectorImpl<MachineInstr*> &NewMIs) {
441   if (RC == PPC::GPRCRegisterClass) {
442     if (DestReg != PPC::LR) {
443       NewMIs.push_back(addFrameReference(BuildMI(TII.get(PPC::LWZ), DestReg),
444                                          FrameIdx));
445     } else {
446       NewMIs.push_back(addFrameReference(BuildMI(TII.get(PPC::LWZ), PPC::R11),
447                                          FrameIdx));
448       NewMIs.push_back(BuildMI(TII.get(PPC::MTLR)).addReg(PPC::R11));
449     }
450   } else if (RC == PPC::G8RCRegisterClass) {
451     if (DestReg != PPC::LR8) {
452       NewMIs.push_back(addFrameReference(BuildMI(TII.get(PPC::LD), DestReg),
453                                          FrameIdx));
454     } else {
455       NewMIs.push_back(addFrameReference(BuildMI(TII.get(PPC::LD), PPC::R11),
456                                          FrameIdx));
457       NewMIs.push_back(BuildMI(TII.get(PPC::MTLR8)).addReg(PPC::R11));
458     }
459   } else if (RC == PPC::F8RCRegisterClass) {
460     NewMIs.push_back(addFrameReference(BuildMI(TII.get(PPC::LFD), DestReg),
461                                        FrameIdx));
462   } else if (RC == PPC::F4RCRegisterClass) {
463     NewMIs.push_back(addFrameReference(BuildMI(TII.get(PPC::LFS), DestReg),
464                                        FrameIdx));
465   } else if (RC == PPC::CRRCRegisterClass) {
466     // FIXME: We use R0 here, because it isn't available for RA.
467     NewMIs.push_back(addFrameReference(BuildMI(TII.get(PPC::LWZ), PPC::R0),
468                                        FrameIdx));
469     
470     // If the reloaded register isn't CR0, shift the bits right so that they are
471     // in the right CR's slot.
472     if (DestReg != PPC::CR0) {
473       unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(DestReg)*4;
474       // rlwinm r11, r11, 32-ShiftBits, 0, 31.
475       NewMIs.push_back(BuildMI(TII.get(PPC::RLWINM), PPC::R0)
476                     .addReg(PPC::R0).addImm(32-ShiftBits).addImm(0).addImm(31));
477     }
478     
479     NewMIs.push_back(BuildMI(TII.get(PPC::MTCRF), DestReg).addReg(PPC::R0));
480   } else if (RC == PPC::VRRCRegisterClass) {
481     // We don't have indexed addressing for vector loads.  Emit:
482     // R0 = ADDI FI#
483     // Dest = LVX 0, R0
484     // 
485     // FIXME: We use R0 here, because it isn't available for RA.
486     NewMIs.push_back(addFrameReference(BuildMI(TII.get(PPC::ADDI), PPC::R0),
487                                        FrameIdx, 0, 0));
488     NewMIs.push_back(BuildMI(TII.get(PPC::LVX),DestReg).addReg(PPC::R0)
489                      .addReg(PPC::R0));
490   } else {
491     assert(0 && "Unknown regclass!");
492     abort();
493   }
494 }
495
496 void
497 PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
498                                       MachineBasicBlock::iterator MI,
499                                       unsigned DestReg, int FrameIdx,
500                                       const TargetRegisterClass *RC) const {
501   SmallVector<MachineInstr*, 4> NewMIs;
502   LoadRegFromStackSlot(*this, DestReg, FrameIdx, RC, NewMIs);
503   for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
504     MBB.insert(MI, NewMIs[i]);
505 }
506
507 void PPCInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
508                                       SmallVectorImpl<MachineOperand> &Addr,
509                                       const TargetRegisterClass *RC,
510                                   SmallVectorImpl<MachineInstr*> &NewMIs) const{
511   if (Addr[0].isFrameIndex()) {
512     LoadRegFromStackSlot(*this, DestReg, Addr[0].getIndex(), RC, NewMIs);
513     return;
514   }
515
516   unsigned Opc = 0;
517   if (RC == PPC::GPRCRegisterClass) {
518     assert(DestReg != PPC::LR && "Can't handle this yet!");
519     Opc = PPC::LWZ;
520   } else if (RC == PPC::G8RCRegisterClass) {
521     assert(DestReg != PPC::LR8 && "Can't handle this yet!");
522     Opc = PPC::LD;
523   } else if (RC == PPC::F8RCRegisterClass) {
524     Opc = PPC::LFD;
525   } else if (RC == PPC::F4RCRegisterClass) {
526     Opc = PPC::LFS;
527   } else if (RC == PPC::VRRCRegisterClass) {
528     Opc = PPC::LVX;
529   } else {
530     assert(0 && "Unknown regclass!");
531     abort();
532   }
533   MachineInstrBuilder MIB = BuildMI(get(Opc), DestReg);
534   for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
535     MachineOperand &MO = Addr[i];
536     if (MO.isRegister())
537       MIB.addReg(MO.getReg());
538     else if (MO.isImmediate())
539       MIB.addImm(MO.getImm());
540     else
541       MIB.addFrameIndex(MO.getIndex());
542   }
543   NewMIs.push_back(MIB);
544   return;
545 }
546
547 /// foldMemoryOperand - PowerPC (like most RISC's) can only fold spills into
548 /// copy instructions, turning them into load/store instructions.
549 MachineInstr *PPCInstrInfo::foldMemoryOperand(MachineFunction &MF,
550                                               MachineInstr *MI,
551                                               SmallVectorImpl<unsigned> &Ops,
552                                               int FrameIndex) const {
553   if (Ops.size() != 1) return NULL;
554
555   // Make sure this is a reg-reg copy.  Note that we can't handle MCRF, because
556   // it takes more than one instruction to store it.
557   unsigned Opc = MI->getOpcode();
558   unsigned OpNum = Ops[0];
559
560   MachineInstr *NewMI = NULL;
561   if ((Opc == PPC::OR &&
562        MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
563     if (OpNum == 0) {  // move -> store
564       unsigned InReg = MI->getOperand(1).getReg();
565       NewMI = addFrameReference(BuildMI(get(PPC::STW)).addReg(InReg),
566                                 FrameIndex);
567     } else {           // move -> load
568       unsigned OutReg = MI->getOperand(0).getReg();
569       NewMI = addFrameReference(BuildMI(get(PPC::LWZ), OutReg),
570                                 FrameIndex);
571     }
572   } else if ((Opc == PPC::OR8 &&
573               MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
574     if (OpNum == 0) {  // move -> store
575       unsigned InReg = MI->getOperand(1).getReg();
576       NewMI = addFrameReference(BuildMI(get(PPC::STD)).addReg(InReg),
577                                 FrameIndex);
578     } else {           // move -> load
579       unsigned OutReg = MI->getOperand(0).getReg();
580       NewMI = addFrameReference(BuildMI(get(PPC::LD), OutReg), FrameIndex);
581     }
582   } else if (Opc == PPC::FMRD) {
583     if (OpNum == 0) {  // move -> store
584       unsigned InReg = MI->getOperand(1).getReg();
585       NewMI = addFrameReference(BuildMI(get(PPC::STFD)).addReg(InReg),
586                                 FrameIndex);
587     } else {           // move -> load
588       unsigned OutReg = MI->getOperand(0).getReg();
589       NewMI = addFrameReference(BuildMI(get(PPC::LFD), OutReg), FrameIndex);
590     }
591   } else if (Opc == PPC::FMRS) {
592     if (OpNum == 0) {  // move -> store
593       unsigned InReg = MI->getOperand(1).getReg();
594       NewMI = addFrameReference(BuildMI(get(PPC::STFS)).addReg(InReg),
595                                 FrameIndex);
596     } else {           // move -> load
597       unsigned OutReg = MI->getOperand(0).getReg();
598       NewMI = addFrameReference(BuildMI(get(PPC::LFS), OutReg), FrameIndex);
599     }
600   }
601
602   if (NewMI)
603     NewMI->copyKillDeadInfo(MI);
604   return NewMI;
605 }
606
607 bool PPCInstrInfo::canFoldMemoryOperand(MachineInstr *MI,
608                                         SmallVectorImpl<unsigned> &Ops) const {
609   if (Ops.size() != 1) return false;
610
611   // Make sure this is a reg-reg copy.  Note that we can't handle MCRF, because
612   // it takes more than one instruction to store it.
613   unsigned Opc = MI->getOpcode();
614
615   if ((Opc == PPC::OR &&
616        MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
617     return true;
618   else if ((Opc == PPC::OR8 &&
619               MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
620     return true;
621   else if (Opc == PPC::FMRD || Opc == PPC::FMRS)
622     return true;
623
624   return false;
625 }
626
627
628 bool PPCInstrInfo::BlockHasNoFallThrough(MachineBasicBlock &MBB) const {
629   if (MBB.empty()) return false;
630   
631   switch (MBB.back().getOpcode()) {
632   case PPC::BLR:   // Return.
633   case PPC::B:     // Uncond branch.
634   case PPC::BCTR:  // Indirect branch.
635     return true;
636   default: return false;
637   }
638 }
639
640 bool PPCInstrInfo::
641 ReverseBranchCondition(std::vector<MachineOperand> &Cond) const {
642   assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
643   // Leave the CR# the same, but invert the condition.
644   Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
645   return false;
646 }