blob: afaf8da59c1ab5161e2d34fc99f8ef091a003bcd [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- PPCInstrInfo.cpp - PowerPC32 Instruction Information -----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the PowerPC implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "PPCInstrInfo.h"
Owen Anderson81875432008-01-01 21:11:32 +000015#include "PPCInstrBuilder.h"
Bill Wendlinga1877c52008-03-03 22:19:16 +000016#include "PPCMachineFunctionInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000017#include "PPCPredicates.h"
18#include "PPCGenInstrInfo.inc"
19#include "PPCTargetMachine.h"
Owen Anderson1636de92007-09-07 04:06:50 +000020#include "llvm/ADT/STLExtras.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include "llvm/CodeGen/MachineInstrBuilder.h"
22using namespace llvm;
23
24PPCInstrInfo::PPCInstrInfo(PPCTargetMachine &tm)
Chris Lattnerd2fd6db2008-01-01 01:03:04 +000025 : TargetInstrInfoImpl(PPCInsts, array_lengthof(PPCInsts)), TM(tm),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026 RI(*TM.getSubtargetImpl(), *this) {}
27
28/// getPointerRegClass - Return the register class to use to hold pointers.
29/// This is used for addressing modes.
30const TargetRegisterClass *PPCInstrInfo::getPointerRegClass() const {
31 if (TM.getSubtargetImpl()->isPPC64())
32 return &PPC::G8RCRegClass;
33 else
34 return &PPC::GPRCRegClass;
35}
36
37
38bool PPCInstrInfo::isMoveInstr(const MachineInstr& MI,
39 unsigned& sourceReg,
40 unsigned& destReg) const {
Chris Lattner99aa3372008-01-07 02:48:55 +000041 unsigned oc = MI.getOpcode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042 if (oc == PPC::OR || oc == PPC::OR8 || oc == PPC::VOR ||
43 oc == PPC::OR4To8 || oc == PPC::OR8To4) { // or r1, r2, r2
44 assert(MI.getNumOperands() >= 3 &&
45 MI.getOperand(0).isRegister() &&
46 MI.getOperand(1).isRegister() &&
47 MI.getOperand(2).isRegister() &&
48 "invalid PPC OR instruction!");
49 if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
50 sourceReg = MI.getOperand(1).getReg();
51 destReg = MI.getOperand(0).getReg();
52 return true;
53 }
54 } else if (oc == PPC::ADDI) { // addi r1, r2, 0
55 assert(MI.getNumOperands() >= 3 &&
56 MI.getOperand(0).isRegister() &&
57 MI.getOperand(2).isImmediate() &&
58 "invalid PPC ADDI instruction!");
Chris Lattnera96056a2007-12-30 20:49:49 +000059 if (MI.getOperand(1).isRegister() && MI.getOperand(2).getImm() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060 sourceReg = MI.getOperand(1).getReg();
61 destReg = MI.getOperand(0).getReg();
62 return true;
63 }
64 } else if (oc == PPC::ORI) { // ori r1, r2, 0
65 assert(MI.getNumOperands() >= 3 &&
66 MI.getOperand(0).isRegister() &&
67 MI.getOperand(1).isRegister() &&
68 MI.getOperand(2).isImmediate() &&
69 "invalid PPC ORI instruction!");
Chris Lattnera96056a2007-12-30 20:49:49 +000070 if (MI.getOperand(2).getImm() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071 sourceReg = MI.getOperand(1).getReg();
72 destReg = MI.getOperand(0).getReg();
73 return true;
74 }
75 } else if (oc == PPC::FMRS || oc == PPC::FMRD ||
76 oc == PPC::FMRSD) { // fmr r1, r2
77 assert(MI.getNumOperands() >= 2 &&
78 MI.getOperand(0).isRegister() &&
79 MI.getOperand(1).isRegister() &&
80 "invalid PPC FMR instruction");
81 sourceReg = MI.getOperand(1).getReg();
82 destReg = MI.getOperand(0).getReg();
83 return true;
84 } else if (oc == PPC::MCRF) { // mcrf cr1, cr2
85 assert(MI.getNumOperands() >= 2 &&
86 MI.getOperand(0).isRegister() &&
87 MI.getOperand(1).isRegister() &&
88 "invalid PPC MCRF instruction");
89 sourceReg = MI.getOperand(1).getReg();
90 destReg = MI.getOperand(0).getReg();
91 return true;
92 }
93 return false;
94}
95
96unsigned PPCInstrInfo::isLoadFromStackSlot(MachineInstr *MI,
97 int &FrameIndex) const {
98 switch (MI->getOpcode()) {
99 default: break;
100 case PPC::LD:
101 case PPC::LWZ:
102 case PPC::LFS:
103 case PPC::LFD:
Chris Lattner6017d482007-12-30 23:10:15 +0000104 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
105 MI->getOperand(2).isFI()) {
106 FrameIndex = MI->getOperand(2).getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107 return MI->getOperand(0).getReg();
108 }
109 break;
110 }
111 return 0;
112}
113
114unsigned PPCInstrInfo::isStoreToStackSlot(MachineInstr *MI,
115 int &FrameIndex) const {
116 switch (MI->getOpcode()) {
117 default: break;
118 case PPC::STD:
119 case PPC::STW:
120 case PPC::STFS:
121 case PPC::STFD:
Chris Lattner6017d482007-12-30 23:10:15 +0000122 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
123 MI->getOperand(2).isFI()) {
124 FrameIndex = MI->getOperand(2).getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125 return MI->getOperand(0).getReg();
126 }
127 break;
128 }
129 return 0;
130}
131
132// commuteInstruction - We can commute rlwimi instructions, but only if the
133// rotate amt is zero. We also have to munge the immediates a bit.
134MachineInstr *PPCInstrInfo::commuteInstruction(MachineInstr *MI) const {
135 // Normal instructions can be commuted the obvious way.
136 if (MI->getOpcode() != PPC::RLWIMI)
Chris Lattner6ca3a8e2008-01-01 01:05:34 +0000137 return TargetInstrInfoImpl::commuteInstruction(MI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138
139 // Cannot commute if it has a non-zero rotate count.
Chris Lattnera96056a2007-12-30 20:49:49 +0000140 if (MI->getOperand(3).getImm() != 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141 return 0;
142
143 // If we have a zero rotate count, we have:
144 // M = mask(MB,ME)
145 // Op0 = (Op1 & ~M) | (Op2 & M)
146 // Change this to:
147 // M = mask((ME+1)&31, (MB-1)&31)
148 // Op0 = (Op2 & ~M) | (Op1 & M)
149
150 // Swap op1/op2
Evan Chengb554e532008-02-13 02:46:49 +0000151 unsigned Reg0 = MI->getOperand(0).getReg();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000152 unsigned Reg1 = MI->getOperand(1).getReg();
153 unsigned Reg2 = MI->getOperand(2).getReg();
154 bool Reg1IsKill = MI->getOperand(1).isKill();
155 bool Reg2IsKill = MI->getOperand(2).isKill();
Evan Chengb554e532008-02-13 02:46:49 +0000156 // If machine instrs are no longer in two-address forms, update
157 // destination register as well.
158 if (Reg0 == Reg1) {
159 // Must be two address instruction!
160 assert(MI->getDesc().getOperandConstraint(0, TOI::TIED_TO) &&
161 "Expecting a two-address instruction!");
162 MI->getOperand(0).setReg(Reg2);
163 Reg2IsKill = false;
164 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 MI->getOperand(2).setReg(Reg1);
166 MI->getOperand(1).setReg(Reg2);
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000167 MI->getOperand(2).setIsKill(Reg1IsKill);
168 MI->getOperand(1).setIsKill(Reg2IsKill);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169
170 // Swap the mask around.
Chris Lattnera96056a2007-12-30 20:49:49 +0000171 unsigned MB = MI->getOperand(4).getImm();
172 unsigned ME = MI->getOperand(5).getImm();
173 MI->getOperand(4).setImm((ME+1) & 31);
174 MI->getOperand(5).setImm((MB-1) & 31);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175 return MI;
176}
177
178void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
179 MachineBasicBlock::iterator MI) const {
180 BuildMI(MBB, MI, get(PPC::NOP));
181}
182
183
184// Branch analysis.
185bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
186 MachineBasicBlock *&FBB,
187 std::vector<MachineOperand> &Cond) const {
188 // If the block has no terminators, it just falls into the block after it.
189 MachineBasicBlock::iterator I = MBB.end();
190 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
191 return false;
192
193 // Get the last instruction in the block.
194 MachineInstr *LastInst = I;
195
196 // If there is only one terminator instruction, process it.
197 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
198 if (LastInst->getOpcode() == PPC::B) {
Chris Lattner6017d482007-12-30 23:10:15 +0000199 TBB = LastInst->getOperand(0).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200 return false;
201 } else if (LastInst->getOpcode() == PPC::BCC) {
202 // Block ends with fall-through condbranch.
Chris Lattner6017d482007-12-30 23:10:15 +0000203 TBB = LastInst->getOperand(2).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000204 Cond.push_back(LastInst->getOperand(0));
205 Cond.push_back(LastInst->getOperand(1));
206 return false;
207 }
208 // Otherwise, don't know what this is.
209 return true;
210 }
211
212 // Get the instruction before it if it's a terminator.
213 MachineInstr *SecondLastInst = I;
214
215 // If there are three terminators, we don't know what sort of block this is.
216 if (SecondLastInst && I != MBB.begin() &&
217 isUnpredicatedTerminator(--I))
218 return true;
219
220 // If the block ends with PPC::B and PPC:BCC, handle it.
221 if (SecondLastInst->getOpcode() == PPC::BCC &&
222 LastInst->getOpcode() == PPC::B) {
Chris Lattner6017d482007-12-30 23:10:15 +0000223 TBB = SecondLastInst->getOperand(2).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000224 Cond.push_back(SecondLastInst->getOperand(0));
225 Cond.push_back(SecondLastInst->getOperand(1));
Chris Lattner6017d482007-12-30 23:10:15 +0000226 FBB = LastInst->getOperand(0).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000227 return false;
228 }
229
230 // If the block ends with two PPC:Bs, handle it. The second one is not
231 // executed, so remove it.
232 if (SecondLastInst->getOpcode() == PPC::B &&
233 LastInst->getOpcode() == PPC::B) {
Chris Lattner6017d482007-12-30 23:10:15 +0000234 TBB = SecondLastInst->getOperand(0).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235 I = LastInst;
236 I->eraseFromParent();
237 return false;
238 }
239
240 // Otherwise, can't handle this.
241 return true;
242}
243
244unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
245 MachineBasicBlock::iterator I = MBB.end();
246 if (I == MBB.begin()) return 0;
247 --I;
248 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC)
249 return 0;
250
251 // Remove the branch.
252 I->eraseFromParent();
253
254 I = MBB.end();
255
256 if (I == MBB.begin()) return 1;
257 --I;
258 if (I->getOpcode() != PPC::BCC)
259 return 1;
260
261 // Remove the branch.
262 I->eraseFromParent();
263 return 2;
264}
265
266unsigned
267PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
268 MachineBasicBlock *FBB,
269 const std::vector<MachineOperand> &Cond) const {
270 // Shouldn't be a fall through.
271 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
272 assert((Cond.size() == 2 || Cond.size() == 0) &&
273 "PPC branch conditions have two components!");
274
275 // One-way branch.
276 if (FBB == 0) {
277 if (Cond.empty()) // Unconditional branch
278 BuildMI(&MBB, get(PPC::B)).addMBB(TBB);
279 else // Conditional branch
280 BuildMI(&MBB, get(PPC::BCC))
281 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
282 return 1;
283 }
284
285 // Two-way Conditional Branch.
286 BuildMI(&MBB, get(PPC::BCC))
287 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
288 BuildMI(&MBB, get(PPC::B)).addMBB(FBB);
289 return 2;
290}
291
Owen Anderson8f2c8932007-12-31 06:32:00 +0000292void PPCInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
293 MachineBasicBlock::iterator MI,
294 unsigned DestReg, unsigned SrcReg,
295 const TargetRegisterClass *DestRC,
296 const TargetRegisterClass *SrcRC) const {
297 if (DestRC != SrcRC) {
298 cerr << "Not yet supported!";
299 abort();
300 }
301
302 if (DestRC == PPC::GPRCRegisterClass) {
303 BuildMI(MBB, MI, get(PPC::OR), DestReg).addReg(SrcReg).addReg(SrcReg);
304 } else if (DestRC == PPC::G8RCRegisterClass) {
305 BuildMI(MBB, MI, get(PPC::OR8), DestReg).addReg(SrcReg).addReg(SrcReg);
306 } else if (DestRC == PPC::F4RCRegisterClass) {
307 BuildMI(MBB, MI, get(PPC::FMRS), DestReg).addReg(SrcReg);
308 } else if (DestRC == PPC::F8RCRegisterClass) {
309 BuildMI(MBB, MI, get(PPC::FMRD), DestReg).addReg(SrcReg);
310 } else if (DestRC == PPC::CRRCRegisterClass) {
311 BuildMI(MBB, MI, get(PPC::MCRF), DestReg).addReg(SrcReg);
312 } else if (DestRC == PPC::VRRCRegisterClass) {
313 BuildMI(MBB, MI, get(PPC::VOR), DestReg).addReg(SrcReg).addReg(SrcReg);
314 } else {
315 cerr << "Attempt to copy register that is not GPR or FPR";
316 abort();
317 }
318}
319
Bill Wendlinga1877c52008-03-03 22:19:16 +0000320static bool StoreRegToStackSlot(const TargetInstrInfo &TII,
Owen Anderson81875432008-01-01 21:11:32 +0000321 unsigned SrcReg, bool isKill, int FrameIdx,
322 const TargetRegisterClass *RC,
Bill Wendlinga1877c52008-03-03 22:19:16 +0000323 SmallVectorImpl<MachineInstr*> &NewMIs,
324 bool isPPC64/*FIXME (64-bit): Remove.*/) {
Owen Anderson81875432008-01-01 21:11:32 +0000325 if (RC == PPC::GPRCRegisterClass) {
326 if (SrcReg != PPC::LR) {
327 NewMIs.push_back(addFrameReference(BuildMI(TII.get(PPC::STW))
328 .addReg(SrcReg, false, false, isKill), FrameIdx));
329 } else {
330 // FIXME: this spills LR immediately to memory in one step. To do this,
331 // we use R11, which we know cannot be used in the prolog/epilog. This is
332 // a hack.
333 NewMIs.push_back(BuildMI(TII.get(PPC::MFLR), PPC::R11));
334 NewMIs.push_back(addFrameReference(BuildMI(TII.get(PPC::STW))
335 .addReg(PPC::R11, false, false, isKill), FrameIdx));
336 }
337 } else if (RC == PPC::G8RCRegisterClass) {
338 if (SrcReg != PPC::LR8) {
339 NewMIs.push_back(addFrameReference(BuildMI(TII.get(PPC::STD))
340 .addReg(SrcReg, false, false, isKill), FrameIdx));
341 } else {
342 // FIXME: this spills LR immediately to memory in one step. To do this,
343 // we use R11, which we know cannot be used in the prolog/epilog. This is
344 // a hack.
345 NewMIs.push_back(BuildMI(TII.get(PPC::MFLR8), PPC::X11));
346 NewMIs.push_back(addFrameReference(BuildMI(TII.get(PPC::STD))
347 .addReg(PPC::X11, false, false, isKill), FrameIdx));
348 }
349 } else if (RC == PPC::F8RCRegisterClass) {
350 NewMIs.push_back(addFrameReference(BuildMI(TII.get(PPC::STFD))
351 .addReg(SrcReg, false, false, isKill), FrameIdx));
352 } else if (RC == PPC::F4RCRegisterClass) {
353 NewMIs.push_back(addFrameReference(BuildMI(TII.get(PPC::STFS))
354 .addReg(SrcReg, false, false, isKill), FrameIdx));
355 } else if (RC == PPC::CRRCRegisterClass) {
Bill Wendlinga1877c52008-03-03 22:19:16 +0000356 if (!isPPC64) { // FIXME (64-bit): Enable
357 NewMIs.push_back(addFrameReference(BuildMI(TII.get(PPC::SPILL_CR))
358 .addReg(SrcReg, false, false, isKill),
359 FrameIdx));
360 return true;
361 } else {
362 // FIXME: We use R0 here, because it isn't available for RA. We need to
363 // store the CR in the low 4-bits of the saved value. First, issue a MFCR
364 // to save all of the CRBits.
365 NewMIs.push_back(BuildMI(TII.get(PPC::MFCR), PPC::R0));
Owen Anderson81875432008-01-01 21:11:32 +0000366
Bill Wendlinga1877c52008-03-03 22:19:16 +0000367 // If the saved register wasn't CR0, shift the bits left so that they are
368 // in CR0's slot.
369 if (SrcReg != PPC::CR0) {
370 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(SrcReg)*4;
371 // rlwinm r0, r0, ShiftBits, 0, 31.
372 NewMIs.push_back(BuildMI(TII.get(PPC::RLWINM), PPC::R0)
373 .addReg(PPC::R0).addImm(ShiftBits).addImm(0).addImm(31));
374 }
375
376 NewMIs.push_back(addFrameReference(BuildMI(TII.get(PPC::STW))
377 .addReg(PPC::R0, false, false, isKill),
378 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000379 }
Owen Anderson81875432008-01-01 21:11:32 +0000380 } else if (RC == PPC::VRRCRegisterClass) {
381 // We don't have indexed addressing for vector loads. Emit:
382 // R0 = ADDI FI#
383 // STVX VAL, 0, R0
384 //
385 // FIXME: We use R0 here, because it isn't available for RA.
386 NewMIs.push_back(addFrameReference(BuildMI(TII.get(PPC::ADDI), PPC::R0),
387 FrameIdx, 0, 0));
388 NewMIs.push_back(BuildMI(TII.get(PPC::STVX))
389 .addReg(SrcReg, false, false, isKill).addReg(PPC::R0).addReg(PPC::R0));
390 } else {
391 assert(0 && "Unknown regclass!");
392 abort();
393 }
Bill Wendlinga1877c52008-03-03 22:19:16 +0000394
395 return false;
Owen Anderson81875432008-01-01 21:11:32 +0000396}
397
398void
399PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Bill Wendlinga1877c52008-03-03 22:19:16 +0000400 MachineBasicBlock::iterator MI,
401 unsigned SrcReg, bool isKill, int FrameIdx,
402 const TargetRegisterClass *RC) const {
Owen Anderson81875432008-01-01 21:11:32 +0000403 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendlinga1877c52008-03-03 22:19:16 +0000404
405 if (StoreRegToStackSlot(*this, SrcReg, isKill, FrameIdx, RC, NewMIs,
406 TM.getSubtargetImpl()->isPPC64()/*FIXME (64-bit): Remove.*/)) {
407 PPCFunctionInfo *FuncInfo = MBB.getParent()->getInfo<PPCFunctionInfo>();
408 FuncInfo->setSpillsCR();
409 }
410
Owen Anderson81875432008-01-01 21:11:32 +0000411 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
412 MBB.insert(MI, NewMIs[i]);
413}
414
415void PPCInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
Bill Wendlinga1877c52008-03-03 22:19:16 +0000416 bool isKill,
417 SmallVectorImpl<MachineOperand> &Addr,
418 const TargetRegisterClass *RC,
419 SmallVectorImpl<MachineInstr*> &NewMIs) const{
Owen Anderson81875432008-01-01 21:11:32 +0000420 if (Addr[0].isFrameIndex()) {
Bill Wendlinga1877c52008-03-03 22:19:16 +0000421 if (StoreRegToStackSlot(*this, SrcReg, isKill, Addr[0].getIndex(), RC, NewMIs,
422 TM.getSubtargetImpl()->isPPC64()/*FIXME (64-bit): Remove.*/)) {
423 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
424 FuncInfo->setSpillsCR();
425 }
426
Owen Anderson81875432008-01-01 21:11:32 +0000427 return;
428 }
429
430 unsigned Opc = 0;
431 if (RC == PPC::GPRCRegisterClass) {
432 Opc = PPC::STW;
433 } else if (RC == PPC::G8RCRegisterClass) {
434 Opc = PPC::STD;
435 } else if (RC == PPC::F8RCRegisterClass) {
436 Opc = PPC::STFD;
437 } else if (RC == PPC::F4RCRegisterClass) {
438 Opc = PPC::STFS;
439 } else if (RC == PPC::VRRCRegisterClass) {
440 Opc = PPC::STVX;
441 } else {
442 assert(0 && "Unknown regclass!");
443 abort();
444 }
445 MachineInstrBuilder MIB = BuildMI(get(Opc))
446 .addReg(SrcReg, false, false, isKill);
447 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
448 MachineOperand &MO = Addr[i];
449 if (MO.isRegister())
450 MIB.addReg(MO.getReg());
451 else if (MO.isImmediate())
452 MIB.addImm(MO.getImm());
453 else
454 MIB.addFrameIndex(MO.getIndex());
455 }
456 NewMIs.push_back(MIB);
457 return;
458}
459
460static void LoadRegFromStackSlot(const TargetInstrInfo &TII,
461 unsigned DestReg, int FrameIdx,
462 const TargetRegisterClass *RC,
463 SmallVectorImpl<MachineInstr*> &NewMIs) {
464 if (RC == PPC::GPRCRegisterClass) {
465 if (DestReg != PPC::LR) {
466 NewMIs.push_back(addFrameReference(BuildMI(TII.get(PPC::LWZ), DestReg),
467 FrameIdx));
468 } else {
469 NewMIs.push_back(addFrameReference(BuildMI(TII.get(PPC::LWZ), PPC::R11),
470 FrameIdx));
471 NewMIs.push_back(BuildMI(TII.get(PPC::MTLR)).addReg(PPC::R11));
472 }
473 } else if (RC == PPC::G8RCRegisterClass) {
474 if (DestReg != PPC::LR8) {
475 NewMIs.push_back(addFrameReference(BuildMI(TII.get(PPC::LD), DestReg),
476 FrameIdx));
477 } else {
478 NewMIs.push_back(addFrameReference(BuildMI(TII.get(PPC::LD), PPC::R11),
479 FrameIdx));
480 NewMIs.push_back(BuildMI(TII.get(PPC::MTLR8)).addReg(PPC::R11));
481 }
482 } else if (RC == PPC::F8RCRegisterClass) {
483 NewMIs.push_back(addFrameReference(BuildMI(TII.get(PPC::LFD), DestReg),
484 FrameIdx));
485 } else if (RC == PPC::F4RCRegisterClass) {
486 NewMIs.push_back(addFrameReference(BuildMI(TII.get(PPC::LFS), DestReg),
487 FrameIdx));
488 } else if (RC == PPC::CRRCRegisterClass) {
489 // FIXME: We use R0 here, because it isn't available for RA.
490 NewMIs.push_back(addFrameReference(BuildMI(TII.get(PPC::LWZ), PPC::R0),
491 FrameIdx));
492
493 // If the reloaded register isn't CR0, shift the bits right so that they are
494 // in the right CR's slot.
495 if (DestReg != PPC::CR0) {
496 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(DestReg)*4;
497 // rlwinm r11, r11, 32-ShiftBits, 0, 31.
498 NewMIs.push_back(BuildMI(TII.get(PPC::RLWINM), PPC::R0)
499 .addReg(PPC::R0).addImm(32-ShiftBits).addImm(0).addImm(31));
500 }
501
502 NewMIs.push_back(BuildMI(TII.get(PPC::MTCRF), DestReg).addReg(PPC::R0));
503 } else if (RC == PPC::VRRCRegisterClass) {
504 // We don't have indexed addressing for vector loads. Emit:
505 // R0 = ADDI FI#
506 // Dest = LVX 0, R0
507 //
508 // FIXME: We use R0 here, because it isn't available for RA.
509 NewMIs.push_back(addFrameReference(BuildMI(TII.get(PPC::ADDI), PPC::R0),
510 FrameIdx, 0, 0));
511 NewMIs.push_back(BuildMI(TII.get(PPC::LVX),DestReg).addReg(PPC::R0)
512 .addReg(PPC::R0));
513 } else {
514 assert(0 && "Unknown regclass!");
515 abort();
516 }
517}
518
519void
520PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Bill Wendlinga1877c52008-03-03 22:19:16 +0000521 MachineBasicBlock::iterator MI,
522 unsigned DestReg, int FrameIdx,
523 const TargetRegisterClass *RC) const {
Owen Anderson81875432008-01-01 21:11:32 +0000524 SmallVector<MachineInstr*, 4> NewMIs;
525 LoadRegFromStackSlot(*this, DestReg, FrameIdx, RC, NewMIs);
526 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
527 MBB.insert(MI, NewMIs[i]);
528}
529
530void PPCInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
Bill Wendlinga1877c52008-03-03 22:19:16 +0000531 SmallVectorImpl<MachineOperand> &Addr,
532 const TargetRegisterClass *RC,
533 SmallVectorImpl<MachineInstr*> &NewMIs)const{
Owen Anderson81875432008-01-01 21:11:32 +0000534 if (Addr[0].isFrameIndex()) {
535 LoadRegFromStackSlot(*this, DestReg, Addr[0].getIndex(), RC, NewMIs);
536 return;
537 }
538
539 unsigned Opc = 0;
540 if (RC == PPC::GPRCRegisterClass) {
541 assert(DestReg != PPC::LR && "Can't handle this yet!");
542 Opc = PPC::LWZ;
543 } else if (RC == PPC::G8RCRegisterClass) {
544 assert(DestReg != PPC::LR8 && "Can't handle this yet!");
545 Opc = PPC::LD;
546 } else if (RC == PPC::F8RCRegisterClass) {
547 Opc = PPC::LFD;
548 } else if (RC == PPC::F4RCRegisterClass) {
549 Opc = PPC::LFS;
550 } else if (RC == PPC::VRRCRegisterClass) {
551 Opc = PPC::LVX;
552 } else {
553 assert(0 && "Unknown regclass!");
554 abort();
555 }
556 MachineInstrBuilder MIB = BuildMI(get(Opc), DestReg);
557 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
558 MachineOperand &MO = Addr[i];
559 if (MO.isRegister())
560 MIB.addReg(MO.getReg());
561 else if (MO.isImmediate())
562 MIB.addImm(MO.getImm());
563 else
564 MIB.addFrameIndex(MO.getIndex());
565 }
566 NewMIs.push_back(MIB);
567 return;
568}
569
Owen Anderson9a184ef2008-01-07 01:35:02 +0000570/// foldMemoryOperand - PowerPC (like most RISC's) can only fold spills into
571/// copy instructions, turning them into load/store instructions.
Evan Cheng4f2f3f62008-02-08 21:20:40 +0000572MachineInstr *PPCInstrInfo::foldMemoryOperand(MachineFunction &MF,
573 MachineInstr *MI,
Owen Anderson9a184ef2008-01-07 01:35:02 +0000574 SmallVectorImpl<unsigned> &Ops,
575 int FrameIndex) const {
576 if (Ops.size() != 1) return NULL;
577
578 // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because
579 // it takes more than one instruction to store it.
580 unsigned Opc = MI->getOpcode();
581 unsigned OpNum = Ops[0];
582
583 MachineInstr *NewMI = NULL;
584 if ((Opc == PPC::OR &&
585 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
586 if (OpNum == 0) { // move -> store
587 unsigned InReg = MI->getOperand(1).getReg();
588 NewMI = addFrameReference(BuildMI(get(PPC::STW)).addReg(InReg),
589 FrameIndex);
590 } else { // move -> load
591 unsigned OutReg = MI->getOperand(0).getReg();
592 NewMI = addFrameReference(BuildMI(get(PPC::LWZ), OutReg),
593 FrameIndex);
594 }
595 } else if ((Opc == PPC::OR8 &&
596 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
597 if (OpNum == 0) { // move -> store
598 unsigned InReg = MI->getOperand(1).getReg();
599 NewMI = addFrameReference(BuildMI(get(PPC::STD)).addReg(InReg),
600 FrameIndex);
601 } else { // move -> load
602 unsigned OutReg = MI->getOperand(0).getReg();
603 NewMI = addFrameReference(BuildMI(get(PPC::LD), OutReg), FrameIndex);
604 }
605 } else if (Opc == PPC::FMRD) {
606 if (OpNum == 0) { // move -> store
607 unsigned InReg = MI->getOperand(1).getReg();
608 NewMI = addFrameReference(BuildMI(get(PPC::STFD)).addReg(InReg),
609 FrameIndex);
610 } else { // move -> load
611 unsigned OutReg = MI->getOperand(0).getReg();
612 NewMI = addFrameReference(BuildMI(get(PPC::LFD), OutReg), FrameIndex);
613 }
614 } else if (Opc == PPC::FMRS) {
615 if (OpNum == 0) { // move -> store
616 unsigned InReg = MI->getOperand(1).getReg();
617 NewMI = addFrameReference(BuildMI(get(PPC::STFS)).addReg(InReg),
618 FrameIndex);
619 } else { // move -> load
620 unsigned OutReg = MI->getOperand(0).getReg();
621 NewMI = addFrameReference(BuildMI(get(PPC::LFS), OutReg), FrameIndex);
622 }
623 }
624
625 if (NewMI)
626 NewMI->copyKillDeadInfo(MI);
627 return NewMI;
628}
629
630bool PPCInstrInfo::canFoldMemoryOperand(MachineInstr *MI,
Evan Cheng4f2f3f62008-02-08 21:20:40 +0000631 SmallVectorImpl<unsigned> &Ops) const {
Owen Anderson9a184ef2008-01-07 01:35:02 +0000632 if (Ops.size() != 1) return false;
633
634 // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because
635 // it takes more than one instruction to store it.
636 unsigned Opc = MI->getOpcode();
637
638 if ((Opc == PPC::OR &&
639 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
640 return true;
641 else if ((Opc == PPC::OR8 &&
642 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
643 return true;
644 else if (Opc == PPC::FMRD || Opc == PPC::FMRS)
645 return true;
646
647 return false;
648}
649
Owen Anderson81875432008-01-01 21:11:32 +0000650
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000651bool PPCInstrInfo::BlockHasNoFallThrough(MachineBasicBlock &MBB) const {
652 if (MBB.empty()) return false;
653
654 switch (MBB.back().getOpcode()) {
655 case PPC::BLR: // Return.
656 case PPC::B: // Uncond branch.
657 case PPC::BCTR: // Indirect branch.
658 return true;
659 default: return false;
660 }
661}
662
663bool PPCInstrInfo::
664ReverseBranchCondition(std::vector<MachineOperand> &Cond) const {
665 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
666 // Leave the CR# the same, but invert the condition.
667 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
668 return false;
669}