1 //===- PPCInstrInfo.cpp - PowerPC32 Instruction Information -----*- C++ -*-===//
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 "PPCInstrBuilder.h"
16 #include "PPCMachineFunctionInfo.h"
17 #include "PPCPredicates.h"
18 #include "PPCGenInstrInfo.inc"
19 #include "PPCTargetMachine.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/CodeGen/MachineRegisterInfo.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/MC/MCAsmInfo.h"
29 extern cl::opt<bool> EnablePPC32RS; // FIXME (64-bit): See PPCRegisterInfo.cpp.
30 extern cl::opt<bool> EnablePPC64RS; // FIXME (64-bit): See PPCRegisterInfo.cpp.
32 PPCInstrInfo::PPCInstrInfo(PPCTargetMachine &tm)
33 : TargetInstrInfoImpl(PPCInsts, array_lengthof(PPCInsts)), TM(tm),
34 RI(*TM.getSubtargetImpl(), *this) {}
36 bool PPCInstrInfo::isMoveInstr(const MachineInstr& MI,
39 unsigned& sourceSubIdx,
40 unsigned& destSubIdx) const {
41 sourceSubIdx = destSubIdx = 0; // No sub-registers.
43 unsigned oc = MI.getOpcode();
44 if (oc == PPC::OR || oc == PPC::OR8 || oc == PPC::VOR ||
45 oc == PPC::OR4To8 || oc == PPC::OR8To4) { // or r1, r2, r2
46 assert(MI.getNumOperands() >= 3 &&
47 MI.getOperand(0).isReg() &&
48 MI.getOperand(1).isReg() &&
49 MI.getOperand(2).isReg() &&
50 "invalid PPC OR instruction!");
51 if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
52 sourceReg = MI.getOperand(1).getReg();
53 destReg = MI.getOperand(0).getReg();
56 } else if (oc == PPC::ADDI) { // addi r1, r2, 0
57 assert(MI.getNumOperands() >= 3 &&
58 MI.getOperand(0).isReg() &&
59 MI.getOperand(2).isImm() &&
60 "invalid PPC ADDI instruction!");
61 if (MI.getOperand(1).isReg() && MI.getOperand(2).getImm() == 0) {
62 sourceReg = MI.getOperand(1).getReg();
63 destReg = MI.getOperand(0).getReg();
66 } else if (oc == PPC::ORI) { // ori r1, r2, 0
67 assert(MI.getNumOperands() >= 3 &&
68 MI.getOperand(0).isReg() &&
69 MI.getOperand(1).isReg() &&
70 MI.getOperand(2).isImm() &&
71 "invalid PPC ORI instruction!");
72 if (MI.getOperand(2).getImm() == 0) {
73 sourceReg = MI.getOperand(1).getReg();
74 destReg = MI.getOperand(0).getReg();
77 } else if (oc == PPC::FMRS || oc == PPC::FMRD ||
78 oc == PPC::FMRSD) { // fmr r1, r2
79 assert(MI.getNumOperands() >= 2 &&
80 MI.getOperand(0).isReg() &&
81 MI.getOperand(1).isReg() &&
82 "invalid PPC FMR instruction");
83 sourceReg = MI.getOperand(1).getReg();
84 destReg = MI.getOperand(0).getReg();
86 } else if (oc == PPC::MCRF) { // mcrf cr1, cr2
87 assert(MI.getNumOperands() >= 2 &&
88 MI.getOperand(0).isReg() &&
89 MI.getOperand(1).isReg() &&
90 "invalid PPC MCRF instruction");
91 sourceReg = MI.getOperand(1).getReg();
92 destReg = MI.getOperand(0).getReg();
98 unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
99 int &FrameIndex) const {
100 switch (MI->getOpcode()) {
106 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
107 MI->getOperand(2).isFI()) {
108 FrameIndex = MI->getOperand(2).getIndex();
109 return MI->getOperand(0).getReg();
116 unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
117 int &FrameIndex) const {
118 switch (MI->getOpcode()) {
124 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
125 MI->getOperand(2).isFI()) {
126 FrameIndex = MI->getOperand(2).getIndex();
127 return MI->getOperand(0).getReg();
134 // commuteInstruction - We can commute rlwimi instructions, but only if the
135 // rotate amt is zero. We also have to munge the immediates a bit.
137 PPCInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
138 MachineFunction &MF = *MI->getParent()->getParent();
140 // Normal instructions can be commuted the obvious way.
141 if (MI->getOpcode() != PPC::RLWIMI)
142 return TargetInstrInfoImpl::commuteInstruction(MI, NewMI);
144 // Cannot commute if it has a non-zero rotate count.
145 if (MI->getOperand(3).getImm() != 0)
148 // If we have a zero rotate count, we have:
150 // Op0 = (Op1 & ~M) | (Op2 & M)
152 // M = mask((ME+1)&31, (MB-1)&31)
153 // Op0 = (Op2 & ~M) | (Op1 & M)
156 unsigned Reg0 = MI->getOperand(0).getReg();
157 unsigned Reg1 = MI->getOperand(1).getReg();
158 unsigned Reg2 = MI->getOperand(2).getReg();
159 bool Reg1IsKill = MI->getOperand(1).isKill();
160 bool Reg2IsKill = MI->getOperand(2).isKill();
161 bool ChangeReg0 = false;
162 // If machine instrs are no longer in two-address forms, update
163 // destination register as well.
165 // Must be two address instruction!
166 assert(MI->getDesc().getOperandConstraint(0, TOI::TIED_TO) &&
167 "Expecting a two-address instruction!");
173 unsigned MB = MI->getOperand(4).getImm();
174 unsigned ME = MI->getOperand(5).getImm();
177 // Create a new instruction.
178 unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg();
179 bool Reg0IsDead = MI->getOperand(0).isDead();
180 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
181 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
182 .addReg(Reg2, getKillRegState(Reg2IsKill))
183 .addReg(Reg1, getKillRegState(Reg1IsKill))
185 .addImm((MB-1) & 31);
189 MI->getOperand(0).setReg(Reg2);
190 MI->getOperand(2).setReg(Reg1);
191 MI->getOperand(1).setReg(Reg2);
192 MI->getOperand(2).setIsKill(Reg1IsKill);
193 MI->getOperand(1).setIsKill(Reg2IsKill);
195 // Swap the mask around.
196 MI->getOperand(4).setImm((ME+1) & 31);
197 MI->getOperand(5).setImm((MB-1) & 31);
201 void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
202 MachineBasicBlock::iterator MI) const {
203 DebugLoc DL = DebugLoc::getUnknownLoc();
204 if (MI != MBB.end()) DL = MI->getDebugLoc();
206 BuildMI(MBB, MI, DL, get(PPC::NOP));
211 bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
212 MachineBasicBlock *&FBB,
213 SmallVectorImpl<MachineOperand> &Cond,
214 bool AllowModify) const {
215 // If the block has no terminators, it just falls into the block after it.
216 MachineBasicBlock::iterator I = MBB.end();
217 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
220 // Get the last instruction in the block.
221 MachineInstr *LastInst = I;
223 // If there is only one terminator instruction, process it.
224 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
225 if (LastInst->getOpcode() == PPC::B) {
226 if (!LastInst->getOperand(0).isMBB())
228 TBB = LastInst->getOperand(0).getMBB();
230 } else if (LastInst->getOpcode() == PPC::BCC) {
231 if (!LastInst->getOperand(2).isMBB())
233 // Block ends with fall-through condbranch.
234 TBB = LastInst->getOperand(2).getMBB();
235 Cond.push_back(LastInst->getOperand(0));
236 Cond.push_back(LastInst->getOperand(1));
239 // Otherwise, don't know what this is.
243 // Get the instruction before it if it's a terminator.
244 MachineInstr *SecondLastInst = I;
246 // If there are three terminators, we don't know what sort of block this is.
247 if (SecondLastInst && I != MBB.begin() &&
248 isUnpredicatedTerminator(--I))
251 // If the block ends with PPC::B and PPC:BCC, handle it.
252 if (SecondLastInst->getOpcode() == PPC::BCC &&
253 LastInst->getOpcode() == PPC::B) {
254 if (!SecondLastInst->getOperand(2).isMBB() ||
255 !LastInst->getOperand(0).isMBB())
257 TBB = SecondLastInst->getOperand(2).getMBB();
258 Cond.push_back(SecondLastInst->getOperand(0));
259 Cond.push_back(SecondLastInst->getOperand(1));
260 FBB = LastInst->getOperand(0).getMBB();
264 // If the block ends with two PPC:Bs, handle it. The second one is not
265 // executed, so remove it.
266 if (SecondLastInst->getOpcode() == PPC::B &&
267 LastInst->getOpcode() == PPC::B) {
268 if (!SecondLastInst->getOperand(0).isMBB())
270 TBB = SecondLastInst->getOperand(0).getMBB();
273 I->eraseFromParent();
277 // Otherwise, can't handle this.
281 unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
282 MachineBasicBlock::iterator I = MBB.end();
283 if (I == MBB.begin()) return 0;
285 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC)
288 // Remove the branch.
289 I->eraseFromParent();
293 if (I == MBB.begin()) return 1;
295 if (I->getOpcode() != PPC::BCC)
298 // Remove the branch.
299 I->eraseFromParent();
304 PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
305 MachineBasicBlock *FBB,
306 const SmallVectorImpl<MachineOperand> &Cond) const {
307 // FIXME this should probably have a DebugLoc argument
308 DebugLoc dl = DebugLoc::getUnknownLoc();
309 // Shouldn't be a fall through.
310 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
311 assert((Cond.size() == 2 || Cond.size() == 0) &&
312 "PPC branch conditions have two components!");
316 if (Cond.empty()) // Unconditional branch
317 BuildMI(&MBB, dl, get(PPC::B)).addMBB(TBB);
318 else // Conditional branch
319 BuildMI(&MBB, dl, get(PPC::BCC))
320 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
324 // Two-way Conditional Branch.
325 BuildMI(&MBB, dl, get(PPC::BCC))
326 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
327 BuildMI(&MBB, dl, get(PPC::B)).addMBB(FBB);
331 bool PPCInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
332 MachineBasicBlock::iterator MI,
333 unsigned DestReg, unsigned SrcReg,
334 const TargetRegisterClass *DestRC,
335 const TargetRegisterClass *SrcRC) const {
336 if (DestRC != SrcRC) {
337 // Not yet supported!
341 DebugLoc DL = DebugLoc::getUnknownLoc();
342 if (MI != MBB.end()) DL = MI->getDebugLoc();
344 if (DestRC == PPC::GPRCRegisterClass) {
345 BuildMI(MBB, MI, DL, get(PPC::OR), DestReg).addReg(SrcReg).addReg(SrcReg);
346 } else if (DestRC == PPC::G8RCRegisterClass) {
347 BuildMI(MBB, MI, DL, get(PPC::OR8), DestReg).addReg(SrcReg).addReg(SrcReg);
348 } else if (DestRC == PPC::F4RCRegisterClass) {
349 BuildMI(MBB, MI, DL, get(PPC::FMRS), DestReg).addReg(SrcReg);
350 } else if (DestRC == PPC::F8RCRegisterClass) {
351 BuildMI(MBB, MI, DL, get(PPC::FMRD), DestReg).addReg(SrcReg);
352 } else if (DestRC == PPC::CRRCRegisterClass) {
353 BuildMI(MBB, MI, DL, get(PPC::MCRF), DestReg).addReg(SrcReg);
354 } else if (DestRC == PPC::VRRCRegisterClass) {
355 BuildMI(MBB, MI, DL, get(PPC::VOR), DestReg).addReg(SrcReg).addReg(SrcReg);
356 } else if (DestRC == PPC::CRBITRCRegisterClass) {
357 BuildMI(MBB, MI, DL, get(PPC::CROR), DestReg).addReg(SrcReg).addReg(SrcReg);
359 // Attempt to copy register that is not GPR or FPR
367 PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
368 unsigned SrcReg, bool isKill,
370 const TargetRegisterClass *RC,
371 SmallVectorImpl<MachineInstr*> &NewMIs) const{
372 DebugLoc DL = DebugLoc::getUnknownLoc();
373 if (RC == PPC::GPRCRegisterClass) {
374 if (SrcReg != PPC::LR) {
375 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
377 getKillRegState(isKill)),
380 // FIXME: this spills LR immediately to memory in one step. To do this,
381 // we use R11, which we know cannot be used in the prolog/epilog. This is
383 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFLR), PPC::R11));
384 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
386 getKillRegState(isKill)),
389 } else if (RC == PPC::G8RCRegisterClass) {
390 if (SrcReg != PPC::LR8) {
391 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
393 getKillRegState(isKill)),
396 // FIXME: this spills LR immediately to memory in one step. To do this,
397 // we use R11, which we know cannot be used in the prolog/epilog. This is
399 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFLR8), PPC::X11));
400 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
402 getKillRegState(isKill)),
405 } else if (RC == PPC::F8RCRegisterClass) {
406 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD))
408 getKillRegState(isKill)),
410 } else if (RC == PPC::F4RCRegisterClass) {
411 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS))
413 getKillRegState(isKill)),
415 } else if (RC == PPC::CRRCRegisterClass) {
416 if ((EnablePPC32RS && !TM.getSubtargetImpl()->isPPC64()) ||
417 (EnablePPC64RS && TM.getSubtargetImpl()->isPPC64())) {
418 // FIXME (64-bit): Enable
419 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR))
421 getKillRegState(isKill)),
425 // FIXME: We need a scatch reg here. The trouble with using R0 is that
426 // it's possible for the stack frame to be so big the save location is
427 // out of range of immediate offsets, necessitating another register.
428 // We hack this on Darwin by reserving R2. It's probably broken on Linux
431 // We need to store the CR in the low 4-bits of the saved value. First,
432 // issue a MFCR to save all of the CRBits.
433 unsigned ScratchReg = TM.getSubtargetImpl()->isDarwinABI() ?
435 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFCR), ScratchReg));
437 // If the saved register wasn't CR0, shift the bits left so that they are
439 if (SrcReg != PPC::CR0) {
440 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(SrcReg)*4;
441 // rlwinm scratch, scratch, ShiftBits, 0, 31.
442 NewMIs.push_back(BuildMI(MF, DL, get(PPC::RLWINM), ScratchReg)
443 .addReg(ScratchReg).addImm(ShiftBits)
444 .addImm(0).addImm(31));
447 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
449 getKillRegState(isKill)),
452 } else if (RC == PPC::CRBITRCRegisterClass) {
453 // FIXME: We use CRi here because there is no mtcrf on a bit. Since the
454 // backend currently only uses CR1EQ as an individual bit, this should
455 // not cause any bug. If we need other uses of CR bits, the following
456 // code may be invalid.
458 if (SrcReg == PPC::CR0LT || SrcReg == PPC::CR0GT ||
459 SrcReg == PPC::CR0EQ || SrcReg == PPC::CR0UN)
461 else if (SrcReg == PPC::CR1LT || SrcReg == PPC::CR1GT ||
462 SrcReg == PPC::CR1EQ || SrcReg == PPC::CR1UN)
464 else if (SrcReg == PPC::CR2LT || SrcReg == PPC::CR2GT ||
465 SrcReg == PPC::CR2EQ || SrcReg == PPC::CR2UN)
467 else if (SrcReg == PPC::CR3LT || SrcReg == PPC::CR3GT ||
468 SrcReg == PPC::CR3EQ || SrcReg == PPC::CR3UN)
470 else if (SrcReg == PPC::CR4LT || SrcReg == PPC::CR4GT ||
471 SrcReg == PPC::CR4EQ || SrcReg == PPC::CR4UN)
473 else if (SrcReg == PPC::CR5LT || SrcReg == PPC::CR5GT ||
474 SrcReg == PPC::CR5EQ || SrcReg == PPC::CR5UN)
476 else if (SrcReg == PPC::CR6LT || SrcReg == PPC::CR6GT ||
477 SrcReg == PPC::CR6EQ || SrcReg == PPC::CR6UN)
479 else if (SrcReg == PPC::CR7LT || SrcReg == PPC::CR7GT ||
480 SrcReg == PPC::CR7EQ || SrcReg == PPC::CR7UN)
483 return StoreRegToStackSlot(MF, Reg, isKill, FrameIdx,
484 PPC::CRRCRegisterClass, NewMIs);
486 } else if (RC == PPC::VRRCRegisterClass) {
487 // We don't have indexed addressing for vector loads. Emit:
491 // FIXME: We use R0 here, because it isn't available for RA.
492 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::ADDI), PPC::R0),
494 NewMIs.push_back(BuildMI(MF, DL, get(PPC::STVX))
495 .addReg(SrcReg, getKillRegState(isKill))
499 llvm_unreachable("Unknown regclass!");
506 PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
507 MachineBasicBlock::iterator MI,
508 unsigned SrcReg, bool isKill, int FrameIdx,
509 const TargetRegisterClass *RC) const {
510 MachineFunction &MF = *MBB.getParent();
511 SmallVector<MachineInstr*, 4> NewMIs;
513 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs)) {
514 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
515 FuncInfo->setSpillsCR();
518 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
519 MBB.insert(MI, NewMIs[i]);
523 PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL,
524 unsigned DestReg, int FrameIdx,
525 const TargetRegisterClass *RC,
526 SmallVectorImpl<MachineInstr*> &NewMIs)const{
527 if (RC == PPC::GPRCRegisterClass) {
528 if (DestReg != PPC::LR) {
529 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
530 DestReg), FrameIdx));
532 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
533 PPC::R11), FrameIdx));
534 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTLR)).addReg(PPC::R11));
536 } else if (RC == PPC::G8RCRegisterClass) {
537 if (DestReg != PPC::LR8) {
538 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg),
541 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD),
542 PPC::R11), FrameIdx));
543 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTLR8)).addReg(PPC::R11));
545 } else if (RC == PPC::F8RCRegisterClass) {
546 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg),
548 } else if (RC == PPC::F4RCRegisterClass) {
549 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg),
551 } else if (RC == PPC::CRRCRegisterClass) {
552 // FIXME: We need a scatch reg here. The trouble with using R0 is that
553 // it's possible for the stack frame to be so big the save location is
554 // out of range of immediate offsets, necessitating another register.
555 // We hack this on Darwin by reserving R2. It's probably broken on Linux
557 unsigned ScratchReg = TM.getSubtargetImpl()->isDarwinABI() ?
559 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
560 ScratchReg), FrameIdx));
562 // If the reloaded register isn't CR0, shift the bits right so that they are
563 // in the right CR's slot.
564 if (DestReg != PPC::CR0) {
565 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(DestReg)*4;
566 // rlwinm r11, r11, 32-ShiftBits, 0, 31.
567 NewMIs.push_back(BuildMI(MF, DL, get(PPC::RLWINM), ScratchReg)
568 .addReg(ScratchReg).addImm(32-ShiftBits).addImm(0)
572 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTCRF), DestReg)
573 .addReg(ScratchReg));
574 } else if (RC == PPC::CRBITRCRegisterClass) {
577 if (DestReg == PPC::CR0LT || DestReg == PPC::CR0GT ||
578 DestReg == PPC::CR0EQ || DestReg == PPC::CR0UN)
580 else if (DestReg == PPC::CR1LT || DestReg == PPC::CR1GT ||
581 DestReg == PPC::CR1EQ || DestReg == PPC::CR1UN)
583 else if (DestReg == PPC::CR2LT || DestReg == PPC::CR2GT ||
584 DestReg == PPC::CR2EQ || DestReg == PPC::CR2UN)
586 else if (DestReg == PPC::CR3LT || DestReg == PPC::CR3GT ||
587 DestReg == PPC::CR3EQ || DestReg == PPC::CR3UN)
589 else if (DestReg == PPC::CR4LT || DestReg == PPC::CR4GT ||
590 DestReg == PPC::CR4EQ || DestReg == PPC::CR4UN)
592 else if (DestReg == PPC::CR5LT || DestReg == PPC::CR5GT ||
593 DestReg == PPC::CR5EQ || DestReg == PPC::CR5UN)
595 else if (DestReg == PPC::CR6LT || DestReg == PPC::CR6GT ||
596 DestReg == PPC::CR6EQ || DestReg == PPC::CR6UN)
598 else if (DestReg == PPC::CR7LT || DestReg == PPC::CR7GT ||
599 DestReg == PPC::CR7EQ || DestReg == PPC::CR7UN)
602 return LoadRegFromStackSlot(MF, DL, Reg, FrameIdx,
603 PPC::CRRCRegisterClass, NewMIs);
605 } else if (RC == PPC::VRRCRegisterClass) {
606 // We don't have indexed addressing for vector loads. Emit:
610 // FIXME: We use R0 here, because it isn't available for RA.
611 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::ADDI), PPC::R0),
613 NewMIs.push_back(BuildMI(MF, DL, get(PPC::LVX),DestReg).addReg(PPC::R0)
616 llvm_unreachable("Unknown regclass!");
621 PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
622 MachineBasicBlock::iterator MI,
623 unsigned DestReg, int FrameIdx,
624 const TargetRegisterClass *RC) const {
625 MachineFunction &MF = *MBB.getParent();
626 SmallVector<MachineInstr*, 4> NewMIs;
627 DebugLoc DL = DebugLoc::getUnknownLoc();
628 if (MI != MBB.end()) DL = MI->getDebugLoc();
629 LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs);
630 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
631 MBB.insert(MI, NewMIs[i]);
634 /// foldMemoryOperand - PowerPC (like most RISC's) can only fold spills into
635 /// copy instructions, turning them into load/store instructions.
636 MachineInstr *PPCInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
638 const SmallVectorImpl<unsigned> &Ops,
639 int FrameIndex) const {
640 if (Ops.size() != 1) return NULL;
642 // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because
643 // it takes more than one instruction to store it.
644 unsigned Opc = MI->getOpcode();
645 unsigned OpNum = Ops[0];
647 MachineInstr *NewMI = NULL;
648 if ((Opc == PPC::OR &&
649 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
650 if (OpNum == 0) { // move -> store
651 unsigned InReg = MI->getOperand(1).getReg();
652 bool isKill = MI->getOperand(1).isKill();
653 bool isUndef = MI->getOperand(1).isUndef();
654 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::STW))
656 getKillRegState(isKill) |
657 getUndefRegState(isUndef)),
659 } else { // move -> load
660 unsigned OutReg = MI->getOperand(0).getReg();
661 bool isDead = MI->getOperand(0).isDead();
662 bool isUndef = MI->getOperand(0).isUndef();
663 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::LWZ))
666 getDeadRegState(isDead) |
667 getUndefRegState(isUndef)),
670 } else if ((Opc == PPC::OR8 &&
671 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
672 if (OpNum == 0) { // move -> store
673 unsigned InReg = MI->getOperand(1).getReg();
674 bool isKill = MI->getOperand(1).isKill();
675 bool isUndef = MI->getOperand(1).isUndef();
676 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::STD))
678 getKillRegState(isKill) |
679 getUndefRegState(isUndef)),
681 } else { // move -> load
682 unsigned OutReg = MI->getOperand(0).getReg();
683 bool isDead = MI->getOperand(0).isDead();
684 bool isUndef = MI->getOperand(0).isUndef();
685 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::LD))
688 getDeadRegState(isDead) |
689 getUndefRegState(isUndef)),
692 } else if (Opc == PPC::FMRD || Opc == PPC::FMRS || Opc == PPC::FMRSD) {
693 // The register may be F4RC or F8RC, and that determines the memory op.
694 unsigned OrigReg = MI->getOperand(OpNum).getReg();
695 // We cannot tell the register class from a physreg alone.
696 if (TargetRegisterInfo::isPhysicalRegister(OrigReg))
698 const TargetRegisterClass *RC = MF.getRegInfo().getRegClass(OrigReg);
699 const bool is64 = RC == PPC::F8RCRegisterClass;
701 if (OpNum == 0) { // move -> store
702 unsigned InReg = MI->getOperand(1).getReg();
703 bool isKill = MI->getOperand(1).isKill();
704 bool isUndef = MI->getOperand(1).isUndef();
705 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(),
706 get(is64 ? PPC::STFD : PPC::STFS))
708 getKillRegState(isKill) |
709 getUndefRegState(isUndef)),
711 } else { // move -> load
712 unsigned OutReg = MI->getOperand(0).getReg();
713 bool isDead = MI->getOperand(0).isDead();
714 bool isUndef = MI->getOperand(0).isUndef();
715 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(),
716 get(is64 ? PPC::LFD : PPC::LFS))
719 getDeadRegState(isDead) |
720 getUndefRegState(isUndef)),
728 bool PPCInstrInfo::canFoldMemoryOperand(const MachineInstr *MI,
729 const SmallVectorImpl<unsigned> &Ops) const {
730 if (Ops.size() != 1) return false;
732 // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because
733 // it takes more than one instruction to store it.
734 unsigned Opc = MI->getOpcode();
736 if ((Opc == PPC::OR &&
737 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
739 else if ((Opc == PPC::OR8 &&
740 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
742 else if (Opc == PPC::FMRD || Opc == PPC::FMRS)
750 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
751 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
752 // Leave the CR# the same, but invert the condition.
753 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
757 /// GetInstSize - Return the number of bytes of code the specified
758 /// instruction may be. This returns the maximum number of bytes.
760 unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
761 switch (MI->getOpcode()) {
762 case PPC::INLINEASM: { // Inline Asm: Variable size.
763 const MachineFunction *MF = MI->getParent()->getParent();
764 const char *AsmStr = MI->getOperand(0).getSymbolName();
765 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
772 return 4; // PowerPC instructions are all 4 bytes