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/Support/CommandLine.h"
23 #include "llvm/Target/TargetAsmInfo.h"
26 extern cl::opt
<bool> EnablePPC32RS
; // FIXME (64-bit): See PPCRegisterInfo.cpp.
27 extern cl::opt
<bool> EnablePPC64RS
; // FIXME (64-bit): See PPCRegisterInfo.cpp.
29 PPCInstrInfo::PPCInstrInfo(PPCTargetMachine
&tm
)
30 : TargetInstrInfoImpl(PPCInsts
, array_lengthof(PPCInsts
)), TM(tm
),
31 RI(*TM
.getSubtargetImpl(), *this) {}
33 bool PPCInstrInfo::isMoveInstr(const MachineInstr
& MI
,
36 unsigned& sourceSubIdx
,
37 unsigned& destSubIdx
) const {
38 sourceSubIdx
= destSubIdx
= 0; // No sub-registers.
40 unsigned oc
= MI
.getOpcode();
41 if (oc
== PPC::OR
|| oc
== PPC::OR8
|| oc
== PPC::VOR
||
42 oc
== PPC::OR4To8
|| oc
== PPC::OR8To4
) { // or r1, r2, r2
43 assert(MI
.getNumOperands() >= 3 &&
44 MI
.getOperand(0).isReg() &&
45 MI
.getOperand(1).isReg() &&
46 MI
.getOperand(2).isReg() &&
47 "invalid PPC OR instruction!");
48 if (MI
.getOperand(1).getReg() == MI
.getOperand(2).getReg()) {
49 sourceReg
= MI
.getOperand(1).getReg();
50 destReg
= MI
.getOperand(0).getReg();
53 } else if (oc
== PPC::ADDI
) { // addi r1, r2, 0
54 assert(MI
.getNumOperands() >= 3 &&
55 MI
.getOperand(0).isReg() &&
56 MI
.getOperand(2).isImm() &&
57 "invalid PPC ADDI instruction!");
58 if (MI
.getOperand(1).isReg() && MI
.getOperand(2).getImm() == 0) {
59 sourceReg
= MI
.getOperand(1).getReg();
60 destReg
= MI
.getOperand(0).getReg();
63 } else if (oc
== PPC::ORI
) { // ori r1, r2, 0
64 assert(MI
.getNumOperands() >= 3 &&
65 MI
.getOperand(0).isReg() &&
66 MI
.getOperand(1).isReg() &&
67 MI
.getOperand(2).isImm() &&
68 "invalid PPC ORI instruction!");
69 if (MI
.getOperand(2).getImm() == 0) {
70 sourceReg
= MI
.getOperand(1).getReg();
71 destReg
= MI
.getOperand(0).getReg();
74 } else if (oc
== PPC::FMRS
|| oc
== PPC::FMRD
||
75 oc
== PPC::FMRSD
) { // fmr r1, r2
76 assert(MI
.getNumOperands() >= 2 &&
77 MI
.getOperand(0).isReg() &&
78 MI
.getOperand(1).isReg() &&
79 "invalid PPC FMR instruction");
80 sourceReg
= MI
.getOperand(1).getReg();
81 destReg
= MI
.getOperand(0).getReg();
83 } else if (oc
== PPC::MCRF
) { // mcrf cr1, cr2
84 assert(MI
.getNumOperands() >= 2 &&
85 MI
.getOperand(0).isReg() &&
86 MI
.getOperand(1).isReg() &&
87 "invalid PPC MCRF instruction");
88 sourceReg
= MI
.getOperand(1).getReg();
89 destReg
= MI
.getOperand(0).getReg();
95 unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr
*MI
,
96 int &FrameIndex
) const {
97 switch (MI
->getOpcode()) {
103 if (MI
->getOperand(1).isImm() && !MI
->getOperand(1).getImm() &&
104 MI
->getOperand(2).isFI()) {
105 FrameIndex
= MI
->getOperand(2).getIndex();
106 return MI
->getOperand(0).getReg();
113 unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr
*MI
,
114 int &FrameIndex
) const {
115 switch (MI
->getOpcode()) {
121 if (MI
->getOperand(1).isImm() && !MI
->getOperand(1).getImm() &&
122 MI
->getOperand(2).isFI()) {
123 FrameIndex
= MI
->getOperand(2).getIndex();
124 return MI
->getOperand(0).getReg();
131 // commuteInstruction - We can commute rlwimi instructions, but only if the
132 // rotate amt is zero. We also have to munge the immediates a bit.
134 PPCInstrInfo::commuteInstruction(MachineInstr
*MI
, bool NewMI
) const {
135 MachineFunction
&MF
= *MI
->getParent()->getParent();
137 // Normal instructions can be commuted the obvious way.
138 if (MI
->getOpcode() != PPC::RLWIMI
)
139 return TargetInstrInfoImpl::commuteInstruction(MI
, NewMI
);
141 // Cannot commute if it has a non-zero rotate count.
142 if (MI
->getOperand(3).getImm() != 0)
145 // If we have a zero rotate count, we have:
147 // Op0 = (Op1 & ~M) | (Op2 & M)
149 // M = mask((ME+1)&31, (MB-1)&31)
150 // Op0 = (Op2 & ~M) | (Op1 & M)
153 unsigned Reg0
= MI
->getOperand(0).getReg();
154 unsigned Reg1
= MI
->getOperand(1).getReg();
155 unsigned Reg2
= MI
->getOperand(2).getReg();
156 bool Reg1IsKill
= MI
->getOperand(1).isKill();
157 bool Reg2IsKill
= MI
->getOperand(2).isKill();
158 bool ChangeReg0
= false;
159 // If machine instrs are no longer in two-address forms, update
160 // destination register as well.
162 // Must be two address instruction!
163 assert(MI
->getDesc().getOperandConstraint(0, TOI::TIED_TO
) &&
164 "Expecting a two-address instruction!");
170 unsigned MB
= MI
->getOperand(4).getImm();
171 unsigned ME
= MI
->getOperand(5).getImm();
174 // Create a new instruction.
175 unsigned Reg0
= ChangeReg0
? Reg2
: MI
->getOperand(0).getReg();
176 bool Reg0IsDead
= MI
->getOperand(0).isDead();
177 return BuildMI(MF
, MI
->getDebugLoc(), MI
->getDesc())
178 .addReg(Reg0
, true, false, false, Reg0IsDead
)
179 .addReg(Reg2
, false, false, Reg2IsKill
)
180 .addReg(Reg1
, false, false, Reg1IsKill
)
182 .addImm((MB
-1) & 31);
186 MI
->getOperand(0).setReg(Reg2
);
187 MI
->getOperand(2).setReg(Reg1
);
188 MI
->getOperand(1).setReg(Reg2
);
189 MI
->getOperand(2).setIsKill(Reg1IsKill
);
190 MI
->getOperand(1).setIsKill(Reg2IsKill
);
192 // Swap the mask around.
193 MI
->getOperand(4).setImm((ME
+1) & 31);
194 MI
->getOperand(5).setImm((MB
-1) & 31);
198 void PPCInstrInfo::insertNoop(MachineBasicBlock
&MBB
,
199 MachineBasicBlock::iterator MI
) const {
200 DebugLoc DL
= DebugLoc::getUnknownLoc();
201 if (MI
!= MBB
.end()) DL
= MI
->getDebugLoc();
203 BuildMI(MBB
, MI
, DL
, get(PPC::NOP
));
208 bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock
&MBB
,MachineBasicBlock
*&TBB
,
209 MachineBasicBlock
*&FBB
,
210 SmallVectorImpl
<MachineOperand
> &Cond
,
211 bool AllowModify
) const {
212 // If the block has no terminators, it just falls into the block after it.
213 MachineBasicBlock::iterator I
= MBB
.end();
214 if (I
== MBB
.begin() || !isUnpredicatedTerminator(--I
))
217 // Get the last instruction in the block.
218 MachineInstr
*LastInst
= I
;
220 // If there is only one terminator instruction, process it.
221 if (I
== MBB
.begin() || !isUnpredicatedTerminator(--I
)) {
222 if (LastInst
->getOpcode() == PPC::B
) {
223 TBB
= LastInst
->getOperand(0).getMBB();
225 } else if (LastInst
->getOpcode() == PPC::BCC
) {
226 // Block ends with fall-through condbranch.
227 TBB
= LastInst
->getOperand(2).getMBB();
228 Cond
.push_back(LastInst
->getOperand(0));
229 Cond
.push_back(LastInst
->getOperand(1));
232 // Otherwise, don't know what this is.
236 // Get the instruction before it if it's a terminator.
237 MachineInstr
*SecondLastInst
= I
;
239 // If there are three terminators, we don't know what sort of block this is.
240 if (SecondLastInst
&& I
!= MBB
.begin() &&
241 isUnpredicatedTerminator(--I
))
244 // If the block ends with PPC::B and PPC:BCC, handle it.
245 if (SecondLastInst
->getOpcode() == PPC::BCC
&&
246 LastInst
->getOpcode() == PPC::B
) {
247 TBB
= SecondLastInst
->getOperand(2).getMBB();
248 Cond
.push_back(SecondLastInst
->getOperand(0));
249 Cond
.push_back(SecondLastInst
->getOperand(1));
250 FBB
= LastInst
->getOperand(0).getMBB();
254 // If the block ends with two PPC:Bs, handle it. The second one is not
255 // executed, so remove it.
256 if (SecondLastInst
->getOpcode() == PPC::B
&&
257 LastInst
->getOpcode() == PPC::B
) {
258 TBB
= SecondLastInst
->getOperand(0).getMBB();
261 I
->eraseFromParent();
265 // Otherwise, can't handle this.
269 unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock
&MBB
) const {
270 MachineBasicBlock::iterator I
= MBB
.end();
271 if (I
== MBB
.begin()) return 0;
273 if (I
->getOpcode() != PPC::B
&& I
->getOpcode() != PPC::BCC
)
276 // Remove the branch.
277 I
->eraseFromParent();
281 if (I
== MBB
.begin()) return 1;
283 if (I
->getOpcode() != PPC::BCC
)
286 // Remove the branch.
287 I
->eraseFromParent();
292 PPCInstrInfo::InsertBranch(MachineBasicBlock
&MBB
, MachineBasicBlock
*TBB
,
293 MachineBasicBlock
*FBB
,
294 const SmallVectorImpl
<MachineOperand
> &Cond
) const {
295 // FIXME this should probably have a DebugLoc argument
296 DebugLoc dl
= DebugLoc::getUnknownLoc();
297 // Shouldn't be a fall through.
298 assert(TBB
&& "InsertBranch must not be told to insert a fallthrough");
299 assert((Cond
.size() == 2 || Cond
.size() == 0) &&
300 "PPC branch conditions have two components!");
304 if (Cond
.empty()) // Unconditional branch
305 BuildMI(&MBB
, dl
, get(PPC::B
)).addMBB(TBB
);
306 else // Conditional branch
307 BuildMI(&MBB
, dl
, get(PPC::BCC
))
308 .addImm(Cond
[0].getImm()).addReg(Cond
[1].getReg()).addMBB(TBB
);
312 // Two-way Conditional Branch.
313 BuildMI(&MBB
, dl
, get(PPC::BCC
))
314 .addImm(Cond
[0].getImm()).addReg(Cond
[1].getReg()).addMBB(TBB
);
315 BuildMI(&MBB
, dl
, get(PPC::B
)).addMBB(FBB
);
319 bool PPCInstrInfo::copyRegToReg(MachineBasicBlock
&MBB
,
320 MachineBasicBlock::iterator MI
,
321 unsigned DestReg
, unsigned SrcReg
,
322 const TargetRegisterClass
*DestRC
,
323 const TargetRegisterClass
*SrcRC
) const {
324 if (DestRC
!= SrcRC
) {
325 // Not yet supported!
329 DebugLoc DL
= DebugLoc::getUnknownLoc();
330 if (MI
!= MBB
.end()) DL
= MI
->getDebugLoc();
332 if (DestRC
== PPC::GPRCRegisterClass
) {
333 BuildMI(MBB
, MI
, DL
, get(PPC::OR
), DestReg
).addReg(SrcReg
).addReg(SrcReg
);
334 } else if (DestRC
== PPC::G8RCRegisterClass
) {
335 BuildMI(MBB
, MI
, DL
, get(PPC::OR8
), DestReg
).addReg(SrcReg
).addReg(SrcReg
);
336 } else if (DestRC
== PPC::F4RCRegisterClass
) {
337 BuildMI(MBB
, MI
, DL
, get(PPC::FMRS
), DestReg
).addReg(SrcReg
);
338 } else if (DestRC
== PPC::F8RCRegisterClass
) {
339 BuildMI(MBB
, MI
, DL
, get(PPC::FMRD
), DestReg
).addReg(SrcReg
);
340 } else if (DestRC
== PPC::CRRCRegisterClass
) {
341 BuildMI(MBB
, MI
, DL
, get(PPC::MCRF
), DestReg
).addReg(SrcReg
);
342 } else if (DestRC
== PPC::VRRCRegisterClass
) {
343 BuildMI(MBB
, MI
, DL
, get(PPC::VOR
), DestReg
).addReg(SrcReg
).addReg(SrcReg
);
344 } else if (DestRC
== PPC::CRBITRCRegisterClass
) {
345 BuildMI(MBB
, MI
, DL
, get(PPC::CROR
), DestReg
).addReg(SrcReg
).addReg(SrcReg
);
347 // Attempt to copy register that is not GPR or FPR
355 PPCInstrInfo::StoreRegToStackSlot(MachineFunction
&MF
,
356 unsigned SrcReg
, bool isKill
,
358 const TargetRegisterClass
*RC
,
359 SmallVectorImpl
<MachineInstr
*> &NewMIs
) const{
360 DebugLoc DL
= DebugLoc::getUnknownLoc();
361 if (RC
== PPC::GPRCRegisterClass
) {
362 if (SrcReg
!= PPC::LR
) {
363 NewMIs
.push_back(addFrameReference(BuildMI(MF
, DL
, get(PPC::STW
))
364 .addReg(SrcReg
, false, false, isKill
),
367 // FIXME: this spills LR immediately to memory in one step. To do this,
368 // we use R11, which we know cannot be used in the prolog/epilog. This is
370 NewMIs
.push_back(BuildMI(MF
, DL
, get(PPC::MFLR
), PPC::R11
));
371 NewMIs
.push_back(addFrameReference(BuildMI(MF
, DL
, get(PPC::STW
))
372 .addReg(PPC::R11
, false, false, isKill
),
375 } else if (RC
== PPC::G8RCRegisterClass
) {
376 if (SrcReg
!= PPC::LR8
) {
377 NewMIs
.push_back(addFrameReference(BuildMI(MF
, DL
, get(PPC::STD
))
378 .addReg(SrcReg
, false, false, isKill
), FrameIdx
));
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::MFLR8
), PPC::X11
));
384 NewMIs
.push_back(addFrameReference(BuildMI(MF
, DL
, get(PPC::STD
))
385 .addReg(PPC::X11
, false, false, isKill
), FrameIdx
));
387 } else if (RC
== PPC::F8RCRegisterClass
) {
388 NewMIs
.push_back(addFrameReference(BuildMI(MF
, DL
, get(PPC::STFD
))
389 .addReg(SrcReg
, false, false, isKill
), FrameIdx
));
390 } else if (RC
== PPC::F4RCRegisterClass
) {
391 NewMIs
.push_back(addFrameReference(BuildMI(MF
, DL
, get(PPC::STFS
))
392 .addReg(SrcReg
, false, false, isKill
), FrameIdx
));
393 } else if (RC
== PPC::CRRCRegisterClass
) {
394 if ((EnablePPC32RS
&& !TM
.getSubtargetImpl()->isPPC64()) ||
395 (EnablePPC64RS
&& TM
.getSubtargetImpl()->isPPC64())) {
396 // FIXME (64-bit): Enable
397 NewMIs
.push_back(addFrameReference(BuildMI(MF
, DL
, get(PPC::SPILL_CR
))
398 .addReg(SrcReg
, false, false, isKill
),
402 // FIXME: We use R0 here, because it isn't available for RA. We need to
403 // store the CR in the low 4-bits of the saved value. First, issue a MFCR
404 // to save all of the CRBits.
405 NewMIs
.push_back(BuildMI(MF
, DL
, get(PPC::MFCR
), PPC::R0
));
407 // If the saved register wasn't CR0, shift the bits left so that they are
409 if (SrcReg
!= PPC::CR0
) {
410 unsigned ShiftBits
= PPCRegisterInfo::getRegisterNumbering(SrcReg
)*4;
411 // rlwinm r0, r0, ShiftBits, 0, 31.
412 NewMIs
.push_back(BuildMI(MF
, DL
, get(PPC::RLWINM
), PPC::R0
)
413 .addReg(PPC::R0
).addImm(ShiftBits
).addImm(0).addImm(31));
416 NewMIs
.push_back(addFrameReference(BuildMI(MF
, DL
, get(PPC::STW
))
417 .addReg(PPC::R0
, false, false, isKill
),
420 } else if (RC
== PPC::CRBITRCRegisterClass
) {
421 // FIXME: We use CRi here because there is no mtcrf on a bit. Since the
422 // backend currently only uses CR1EQ as an individual bit, this should
423 // not cause any bug. If we need other uses of CR bits, the following
424 // code may be invalid.
426 if (SrcReg
>= PPC::CR0LT
|| SrcReg
<= PPC::CR0UN
)
428 else if (SrcReg
>= PPC::CR1LT
|| SrcReg
<= PPC::CR1UN
)
430 else if (SrcReg
>= PPC::CR2LT
|| SrcReg
<= PPC::CR2UN
)
432 else if (SrcReg
>= PPC::CR3LT
|| SrcReg
<= PPC::CR3UN
)
434 else if (SrcReg
>= PPC::CR4LT
|| SrcReg
<= PPC::CR4UN
)
436 else if (SrcReg
>= PPC::CR5LT
|| SrcReg
<= PPC::CR5UN
)
438 else if (SrcReg
>= PPC::CR6LT
|| SrcReg
<= PPC::CR6UN
)
440 else if (SrcReg
>= PPC::CR7LT
|| SrcReg
<= PPC::CR7UN
)
443 return StoreRegToStackSlot(MF
, Reg
, isKill
, FrameIdx
,
444 PPC::CRRCRegisterClass
, NewMIs
);
446 } else if (RC
== PPC::VRRCRegisterClass
) {
447 // We don't have indexed addressing for vector loads. Emit:
451 // FIXME: We use R0 here, because it isn't available for RA.
452 NewMIs
.push_back(addFrameReference(BuildMI(MF
, DL
, get(PPC::ADDI
), PPC::R0
),
454 NewMIs
.push_back(BuildMI(MF
, DL
, get(PPC::STVX
))
455 .addReg(SrcReg
, false, false, isKill
).addReg(PPC::R0
).addReg(PPC::R0
));
457 assert(0 && "Unknown regclass!");
465 PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock
&MBB
,
466 MachineBasicBlock::iterator MI
,
467 unsigned SrcReg
, bool isKill
, int FrameIdx
,
468 const TargetRegisterClass
*RC
) const {
469 MachineFunction
&MF
= *MBB
.getParent();
470 SmallVector
<MachineInstr
*, 4> NewMIs
;
472 if (StoreRegToStackSlot(MF
, SrcReg
, isKill
, FrameIdx
, RC
, NewMIs
)) {
473 PPCFunctionInfo
*FuncInfo
= MF
.getInfo
<PPCFunctionInfo
>();
474 FuncInfo
->setSpillsCR();
477 for (unsigned i
= 0, e
= NewMIs
.size(); i
!= e
; ++i
)
478 MBB
.insert(MI
, NewMIs
[i
]);
481 void PPCInstrInfo::storeRegToAddr(MachineFunction
&MF
, unsigned SrcReg
,
483 SmallVectorImpl
<MachineOperand
> &Addr
,
484 const TargetRegisterClass
*RC
,
485 SmallVectorImpl
<MachineInstr
*> &NewMIs
) const{
486 if (Addr
[0].isFI()) {
487 if (StoreRegToStackSlot(MF
, SrcReg
, isKill
,
488 Addr
[0].getIndex(), RC
, NewMIs
)) {
489 PPCFunctionInfo
*FuncInfo
= MF
.getInfo
<PPCFunctionInfo
>();
490 FuncInfo
->setSpillsCR();
496 DebugLoc DL
= DebugLoc::getUnknownLoc();
498 if (RC
== PPC::GPRCRegisterClass
) {
500 } else if (RC
== PPC::G8RCRegisterClass
) {
502 } else if (RC
== PPC::F8RCRegisterClass
) {
504 } else if (RC
== PPC::F4RCRegisterClass
) {
506 } else if (RC
== PPC::VRRCRegisterClass
) {
509 assert(0 && "Unknown regclass!");
512 MachineInstrBuilder MIB
= BuildMI(MF
, DL
, get(Opc
))
513 .addReg(SrcReg
, false, false, isKill
);
514 for (unsigned i
= 0, e
= Addr
.size(); i
!= e
; ++i
)
515 MIB
.addOperand(Addr
[i
]);
516 NewMIs
.push_back(MIB
);
521 PPCInstrInfo::LoadRegFromStackSlot(MachineFunction
&MF
, DebugLoc DL
,
522 unsigned DestReg
, int FrameIdx
,
523 const TargetRegisterClass
*RC
,
524 SmallVectorImpl
<MachineInstr
*> &NewMIs
)const{
525 if (RC
== PPC::GPRCRegisterClass
) {
526 if (DestReg
!= PPC::LR
) {
527 NewMIs
.push_back(addFrameReference(BuildMI(MF
, DL
, get(PPC::LWZ
),
528 DestReg
), FrameIdx
));
530 NewMIs
.push_back(addFrameReference(BuildMI(MF
, DL
, get(PPC::LWZ
),
531 PPC::R11
), FrameIdx
));
532 NewMIs
.push_back(BuildMI(MF
, DL
, get(PPC::MTLR
)).addReg(PPC::R11
));
534 } else if (RC
== PPC::G8RCRegisterClass
) {
535 if (DestReg
!= PPC::LR8
) {
536 NewMIs
.push_back(addFrameReference(BuildMI(MF
, DL
, get(PPC::LD
), DestReg
),
539 NewMIs
.push_back(addFrameReference(BuildMI(MF
, DL
, get(PPC::LD
),
540 PPC::R11
), FrameIdx
));
541 NewMIs
.push_back(BuildMI(MF
, DL
, get(PPC::MTLR8
)).addReg(PPC::R11
));
543 } else if (RC
== PPC::F8RCRegisterClass
) {
544 NewMIs
.push_back(addFrameReference(BuildMI(MF
, DL
, get(PPC::LFD
), DestReg
),
546 } else if (RC
== PPC::F4RCRegisterClass
) {
547 NewMIs
.push_back(addFrameReference(BuildMI(MF
, DL
, get(PPC::LFS
), DestReg
),
549 } else if (RC
== PPC::CRRCRegisterClass
) {
550 // FIXME: We use R0 here, because it isn't available for RA.
551 NewMIs
.push_back(addFrameReference(BuildMI(MF
, DL
, get(PPC::LWZ
), PPC::R0
),
554 // If the reloaded register isn't CR0, shift the bits right so that they are
555 // in the right CR's slot.
556 if (DestReg
!= PPC::CR0
) {
557 unsigned ShiftBits
= PPCRegisterInfo::getRegisterNumbering(DestReg
)*4;
558 // rlwinm r11, r11, 32-ShiftBits, 0, 31.
559 NewMIs
.push_back(BuildMI(MF
, DL
, get(PPC::RLWINM
), PPC::R0
)
560 .addReg(PPC::R0
).addImm(32-ShiftBits
).addImm(0).addImm(31));
563 NewMIs
.push_back(BuildMI(MF
, DL
, get(PPC::MTCRF
), DestReg
).addReg(PPC::R0
));
564 } else if (RC
== PPC::CRBITRCRegisterClass
) {
567 if (DestReg
>= PPC::CR0LT
|| DestReg
<= PPC::CR0UN
)
569 else if (DestReg
>= PPC::CR1LT
|| DestReg
<= PPC::CR1UN
)
571 else if (DestReg
>= PPC::CR2LT
|| DestReg
<= PPC::CR2UN
)
573 else if (DestReg
>= PPC::CR3LT
|| DestReg
<= PPC::CR3UN
)
575 else if (DestReg
>= PPC::CR4LT
|| DestReg
<= PPC::CR4UN
)
577 else if (DestReg
>= PPC::CR5LT
|| DestReg
<= PPC::CR5UN
)
579 else if (DestReg
>= PPC::CR6LT
|| DestReg
<= PPC::CR6UN
)
581 else if (DestReg
>= PPC::CR7LT
|| DestReg
<= PPC::CR7UN
)
584 return LoadRegFromStackSlot(MF
, DL
, Reg
, FrameIdx
,
585 PPC::CRRCRegisterClass
, NewMIs
);
587 } else if (RC
== PPC::VRRCRegisterClass
) {
588 // We don't have indexed addressing for vector loads. Emit:
592 // FIXME: We use R0 here, because it isn't available for RA.
593 NewMIs
.push_back(addFrameReference(BuildMI(MF
, DL
, get(PPC::ADDI
), PPC::R0
),
595 NewMIs
.push_back(BuildMI(MF
, DL
, get(PPC::LVX
),DestReg
).addReg(PPC::R0
)
598 assert(0 && "Unknown regclass!");
604 PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock
&MBB
,
605 MachineBasicBlock::iterator MI
,
606 unsigned DestReg
, int FrameIdx
,
607 const TargetRegisterClass
*RC
) const {
608 MachineFunction
&MF
= *MBB
.getParent();
609 SmallVector
<MachineInstr
*, 4> NewMIs
;
610 DebugLoc DL
= DebugLoc::getUnknownLoc();
611 if (MI
!= MBB
.end()) DL
= MI
->getDebugLoc();
612 LoadRegFromStackSlot(MF
, DL
, DestReg
, FrameIdx
, RC
, NewMIs
);
613 for (unsigned i
= 0, e
= NewMIs
.size(); i
!= e
; ++i
)
614 MBB
.insert(MI
, NewMIs
[i
]);
617 void PPCInstrInfo::loadRegFromAddr(MachineFunction
&MF
, unsigned DestReg
,
618 SmallVectorImpl
<MachineOperand
> &Addr
,
619 const TargetRegisterClass
*RC
,
620 SmallVectorImpl
<MachineInstr
*> &NewMIs
)const{
621 if (Addr
[0].isFI()) {
622 LoadRegFromStackSlot(MF
, DebugLoc::getUnknownLoc(),
623 DestReg
, Addr
[0].getIndex(), RC
, NewMIs
);
628 if (RC
== PPC::GPRCRegisterClass
) {
629 assert(DestReg
!= PPC::LR
&& "Can't handle this yet!");
631 } else if (RC
== PPC::G8RCRegisterClass
) {
632 assert(DestReg
!= PPC::LR8
&& "Can't handle this yet!");
634 } else if (RC
== PPC::F8RCRegisterClass
) {
636 } else if (RC
== PPC::F4RCRegisterClass
) {
638 } else if (RC
== PPC::VRRCRegisterClass
) {
641 assert(0 && "Unknown regclass!");
644 DebugLoc DL
= DebugLoc::getUnknownLoc();
645 MachineInstrBuilder MIB
= BuildMI(MF
, DL
, get(Opc
), DestReg
);
646 for (unsigned i
= 0, e
= Addr
.size(); i
!= e
; ++i
)
647 MIB
.addOperand(Addr
[i
]);
648 NewMIs
.push_back(MIB
);
652 /// foldMemoryOperand - PowerPC (like most RISC's) can only fold spills into
653 /// copy instructions, turning them into load/store instructions.
654 MachineInstr
*PPCInstrInfo::foldMemoryOperandImpl(MachineFunction
&MF
,
656 const SmallVectorImpl
<unsigned> &Ops
,
657 int FrameIndex
) const {
658 if (Ops
.size() != 1) return NULL
;
660 // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because
661 // it takes more than one instruction to store it.
662 unsigned Opc
= MI
->getOpcode();
663 unsigned OpNum
= Ops
[0];
665 MachineInstr
*NewMI
= NULL
;
666 if ((Opc
== PPC::OR
&&
667 MI
->getOperand(1).getReg() == MI
->getOperand(2).getReg())) {
668 if (OpNum
== 0) { // move -> store
669 unsigned InReg
= MI
->getOperand(1).getReg();
670 bool isKill
= MI
->getOperand(1).isKill();
671 NewMI
= addFrameReference(BuildMI(MF
, MI
->getDebugLoc(), get(PPC::STW
))
672 .addReg(InReg
, false, false, isKill
),
674 } else { // move -> load
675 unsigned OutReg
= MI
->getOperand(0).getReg();
676 bool isDead
= MI
->getOperand(0).isDead();
677 NewMI
= addFrameReference(BuildMI(MF
, MI
->getDebugLoc(), get(PPC::LWZ
))
678 .addReg(OutReg
, true, false, false, isDead
),
681 } else if ((Opc
== PPC::OR8
&&
682 MI
->getOperand(1).getReg() == MI
->getOperand(2).getReg())) {
683 if (OpNum
== 0) { // move -> store
684 unsigned InReg
= MI
->getOperand(1).getReg();
685 bool isKill
= MI
->getOperand(1).isKill();
686 NewMI
= addFrameReference(BuildMI(MF
, MI
->getDebugLoc(), get(PPC::STD
))
687 .addReg(InReg
, false, false, isKill
),
689 } else { // move -> load
690 unsigned OutReg
= MI
->getOperand(0).getReg();
691 bool isDead
= MI
->getOperand(0).isDead();
692 NewMI
= addFrameReference(BuildMI(MF
, MI
->getDebugLoc(), get(PPC::LD
))
693 .addReg(OutReg
, true, false, false, isDead
),
696 } else if (Opc
== PPC::FMRD
) {
697 if (OpNum
== 0) { // move -> store
698 unsigned InReg
= MI
->getOperand(1).getReg();
699 bool isKill
= MI
->getOperand(1).isKill();
700 NewMI
= addFrameReference(BuildMI(MF
, MI
->getDebugLoc(), get(PPC::STFD
))
701 .addReg(InReg
, false, false, isKill
),
703 } else { // move -> load
704 unsigned OutReg
= MI
->getOperand(0).getReg();
705 bool isDead
= MI
->getOperand(0).isDead();
706 NewMI
= addFrameReference(BuildMI(MF
, MI
->getDebugLoc(), get(PPC::LFD
))
707 .addReg(OutReg
, true, false, false, isDead
),
710 } else if (Opc
== PPC::FMRS
) {
711 if (OpNum
== 0) { // move -> store
712 unsigned InReg
= MI
->getOperand(1).getReg();
713 bool isKill
= MI
->getOperand(1).isKill();
714 NewMI
= addFrameReference(BuildMI(MF
, MI
->getDebugLoc(), get(PPC::STFS
))
715 .addReg(InReg
, false, false, isKill
),
717 } else { // move -> load
718 unsigned OutReg
= MI
->getOperand(0).getReg();
719 bool isDead
= MI
->getOperand(0).isDead();
720 NewMI
= addFrameReference(BuildMI(MF
, MI
->getDebugLoc(), get(PPC::LFS
))
721 .addReg(OutReg
, true, false, false, isDead
),
729 bool PPCInstrInfo::canFoldMemoryOperand(const MachineInstr
*MI
,
730 const SmallVectorImpl
<unsigned> &Ops
) const {
731 if (Ops
.size() != 1) return false;
733 // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because
734 // it takes more than one instruction to store it.
735 unsigned Opc
= MI
->getOpcode();
737 if ((Opc
== PPC::OR
&&
738 MI
->getOperand(1).getReg() == MI
->getOperand(2).getReg()))
740 else if ((Opc
== PPC::OR8
&&
741 MI
->getOperand(1).getReg() == MI
->getOperand(2).getReg()))
743 else if (Opc
== PPC::FMRD
|| Opc
== PPC::FMRS
)
750 bool PPCInstrInfo::BlockHasNoFallThrough(const MachineBasicBlock
&MBB
) const {
751 if (MBB
.empty()) return false;
753 switch (MBB
.back().getOpcode()) {
754 case PPC::BLR
: // Return.
755 case PPC::B
: // Uncond branch.
756 case PPC::BCTR
: // Indirect branch.
758 default: return false;
763 ReverseBranchCondition(SmallVectorImpl
<MachineOperand
> &Cond
) const {
764 assert(Cond
.size() == 2 && "Invalid PPC branch opcode!");
765 // Leave the CR# the same, but invert the condition.
766 Cond
[0].setImm(PPC::InvertPredicate((PPC::Predicate
)Cond
[0].getImm()));
770 /// GetInstSize - Return the number of bytes of code the specified
771 /// instruction may be. This returns the maximum number of bytes.
773 unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr
*MI
) const {
774 switch (MI
->getOpcode()) {
775 case PPC::INLINEASM
: { // Inline Asm: Variable size.
776 const MachineFunction
*MF
= MI
->getParent()->getParent();
777 const char *AsmStr
= MI
->getOperand(0).getSymbolName();
778 return MF
->getTarget().getTargetAsmInfo()->getInlineAsmLength(AsmStr
);
785 return 4; // PowerPC instructions are all 4 bytes