PPCInstrInfo.cpp revision 64c34e253563a8ba6b41fbce2bb020632cf65961
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 "PPC.h"
16#include "PPCInstrBuilder.h"
17#include "PPCMachineFunctionInfo.h"
18#include "PPCTargetMachine.h"
19#include "PPCHazardRecognizers.h"
20#include "MCTargetDesc/PPCPredicates.h"
21#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/MachineInstrBuilder.h"
23#include "llvm/CodeGen/MachineMemOperand.h"
24#include "llvm/CodeGen/MachineRegisterInfo.h"
25#include "llvm/MC/MCAsmInfo.h"
26#include "llvm/Support/CommandLine.h"
27#include "llvm/Support/ErrorHandling.h"
28#include "llvm/Support/TargetRegistry.h"
29#include "llvm/Support/raw_ostream.h"
30#include "llvm/ADT/STLExtras.h"
31
32#define GET_INSTRINFO_CTOR
33#include "PPCGenInstrInfo.inc"
34
35namespace llvm {
36extern cl::opt<bool> EnablePPC32RS;  // FIXME (64-bit): See PPCRegisterInfo.cpp.
37extern cl::opt<bool> EnablePPC64RS;  // FIXME (64-bit): See PPCRegisterInfo.cpp.
38}
39
40using namespace llvm;
41
42PPCInstrInfo::PPCInstrInfo(PPCTargetMachine &tm)
43  : PPCGenInstrInfo(PPC::ADJCALLSTACKDOWN, PPC::ADJCALLSTACKUP),
44    TM(tm), RI(*TM.getSubtargetImpl(), *this) {}
45
46/// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
47/// this target when scheduling the DAG.
48ScheduleHazardRecognizer *PPCInstrInfo::CreateTargetHazardRecognizer(
49  const TargetMachine *TM,
50  const ScheduleDAG *DAG) const {
51  unsigned Directive = TM->getSubtarget<PPCSubtarget>().getDarwinDirective();
52  if (Directive == PPC::DIR_440) {
53    const InstrItineraryData *II = TM->getInstrItineraryData();
54    return new PPCHazardRecognizer440(II, DAG);
55  }
56
57  return TargetInstrInfoImpl::CreateTargetHazardRecognizer(TM, DAG);
58}
59
60/// CreateTargetPostRAHazardRecognizer - Return the postRA hazard recognizer
61/// to use for this target when scheduling the DAG.
62ScheduleHazardRecognizer *PPCInstrInfo::CreateTargetPostRAHazardRecognizer(
63  const InstrItineraryData *II,
64  const ScheduleDAG *DAG) const {
65  unsigned Directive = TM.getSubtarget<PPCSubtarget>().getDarwinDirective();
66
67  // Most subtargets use a PPC970 recognizer.
68  if (Directive != PPC::DIR_440) {
69    const TargetInstrInfo *TII = TM.getInstrInfo();
70    assert(TII && "No InstrInfo?");
71
72    return new PPCHazardRecognizer970(*TII);
73  }
74
75  return TargetInstrInfoImpl::CreateTargetPostRAHazardRecognizer(II, DAG);
76}
77unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
78                                           int &FrameIndex) const {
79  switch (MI->getOpcode()) {
80  default: break;
81  case PPC::LD:
82  case PPC::LWZ:
83  case PPC::LFS:
84  case PPC::LFD:
85    if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
86        MI->getOperand(2).isFI()) {
87      FrameIndex = MI->getOperand(2).getIndex();
88      return MI->getOperand(0).getReg();
89    }
90    break;
91  }
92  return 0;
93}
94
95unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
96                                          int &FrameIndex) const {
97  switch (MI->getOpcode()) {
98  default: break;
99  case PPC::STD:
100  case PPC::STW:
101  case PPC::STFS:
102  case PPC::STFD:
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// commuteInstruction - We can commute rlwimi instructions, but only if the
114// rotate amt is zero.  We also have to munge the immediates a bit.
115MachineInstr *
116PPCInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
117  MachineFunction &MF = *MI->getParent()->getParent();
118
119  // Normal instructions can be commuted the obvious way.
120  if (MI->getOpcode() != PPC::RLWIMI)
121    return TargetInstrInfoImpl::commuteInstruction(MI, NewMI);
122
123  // Cannot commute if it has a non-zero rotate count.
124  if (MI->getOperand(3).getImm() != 0)
125    return 0;
126
127  // If we have a zero rotate count, we have:
128  //   M = mask(MB,ME)
129  //   Op0 = (Op1 & ~M) | (Op2 & M)
130  // Change this to:
131  //   M = mask((ME+1)&31, (MB-1)&31)
132  //   Op0 = (Op2 & ~M) | (Op1 & M)
133
134  // Swap op1/op2
135  unsigned Reg0 = MI->getOperand(0).getReg();
136  unsigned Reg1 = MI->getOperand(1).getReg();
137  unsigned Reg2 = MI->getOperand(2).getReg();
138  bool Reg1IsKill = MI->getOperand(1).isKill();
139  bool Reg2IsKill = MI->getOperand(2).isKill();
140  bool ChangeReg0 = false;
141  // If machine instrs are no longer in two-address forms, update
142  // destination register as well.
143  if (Reg0 == Reg1) {
144    // Must be two address instruction!
145    assert(MI->getDesc().getOperandConstraint(0, MCOI::TIED_TO) &&
146           "Expecting a two-address instruction!");
147    Reg2IsKill = false;
148    ChangeReg0 = true;
149  }
150
151  // Masks.
152  unsigned MB = MI->getOperand(4).getImm();
153  unsigned ME = MI->getOperand(5).getImm();
154
155  if (NewMI) {
156    // Create a new instruction.
157    unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg();
158    bool Reg0IsDead = MI->getOperand(0).isDead();
159    return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
160      .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
161      .addReg(Reg2, getKillRegState(Reg2IsKill))
162      .addReg(Reg1, getKillRegState(Reg1IsKill))
163      .addImm((ME+1) & 31)
164      .addImm((MB-1) & 31);
165  }
166
167  if (ChangeReg0)
168    MI->getOperand(0).setReg(Reg2);
169  MI->getOperand(2).setReg(Reg1);
170  MI->getOperand(1).setReg(Reg2);
171  MI->getOperand(2).setIsKill(Reg1IsKill);
172  MI->getOperand(1).setIsKill(Reg2IsKill);
173
174  // Swap the mask around.
175  MI->getOperand(4).setImm((ME+1) & 31);
176  MI->getOperand(5).setImm((MB-1) & 31);
177  return MI;
178}
179
180void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
181                              MachineBasicBlock::iterator MI) const {
182  DebugLoc DL;
183  BuildMI(MBB, MI, DL, get(PPC::NOP));
184}
185
186
187// Branch analysis.
188bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
189                                 MachineBasicBlock *&FBB,
190                                 SmallVectorImpl<MachineOperand> &Cond,
191                                 bool AllowModify) const {
192  // If the block has no terminators, it just falls into the block after it.
193  MachineBasicBlock::iterator I = MBB.end();
194  if (I == MBB.begin())
195    return false;
196  --I;
197  while (I->isDebugValue()) {
198    if (I == MBB.begin())
199      return false;
200    --I;
201  }
202  if (!isUnpredicatedTerminator(I))
203    return false;
204
205  // Get the last instruction in the block.
206  MachineInstr *LastInst = I;
207
208  // If there is only one terminator instruction, process it.
209  if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
210    if (LastInst->getOpcode() == PPC::B) {
211      if (!LastInst->getOperand(0).isMBB())
212        return true;
213      TBB = LastInst->getOperand(0).getMBB();
214      return false;
215    } else if (LastInst->getOpcode() == PPC::BCC) {
216      if (!LastInst->getOperand(2).isMBB())
217        return true;
218      // Block ends with fall-through condbranch.
219      TBB = LastInst->getOperand(2).getMBB();
220      Cond.push_back(LastInst->getOperand(0));
221      Cond.push_back(LastInst->getOperand(1));
222      return false;
223    }
224    // Otherwise, don't know what this is.
225    return true;
226  }
227
228  // Get the instruction before it if it's a terminator.
229  MachineInstr *SecondLastInst = I;
230
231  // If there are three terminators, we don't know what sort of block this is.
232  if (SecondLastInst && I != MBB.begin() &&
233      isUnpredicatedTerminator(--I))
234    return true;
235
236  // If the block ends with PPC::B and PPC:BCC, handle it.
237  if (SecondLastInst->getOpcode() == PPC::BCC &&
238      LastInst->getOpcode() == PPC::B) {
239    if (!SecondLastInst->getOperand(2).isMBB() ||
240        !LastInst->getOperand(0).isMBB())
241      return true;
242    TBB =  SecondLastInst->getOperand(2).getMBB();
243    Cond.push_back(SecondLastInst->getOperand(0));
244    Cond.push_back(SecondLastInst->getOperand(1));
245    FBB = LastInst->getOperand(0).getMBB();
246    return false;
247  }
248
249  // If the block ends with two PPC:Bs, handle it.  The second one is not
250  // executed, so remove it.
251  if (SecondLastInst->getOpcode() == PPC::B &&
252      LastInst->getOpcode() == PPC::B) {
253    if (!SecondLastInst->getOperand(0).isMBB())
254      return true;
255    TBB = SecondLastInst->getOperand(0).getMBB();
256    I = LastInst;
257    if (AllowModify)
258      I->eraseFromParent();
259    return false;
260  }
261
262  // Otherwise, can't handle this.
263  return true;
264}
265
266unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
267  MachineBasicBlock::iterator I = MBB.end();
268  if (I == MBB.begin()) return 0;
269  --I;
270  while (I->isDebugValue()) {
271    if (I == MBB.begin())
272      return 0;
273    --I;
274  }
275  if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC)
276    return 0;
277
278  // Remove the branch.
279  I->eraseFromParent();
280
281  I = MBB.end();
282
283  if (I == MBB.begin()) return 1;
284  --I;
285  if (I->getOpcode() != PPC::BCC)
286    return 1;
287
288  // Remove the branch.
289  I->eraseFromParent();
290  return 2;
291}
292
293unsigned
294PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
295                           MachineBasicBlock *FBB,
296                           const SmallVectorImpl<MachineOperand> &Cond,
297                           DebugLoc DL) const {
298  // Shouldn't be a fall through.
299  assert(TBB && "InsertBranch must not be told to insert a fallthrough");
300  assert((Cond.size() == 2 || Cond.size() == 0) &&
301         "PPC branch conditions have two components!");
302
303  // One-way branch.
304  if (FBB == 0) {
305    if (Cond.empty())   // Unconditional branch
306      BuildMI(&MBB, DL, get(PPC::B)).addMBB(TBB);
307    else                // Conditional branch
308      BuildMI(&MBB, DL, get(PPC::BCC))
309        .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
310    return 1;
311  }
312
313  // Two-way Conditional Branch.
314  BuildMI(&MBB, DL, get(PPC::BCC))
315    .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
316  BuildMI(&MBB, DL, get(PPC::B)).addMBB(FBB);
317  return 2;
318}
319
320void PPCInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
321                               MachineBasicBlock::iterator I, DebugLoc DL,
322                               unsigned DestReg, unsigned SrcReg,
323                               bool KillSrc) const {
324  unsigned Opc;
325  if (PPC::GPRCRegClass.contains(DestReg, SrcReg))
326    Opc = PPC::OR;
327  else if (PPC::G8RCRegClass.contains(DestReg, SrcReg))
328    Opc = PPC::OR8;
329  else if (PPC::F4RCRegClass.contains(DestReg, SrcReg))
330    Opc = PPC::FMR;
331  else if (PPC::CRRCRegClass.contains(DestReg, SrcReg))
332    Opc = PPC::MCRF;
333  else if (PPC::VRRCRegClass.contains(DestReg, SrcReg))
334    Opc = PPC::VOR;
335  else if (PPC::CRBITRCRegClass.contains(DestReg, SrcReg))
336    Opc = PPC::CROR;
337  else
338    llvm_unreachable("Impossible reg-to-reg copy");
339
340  const MCInstrDesc &MCID = get(Opc);
341  if (MCID.getNumOperands() == 3)
342    BuildMI(MBB, I, DL, MCID, DestReg)
343      .addReg(SrcReg).addReg(SrcReg, getKillRegState(KillSrc));
344  else
345    BuildMI(MBB, I, DL, MCID, DestReg).addReg(SrcReg, getKillRegState(KillSrc));
346}
347
348bool
349PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
350                                  unsigned SrcReg, bool isKill,
351                                  int FrameIdx,
352                                  const TargetRegisterClass *RC,
353                                  SmallVectorImpl<MachineInstr*> &NewMIs) const{
354  DebugLoc DL;
355  if (PPC::GPRCRegisterClass->hasSubClassEq(RC)) {
356    if (SrcReg != PPC::LR) {
357      NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
358                                         .addReg(SrcReg,
359                                                 getKillRegState(isKill)),
360                                         FrameIdx));
361    } else {
362      // FIXME: this spills LR immediately to memory in one step.  To do this,
363      // we use R11, which we know cannot be used in the prolog/epilog.  This is
364      // a hack.
365      NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFLR), PPC::R11));
366      NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
367                                         .addReg(PPC::R11,
368                                                 getKillRegState(isKill)),
369                                         FrameIdx));
370    }
371  } else if (PPC::G8RCRegisterClass->hasSubClassEq(RC)) {
372    if (SrcReg != PPC::LR8) {
373      NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
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::MFLR8), PPC::X11));
382      NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
383                                         .addReg(PPC::X11,
384                                                 getKillRegState(isKill)),
385                                         FrameIdx));
386    }
387  } else if (PPC::F8RCRegisterClass->hasSubClassEq(RC)) {
388    NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD))
389                                       .addReg(SrcReg,
390                                               getKillRegState(isKill)),
391                                       FrameIdx));
392  } else if (PPC::F4RCRegisterClass->hasSubClassEq(RC)) {
393    NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS))
394                                       .addReg(SrcReg,
395                                               getKillRegState(isKill)),
396                                       FrameIdx));
397  } else if (PPC::CRRCRegisterClass->hasSubClassEq(RC)) {
398    if ((EnablePPC32RS && !TM.getSubtargetImpl()->isPPC64()) ||
399        (EnablePPC64RS && TM.getSubtargetImpl()->isPPC64())) {
400      // FIXME (64-bit): Enable
401      NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR))
402                                         .addReg(SrcReg,
403                                                 getKillRegState(isKill)),
404                                         FrameIdx));
405      return true;
406    } else {
407      // FIXME: We need a scatch reg here.  The trouble with using R0 is that
408      // it's possible for the stack frame to be so big the save location is
409      // out of range of immediate offsets, necessitating another register.
410      // We hack this on Darwin by reserving R2.  It's probably broken on Linux
411      // at the moment.
412
413      // We need to store the CR in the low 4-bits of the saved value.  First,
414      // issue a MFCR to save all of the CRBits.
415      unsigned ScratchReg = TM.getSubtargetImpl()->isDarwinABI() ?
416                                                           PPC::R2 : PPC::R0;
417      NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFCRpseud), ScratchReg)
418                               .addReg(SrcReg, getKillRegState(isKill)));
419
420      // If the saved register wasn't CR0, shift the bits left so that they are
421      // in CR0's slot.
422      if (SrcReg != PPC::CR0) {
423        unsigned ShiftBits = getPPCRegisterNumbering(SrcReg)*4;
424        // rlwinm scratch, scratch, ShiftBits, 0, 31.
425        NewMIs.push_back(BuildMI(MF, DL, get(PPC::RLWINM), ScratchReg)
426                       .addReg(ScratchReg).addImm(ShiftBits)
427                       .addImm(0).addImm(31));
428      }
429
430      NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
431                                         .addReg(ScratchReg,
432                                                 getKillRegState(isKill)),
433                                         FrameIdx));
434    }
435  } else if (PPC::CRBITRCRegisterClass->hasSubClassEq(RC)) {
436    // FIXME: We use CRi here because there is no mtcrf on a bit. Since the
437    // backend currently only uses CR1EQ as an individual bit, this should
438    // not cause any bug. If we need other uses of CR bits, the following
439    // code may be invalid.
440    unsigned Reg = 0;
441    if (SrcReg == PPC::CR0LT || SrcReg == PPC::CR0GT ||
442        SrcReg == PPC::CR0EQ || SrcReg == PPC::CR0UN)
443      Reg = PPC::CR0;
444    else if (SrcReg == PPC::CR1LT || SrcReg == PPC::CR1GT ||
445             SrcReg == PPC::CR1EQ || SrcReg == PPC::CR1UN)
446      Reg = PPC::CR1;
447    else if (SrcReg == PPC::CR2LT || SrcReg == PPC::CR2GT ||
448             SrcReg == PPC::CR2EQ || SrcReg == PPC::CR2UN)
449      Reg = PPC::CR2;
450    else if (SrcReg == PPC::CR3LT || SrcReg == PPC::CR3GT ||
451             SrcReg == PPC::CR3EQ || SrcReg == PPC::CR3UN)
452      Reg = PPC::CR3;
453    else if (SrcReg == PPC::CR4LT || SrcReg == PPC::CR4GT ||
454             SrcReg == PPC::CR4EQ || SrcReg == PPC::CR4UN)
455      Reg = PPC::CR4;
456    else if (SrcReg == PPC::CR5LT || SrcReg == PPC::CR5GT ||
457             SrcReg == PPC::CR5EQ || SrcReg == PPC::CR5UN)
458      Reg = PPC::CR5;
459    else if (SrcReg == PPC::CR6LT || SrcReg == PPC::CR6GT ||
460             SrcReg == PPC::CR6EQ || SrcReg == PPC::CR6UN)
461      Reg = PPC::CR6;
462    else if (SrcReg == PPC::CR7LT || SrcReg == PPC::CR7GT ||
463             SrcReg == PPC::CR7EQ || SrcReg == PPC::CR7UN)
464      Reg = PPC::CR7;
465
466    return StoreRegToStackSlot(MF, Reg, isKill, FrameIdx,
467                               PPC::CRRCRegisterClass, NewMIs);
468
469  } else if (PPC::VRRCRegisterClass->hasSubClassEq(RC)) {
470    // We don't have indexed addressing for vector loads.  Emit:
471    // R0 = ADDI FI#
472    // STVX VAL, 0, R0
473    //
474    // FIXME: We use R0 here, because it isn't available for RA.
475    NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::ADDI), PPC::R0),
476                                       FrameIdx, 0, 0));
477    NewMIs.push_back(BuildMI(MF, DL, get(PPC::STVX))
478                     .addReg(SrcReg, getKillRegState(isKill))
479                     .addReg(PPC::R0)
480                     .addReg(PPC::R0));
481  } else {
482    llvm_unreachable("Unknown regclass!");
483  }
484
485  return false;
486}
487
488void
489PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
490                                  MachineBasicBlock::iterator MI,
491                                  unsigned SrcReg, bool isKill, int FrameIdx,
492                                  const TargetRegisterClass *RC,
493                                  const TargetRegisterInfo *TRI) const {
494  MachineFunction &MF = *MBB.getParent();
495  SmallVector<MachineInstr*, 4> NewMIs;
496
497  if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs)) {
498    PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
499    FuncInfo->setSpillsCR();
500  }
501
502  for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
503    MBB.insert(MI, NewMIs[i]);
504
505  const MachineFrameInfo &MFI = *MF.getFrameInfo();
506  MachineMemOperand *MMO =
507    MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
508                            MachineMemOperand::MOStore,
509                            MFI.getObjectSize(FrameIdx),
510                            MFI.getObjectAlignment(FrameIdx));
511  NewMIs.back()->addMemOperand(MF, MMO);
512}
513
514void
515PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL,
516                                   unsigned DestReg, int FrameIdx,
517                                   const TargetRegisterClass *RC,
518                                   SmallVectorImpl<MachineInstr*> &NewMIs)const{
519  if (PPC::GPRCRegisterClass->hasSubClassEq(RC)) {
520    if (DestReg != PPC::LR) {
521      NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
522                                                 DestReg), FrameIdx));
523    } else {
524      NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
525                                                 PPC::R11), FrameIdx));
526      NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTLR)).addReg(PPC::R11));
527    }
528  } else if (PPC::G8RCRegisterClass->hasSubClassEq(RC)) {
529    if (DestReg != PPC::LR8) {
530      NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg),
531                                         FrameIdx));
532    } else {
533      NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD),
534                                                 PPC::R11), FrameIdx));
535      NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTLR8)).addReg(PPC::R11));
536    }
537  } else if (PPC::F8RCRegisterClass->hasSubClassEq(RC)) {
538    NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg),
539                                       FrameIdx));
540  } else if (PPC::F4RCRegisterClass->hasSubClassEq(RC)) {
541    NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg),
542                                       FrameIdx));
543  } else if (PPC::CRRCRegisterClass->hasSubClassEq(RC)) {
544    // FIXME: We need a scatch reg here.  The trouble with using R0 is that
545    // it's possible for the stack frame to be so big the save location is
546    // out of range of immediate offsets, necessitating another register.
547    // We hack this on Darwin by reserving R2.  It's probably broken on Linux
548    // at the moment.
549    unsigned ScratchReg = TM.getSubtargetImpl()->isDarwinABI() ?
550                                                          PPC::R2 : PPC::R0;
551    NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
552                                       ScratchReg), FrameIdx));
553
554    // If the reloaded register isn't CR0, shift the bits right so that they are
555    // in the right CR's slot.
556    if (DestReg != PPC::CR0) {
557      unsigned ShiftBits = getPPCRegisterNumbering(DestReg)*4;
558      // rlwinm r11, r11, 32-ShiftBits, 0, 31.
559      NewMIs.push_back(BuildMI(MF, DL, get(PPC::RLWINM), ScratchReg)
560                    .addReg(ScratchReg).addImm(32-ShiftBits).addImm(0)
561                    .addImm(31));
562    }
563
564    NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTCRF), DestReg)
565                     .addReg(ScratchReg));
566  } else if (PPC::CRBITRCRegisterClass->hasSubClassEq(RC)) {
567
568    unsigned Reg = 0;
569    if (DestReg == PPC::CR0LT || DestReg == PPC::CR0GT ||
570        DestReg == PPC::CR0EQ || DestReg == PPC::CR0UN)
571      Reg = PPC::CR0;
572    else if (DestReg == PPC::CR1LT || DestReg == PPC::CR1GT ||
573             DestReg == PPC::CR1EQ || DestReg == PPC::CR1UN)
574      Reg = PPC::CR1;
575    else if (DestReg == PPC::CR2LT || DestReg == PPC::CR2GT ||
576             DestReg == PPC::CR2EQ || DestReg == PPC::CR2UN)
577      Reg = PPC::CR2;
578    else if (DestReg == PPC::CR3LT || DestReg == PPC::CR3GT ||
579             DestReg == PPC::CR3EQ || DestReg == PPC::CR3UN)
580      Reg = PPC::CR3;
581    else if (DestReg == PPC::CR4LT || DestReg == PPC::CR4GT ||
582             DestReg == PPC::CR4EQ || DestReg == PPC::CR4UN)
583      Reg = PPC::CR4;
584    else if (DestReg == PPC::CR5LT || DestReg == PPC::CR5GT ||
585             DestReg == PPC::CR5EQ || DestReg == PPC::CR5UN)
586      Reg = PPC::CR5;
587    else if (DestReg == PPC::CR6LT || DestReg == PPC::CR6GT ||
588             DestReg == PPC::CR6EQ || DestReg == PPC::CR6UN)
589      Reg = PPC::CR6;
590    else if (DestReg == PPC::CR7LT || DestReg == PPC::CR7GT ||
591             DestReg == PPC::CR7EQ || DestReg == PPC::CR7UN)
592      Reg = PPC::CR7;
593
594    return LoadRegFromStackSlot(MF, DL, Reg, FrameIdx,
595                                PPC::CRRCRegisterClass, NewMIs);
596
597  } else if (PPC::VRRCRegisterClass->hasSubClassEq(RC)) {
598    // We don't have indexed addressing for vector loads.  Emit:
599    // R0 = ADDI FI#
600    // Dest = LVX 0, R0
601    //
602    // FIXME: We use R0 here, because it isn't available for RA.
603    NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::ADDI), PPC::R0),
604                                       FrameIdx, 0, 0));
605    NewMIs.push_back(BuildMI(MF, DL, get(PPC::LVX),DestReg).addReg(PPC::R0)
606                     .addReg(PPC::R0));
607  } else {
608    llvm_unreachable("Unknown regclass!");
609  }
610}
611
612void
613PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
614                                   MachineBasicBlock::iterator MI,
615                                   unsigned DestReg, int FrameIdx,
616                                   const TargetRegisterClass *RC,
617                                   const TargetRegisterInfo *TRI) const {
618  MachineFunction &MF = *MBB.getParent();
619  SmallVector<MachineInstr*, 4> NewMIs;
620  DebugLoc DL;
621  if (MI != MBB.end()) DL = MI->getDebugLoc();
622  LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs);
623  for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
624    MBB.insert(MI, NewMIs[i]);
625
626  const MachineFrameInfo &MFI = *MF.getFrameInfo();
627  MachineMemOperand *MMO =
628    MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
629                            MachineMemOperand::MOLoad,
630                            MFI.getObjectSize(FrameIdx),
631                            MFI.getObjectAlignment(FrameIdx));
632  NewMIs.back()->addMemOperand(MF, MMO);
633}
634
635MachineInstr*
636PPCInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF,
637                                       int FrameIx, uint64_t Offset,
638                                       const MDNode *MDPtr,
639                                       DebugLoc DL) const {
640  MachineInstrBuilder MIB = BuildMI(MF, DL, get(PPC::DBG_VALUE));
641  addFrameReference(MIB, FrameIx, 0, false).addImm(Offset).addMetadata(MDPtr);
642  return &*MIB;
643}
644
645bool PPCInstrInfo::
646ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
647  assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
648  // Leave the CR# the same, but invert the condition.
649  Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
650  return false;
651}
652
653/// GetInstSize - Return the number of bytes of code the specified
654/// instruction may be.  This returns the maximum number of bytes.
655///
656unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
657  switch (MI->getOpcode()) {
658  case PPC::INLINEASM: {       // Inline Asm: Variable size.
659    const MachineFunction *MF = MI->getParent()->getParent();
660    const char *AsmStr = MI->getOperand(0).getSymbolName();
661    return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
662  }
663  case PPC::PROLOG_LABEL:
664  case PPC::EH_LABEL:
665  case PPC::GC_LABEL:
666  case PPC::DBG_VALUE:
667    return 0;
668  default:
669    return 4; // PowerPC instructions are all 4 bytes
670  }
671}
672