Enable early if conversion on PPC
[oota-llvm.git] / lib / Target / PowerPC / PPCInstrInfo.cpp
1 //===-- PPCInstrInfo.cpp - PowerPC Instruction Information ----------------===//
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 "MCTargetDesc/PPCPredicates.h"
16 #include "PPC.h"
17 #include "PPCHazardRecognizers.h"
18 #include "PPCInstrBuilder.h"
19 #include "PPCMachineFunctionInfo.h"
20 #include "PPCTargetMachine.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/CodeGen/MachineFrameInfo.h"
23 #include "llvm/CodeGen/MachineInstrBuilder.h"
24 #include "llvm/CodeGen/MachineMemOperand.h"
25 #include "llvm/CodeGen/MachineRegisterInfo.h"
26 #include "llvm/CodeGen/PseudoSourceValue.h"
27 #include "llvm/MC/MCAsmInfo.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/ErrorHandling.h"
30 #include "llvm/Support/TargetRegistry.h"
31 #include "llvm/Support/raw_ostream.h"
32
33 #define GET_INSTRINFO_CTOR
34 #include "PPCGenInstrInfo.inc"
35
36 using namespace llvm;
37
38 static cl::
39 opt<bool> DisableCTRLoopAnal("disable-ppc-ctrloop-analysis", cl::Hidden,
40             cl::desc("Disable analysis for CTR loops"));
41
42 PPCInstrInfo::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.
48 ScheduleHazardRecognizer *PPCInstrInfo::CreateTargetHazardRecognizer(
49   const TargetMachine *TM,
50   const ScheduleDAG *DAG) const {
51   unsigned Directive = TM->getSubtarget<PPCSubtarget>().getDarwinDirective();
52   if (Directive == PPC::DIR_440 || Directive == PPC::DIR_A2 ||
53       Directive == PPC::DIR_E500mc || Directive == PPC::DIR_E5500) {
54     const InstrItineraryData *II = TM->getInstrItineraryData();
55     return new PPCScoreboardHazardRecognizer(II, DAG);
56   }
57
58   return TargetInstrInfo::CreateTargetHazardRecognizer(TM, DAG);
59 }
60
61 /// CreateTargetPostRAHazardRecognizer - Return the postRA hazard recognizer
62 /// to use for this target when scheduling the DAG.
63 ScheduleHazardRecognizer *PPCInstrInfo::CreateTargetPostRAHazardRecognizer(
64   const InstrItineraryData *II,
65   const ScheduleDAG *DAG) const {
66   unsigned Directive = TM.getSubtarget<PPCSubtarget>().getDarwinDirective();
67
68   // Most subtargets use a PPC970 recognizer.
69   if (Directive != PPC::DIR_440 && Directive != PPC::DIR_A2 &&
70       Directive != PPC::DIR_E500mc && Directive != PPC::DIR_E5500) {
71     const TargetInstrInfo *TII = TM.getInstrInfo();
72     assert(TII && "No InstrInfo?");
73
74     return new PPCHazardRecognizer970(*TII);
75   }
76
77   return new PPCScoreboardHazardRecognizer(II, DAG);
78 }
79
80 // Detect 32 -> 64-bit extensions where we may reuse the low sub-register.
81 bool PPCInstrInfo::isCoalescableExtInstr(const MachineInstr &MI,
82                                          unsigned &SrcReg, unsigned &DstReg,
83                                          unsigned &SubIdx) const {
84   switch (MI.getOpcode()) {
85   default: return false;
86   case PPC::EXTSW:
87   case PPC::EXTSW_32_64:
88     SrcReg = MI.getOperand(1).getReg();
89     DstReg = MI.getOperand(0).getReg();
90     SubIdx = PPC::sub_32;
91     return true;
92   }
93 }
94
95 unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
96                                            int &FrameIndex) const {
97   // Note: This list must be kept consistent with LoadRegFromStackSlot.
98   switch (MI->getOpcode()) {
99   default: break;
100   case PPC::LD:
101   case PPC::LWZ:
102   case PPC::LFS:
103   case PPC::LFD:
104   case PPC::RESTORE_CR:
105   case PPC::LVX:
106   case PPC::RESTORE_VRSAVE:
107     // Check for the operands added by addFrameReference (the immediate is the
108     // offset which defaults to 0).
109     if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
110         MI->getOperand(2).isFI()) {
111       FrameIndex = MI->getOperand(2).getIndex();
112       return MI->getOperand(0).getReg();
113     }
114     break;
115   }
116   return 0;
117 }
118
119 unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
120                                           int &FrameIndex) const {
121   // Note: This list must be kept consistent with StoreRegToStackSlot.
122   switch (MI->getOpcode()) {
123   default: break;
124   case PPC::STD:
125   case PPC::STW:
126   case PPC::STFS:
127   case PPC::STFD:
128   case PPC::SPILL_CR:
129   case PPC::STVX:
130   case PPC::SPILL_VRSAVE:
131     // Check for the operands added by addFrameReference (the immediate is the
132     // offset which defaults to 0).
133     if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
134         MI->getOperand(2).isFI()) {
135       FrameIndex = MI->getOperand(2).getIndex();
136       return MI->getOperand(0).getReg();
137     }
138     break;
139   }
140   return 0;
141 }
142
143 // commuteInstruction - We can commute rlwimi instructions, but only if the
144 // rotate amt is zero.  We also have to munge the immediates a bit.
145 MachineInstr *
146 PPCInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
147   MachineFunction &MF = *MI->getParent()->getParent();
148
149   // Normal instructions can be commuted the obvious way.
150   if (MI->getOpcode() != PPC::RLWIMI)
151     return TargetInstrInfo::commuteInstruction(MI, NewMI);
152
153   // Cannot commute if it has a non-zero rotate count.
154   if (MI->getOperand(3).getImm() != 0)
155     return 0;
156
157   // If we have a zero rotate count, we have:
158   //   M = mask(MB,ME)
159   //   Op0 = (Op1 & ~M) | (Op2 & M)
160   // Change this to:
161   //   M = mask((ME+1)&31, (MB-1)&31)
162   //   Op0 = (Op2 & ~M) | (Op1 & M)
163
164   // Swap op1/op2
165   unsigned Reg0 = MI->getOperand(0).getReg();
166   unsigned Reg1 = MI->getOperand(1).getReg();
167   unsigned Reg2 = MI->getOperand(2).getReg();
168   bool Reg1IsKill = MI->getOperand(1).isKill();
169   bool Reg2IsKill = MI->getOperand(2).isKill();
170   bool ChangeReg0 = false;
171   // If machine instrs are no longer in two-address forms, update
172   // destination register as well.
173   if (Reg0 == Reg1) {
174     // Must be two address instruction!
175     assert(MI->getDesc().getOperandConstraint(0, MCOI::TIED_TO) &&
176            "Expecting a two-address instruction!");
177     Reg2IsKill = false;
178     ChangeReg0 = true;
179   }
180
181   // Masks.
182   unsigned MB = MI->getOperand(4).getImm();
183   unsigned ME = MI->getOperand(5).getImm();
184
185   if (NewMI) {
186     // Create a new instruction.
187     unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg();
188     bool Reg0IsDead = MI->getOperand(0).isDead();
189     return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
190       .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
191       .addReg(Reg2, getKillRegState(Reg2IsKill))
192       .addReg(Reg1, getKillRegState(Reg1IsKill))
193       .addImm((ME+1) & 31)
194       .addImm((MB-1) & 31);
195   }
196
197   if (ChangeReg0)
198     MI->getOperand(0).setReg(Reg2);
199   MI->getOperand(2).setReg(Reg1);
200   MI->getOperand(1).setReg(Reg2);
201   MI->getOperand(2).setIsKill(Reg1IsKill);
202   MI->getOperand(1).setIsKill(Reg2IsKill);
203
204   // Swap the mask around.
205   MI->getOperand(4).setImm((ME+1) & 31);
206   MI->getOperand(5).setImm((MB-1) & 31);
207   return MI;
208 }
209
210 void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
211                               MachineBasicBlock::iterator MI) const {
212   DebugLoc DL;
213   BuildMI(MBB, MI, DL, get(PPC::NOP));
214 }
215
216
217 // Branch analysis.
218 // Note: If the condition register is set to CTR or CTR8 then this is a
219 // BDNZ (imm == 1) or BDZ (imm == 0) branch.
220 bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
221                                  MachineBasicBlock *&FBB,
222                                  SmallVectorImpl<MachineOperand> &Cond,
223                                  bool AllowModify) const {
224   bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
225
226   // If the block has no terminators, it just falls into the block after it.
227   MachineBasicBlock::iterator I = MBB.end();
228   if (I == MBB.begin())
229     return false;
230   --I;
231   while (I->isDebugValue()) {
232     if (I == MBB.begin())
233       return false;
234     --I;
235   }
236   if (!isUnpredicatedTerminator(I))
237     return false;
238
239   // Get the last instruction in the block.
240   MachineInstr *LastInst = I;
241
242   // If there is only one terminator instruction, process it.
243   if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
244     if (LastInst->getOpcode() == PPC::B) {
245       if (!LastInst->getOperand(0).isMBB())
246         return true;
247       TBB = LastInst->getOperand(0).getMBB();
248       return false;
249     } else if (LastInst->getOpcode() == PPC::BCC) {
250       if (!LastInst->getOperand(2).isMBB())
251         return true;
252       // Block ends with fall-through condbranch.
253       TBB = LastInst->getOperand(2).getMBB();
254       Cond.push_back(LastInst->getOperand(0));
255       Cond.push_back(LastInst->getOperand(1));
256       return false;
257     } else if (LastInst->getOpcode() == PPC::BDNZ8 ||
258                LastInst->getOpcode() == PPC::BDNZ) {
259       if (!LastInst->getOperand(0).isMBB())
260         return true;
261       if (DisableCTRLoopAnal)
262         return true;
263       TBB = LastInst->getOperand(0).getMBB();
264       Cond.push_back(MachineOperand::CreateImm(1));
265       Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
266                                                true));
267       return false;
268     } else if (LastInst->getOpcode() == PPC::BDZ8 ||
269                LastInst->getOpcode() == PPC::BDZ) {
270       if (!LastInst->getOperand(0).isMBB())
271         return true;
272       if (DisableCTRLoopAnal)
273         return true;
274       TBB = LastInst->getOperand(0).getMBB();
275       Cond.push_back(MachineOperand::CreateImm(0));
276       Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
277                                                true));
278       return false;
279     }
280
281     // Otherwise, don't know what this is.
282     return true;
283   }
284
285   // Get the instruction before it if it's a terminator.
286   MachineInstr *SecondLastInst = I;
287
288   // If there are three terminators, we don't know what sort of block this is.
289   if (SecondLastInst && I != MBB.begin() &&
290       isUnpredicatedTerminator(--I))
291     return true;
292
293   // If the block ends with PPC::B and PPC:BCC, handle it.
294   if (SecondLastInst->getOpcode() == PPC::BCC &&
295       LastInst->getOpcode() == PPC::B) {
296     if (!SecondLastInst->getOperand(2).isMBB() ||
297         !LastInst->getOperand(0).isMBB())
298       return true;
299     TBB =  SecondLastInst->getOperand(2).getMBB();
300     Cond.push_back(SecondLastInst->getOperand(0));
301     Cond.push_back(SecondLastInst->getOperand(1));
302     FBB = LastInst->getOperand(0).getMBB();
303     return false;
304   } else if ((SecondLastInst->getOpcode() == PPC::BDNZ8 ||
305               SecondLastInst->getOpcode() == PPC::BDNZ) &&
306       LastInst->getOpcode() == PPC::B) {
307     if (!SecondLastInst->getOperand(0).isMBB() ||
308         !LastInst->getOperand(0).isMBB())
309       return true;
310     if (DisableCTRLoopAnal)
311       return true;
312     TBB = SecondLastInst->getOperand(0).getMBB();
313     Cond.push_back(MachineOperand::CreateImm(1));
314     Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
315                                              true));
316     FBB = LastInst->getOperand(0).getMBB();
317     return false;
318   } else if ((SecondLastInst->getOpcode() == PPC::BDZ8 ||
319               SecondLastInst->getOpcode() == PPC::BDZ) &&
320       LastInst->getOpcode() == PPC::B) {
321     if (!SecondLastInst->getOperand(0).isMBB() ||
322         !LastInst->getOperand(0).isMBB())
323       return true;
324     if (DisableCTRLoopAnal)
325       return true;
326     TBB = SecondLastInst->getOperand(0).getMBB();
327     Cond.push_back(MachineOperand::CreateImm(0));
328     Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
329                                              true));
330     FBB = LastInst->getOperand(0).getMBB();
331     return false;
332   }
333
334   // If the block ends with two PPC:Bs, handle it.  The second one is not
335   // executed, so remove it.
336   if (SecondLastInst->getOpcode() == PPC::B &&
337       LastInst->getOpcode() == PPC::B) {
338     if (!SecondLastInst->getOperand(0).isMBB())
339       return true;
340     TBB = SecondLastInst->getOperand(0).getMBB();
341     I = LastInst;
342     if (AllowModify)
343       I->eraseFromParent();
344     return false;
345   }
346
347   // Otherwise, can't handle this.
348   return true;
349 }
350
351 unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
352   MachineBasicBlock::iterator I = MBB.end();
353   if (I == MBB.begin()) return 0;
354   --I;
355   while (I->isDebugValue()) {
356     if (I == MBB.begin())
357       return 0;
358     --I;
359   }
360   if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC &&
361       I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
362       I->getOpcode() != PPC::BDZ8  && I->getOpcode() != PPC::BDZ)
363     return 0;
364
365   // Remove the branch.
366   I->eraseFromParent();
367
368   I = MBB.end();
369
370   if (I == MBB.begin()) return 1;
371   --I;
372   if (I->getOpcode() != PPC::BCC &&
373       I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
374       I->getOpcode() != PPC::BDZ8  && I->getOpcode() != PPC::BDZ)
375     return 1;
376
377   // Remove the branch.
378   I->eraseFromParent();
379   return 2;
380 }
381
382 unsigned
383 PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
384                            MachineBasicBlock *FBB,
385                            const SmallVectorImpl<MachineOperand> &Cond,
386                            DebugLoc DL) const {
387   // Shouldn't be a fall through.
388   assert(TBB && "InsertBranch must not be told to insert a fallthrough");
389   assert((Cond.size() == 2 || Cond.size() == 0) &&
390          "PPC branch conditions have two components!");
391
392   bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
393
394   // One-way branch.
395   if (FBB == 0) {
396     if (Cond.empty())   // Unconditional branch
397       BuildMI(&MBB, DL, get(PPC::B)).addMBB(TBB);
398     else if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
399       BuildMI(&MBB, DL, get(Cond[0].getImm() ?
400                               (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
401                               (isPPC64 ? PPC::BDZ8  : PPC::BDZ))).addMBB(TBB);
402     else                // Conditional branch
403       BuildMI(&MBB, DL, get(PPC::BCC))
404         .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
405     return 1;
406   }
407
408   // Two-way Conditional Branch.
409   if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
410     BuildMI(&MBB, DL, get(Cond[0].getImm() ?
411                             (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
412                             (isPPC64 ? PPC::BDZ8  : PPC::BDZ))).addMBB(TBB);
413   else
414     BuildMI(&MBB, DL, get(PPC::BCC))
415       .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
416   BuildMI(&MBB, DL, get(PPC::B)).addMBB(FBB);
417   return 2;
418 }
419
420 // Select analysis.
421 bool PPCInstrInfo::canInsertSelect(const MachineBasicBlock &MBB,
422                 const SmallVectorImpl<MachineOperand> &Cond,
423                 unsigned TrueReg, unsigned FalseReg,
424                 int &CondCycles, int &TrueCycles, int &FalseCycles) const {
425   if (!TM.getSubtargetImpl()->hasISEL())
426     return false;
427
428   if (Cond.size() != 2)
429     return false;
430
431   // If this is really a bdnz-like condition, then it cannot be turned into a
432   // select.
433   if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
434     return false;
435
436   // Check register classes.
437   const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
438   const TargetRegisterClass *RC =
439     RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
440   if (!RC)
441     return false;
442
443   // isel is for regular integer GPRs only.
444   if (!PPC::GPRCRegClass.hasSubClassEq(RC) &&
445       !PPC::G8RCRegClass.hasSubClassEq(RC))
446     return false;
447
448   // FIXME: These numbers are for the A2, how well they work for other cores is
449   // an open question. On the A2, the isel instruction has a 2-cycle latency
450   // but single-cycle throughput. These numbers are used in combination with
451   // the MispredictPenalty setting from the active SchedMachineModel.
452   CondCycles = 1;
453   TrueCycles = 1;
454   FalseCycles = 1;
455
456   return true;
457 }
458
459 void PPCInstrInfo::insertSelect(MachineBasicBlock &MBB,
460                                 MachineBasicBlock::iterator MI, DebugLoc dl,
461                                 unsigned DestReg,
462                                 const SmallVectorImpl<MachineOperand> &Cond,
463                                 unsigned TrueReg, unsigned FalseReg) const {
464   assert(Cond.size() == 2 &&
465          "PPC branch conditions have two components!");
466
467   assert(TM.getSubtargetImpl()->hasISEL() &&
468          "Cannot insert select on target without ISEL support");
469
470   // Get the register classes.
471   MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
472   const TargetRegisterClass *RC =
473     RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
474   assert(RC && "TrueReg and FalseReg must have overlapping register classes");
475   assert((PPC::GPRCRegClass.hasSubClassEq(RC) ||
476           PPC::G8RCRegClass.hasSubClassEq(RC)) &&
477          "isel is for regular integer GPRs only");
478
479   unsigned OpCode =
480     PPC::GPRCRegClass.hasSubClassEq(RC) ? PPC::ISEL : PPC::ISEL8;
481   unsigned SelectPred = Cond[0].getImm();
482
483   unsigned SubIdx;
484   bool SwapOps;
485   switch (SelectPred) {
486   default: llvm_unreachable("invalid predicate for isel");
487   case PPC::PRED_EQ: SubIdx = PPC::sub_eq; SwapOps = false; break;
488   case PPC::PRED_NE: SubIdx = PPC::sub_eq; SwapOps = true; break;
489   case PPC::PRED_LT: SubIdx = PPC::sub_lt; SwapOps = false; break;
490   case PPC::PRED_GE: SubIdx = PPC::sub_lt; SwapOps = true; break;
491   case PPC::PRED_GT: SubIdx = PPC::sub_gt; SwapOps = false; break;
492   case PPC::PRED_LE: SubIdx = PPC::sub_gt; SwapOps = true; break;
493   case PPC::PRED_UN: SubIdx = PPC::sub_un; SwapOps = false; break;
494   case PPC::PRED_NU: SubIdx = PPC::sub_un; SwapOps = true; break;
495   }
496
497   unsigned FirstReg =  SwapOps ? FalseReg : TrueReg,
498            SecondReg = SwapOps ? TrueReg  : FalseReg;
499
500   // The first input register of isel cannot be r0. If it is a member
501   // of a register class that can be r0, then copy it first (the
502   // register allocator should eliminate the copy).
503   if (MRI.getRegClass(FirstReg)->contains(PPC::R0) ||
504       MRI.getRegClass(FirstReg)->contains(PPC::X0)) {
505     const TargetRegisterClass *FirstRC =
506       MRI.getRegClass(FirstReg)->contains(PPC::X0) ?
507         &PPC::G8RC_NOX0RegClass : &PPC::GPRC_NOR0RegClass;
508     unsigned OldFirstReg = FirstReg;
509     FirstReg = MRI.createVirtualRegister(FirstRC);
510     BuildMI(MBB, MI, dl, get(TargetOpcode::COPY), FirstReg)
511       .addReg(OldFirstReg);
512   }
513
514   BuildMI(MBB, MI, dl, get(OpCode), DestReg)
515     .addReg(FirstReg).addReg(SecondReg)
516     .addReg(Cond[1].getReg(), 0, SubIdx);
517 }
518
519 void PPCInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
520                                MachineBasicBlock::iterator I, DebugLoc DL,
521                                unsigned DestReg, unsigned SrcReg,
522                                bool KillSrc) const {
523   unsigned Opc;
524   if (PPC::GPRCRegClass.contains(DestReg, SrcReg))
525     Opc = PPC::OR;
526   else if (PPC::G8RCRegClass.contains(DestReg, SrcReg))
527     Opc = PPC::OR8;
528   else if (PPC::F4RCRegClass.contains(DestReg, SrcReg))
529     Opc = PPC::FMR;
530   else if (PPC::CRRCRegClass.contains(DestReg, SrcReg))
531     Opc = PPC::MCRF;
532   else if (PPC::VRRCRegClass.contains(DestReg, SrcReg))
533     Opc = PPC::VOR;
534   else if (PPC::CRBITRCRegClass.contains(DestReg, SrcReg))
535     Opc = PPC::CROR;
536   else
537     llvm_unreachable("Impossible reg-to-reg copy");
538
539   const MCInstrDesc &MCID = get(Opc);
540   if (MCID.getNumOperands() == 3)
541     BuildMI(MBB, I, DL, MCID, DestReg)
542       .addReg(SrcReg).addReg(SrcReg, getKillRegState(KillSrc));
543   else
544     BuildMI(MBB, I, DL, MCID, DestReg).addReg(SrcReg, getKillRegState(KillSrc));
545 }
546
547 // This function returns true if a CR spill is necessary and false otherwise.
548 bool
549 PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
550                                   unsigned SrcReg, bool isKill,
551                                   int FrameIdx,
552                                   const TargetRegisterClass *RC,
553                                   SmallVectorImpl<MachineInstr*> &NewMIs,
554                                   bool &NonRI, bool &SpillsVRS) const{
555   // Note: If additional store instructions are added here,
556   // update isStoreToStackSlot.
557
558   DebugLoc DL;
559   if (PPC::GPRCRegClass.hasSubClassEq(RC)) {
560     NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
561                                        .addReg(SrcReg,
562                                                getKillRegState(isKill)),
563                                        FrameIdx));
564   } else if (PPC::G8RCRegClass.hasSubClassEq(RC)) {
565     NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
566                                        .addReg(SrcReg,
567                                                getKillRegState(isKill)),
568                                        FrameIdx));
569   } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
570     NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD))
571                                        .addReg(SrcReg,
572                                                getKillRegState(isKill)),
573                                        FrameIdx));
574   } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
575     NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS))
576                                        .addReg(SrcReg,
577                                                getKillRegState(isKill)),
578                                        FrameIdx));
579   } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
580     NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR))
581                                        .addReg(SrcReg,
582                                                getKillRegState(isKill)),
583                                        FrameIdx));
584     return true;
585   } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
586     // FIXME: We use CRi here because there is no mtcrf on a bit. Since the
587     // backend currently only uses CR1EQ as an individual bit, this should
588     // not cause any bug. If we need other uses of CR bits, the following
589     // code may be invalid.
590     unsigned Reg = 0;
591     if (SrcReg == PPC::CR0LT || SrcReg == PPC::CR0GT ||
592         SrcReg == PPC::CR0EQ || SrcReg == PPC::CR0UN)
593       Reg = PPC::CR0;
594     else if (SrcReg == PPC::CR1LT || SrcReg == PPC::CR1GT ||
595              SrcReg == PPC::CR1EQ || SrcReg == PPC::CR1UN)
596       Reg = PPC::CR1;
597     else if (SrcReg == PPC::CR2LT || SrcReg == PPC::CR2GT ||
598              SrcReg == PPC::CR2EQ || SrcReg == PPC::CR2UN)
599       Reg = PPC::CR2;
600     else if (SrcReg == PPC::CR3LT || SrcReg == PPC::CR3GT ||
601              SrcReg == PPC::CR3EQ || SrcReg == PPC::CR3UN)
602       Reg = PPC::CR3;
603     else if (SrcReg == PPC::CR4LT || SrcReg == PPC::CR4GT ||
604              SrcReg == PPC::CR4EQ || SrcReg == PPC::CR4UN)
605       Reg = PPC::CR4;
606     else if (SrcReg == PPC::CR5LT || SrcReg == PPC::CR5GT ||
607              SrcReg == PPC::CR5EQ || SrcReg == PPC::CR5UN)
608       Reg = PPC::CR5;
609     else if (SrcReg == PPC::CR6LT || SrcReg == PPC::CR6GT ||
610              SrcReg == PPC::CR6EQ || SrcReg == PPC::CR6UN)
611       Reg = PPC::CR6;
612     else if (SrcReg == PPC::CR7LT || SrcReg == PPC::CR7GT ||
613              SrcReg == PPC::CR7EQ || SrcReg == PPC::CR7UN)
614       Reg = PPC::CR7;
615
616     return StoreRegToStackSlot(MF, Reg, isKill, FrameIdx,
617                                &PPC::CRRCRegClass, NewMIs, NonRI, SpillsVRS);
618
619   } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
620     NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STVX))
621                                        .addReg(SrcReg,
622                                                getKillRegState(isKill)),
623                                        FrameIdx));
624     NonRI = true;
625   } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
626     assert(TM.getSubtargetImpl()->isDarwin() &&
627            "VRSAVE only needs spill/restore on Darwin");
628     NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_VRSAVE))
629                                        .addReg(SrcReg,
630                                                getKillRegState(isKill)),
631                                        FrameIdx));
632     SpillsVRS = true;
633   } else {
634     llvm_unreachable("Unknown regclass!");
635   }
636
637   return false;
638 }
639
640 void
641 PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
642                                   MachineBasicBlock::iterator MI,
643                                   unsigned SrcReg, bool isKill, int FrameIdx,
644                                   const TargetRegisterClass *RC,
645                                   const TargetRegisterInfo *TRI) const {
646   MachineFunction &MF = *MBB.getParent();
647   SmallVector<MachineInstr*, 4> NewMIs;
648
649   PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
650   FuncInfo->setHasSpills();
651
652   bool NonRI = false, SpillsVRS = false;
653   if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs,
654                           NonRI, SpillsVRS))
655     FuncInfo->setSpillsCR();
656
657   if (SpillsVRS)
658     FuncInfo->setSpillsVRSAVE();
659
660   if (NonRI)
661     FuncInfo->setHasNonRISpills();
662
663   for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
664     MBB.insert(MI, NewMIs[i]);
665
666   const MachineFrameInfo &MFI = *MF.getFrameInfo();
667   MachineMemOperand *MMO =
668     MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
669                             MachineMemOperand::MOStore,
670                             MFI.getObjectSize(FrameIdx),
671                             MFI.getObjectAlignment(FrameIdx));
672   NewMIs.back()->addMemOperand(MF, MMO);
673 }
674
675 bool
676 PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL,
677                                    unsigned DestReg, int FrameIdx,
678                                    const TargetRegisterClass *RC,
679                                    SmallVectorImpl<MachineInstr*> &NewMIs,
680                                    bool &NonRI, bool &SpillsVRS) const{
681   // Note: If additional load instructions are added here,
682   // update isLoadFromStackSlot.
683
684   if (PPC::GPRCRegClass.hasSubClassEq(RC)) {
685     NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
686                                                DestReg), FrameIdx));
687   } else if (PPC::G8RCRegClass.hasSubClassEq(RC)) {
688     NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg),
689                                        FrameIdx));
690   } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
691     NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg),
692                                        FrameIdx));
693   } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
694     NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg),
695                                        FrameIdx));
696   } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
697     NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
698                                                get(PPC::RESTORE_CR), DestReg),
699                                        FrameIdx));
700     return true;
701   } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
702
703     unsigned Reg = 0;
704     if (DestReg == PPC::CR0LT || DestReg == PPC::CR0GT ||
705         DestReg == PPC::CR0EQ || DestReg == PPC::CR0UN)
706       Reg = PPC::CR0;
707     else if (DestReg == PPC::CR1LT || DestReg == PPC::CR1GT ||
708              DestReg == PPC::CR1EQ || DestReg == PPC::CR1UN)
709       Reg = PPC::CR1;
710     else if (DestReg == PPC::CR2LT || DestReg == PPC::CR2GT ||
711              DestReg == PPC::CR2EQ || DestReg == PPC::CR2UN)
712       Reg = PPC::CR2;
713     else if (DestReg == PPC::CR3LT || DestReg == PPC::CR3GT ||
714              DestReg == PPC::CR3EQ || DestReg == PPC::CR3UN)
715       Reg = PPC::CR3;
716     else if (DestReg == PPC::CR4LT || DestReg == PPC::CR4GT ||
717              DestReg == PPC::CR4EQ || DestReg == PPC::CR4UN)
718       Reg = PPC::CR4;
719     else if (DestReg == PPC::CR5LT || DestReg == PPC::CR5GT ||
720              DestReg == PPC::CR5EQ || DestReg == PPC::CR5UN)
721       Reg = PPC::CR5;
722     else if (DestReg == PPC::CR6LT || DestReg == PPC::CR6GT ||
723              DestReg == PPC::CR6EQ || DestReg == PPC::CR6UN)
724       Reg = PPC::CR6;
725     else if (DestReg == PPC::CR7LT || DestReg == PPC::CR7GT ||
726              DestReg == PPC::CR7EQ || DestReg == PPC::CR7UN)
727       Reg = PPC::CR7;
728
729     return LoadRegFromStackSlot(MF, DL, Reg, FrameIdx,
730                                 &PPC::CRRCRegClass, NewMIs, NonRI, SpillsVRS);
731
732   } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
733     NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LVX), DestReg),
734                                        FrameIdx));
735     NonRI = true;
736   } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
737     assert(TM.getSubtargetImpl()->isDarwin() &&
738            "VRSAVE only needs spill/restore on Darwin");
739     NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
740                                                get(PPC::RESTORE_VRSAVE),
741                                                DestReg),
742                                        FrameIdx));
743     SpillsVRS = true;
744   } else {
745     llvm_unreachable("Unknown regclass!");
746   }
747
748   return false;
749 }
750
751 void
752 PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
753                                    MachineBasicBlock::iterator MI,
754                                    unsigned DestReg, int FrameIdx,
755                                    const TargetRegisterClass *RC,
756                                    const TargetRegisterInfo *TRI) const {
757   MachineFunction &MF = *MBB.getParent();
758   SmallVector<MachineInstr*, 4> NewMIs;
759   DebugLoc DL;
760   if (MI != MBB.end()) DL = MI->getDebugLoc();
761
762   PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
763   FuncInfo->setHasSpills();
764
765   bool NonRI = false, SpillsVRS = false;
766   if (LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs,
767                            NonRI, SpillsVRS))
768     FuncInfo->setSpillsCR();
769
770   if (SpillsVRS)
771     FuncInfo->setSpillsVRSAVE();
772
773   if (NonRI)
774     FuncInfo->setHasNonRISpills();
775
776   for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
777     MBB.insert(MI, NewMIs[i]);
778
779   const MachineFrameInfo &MFI = *MF.getFrameInfo();
780   MachineMemOperand *MMO =
781     MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
782                             MachineMemOperand::MOLoad,
783                             MFI.getObjectSize(FrameIdx),
784                             MFI.getObjectAlignment(FrameIdx));
785   NewMIs.back()->addMemOperand(MF, MMO);
786 }
787
788 MachineInstr*
789 PPCInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF,
790                                        int FrameIx, uint64_t Offset,
791                                        const MDNode *MDPtr,
792                                        DebugLoc DL) const {
793   MachineInstrBuilder MIB = BuildMI(MF, DL, get(PPC::DBG_VALUE));
794   addFrameReference(MIB, FrameIx, 0, false).addImm(Offset).addMetadata(MDPtr);
795   return &*MIB;
796 }
797
798 bool PPCInstrInfo::
799 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
800   assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
801   if (Cond[1].getReg() == PPC::CTR8 || Cond[1].getReg() == PPC::CTR)
802     Cond[0].setImm(Cond[0].getImm() == 0 ? 1 : 0);
803   else
804     // Leave the CR# the same, but invert the condition.
805     Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
806   return false;
807 }
808
809 /// GetInstSize - Return the number of bytes of code the specified
810 /// instruction may be.  This returns the maximum number of bytes.
811 ///
812 unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
813   switch (MI->getOpcode()) {
814   case PPC::INLINEASM: {       // Inline Asm: Variable size.
815     const MachineFunction *MF = MI->getParent()->getParent();
816     const char *AsmStr = MI->getOperand(0).getSymbolName();
817     return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
818   }
819   case PPC::PROLOG_LABEL:
820   case PPC::EH_LABEL:
821   case PPC::GC_LABEL:
822   case PPC::DBG_VALUE:
823     return 0;
824   case PPC::BL8_NOP:
825   case PPC::BLA8_NOP:
826     return 8;
827   default:
828     return 4; // PowerPC instructions are all 4 bytes
829   }
830 }