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"
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/CodeGen/PseudoSourceValue.h"
26 #include "llvm/MC/MCAsmInfo.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Support/TargetRegistry.h"
30 #include "llvm/Support/raw_ostream.h"
31 #include "llvm/ADT/STLExtras.h"
33 #define GET_INSTRINFO_CTOR
34 #include "PPCGenInstrInfo.inc"
37 extern cl::opt<bool> DisablePPC32RS;
38 extern cl::opt<bool> DisablePPC64RS;
44 opt<bool> DisableCTRLoopAnal("disable-ppc-ctrloop-analysis", cl::Hidden,
45 cl::desc("Disable analysis for CTR loops"));
47 PPCInstrInfo::PPCInstrInfo(PPCTargetMachine &tm)
48 : PPCGenInstrInfo(PPC::ADJCALLSTACKDOWN, PPC::ADJCALLSTACKUP),
49 TM(tm), RI(*TM.getSubtargetImpl(), *this) {}
51 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
52 /// this target when scheduling the DAG.
53 ScheduleHazardRecognizer *PPCInstrInfo::CreateTargetHazardRecognizer(
54 const TargetMachine *TM,
55 const ScheduleDAG *DAG) const {
56 unsigned Directive = TM->getSubtarget<PPCSubtarget>().getDarwinDirective();
57 if (Directive == PPC::DIR_440 || Directive == PPC::DIR_A2 ||
58 Directive == PPC::DIR_E500mc || Directive == PPC::DIR_E5500) {
59 const InstrItineraryData *II = TM->getInstrItineraryData();
60 return new PPCScoreboardHazardRecognizer(II, DAG);
63 return TargetInstrInfo::CreateTargetHazardRecognizer(TM, DAG);
66 /// CreateTargetPostRAHazardRecognizer - Return the postRA hazard recognizer
67 /// to use for this target when scheduling the DAG.
68 ScheduleHazardRecognizer *PPCInstrInfo::CreateTargetPostRAHazardRecognizer(
69 const InstrItineraryData *II,
70 const ScheduleDAG *DAG) const {
71 unsigned Directive = TM.getSubtarget<PPCSubtarget>().getDarwinDirective();
73 // Most subtargets use a PPC970 recognizer.
74 if (Directive != PPC::DIR_440 && Directive != PPC::DIR_A2 &&
75 Directive != PPC::DIR_E500mc && Directive != PPC::DIR_E5500) {
76 const TargetInstrInfo *TII = TM.getInstrInfo();
77 assert(TII && "No InstrInfo?");
79 return new PPCHazardRecognizer970(*TII);
82 return new PPCScoreboardHazardRecognizer(II, DAG);
85 // Detect 32 -> 64-bit extensions where we may reuse the low sub-register.
86 bool PPCInstrInfo::isCoalescableExtInstr(const MachineInstr &MI,
87 unsigned &SrcReg, unsigned &DstReg,
88 unsigned &SubIdx) const {
89 switch (MI.getOpcode()) {
90 default: return false;
92 case PPC::EXTSW_32_64:
93 SrcReg = MI.getOperand(1).getReg();
94 DstReg = MI.getOperand(0).getReg();
100 unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
101 int &FrameIndex) const {
102 switch (MI->getOpcode()) {
108 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
109 MI->getOperand(2).isFI()) {
110 FrameIndex = MI->getOperand(2).getIndex();
111 return MI->getOperand(0).getReg();
118 unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
119 int &FrameIndex) const {
120 switch (MI->getOpcode()) {
126 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
127 MI->getOperand(2).isFI()) {
128 FrameIndex = MI->getOperand(2).getIndex();
129 return MI->getOperand(0).getReg();
136 // commuteInstruction - We can commute rlwimi instructions, but only if the
137 // rotate amt is zero. We also have to munge the immediates a bit.
139 PPCInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
140 MachineFunction &MF = *MI->getParent()->getParent();
142 // Normal instructions can be commuted the obvious way.
143 if (MI->getOpcode() != PPC::RLWIMI)
144 return TargetInstrInfo::commuteInstruction(MI, NewMI);
146 // Cannot commute if it has a non-zero rotate count.
147 if (MI->getOperand(3).getImm() != 0)
150 // If we have a zero rotate count, we have:
152 // Op0 = (Op1 & ~M) | (Op2 & M)
154 // M = mask((ME+1)&31, (MB-1)&31)
155 // Op0 = (Op2 & ~M) | (Op1 & M)
158 unsigned Reg0 = MI->getOperand(0).getReg();
159 unsigned Reg1 = MI->getOperand(1).getReg();
160 unsigned Reg2 = MI->getOperand(2).getReg();
161 bool Reg1IsKill = MI->getOperand(1).isKill();
162 bool Reg2IsKill = MI->getOperand(2).isKill();
163 bool ChangeReg0 = false;
164 // If machine instrs are no longer in two-address forms, update
165 // destination register as well.
167 // Must be two address instruction!
168 assert(MI->getDesc().getOperandConstraint(0, MCOI::TIED_TO) &&
169 "Expecting a two-address instruction!");
175 unsigned MB = MI->getOperand(4).getImm();
176 unsigned ME = MI->getOperand(5).getImm();
179 // Create a new instruction.
180 unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg();
181 bool Reg0IsDead = MI->getOperand(0).isDead();
182 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
183 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
184 .addReg(Reg2, getKillRegState(Reg2IsKill))
185 .addReg(Reg1, getKillRegState(Reg1IsKill))
187 .addImm((MB-1) & 31);
191 MI->getOperand(0).setReg(Reg2);
192 MI->getOperand(2).setReg(Reg1);
193 MI->getOperand(1).setReg(Reg2);
194 MI->getOperand(2).setIsKill(Reg1IsKill);
195 MI->getOperand(1).setIsKill(Reg2IsKill);
197 // Swap the mask around.
198 MI->getOperand(4).setImm((ME+1) & 31);
199 MI->getOperand(5).setImm((MB-1) & 31);
203 void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
204 MachineBasicBlock::iterator MI) const {
206 BuildMI(MBB, MI, DL, get(PPC::NOP));
211 // Note: If the condition register is set to CTR or CTR8 then this is a
212 // BDNZ (imm == 1) or BDZ (imm == 0) branch.
213 bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
214 MachineBasicBlock *&FBB,
215 SmallVectorImpl<MachineOperand> &Cond,
216 bool AllowModify) const {
217 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
219 // If the block has no terminators, it just falls into the block after it.
220 MachineBasicBlock::iterator I = MBB.end();
221 if (I == MBB.begin())
224 while (I->isDebugValue()) {
225 if (I == MBB.begin())
229 if (!isUnpredicatedTerminator(I))
232 // Get the last instruction in the block.
233 MachineInstr *LastInst = I;
235 // If there is only one terminator instruction, process it.
236 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
237 if (LastInst->getOpcode() == PPC::B) {
238 if (!LastInst->getOperand(0).isMBB())
240 TBB = LastInst->getOperand(0).getMBB();
242 } else if (LastInst->getOpcode() == PPC::BCC) {
243 if (!LastInst->getOperand(2).isMBB())
245 // Block ends with fall-through condbranch.
246 TBB = LastInst->getOperand(2).getMBB();
247 Cond.push_back(LastInst->getOperand(0));
248 Cond.push_back(LastInst->getOperand(1));
250 } else if (LastInst->getOpcode() == PPC::BDNZ8 ||
251 LastInst->getOpcode() == PPC::BDNZ) {
252 if (!LastInst->getOperand(0).isMBB())
254 if (DisableCTRLoopAnal)
256 TBB = LastInst->getOperand(0).getMBB();
257 Cond.push_back(MachineOperand::CreateImm(1));
258 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
261 } else if (LastInst->getOpcode() == PPC::BDZ8 ||
262 LastInst->getOpcode() == PPC::BDZ) {
263 if (!LastInst->getOperand(0).isMBB())
265 if (DisableCTRLoopAnal)
267 TBB = LastInst->getOperand(0).getMBB();
268 Cond.push_back(MachineOperand::CreateImm(0));
269 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
274 // Otherwise, don't know what this is.
278 // Get the instruction before it if it's a terminator.
279 MachineInstr *SecondLastInst = I;
281 // If there are three terminators, we don't know what sort of block this is.
282 if (SecondLastInst && I != MBB.begin() &&
283 isUnpredicatedTerminator(--I))
286 // If the block ends with PPC::B and PPC:BCC, handle it.
287 if (SecondLastInst->getOpcode() == PPC::BCC &&
288 LastInst->getOpcode() == PPC::B) {
289 if (!SecondLastInst->getOperand(2).isMBB() ||
290 !LastInst->getOperand(0).isMBB())
292 TBB = SecondLastInst->getOperand(2).getMBB();
293 Cond.push_back(SecondLastInst->getOperand(0));
294 Cond.push_back(SecondLastInst->getOperand(1));
295 FBB = LastInst->getOperand(0).getMBB();
297 } else if ((SecondLastInst->getOpcode() == PPC::BDNZ8 ||
298 SecondLastInst->getOpcode() == PPC::BDNZ) &&
299 LastInst->getOpcode() == PPC::B) {
300 if (!SecondLastInst->getOperand(0).isMBB() ||
301 !LastInst->getOperand(0).isMBB())
303 if (DisableCTRLoopAnal)
305 TBB = SecondLastInst->getOperand(0).getMBB();
306 Cond.push_back(MachineOperand::CreateImm(1));
307 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
309 FBB = LastInst->getOperand(0).getMBB();
311 } else if ((SecondLastInst->getOpcode() == PPC::BDZ8 ||
312 SecondLastInst->getOpcode() == PPC::BDZ) &&
313 LastInst->getOpcode() == PPC::B) {
314 if (!SecondLastInst->getOperand(0).isMBB() ||
315 !LastInst->getOperand(0).isMBB())
317 if (DisableCTRLoopAnal)
319 TBB = SecondLastInst->getOperand(0).getMBB();
320 Cond.push_back(MachineOperand::CreateImm(0));
321 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
323 FBB = LastInst->getOperand(0).getMBB();
327 // If the block ends with two PPC:Bs, handle it. The second one is not
328 // executed, so remove it.
329 if (SecondLastInst->getOpcode() == PPC::B &&
330 LastInst->getOpcode() == PPC::B) {
331 if (!SecondLastInst->getOperand(0).isMBB())
333 TBB = SecondLastInst->getOperand(0).getMBB();
336 I->eraseFromParent();
340 // Otherwise, can't handle this.
344 unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
345 MachineBasicBlock::iterator I = MBB.end();
346 if (I == MBB.begin()) return 0;
348 while (I->isDebugValue()) {
349 if (I == MBB.begin())
353 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC &&
354 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
355 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
358 // Remove the branch.
359 I->eraseFromParent();
363 if (I == MBB.begin()) return 1;
365 if (I->getOpcode() != PPC::BCC &&
366 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
367 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
370 // Remove the branch.
371 I->eraseFromParent();
376 PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
377 MachineBasicBlock *FBB,
378 const SmallVectorImpl<MachineOperand> &Cond,
380 // Shouldn't be a fall through.
381 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
382 assert((Cond.size() == 2 || Cond.size() == 0) &&
383 "PPC branch conditions have two components!");
385 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
389 if (Cond.empty()) // Unconditional branch
390 BuildMI(&MBB, DL, get(PPC::B)).addMBB(TBB);
391 else if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
392 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
393 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
394 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
395 else // Conditional branch
396 BuildMI(&MBB, DL, get(PPC::BCC))
397 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
401 // Two-way Conditional Branch.
402 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
403 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
404 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
405 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
407 BuildMI(&MBB, DL, get(PPC::BCC))
408 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
409 BuildMI(&MBB, DL, get(PPC::B)).addMBB(FBB);
413 void PPCInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
414 MachineBasicBlock::iterator I, DebugLoc DL,
415 unsigned DestReg, unsigned SrcReg,
416 bool KillSrc) const {
418 if (PPC::GPRCRegClass.contains(DestReg, SrcReg))
420 else if (PPC::G8RCRegClass.contains(DestReg, SrcReg))
422 else if (PPC::F4RCRegClass.contains(DestReg, SrcReg))
424 else if (PPC::CRRCRegClass.contains(DestReg, SrcReg))
426 else if (PPC::VRRCRegClass.contains(DestReg, SrcReg))
428 else if (PPC::CRBITRCRegClass.contains(DestReg, SrcReg))
431 llvm_unreachable("Impossible reg-to-reg copy");
433 const MCInstrDesc &MCID = get(Opc);
434 if (MCID.getNumOperands() == 3)
435 BuildMI(MBB, I, DL, MCID, DestReg)
436 .addReg(SrcReg).addReg(SrcReg, getKillRegState(KillSrc));
438 BuildMI(MBB, I, DL, MCID, DestReg).addReg(SrcReg, getKillRegState(KillSrc));
441 // This function returns true if a CR spill is necessary and false otherwise.
443 PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
444 unsigned SrcReg, bool isKill,
446 const TargetRegisterClass *RC,
447 SmallVectorImpl<MachineInstr*> &NewMIs) const{
449 if (PPC::GPRCRegClass.hasSubClassEq(RC)) {
450 if (SrcReg != PPC::LR) {
451 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
453 getKillRegState(isKill)),
456 // FIXME: this spills LR immediately to memory in one step. To do this,
457 // we use R11, which we know cannot be used in the prolog/epilog. This is
459 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFLR), PPC::R11));
460 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
462 getKillRegState(isKill)),
465 } else if (PPC::G8RCRegClass.hasSubClassEq(RC)) {
466 if (SrcReg != PPC::LR8) {
467 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
469 getKillRegState(isKill)),
472 // FIXME: this spills LR immediately to memory in one step. To do this,
473 // we use X11, which we know cannot be used in the prolog/epilog. This is
475 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFLR8), PPC::X11));
476 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
478 getKillRegState(isKill)),
481 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
482 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD))
484 getKillRegState(isKill)),
486 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
487 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS))
489 getKillRegState(isKill)),
491 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
492 if ((!DisablePPC32RS && !TM.getSubtargetImpl()->isPPC64()) ||
493 (!DisablePPC64RS && TM.getSubtargetImpl()->isPPC64())) {
494 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR))
496 getKillRegState(isKill)),
500 // FIXME: We need a scatch reg here. The trouble with using R0 is that
501 // it's possible for the stack frame to be so big the save location is
502 // out of range of immediate offsets, necessitating another register.
503 // We hack this on Darwin by reserving R2. It's probably broken on Linux
506 bool is64Bit = TM.getSubtargetImpl()->isPPC64();
507 // We need to store the CR in the low 4-bits of the saved value. First,
508 // issue a MFCR to save all of the CRBits.
509 unsigned ScratchReg = TM.getSubtargetImpl()->isDarwinABI() ?
510 (is64Bit ? PPC::X2 : PPC::R2) :
511 (is64Bit ? PPC::X0 : PPC::R0);
512 NewMIs.push_back(BuildMI(MF, DL, get(is64Bit ? PPC::MFCR8pseud :
513 PPC::MFCRpseud), ScratchReg)
514 .addReg(SrcReg, getKillRegState(isKill)));
516 // If the saved register wasn't CR0, shift the bits left so that they are
518 if (SrcReg != PPC::CR0) {
519 unsigned ShiftBits = getPPCRegisterNumbering(SrcReg)*4;
520 // rlwinm scratch, scratch, ShiftBits, 0, 31.
521 NewMIs.push_back(BuildMI(MF, DL, get(is64Bit ? PPC::RLWINM8 :
522 PPC::RLWINM), ScratchReg)
523 .addReg(ScratchReg).addImm(ShiftBits)
524 .addImm(0).addImm(31));
527 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(is64Bit ?
528 PPC::STW8 : PPC::STW))
530 getKillRegState(isKill)),
533 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
534 // FIXME: We use CRi here because there is no mtcrf on a bit. Since the
535 // backend currently only uses CR1EQ as an individual bit, this should
536 // not cause any bug. If we need other uses of CR bits, the following
537 // code may be invalid.
539 if (SrcReg == PPC::CR0LT || SrcReg == PPC::CR0GT ||
540 SrcReg == PPC::CR0EQ || SrcReg == PPC::CR0UN)
542 else if (SrcReg == PPC::CR1LT || SrcReg == PPC::CR1GT ||
543 SrcReg == PPC::CR1EQ || SrcReg == PPC::CR1UN)
545 else if (SrcReg == PPC::CR2LT || SrcReg == PPC::CR2GT ||
546 SrcReg == PPC::CR2EQ || SrcReg == PPC::CR2UN)
548 else if (SrcReg == PPC::CR3LT || SrcReg == PPC::CR3GT ||
549 SrcReg == PPC::CR3EQ || SrcReg == PPC::CR3UN)
551 else if (SrcReg == PPC::CR4LT || SrcReg == PPC::CR4GT ||
552 SrcReg == PPC::CR4EQ || SrcReg == PPC::CR4UN)
554 else if (SrcReg == PPC::CR5LT || SrcReg == PPC::CR5GT ||
555 SrcReg == PPC::CR5EQ || SrcReg == PPC::CR5UN)
557 else if (SrcReg == PPC::CR6LT || SrcReg == PPC::CR6GT ||
558 SrcReg == PPC::CR6EQ || SrcReg == PPC::CR6UN)
560 else if (SrcReg == PPC::CR7LT || SrcReg == PPC::CR7GT ||
561 SrcReg == PPC::CR7EQ || SrcReg == PPC::CR7UN)
564 return StoreRegToStackSlot(MF, Reg, isKill, FrameIdx,
565 &PPC::CRRCRegClass, NewMIs);
567 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
568 // We don't have indexed addressing for vector loads. Emit:
572 // FIXME: We use R0 here, because it isn't available for RA.
573 bool Is64Bit = TM.getSubtargetImpl()->isPPC64();
574 unsigned Instr = Is64Bit ? PPC::ADDI8 : PPC::ADDI;
575 unsigned GPR0 = Is64Bit ? PPC::X0 : PPC::R0;
576 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Instr), GPR0),
578 NewMIs.push_back(BuildMI(MF, DL, get(PPC::STVX))
579 .addReg(SrcReg, getKillRegState(isKill))
583 llvm_unreachable("Unknown regclass!");
590 PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
591 MachineBasicBlock::iterator MI,
592 unsigned SrcReg, bool isKill, int FrameIdx,
593 const TargetRegisterClass *RC,
594 const TargetRegisterInfo *TRI) const {
595 MachineFunction &MF = *MBB.getParent();
596 SmallVector<MachineInstr*, 4> NewMIs;
598 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs)) {
599 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
600 FuncInfo->setSpillsCR();
603 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
604 MBB.insert(MI, NewMIs[i]);
606 const MachineFrameInfo &MFI = *MF.getFrameInfo();
607 MachineMemOperand *MMO =
608 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
609 MachineMemOperand::MOStore,
610 MFI.getObjectSize(FrameIdx),
611 MFI.getObjectAlignment(FrameIdx));
612 NewMIs.back()->addMemOperand(MF, MMO);
616 PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL,
617 unsigned DestReg, int FrameIdx,
618 const TargetRegisterClass *RC,
619 SmallVectorImpl<MachineInstr*> &NewMIs)const{
620 if (PPC::GPRCRegClass.hasSubClassEq(RC)) {
621 if (DestReg != PPC::LR) {
622 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
623 DestReg), FrameIdx));
625 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
626 PPC::R11), FrameIdx));
627 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTLR)).addReg(PPC::R11));
629 } else if (PPC::G8RCRegClass.hasSubClassEq(RC)) {
630 if (DestReg != PPC::LR8) {
631 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg),
634 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD),
635 PPC::X11), FrameIdx));
636 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTLR8)).addReg(PPC::X11));
638 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
639 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg),
641 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
642 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg),
644 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
645 if ((!DisablePPC32RS && !TM.getSubtargetImpl()->isPPC64()) ||
646 (!DisablePPC64RS && TM.getSubtargetImpl()->isPPC64())) {
647 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
648 get(PPC::RESTORE_CR), DestReg)
652 // FIXME: We need a scatch reg here. The trouble with using R0 is that
653 // it's possible for the stack frame to be so big the save location is
654 // out of range of immediate offsets, necessitating another register.
655 // We hack this on Darwin by reserving R2. It's probably broken on Linux
657 unsigned ScratchReg = TM.getSubtargetImpl()->isDarwinABI() ?
659 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
660 ScratchReg), FrameIdx));
662 // If the reloaded register isn't CR0, shift the bits right so that they are
663 // in the right CR's slot.
664 if (DestReg != PPC::CR0) {
665 unsigned ShiftBits = getPPCRegisterNumbering(DestReg)*4;
666 // rlwinm r11, r11, 32-ShiftBits, 0, 31.
667 NewMIs.push_back(BuildMI(MF, DL, get(PPC::RLWINM), ScratchReg)
668 .addReg(ScratchReg).addImm(32-ShiftBits).addImm(0)
672 NewMIs.push_back(BuildMI(MF, DL, get(TM.getSubtargetImpl()->isPPC64() ?
673 PPC::MTCRF8 : PPC::MTCRF), DestReg)
674 .addReg(ScratchReg));
676 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
679 if (DestReg == PPC::CR0LT || DestReg == PPC::CR0GT ||
680 DestReg == PPC::CR0EQ || DestReg == PPC::CR0UN)
682 else if (DestReg == PPC::CR1LT || DestReg == PPC::CR1GT ||
683 DestReg == PPC::CR1EQ || DestReg == PPC::CR1UN)
685 else if (DestReg == PPC::CR2LT || DestReg == PPC::CR2GT ||
686 DestReg == PPC::CR2EQ || DestReg == PPC::CR2UN)
688 else if (DestReg == PPC::CR3LT || DestReg == PPC::CR3GT ||
689 DestReg == PPC::CR3EQ || DestReg == PPC::CR3UN)
691 else if (DestReg == PPC::CR4LT || DestReg == PPC::CR4GT ||
692 DestReg == PPC::CR4EQ || DestReg == PPC::CR4UN)
694 else if (DestReg == PPC::CR5LT || DestReg == PPC::CR5GT ||
695 DestReg == PPC::CR5EQ || DestReg == PPC::CR5UN)
697 else if (DestReg == PPC::CR6LT || DestReg == PPC::CR6GT ||
698 DestReg == PPC::CR6EQ || DestReg == PPC::CR6UN)
700 else if (DestReg == PPC::CR7LT || DestReg == PPC::CR7GT ||
701 DestReg == PPC::CR7EQ || DestReg == PPC::CR7UN)
704 return LoadRegFromStackSlot(MF, DL, Reg, FrameIdx,
705 &PPC::CRRCRegClass, NewMIs);
707 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
708 // We don't have indexed addressing for vector loads. Emit:
712 // FIXME: We use R0 here, because it isn't available for RA.
713 bool Is64Bit = TM.getSubtargetImpl()->isPPC64();
714 unsigned Instr = Is64Bit ? PPC::ADDI8 : PPC::ADDI;
715 unsigned GPR0 = Is64Bit ? PPC::X0 : PPC::R0;
716 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Instr), GPR0),
718 NewMIs.push_back(BuildMI(MF, DL, get(PPC::LVX),DestReg).addReg(GPR0)
721 llvm_unreachable("Unknown regclass!");
728 PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
729 MachineBasicBlock::iterator MI,
730 unsigned DestReg, int FrameIdx,
731 const TargetRegisterClass *RC,
732 const TargetRegisterInfo *TRI) const {
733 MachineFunction &MF = *MBB.getParent();
734 SmallVector<MachineInstr*, 4> NewMIs;
736 if (MI != MBB.end()) DL = MI->getDebugLoc();
737 if (LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs)) {
738 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
739 FuncInfo->setSpillsCR();
741 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
742 MBB.insert(MI, NewMIs[i]);
744 const MachineFrameInfo &MFI = *MF.getFrameInfo();
745 MachineMemOperand *MMO =
746 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
747 MachineMemOperand::MOLoad,
748 MFI.getObjectSize(FrameIdx),
749 MFI.getObjectAlignment(FrameIdx));
750 NewMIs.back()->addMemOperand(MF, MMO);
754 PPCInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF,
755 int FrameIx, uint64_t Offset,
758 MachineInstrBuilder MIB = BuildMI(MF, DL, get(PPC::DBG_VALUE));
759 addFrameReference(MIB, FrameIx, 0, false).addImm(Offset).addMetadata(MDPtr);
764 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
765 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
766 if (Cond[1].getReg() == PPC::CTR8 || Cond[1].getReg() == PPC::CTR)
767 Cond[0].setImm(Cond[0].getImm() == 0 ? 1 : 0);
769 // Leave the CR# the same, but invert the condition.
770 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
774 /// GetInstSize - Return the number of bytes of code the specified
775 /// instruction may be. This returns the maximum number of bytes.
777 unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
778 switch (MI->getOpcode()) {
779 case PPC::INLINEASM: { // Inline Asm: Variable size.
780 const MachineFunction *MF = MI->getParent()->getParent();
781 const char *AsmStr = MI->getOperand(0).getSymbolName();
782 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
784 case PPC::PROLOG_LABEL:
789 case PPC::BL8_NOP_ELF:
790 case PPC::BLA8_NOP_ELF:
793 return 4; // PowerPC instructions are all 4 bytes