1 //===-- PPCInstrInfo.cpp - PowerPC Instruction Information ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the PowerPC implementation of the TargetInstrInfo class.
12 //===----------------------------------------------------------------------===//
14 #include "PPCInstrInfo.h"
15 #include "MCTargetDesc/PPCPredicates.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"
33 #define GET_INSTRINFO_CTOR
34 #include "PPCGenInstrInfo.inc"
39 opt<bool> DisableCTRLoopAnal("disable-ppc-ctrloop-analysis", cl::Hidden,
40 cl::desc("Disable analysis for CTR loops"));
42 PPCInstrInfo::PPCInstrInfo(PPCTargetMachine &tm)
43 : PPCGenInstrInfo(PPC::ADJCALLSTACKDOWN, PPC::ADJCALLSTACKUP),
44 TM(tm), RI(*TM.getSubtargetImpl(), *this) {}
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);
58 return TargetInstrInfo::CreateTargetHazardRecognizer(TM, DAG);
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();
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?");
74 return new PPCHazardRecognizer970(*TII);
77 return new PPCScoreboardHazardRecognizer(II, DAG);
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;
87 case PPC::EXTSW_32_64:
88 SrcReg = MI.getOperand(1).getReg();
89 DstReg = MI.getOperand(0).getReg();
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()) {
104 case PPC::RESTORE_CR:
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();
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()) {
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();
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.
146 PPCInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
147 MachineFunction &MF = *MI->getParent()->getParent();
149 // Normal instructions can be commuted the obvious way.
150 if (MI->getOpcode() != PPC::RLWIMI)
151 return TargetInstrInfo::commuteInstruction(MI, NewMI);
153 // Cannot commute if it has a non-zero rotate count.
154 if (MI->getOperand(3).getImm() != 0)
157 // If we have a zero rotate count, we have:
159 // Op0 = (Op1 & ~M) | (Op2 & M)
161 // M = mask((ME+1)&31, (MB-1)&31)
162 // Op0 = (Op2 & ~M) | (Op1 & M)
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.
174 // Must be two address instruction!
175 assert(MI->getDesc().getOperandConstraint(0, MCOI::TIED_TO) &&
176 "Expecting a two-address instruction!");
182 unsigned MB = MI->getOperand(4).getImm();
183 unsigned ME = MI->getOperand(5).getImm();
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))
194 .addImm((MB-1) & 31);
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);
204 // Swap the mask around.
205 MI->getOperand(4).setImm((ME+1) & 31);
206 MI->getOperand(5).setImm((MB-1) & 31);
210 void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
211 MachineBasicBlock::iterator MI) const {
213 BuildMI(MBB, MI, DL, get(PPC::NOP));
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();
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())
231 while (I->isDebugValue()) {
232 if (I == MBB.begin())
236 if (!isUnpredicatedTerminator(I))
239 // Get the last instruction in the block.
240 MachineInstr *LastInst = I;
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())
247 TBB = LastInst->getOperand(0).getMBB();
249 } else if (LastInst->getOpcode() == PPC::BCC) {
250 if (!LastInst->getOperand(2).isMBB())
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));
257 } else if (LastInst->getOpcode() == PPC::BDNZ8 ||
258 LastInst->getOpcode() == PPC::BDNZ) {
259 if (!LastInst->getOperand(0).isMBB())
261 if (DisableCTRLoopAnal)
263 TBB = LastInst->getOperand(0).getMBB();
264 Cond.push_back(MachineOperand::CreateImm(1));
265 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
268 } else if (LastInst->getOpcode() == PPC::BDZ8 ||
269 LastInst->getOpcode() == PPC::BDZ) {
270 if (!LastInst->getOperand(0).isMBB())
272 if (DisableCTRLoopAnal)
274 TBB = LastInst->getOperand(0).getMBB();
275 Cond.push_back(MachineOperand::CreateImm(0));
276 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
281 // Otherwise, don't know what this is.
285 // Get the instruction before it if it's a terminator.
286 MachineInstr *SecondLastInst = I;
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))
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())
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();
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())
310 if (DisableCTRLoopAnal)
312 TBB = SecondLastInst->getOperand(0).getMBB();
313 Cond.push_back(MachineOperand::CreateImm(1));
314 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
316 FBB = LastInst->getOperand(0).getMBB();
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())
324 if (DisableCTRLoopAnal)
326 TBB = SecondLastInst->getOperand(0).getMBB();
327 Cond.push_back(MachineOperand::CreateImm(0));
328 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
330 FBB = LastInst->getOperand(0).getMBB();
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())
340 TBB = SecondLastInst->getOperand(0).getMBB();
343 I->eraseFromParent();
347 // Otherwise, can't handle this.
351 unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
352 MachineBasicBlock::iterator I = MBB.end();
353 if (I == MBB.begin()) return 0;
355 while (I->isDebugValue()) {
356 if (I == MBB.begin())
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)
365 // Remove the branch.
366 I->eraseFromParent();
370 if (I == MBB.begin()) return 1;
372 if (I->getOpcode() != PPC::BCC &&
373 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
374 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
377 // Remove the branch.
378 I->eraseFromParent();
383 PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
384 MachineBasicBlock *FBB,
385 const SmallVectorImpl<MachineOperand> &Cond,
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!");
392 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
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);
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);
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);
420 void PPCInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
421 MachineBasicBlock::iterator I, DebugLoc DL,
422 unsigned DestReg, unsigned SrcReg,
423 bool KillSrc) const {
425 if (PPC::GPRCRegClass.contains(DestReg, SrcReg))
427 else if (PPC::G8RCRegClass.contains(DestReg, SrcReg))
429 else if (PPC::F4RCRegClass.contains(DestReg, SrcReg))
431 else if (PPC::CRRCRegClass.contains(DestReg, SrcReg))
433 else if (PPC::VRRCRegClass.contains(DestReg, SrcReg))
435 else if (PPC::CRBITRCRegClass.contains(DestReg, SrcReg))
438 llvm_unreachable("Impossible reg-to-reg copy");
440 const MCInstrDesc &MCID = get(Opc);
441 if (MCID.getNumOperands() == 3)
442 BuildMI(MBB, I, DL, MCID, DestReg)
443 .addReg(SrcReg).addReg(SrcReg, getKillRegState(KillSrc));
445 BuildMI(MBB, I, DL, MCID, DestReg).addReg(SrcReg, getKillRegState(KillSrc));
448 // This function returns true if a CR spill is necessary and false otherwise.
450 PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
451 unsigned SrcReg, bool isKill,
453 const TargetRegisterClass *RC,
454 SmallVectorImpl<MachineInstr*> &NewMIs,
455 bool &NonRI, bool &SpillsVRS) const{
456 // Note: If additional store instructions are added here,
457 // update isStoreToStackSlot.
460 if (PPC::GPRCRegClass.hasSubClassEq(RC)) {
461 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
463 getKillRegState(isKill)),
465 } else if (PPC::G8RCRegClass.hasSubClassEq(RC)) {
466 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
468 getKillRegState(isKill)),
470 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
471 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD))
473 getKillRegState(isKill)),
475 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
476 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS))
478 getKillRegState(isKill)),
480 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
481 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR))
483 getKillRegState(isKill)),
486 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
487 // FIXME: We use CRi here because there is no mtcrf on a bit. Since the
488 // backend currently only uses CR1EQ as an individual bit, this should
489 // not cause any bug. If we need other uses of CR bits, the following
490 // code may be invalid.
492 if (SrcReg == PPC::CR0LT || SrcReg == PPC::CR0GT ||
493 SrcReg == PPC::CR0EQ || SrcReg == PPC::CR0UN)
495 else if (SrcReg == PPC::CR1LT || SrcReg == PPC::CR1GT ||
496 SrcReg == PPC::CR1EQ || SrcReg == PPC::CR1UN)
498 else if (SrcReg == PPC::CR2LT || SrcReg == PPC::CR2GT ||
499 SrcReg == PPC::CR2EQ || SrcReg == PPC::CR2UN)
501 else if (SrcReg == PPC::CR3LT || SrcReg == PPC::CR3GT ||
502 SrcReg == PPC::CR3EQ || SrcReg == PPC::CR3UN)
504 else if (SrcReg == PPC::CR4LT || SrcReg == PPC::CR4GT ||
505 SrcReg == PPC::CR4EQ || SrcReg == PPC::CR4UN)
507 else if (SrcReg == PPC::CR5LT || SrcReg == PPC::CR5GT ||
508 SrcReg == PPC::CR5EQ || SrcReg == PPC::CR5UN)
510 else if (SrcReg == PPC::CR6LT || SrcReg == PPC::CR6GT ||
511 SrcReg == PPC::CR6EQ || SrcReg == PPC::CR6UN)
513 else if (SrcReg == PPC::CR7LT || SrcReg == PPC::CR7GT ||
514 SrcReg == PPC::CR7EQ || SrcReg == PPC::CR7UN)
517 return StoreRegToStackSlot(MF, Reg, isKill, FrameIdx,
518 &PPC::CRRCRegClass, NewMIs, NonRI, SpillsVRS);
520 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
521 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STVX))
523 getKillRegState(isKill)),
526 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
527 assert(TM.getSubtargetImpl()->isDarwin() &&
528 "VRSAVE only needs spill/restore on Darwin");
529 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_VRSAVE))
531 getKillRegState(isKill)),
535 llvm_unreachable("Unknown regclass!");
542 PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
543 MachineBasicBlock::iterator MI,
544 unsigned SrcReg, bool isKill, int FrameIdx,
545 const TargetRegisterClass *RC,
546 const TargetRegisterInfo *TRI) const {
547 MachineFunction &MF = *MBB.getParent();
548 SmallVector<MachineInstr*, 4> NewMIs;
550 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
551 FuncInfo->setHasSpills();
553 bool NonRI = false, SpillsVRS = false;
554 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs,
556 FuncInfo->setSpillsCR();
559 FuncInfo->setSpillsVRSAVE();
562 FuncInfo->setHasNonRISpills();
564 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
565 MBB.insert(MI, NewMIs[i]);
567 const MachineFrameInfo &MFI = *MF.getFrameInfo();
568 MachineMemOperand *MMO =
569 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
570 MachineMemOperand::MOStore,
571 MFI.getObjectSize(FrameIdx),
572 MFI.getObjectAlignment(FrameIdx));
573 NewMIs.back()->addMemOperand(MF, MMO);
577 PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL,
578 unsigned DestReg, int FrameIdx,
579 const TargetRegisterClass *RC,
580 SmallVectorImpl<MachineInstr*> &NewMIs,
581 bool &NonRI, bool &SpillsVRS) const{
582 // Note: If additional load instructions are added here,
583 // update isLoadFromStackSlot.
585 if (PPC::GPRCRegClass.hasSubClassEq(RC)) {
586 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
587 DestReg), FrameIdx));
588 } else if (PPC::G8RCRegClass.hasSubClassEq(RC)) {
589 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg),
591 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
592 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg),
594 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
595 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg),
597 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
598 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
599 get(PPC::RESTORE_CR), DestReg),
602 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
605 if (DestReg == PPC::CR0LT || DestReg == PPC::CR0GT ||
606 DestReg == PPC::CR0EQ || DestReg == PPC::CR0UN)
608 else if (DestReg == PPC::CR1LT || DestReg == PPC::CR1GT ||
609 DestReg == PPC::CR1EQ || DestReg == PPC::CR1UN)
611 else if (DestReg == PPC::CR2LT || DestReg == PPC::CR2GT ||
612 DestReg == PPC::CR2EQ || DestReg == PPC::CR2UN)
614 else if (DestReg == PPC::CR3LT || DestReg == PPC::CR3GT ||
615 DestReg == PPC::CR3EQ || DestReg == PPC::CR3UN)
617 else if (DestReg == PPC::CR4LT || DestReg == PPC::CR4GT ||
618 DestReg == PPC::CR4EQ || DestReg == PPC::CR4UN)
620 else if (DestReg == PPC::CR5LT || DestReg == PPC::CR5GT ||
621 DestReg == PPC::CR5EQ || DestReg == PPC::CR5UN)
623 else if (DestReg == PPC::CR6LT || DestReg == PPC::CR6GT ||
624 DestReg == PPC::CR6EQ || DestReg == PPC::CR6UN)
626 else if (DestReg == PPC::CR7LT || DestReg == PPC::CR7GT ||
627 DestReg == PPC::CR7EQ || DestReg == PPC::CR7UN)
630 return LoadRegFromStackSlot(MF, DL, Reg, FrameIdx,
631 &PPC::CRRCRegClass, NewMIs, NonRI, SpillsVRS);
633 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
634 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LVX), DestReg),
637 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
638 assert(TM.getSubtargetImpl()->isDarwin() &&
639 "VRSAVE only needs spill/restore on Darwin");
640 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
641 get(PPC::RESTORE_VRSAVE),
646 llvm_unreachable("Unknown regclass!");
653 PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
654 MachineBasicBlock::iterator MI,
655 unsigned DestReg, int FrameIdx,
656 const TargetRegisterClass *RC,
657 const TargetRegisterInfo *TRI) const {
658 MachineFunction &MF = *MBB.getParent();
659 SmallVector<MachineInstr*, 4> NewMIs;
661 if (MI != MBB.end()) DL = MI->getDebugLoc();
663 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
664 FuncInfo->setHasSpills();
666 bool NonRI = false, SpillsVRS = false;
667 if (LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs,
669 FuncInfo->setSpillsCR();
672 FuncInfo->setSpillsVRSAVE();
675 FuncInfo->setHasNonRISpills();
677 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
678 MBB.insert(MI, NewMIs[i]);
680 const MachineFrameInfo &MFI = *MF.getFrameInfo();
681 MachineMemOperand *MMO =
682 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
683 MachineMemOperand::MOLoad,
684 MFI.getObjectSize(FrameIdx),
685 MFI.getObjectAlignment(FrameIdx));
686 NewMIs.back()->addMemOperand(MF, MMO);
690 PPCInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF,
691 int FrameIx, uint64_t Offset,
694 MachineInstrBuilder MIB = BuildMI(MF, DL, get(PPC::DBG_VALUE));
695 addFrameReference(MIB, FrameIx, 0, false).addImm(Offset).addMetadata(MDPtr);
700 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
701 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
702 if (Cond[1].getReg() == PPC::CTR8 || Cond[1].getReg() == PPC::CTR)
703 Cond[0].setImm(Cond[0].getImm() == 0 ? 1 : 0);
705 // Leave the CR# the same, but invert the condition.
706 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
710 /// GetInstSize - Return the number of bytes of code the specified
711 /// instruction may be. This returns the maximum number of bytes.
713 unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
714 switch (MI->getOpcode()) {
715 case PPC::INLINEASM: { // Inline Asm: Variable size.
716 const MachineFunction *MF = MI->getParent()->getParent();
717 const char *AsmStr = MI->getOperand(0).getSymbolName();
718 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
720 case PPC::PROLOG_LABEL:
729 return 4; // PowerPC instructions are all 4 bytes