blob: b008a1d2e8061dd2e20c2cc5488d5089ab546f07 [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"
Bill Wendling03598502008-03-04 23:13:51 +000022#include "llvm/Support/CommandLine.h"
Nicolas Geoffraycb162a02008-04-16 20:10:13 +000023#include "llvm/Target/TargetAsmInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024using namespace llvm;
25
Bill Wendling4eaadfb2008-03-10 22:49:16 +000026extern cl::opt<bool> EnablePPC32RS; // FIXME (64-bit): See PPCRegisterInfo.cpp.
27extern cl::opt<bool> EnablePPC64RS; // FIXME (64-bit): See PPCRegisterInfo.cpp.
Bill Wendling03598502008-03-04 23:13:51 +000028
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029PPCInstrInfo::PPCInstrInfo(PPCTargetMachine &tm)
Chris Lattnerd2fd6db2008-01-01 01:03:04 +000030 : TargetInstrInfoImpl(PPCInsts, array_lengthof(PPCInsts)), TM(tm),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031 RI(*TM.getSubtargetImpl(), *this) {}
32
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033bool PPCInstrInfo::isMoveInstr(const MachineInstr& MI,
34 unsigned& sourceReg,
Evan Chengf97496a2009-01-20 19:12:24 +000035 unsigned& destReg,
36 unsigned& sourceSubIdx,
37 unsigned& destSubIdx) const {
38 sourceSubIdx = destSubIdx = 0; // No sub-registers.
39
Chris Lattner99aa3372008-01-07 02:48:55 +000040 unsigned oc = MI.getOpcode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041 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 &&
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000044 MI.getOperand(0).isReg() &&
45 MI.getOperand(1).isReg() &&
46 MI.getOperand(2).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047 "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();
51 return true;
52 }
53 } else if (oc == PPC::ADDI) { // addi r1, r2, 0
54 assert(MI.getNumOperands() >= 3 &&
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000055 MI.getOperand(0).isReg() &&
56 MI.getOperand(2).isImm() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057 "invalid PPC ADDI instruction!");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000058 if (MI.getOperand(1).isReg() && MI.getOperand(2).getImm() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059 sourceReg = MI.getOperand(1).getReg();
60 destReg = MI.getOperand(0).getReg();
61 return true;
62 }
63 } else if (oc == PPC::ORI) { // ori r1, r2, 0
64 assert(MI.getNumOperands() >= 3 &&
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000065 MI.getOperand(0).isReg() &&
66 MI.getOperand(1).isReg() &&
67 MI.getOperand(2).isImm() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068 "invalid PPC ORI instruction!");
Chris Lattnera96056a2007-12-30 20:49:49 +000069 if (MI.getOperand(2).getImm() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070 sourceReg = MI.getOperand(1).getReg();
71 destReg = MI.getOperand(0).getReg();
72 return true;
73 }
74 } else if (oc == PPC::FMRS || oc == PPC::FMRD ||
75 oc == PPC::FMRSD) { // fmr r1, r2
76 assert(MI.getNumOperands() >= 2 &&
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000077 MI.getOperand(0).isReg() &&
78 MI.getOperand(1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079 "invalid PPC FMR instruction");
80 sourceReg = MI.getOperand(1).getReg();
81 destReg = MI.getOperand(0).getReg();
82 return true;
83 } else if (oc == PPC::MCRF) { // mcrf cr1, cr2
84 assert(MI.getNumOperands() >= 2 &&
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000085 MI.getOperand(0).isReg() &&
86 MI.getOperand(1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087 "invalid PPC MCRF instruction");
88 sourceReg = MI.getOperand(1).getReg();
89 destReg = MI.getOperand(0).getReg();
90 return true;
91 }
92 return false;
93}
94
Dan Gohman90feee22008-11-18 19:49:32 +000095unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096 int &FrameIndex) const {
97 switch (MI->getOpcode()) {
98 default: break;
99 case PPC::LD:
100 case PPC::LWZ:
101 case PPC::LFS:
102 case PPC::LFD:
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000103 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
104 MI->getOperand(2).isFI()) {
Chris Lattner6017d482007-12-30 23:10:15 +0000105 FrameIndex = MI->getOperand(2).getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106 return MI->getOperand(0).getReg();
107 }
108 break;
109 }
110 return 0;
111}
112
Dan Gohman90feee22008-11-18 19:49:32 +0000113unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114 int &FrameIndex) const {
115 switch (MI->getOpcode()) {
116 default: break;
117 case PPC::STD:
118 case PPC::STW:
119 case PPC::STFS:
120 case PPC::STFD:
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000121 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
122 MI->getOperand(2).isFI()) {
Chris Lattner6017d482007-12-30 23:10:15 +0000123 FrameIndex = MI->getOperand(2).getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124 return MI->getOperand(0).getReg();
125 }
126 break;
127 }
128 return 0;
129}
130
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.
Evan Cheng5de1aaf2008-06-16 07:33:11 +0000133MachineInstr *
134PPCInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
Dan Gohman221a4372008-07-07 23:14:23 +0000135 MachineFunction &MF = *MI->getParent()->getParent();
136
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137 // Normal instructions can be commuted the obvious way.
138 if (MI->getOpcode() != PPC::RLWIMI)
Evan Cheng5de1aaf2008-06-16 07:33:11 +0000139 return TargetInstrInfoImpl::commuteInstruction(MI, NewMI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140
141 // Cannot commute if it has a non-zero rotate count.
Chris Lattnera96056a2007-12-30 20:49:49 +0000142 if (MI->getOperand(3).getImm() != 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000143 return 0;
144
145 // If we have a zero rotate count, we have:
146 // M = mask(MB,ME)
147 // Op0 = (Op1 & ~M) | (Op2 & M)
148 // Change this to:
149 // M = mask((ME+1)&31, (MB-1)&31)
150 // Op0 = (Op2 & ~M) | (Op1 & M)
151
152 // Swap op1/op2
Evan Chengb554e532008-02-13 02:46:49 +0000153 unsigned Reg0 = MI->getOperand(0).getReg();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154 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();
Evan Cheng5de1aaf2008-06-16 07:33:11 +0000158 bool ChangeReg0 = false;
Evan Chengb554e532008-02-13 02:46:49 +0000159 // If machine instrs are no longer in two-address forms, update
160 // destination register as well.
161 if (Reg0 == Reg1) {
162 // Must be two address instruction!
163 assert(MI->getDesc().getOperandConstraint(0, TOI::TIED_TO) &&
164 "Expecting a two-address instruction!");
Evan Chengb554e532008-02-13 02:46:49 +0000165 Reg2IsKill = false;
Evan Cheng5de1aaf2008-06-16 07:33:11 +0000166 ChangeReg0 = true;
Evan Chengb554e532008-02-13 02:46:49 +0000167 }
Evan Cheng5de1aaf2008-06-16 07:33:11 +0000168
169 // Masks.
170 unsigned MB = MI->getOperand(4).getImm();
171 unsigned ME = MI->getOperand(5).getImm();
172
173 if (NewMI) {
174 // Create a new instruction.
175 unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg();
176 bool Reg0IsDead = MI->getOperand(0).isDead();
Dan Gohman221a4372008-07-07 23:14:23 +0000177 return BuildMI(MF, MI->getDesc())
178 .addReg(Reg0, true, false, false, Reg0IsDead)
Evan Cheng5de1aaf2008-06-16 07:33:11 +0000179 .addReg(Reg2, false, false, Reg2IsKill)
180 .addReg(Reg1, false, false, Reg1IsKill)
181 .addImm((ME+1) & 31)
182 .addImm((MB-1) & 31);
183 }
184
185 if (ChangeReg0)
186 MI->getOperand(0).setReg(Reg2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 MI->getOperand(2).setReg(Reg1);
188 MI->getOperand(1).setReg(Reg2);
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000189 MI->getOperand(2).setIsKill(Reg1IsKill);
190 MI->getOperand(1).setIsKill(Reg2IsKill);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191
192 // Swap the mask around.
Chris Lattnera96056a2007-12-30 20:49:49 +0000193 MI->getOperand(4).setImm((ME+1) & 31);
194 MI->getOperand(5).setImm((MB-1) & 31);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195 return MI;
196}
197
198void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
199 MachineBasicBlock::iterator MI) const {
200 BuildMI(MBB, MI, get(PPC::NOP));
201}
202
203
204// Branch analysis.
205bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
206 MachineBasicBlock *&FBB,
Owen Andersond131b5b2008-08-14 22:49:33 +0000207 SmallVectorImpl<MachineOperand> &Cond) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 // If the block has no terminators, it just falls into the block after it.
209 MachineBasicBlock::iterator I = MBB.end();
210 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
211 return false;
212
213 // Get the last instruction in the block.
214 MachineInstr *LastInst = I;
215
216 // If there is only one terminator instruction, process it.
217 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
218 if (LastInst->getOpcode() == PPC::B) {
Chris Lattner6017d482007-12-30 23:10:15 +0000219 TBB = LastInst->getOperand(0).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000220 return false;
221 } else if (LastInst->getOpcode() == PPC::BCC) {
222 // Block ends with fall-through condbranch.
Chris Lattner6017d482007-12-30 23:10:15 +0000223 TBB = LastInst->getOperand(2).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000224 Cond.push_back(LastInst->getOperand(0));
225 Cond.push_back(LastInst->getOperand(1));
226 return false;
227 }
228 // Otherwise, don't know what this is.
229 return true;
230 }
231
232 // Get the instruction before it if it's a terminator.
233 MachineInstr *SecondLastInst = I;
234
235 // If there are three terminators, we don't know what sort of block this is.
236 if (SecondLastInst && I != MBB.begin() &&
237 isUnpredicatedTerminator(--I))
238 return true;
239
240 // If the block ends with PPC::B and PPC:BCC, handle it.
241 if (SecondLastInst->getOpcode() == PPC::BCC &&
242 LastInst->getOpcode() == PPC::B) {
Chris Lattner6017d482007-12-30 23:10:15 +0000243 TBB = SecondLastInst->getOperand(2).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000244 Cond.push_back(SecondLastInst->getOperand(0));
245 Cond.push_back(SecondLastInst->getOperand(1));
Chris Lattner6017d482007-12-30 23:10:15 +0000246 FBB = LastInst->getOperand(0).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 return false;
248 }
249
250 // If the block ends with two PPC:Bs, handle it. The second one is not
251 // executed, so remove it.
252 if (SecondLastInst->getOpcode() == PPC::B &&
253 LastInst->getOpcode() == PPC::B) {
Chris Lattner6017d482007-12-30 23:10:15 +0000254 TBB = SecondLastInst->getOperand(0).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 I = LastInst;
256 I->eraseFromParent();
257 return false;
258 }
259
260 // Otherwise, can't handle this.
261 return true;
262}
263
264unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
265 MachineBasicBlock::iterator I = MBB.end();
266 if (I == MBB.begin()) return 0;
267 --I;
268 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC)
269 return 0;
270
271 // Remove the branch.
272 I->eraseFromParent();
273
274 I = MBB.end();
275
276 if (I == MBB.begin()) return 1;
277 --I;
278 if (I->getOpcode() != PPC::BCC)
279 return 1;
280
281 // Remove the branch.
282 I->eraseFromParent();
283 return 2;
284}
285
286unsigned
287PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
288 MachineBasicBlock *FBB,
Owen Andersond131b5b2008-08-14 22:49:33 +0000289 const SmallVectorImpl<MachineOperand> &Cond) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000290 // Shouldn't be a fall through.
291 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
292 assert((Cond.size() == 2 || Cond.size() == 0) &&
293 "PPC branch conditions have two components!");
294
295 // One-way branch.
296 if (FBB == 0) {
297 if (Cond.empty()) // Unconditional branch
298 BuildMI(&MBB, get(PPC::B)).addMBB(TBB);
299 else // Conditional branch
300 BuildMI(&MBB, get(PPC::BCC))
301 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
302 return 1;
303 }
304
305 // Two-way Conditional Branch.
306 BuildMI(&MBB, get(PPC::BCC))
307 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
308 BuildMI(&MBB, get(PPC::B)).addMBB(FBB);
309 return 2;
310}
311
Owen Anderson9fa72d92008-08-26 18:03:31 +0000312bool PPCInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
Owen Anderson8f2c8932007-12-31 06:32:00 +0000313 MachineBasicBlock::iterator MI,
314 unsigned DestReg, unsigned SrcReg,
315 const TargetRegisterClass *DestRC,
316 const TargetRegisterClass *SrcRC) const {
317 if (DestRC != SrcRC) {
Owen Anderson9fa72d92008-08-26 18:03:31 +0000318 // Not yet supported!
319 return false;
Owen Anderson8f2c8932007-12-31 06:32:00 +0000320 }
321
322 if (DestRC == PPC::GPRCRegisterClass) {
323 BuildMI(MBB, MI, get(PPC::OR), DestReg).addReg(SrcReg).addReg(SrcReg);
324 } else if (DestRC == PPC::G8RCRegisterClass) {
325 BuildMI(MBB, MI, get(PPC::OR8), DestReg).addReg(SrcReg).addReg(SrcReg);
326 } else if (DestRC == PPC::F4RCRegisterClass) {
327 BuildMI(MBB, MI, get(PPC::FMRS), DestReg).addReg(SrcReg);
328 } else if (DestRC == PPC::F8RCRegisterClass) {
329 BuildMI(MBB, MI, get(PPC::FMRD), DestReg).addReg(SrcReg);
330 } else if (DestRC == PPC::CRRCRegisterClass) {
331 BuildMI(MBB, MI, get(PPC::MCRF), DestReg).addReg(SrcReg);
332 } else if (DestRC == PPC::VRRCRegisterClass) {
333 BuildMI(MBB, MI, get(PPC::VOR), DestReg).addReg(SrcReg).addReg(SrcReg);
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000334 } else if (DestRC == PPC::CRBITRCRegisterClass) {
335 BuildMI(MBB, MI, get(PPC::CROR), DestReg).addReg(SrcReg).addReg(SrcReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000336 } else {
Owen Anderson9fa72d92008-08-26 18:03:31 +0000337 // Attempt to copy register that is not GPR or FPR
338 return false;
Owen Anderson8f2c8932007-12-31 06:32:00 +0000339 }
Owen Anderson9fa72d92008-08-26 18:03:31 +0000340
341 return true;
Owen Anderson8f2c8932007-12-31 06:32:00 +0000342}
343
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000344bool
Dan Gohman221a4372008-07-07 23:14:23 +0000345PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
346 unsigned SrcReg, bool isKill,
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000347 int FrameIdx,
348 const TargetRegisterClass *RC,
349 SmallVectorImpl<MachineInstr*> &NewMIs) const{
Owen Anderson81875432008-01-01 21:11:32 +0000350 if (RC == PPC::GPRCRegisterClass) {
351 if (SrcReg != PPC::LR) {
Dan Gohman221a4372008-07-07 23:14:23 +0000352 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STW))
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000353 .addReg(SrcReg, false, false, isKill),
354 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000355 } else {
356 // FIXME: this spills LR immediately to memory in one step. To do this,
357 // we use R11, which we know cannot be used in the prolog/epilog. This is
358 // a hack.
Dan Gohman221a4372008-07-07 23:14:23 +0000359 NewMIs.push_back(BuildMI(MF, get(PPC::MFLR), PPC::R11));
360 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STW))
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000361 .addReg(PPC::R11, false, false, isKill),
362 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000363 }
364 } else if (RC == PPC::G8RCRegisterClass) {
365 if (SrcReg != PPC::LR8) {
Dan Gohman221a4372008-07-07 23:14:23 +0000366 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STD))
Chris Lattner7b7371c2008-03-10 18:55:53 +0000367 .addReg(SrcReg, false, false, isKill), FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000368 } else {
369 // FIXME: this spills LR immediately to memory in one step. To do this,
370 // we use R11, which we know cannot be used in the prolog/epilog. This is
371 // a hack.
Dan Gohman221a4372008-07-07 23:14:23 +0000372 NewMIs.push_back(BuildMI(MF, get(PPC::MFLR8), PPC::X11));
373 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STD))
Chris Lattner7b7371c2008-03-10 18:55:53 +0000374 .addReg(PPC::X11, false, false, isKill), FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000375 }
376 } else if (RC == PPC::F8RCRegisterClass) {
Dan Gohman221a4372008-07-07 23:14:23 +0000377 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STFD))
Chris Lattner7b7371c2008-03-10 18:55:53 +0000378 .addReg(SrcReg, false, false, isKill), FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000379 } else if (RC == PPC::F4RCRegisterClass) {
Dan Gohman221a4372008-07-07 23:14:23 +0000380 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STFS))
Chris Lattner7b7371c2008-03-10 18:55:53 +0000381 .addReg(SrcReg, false, false, isKill), FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000382 } else if (RC == PPC::CRRCRegisterClass) {
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000383 if ((EnablePPC32RS && !TM.getSubtargetImpl()->isPPC64()) ||
384 (EnablePPC64RS && TM.getSubtargetImpl()->isPPC64())) {
385 // FIXME (64-bit): Enable
Dan Gohman221a4372008-07-07 23:14:23 +0000386 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::SPILL_CR))
Bill Wendlinga1877c52008-03-03 22:19:16 +0000387 .addReg(SrcReg, false, false, isKill),
Chris Lattner6734c3a2008-03-20 01:22:40 +0000388 FrameIdx));
Bill Wendlinga1877c52008-03-03 22:19:16 +0000389 return true;
390 } else {
391 // FIXME: We use R0 here, because it isn't available for RA. We need to
392 // store the CR in the low 4-bits of the saved value. First, issue a MFCR
393 // to save all of the CRBits.
Dan Gohman221a4372008-07-07 23:14:23 +0000394 NewMIs.push_back(BuildMI(MF, get(PPC::MFCR), PPC::R0));
Owen Anderson81875432008-01-01 21:11:32 +0000395
Bill Wendlinga1877c52008-03-03 22:19:16 +0000396 // If the saved register wasn't CR0, shift the bits left so that they are
397 // in CR0's slot.
398 if (SrcReg != PPC::CR0) {
399 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(SrcReg)*4;
400 // rlwinm r0, r0, ShiftBits, 0, 31.
Dan Gohman221a4372008-07-07 23:14:23 +0000401 NewMIs.push_back(BuildMI(MF, get(PPC::RLWINM), PPC::R0)
Chris Lattner7b7371c2008-03-10 18:55:53 +0000402 .addReg(PPC::R0).addImm(ShiftBits).addImm(0).addImm(31));
Bill Wendlinga1877c52008-03-03 22:19:16 +0000403 }
404
Dan Gohman221a4372008-07-07 23:14:23 +0000405 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STW))
Bill Wendlinga1877c52008-03-03 22:19:16 +0000406 .addReg(PPC::R0, false, false, isKill),
407 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000408 }
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000409 } else if (RC == PPC::CRBITRCRegisterClass) {
410 // FIXME: We use CRi here because there is no mtcrf on a bit. Since the
411 // backend currently only uses CR1EQ as an individual bit, this should
412 // not cause any bug. If we need other uses of CR bits, the following
413 // code may be invalid.
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000414 unsigned Reg = 0;
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000415 if (SrcReg >= PPC::CR0LT || SrcReg <= PPC::CR0UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000416 Reg = PPC::CR0;
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000417 else if (SrcReg >= PPC::CR1LT || SrcReg <= PPC::CR1UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000418 Reg = PPC::CR1;
419 else if (SrcReg >= PPC::CR2LT || SrcReg <= PPC::CR2UN)
420 Reg = PPC::CR2;
421 else if (SrcReg >= PPC::CR3LT || SrcReg <= PPC::CR3UN)
422 Reg = PPC::CR3;
423 else if (SrcReg >= PPC::CR4LT || SrcReg <= PPC::CR4UN)
424 Reg = PPC::CR4;
425 else if (SrcReg >= PPC::CR5LT || SrcReg <= PPC::CR5UN)
426 Reg = PPC::CR5;
427 else if (SrcReg >= PPC::CR6LT || SrcReg <= PPC::CR6UN)
428 Reg = PPC::CR6;
429 else if (SrcReg >= PPC::CR7LT || SrcReg <= PPC::CR7UN)
430 Reg = PPC::CR7;
431
Dan Gohman221a4372008-07-07 23:14:23 +0000432 return StoreRegToStackSlot(MF, Reg, isKill, FrameIdx,
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000433 PPC::CRRCRegisterClass, NewMIs);
434
Owen Anderson81875432008-01-01 21:11:32 +0000435 } else if (RC == PPC::VRRCRegisterClass) {
436 // We don't have indexed addressing for vector loads. Emit:
437 // R0 = ADDI FI#
438 // STVX VAL, 0, R0
439 //
440 // FIXME: We use R0 here, because it isn't available for RA.
Dan Gohman221a4372008-07-07 23:14:23 +0000441 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::ADDI), PPC::R0),
Owen Anderson81875432008-01-01 21:11:32 +0000442 FrameIdx, 0, 0));
Dan Gohman221a4372008-07-07 23:14:23 +0000443 NewMIs.push_back(BuildMI(MF, get(PPC::STVX))
Chris Lattner7b7371c2008-03-10 18:55:53 +0000444 .addReg(SrcReg, false, false, isKill).addReg(PPC::R0).addReg(PPC::R0));
Owen Anderson81875432008-01-01 21:11:32 +0000445 } else {
446 assert(0 && "Unknown regclass!");
447 abort();
448 }
Bill Wendlinga1877c52008-03-03 22:19:16 +0000449
450 return false;
Owen Anderson81875432008-01-01 21:11:32 +0000451}
452
453void
454PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Bill Wendlinga1877c52008-03-03 22:19:16 +0000455 MachineBasicBlock::iterator MI,
456 unsigned SrcReg, bool isKill, int FrameIdx,
457 const TargetRegisterClass *RC) const {
Dan Gohman221a4372008-07-07 23:14:23 +0000458 MachineFunction &MF = *MBB.getParent();
Owen Anderson81875432008-01-01 21:11:32 +0000459 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendlinga1877c52008-03-03 22:19:16 +0000460
Dan Gohman221a4372008-07-07 23:14:23 +0000461 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs)) {
462 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Bill Wendlinga1877c52008-03-03 22:19:16 +0000463 FuncInfo->setSpillsCR();
464 }
465
Owen Anderson81875432008-01-01 21:11:32 +0000466 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
467 MBB.insert(MI, NewMIs[i]);
468}
469
470void PPCInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
Bill Wendlinga1877c52008-03-03 22:19:16 +0000471 bool isKill,
472 SmallVectorImpl<MachineOperand> &Addr,
473 const TargetRegisterClass *RC,
474 SmallVectorImpl<MachineInstr*> &NewMIs) const{
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000475 if (Addr[0].isFI()) {
Dan Gohman221a4372008-07-07 23:14:23 +0000476 if (StoreRegToStackSlot(MF, SrcReg, isKill,
477 Addr[0].getIndex(), RC, NewMIs)) {
Bill Wendlinga1877c52008-03-03 22:19:16 +0000478 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
479 FuncInfo->setSpillsCR();
480 }
481
Owen Anderson81875432008-01-01 21:11:32 +0000482 return;
483 }
484
485 unsigned Opc = 0;
486 if (RC == PPC::GPRCRegisterClass) {
487 Opc = PPC::STW;
488 } else if (RC == PPC::G8RCRegisterClass) {
489 Opc = PPC::STD;
490 } else if (RC == PPC::F8RCRegisterClass) {
491 Opc = PPC::STFD;
492 } else if (RC == PPC::F4RCRegisterClass) {
493 Opc = PPC::STFS;
494 } else if (RC == PPC::VRRCRegisterClass) {
495 Opc = PPC::STVX;
496 } else {
497 assert(0 && "Unknown regclass!");
498 abort();
499 }
Dan Gohman221a4372008-07-07 23:14:23 +0000500 MachineInstrBuilder MIB = BuildMI(MF, get(Opc))
Owen Anderson81875432008-01-01 21:11:32 +0000501 .addReg(SrcReg, false, false, isKill);
502 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
503 MachineOperand &MO = Addr[i];
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000504 if (MO.isReg())
Owen Anderson81875432008-01-01 21:11:32 +0000505 MIB.addReg(MO.getReg());
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000506 else if (MO.isImm())
Owen Anderson81875432008-01-01 21:11:32 +0000507 MIB.addImm(MO.getImm());
508 else
509 MIB.addFrameIndex(MO.getIndex());
510 }
511 NewMIs.push_back(MIB);
512 return;
513}
514
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000515void
Dan Gohman221a4372008-07-07 23:14:23 +0000516PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF,
517 unsigned DestReg, int FrameIdx,
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000518 const TargetRegisterClass *RC,
519 SmallVectorImpl<MachineInstr*> &NewMIs)const{
Owen Anderson81875432008-01-01 21:11:32 +0000520 if (RC == PPC::GPRCRegisterClass) {
521 if (DestReg != PPC::LR) {
Dan Gohman221a4372008-07-07 23:14:23 +0000522 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LWZ), DestReg),
Owen Anderson81875432008-01-01 21:11:32 +0000523 FrameIdx));
524 } else {
Dan Gohman221a4372008-07-07 23:14:23 +0000525 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LWZ), PPC::R11),
Owen Anderson81875432008-01-01 21:11:32 +0000526 FrameIdx));
Dan Gohman221a4372008-07-07 23:14:23 +0000527 NewMIs.push_back(BuildMI(MF, get(PPC::MTLR)).addReg(PPC::R11));
Owen Anderson81875432008-01-01 21:11:32 +0000528 }
529 } else if (RC == PPC::G8RCRegisterClass) {
530 if (DestReg != PPC::LR8) {
Dan Gohman221a4372008-07-07 23:14:23 +0000531 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LD), DestReg),
Owen Anderson81875432008-01-01 21:11:32 +0000532 FrameIdx));
533 } else {
Dan Gohman221a4372008-07-07 23:14:23 +0000534 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LD), PPC::R11),
Owen Anderson81875432008-01-01 21:11:32 +0000535 FrameIdx));
Dan Gohman221a4372008-07-07 23:14:23 +0000536 NewMIs.push_back(BuildMI(MF, get(PPC::MTLR8)).addReg(PPC::R11));
Owen Anderson81875432008-01-01 21:11:32 +0000537 }
538 } else if (RC == PPC::F8RCRegisterClass) {
Dan Gohman221a4372008-07-07 23:14:23 +0000539 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LFD), DestReg),
Owen Anderson81875432008-01-01 21:11:32 +0000540 FrameIdx));
541 } else if (RC == PPC::F4RCRegisterClass) {
Dan Gohman221a4372008-07-07 23:14:23 +0000542 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LFS), DestReg),
Owen Anderson81875432008-01-01 21:11:32 +0000543 FrameIdx));
544 } else if (RC == PPC::CRRCRegisterClass) {
545 // FIXME: We use R0 here, because it isn't available for RA.
Dan Gohman221a4372008-07-07 23:14:23 +0000546 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LWZ), PPC::R0),
Owen Anderson81875432008-01-01 21:11:32 +0000547 FrameIdx));
548
549 // If the reloaded register isn't CR0, shift the bits right so that they are
550 // in the right CR's slot.
551 if (DestReg != PPC::CR0) {
552 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(DestReg)*4;
553 // rlwinm r11, r11, 32-ShiftBits, 0, 31.
Dan Gohman221a4372008-07-07 23:14:23 +0000554 NewMIs.push_back(BuildMI(MF, get(PPC::RLWINM), PPC::R0)
Owen Anderson81875432008-01-01 21:11:32 +0000555 .addReg(PPC::R0).addImm(32-ShiftBits).addImm(0).addImm(31));
556 }
557
Dan Gohman221a4372008-07-07 23:14:23 +0000558 NewMIs.push_back(BuildMI(MF, get(PPC::MTCRF), DestReg).addReg(PPC::R0));
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000559 } else if (RC == PPC::CRBITRCRegisterClass) {
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000560
561 unsigned Reg = 0;
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000562 if (DestReg >= PPC::CR0LT || DestReg <= PPC::CR0UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000563 Reg = PPC::CR0;
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000564 else if (DestReg >= PPC::CR1LT || DestReg <= PPC::CR1UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000565 Reg = PPC::CR1;
566 else if (DestReg >= PPC::CR2LT || DestReg <= PPC::CR2UN)
567 Reg = PPC::CR2;
568 else if (DestReg >= PPC::CR3LT || DestReg <= PPC::CR3UN)
569 Reg = PPC::CR3;
570 else if (DestReg >= PPC::CR4LT || DestReg <= PPC::CR4UN)
571 Reg = PPC::CR4;
572 else if (DestReg >= PPC::CR5LT || DestReg <= PPC::CR5UN)
573 Reg = PPC::CR5;
574 else if (DestReg >= PPC::CR6LT || DestReg <= PPC::CR6UN)
575 Reg = PPC::CR6;
576 else if (DestReg >= PPC::CR7LT || DestReg <= PPC::CR7UN)
577 Reg = PPC::CR7;
578
Dan Gohman221a4372008-07-07 23:14:23 +0000579 return LoadRegFromStackSlot(MF, Reg, FrameIdx,
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000580 PPC::CRRCRegisterClass, NewMIs);
581
Owen Anderson81875432008-01-01 21:11:32 +0000582 } else if (RC == PPC::VRRCRegisterClass) {
583 // We don't have indexed addressing for vector loads. Emit:
584 // R0 = ADDI FI#
585 // Dest = LVX 0, R0
586 //
587 // FIXME: We use R0 here, because it isn't available for RA.
Dan Gohman221a4372008-07-07 23:14:23 +0000588 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::ADDI), PPC::R0),
Owen Anderson81875432008-01-01 21:11:32 +0000589 FrameIdx, 0, 0));
Dan Gohman221a4372008-07-07 23:14:23 +0000590 NewMIs.push_back(BuildMI(MF, get(PPC::LVX),DestReg).addReg(PPC::R0)
Owen Anderson81875432008-01-01 21:11:32 +0000591 .addReg(PPC::R0));
592 } else {
593 assert(0 && "Unknown regclass!");
594 abort();
595 }
596}
597
598void
599PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Bill Wendlinga1877c52008-03-03 22:19:16 +0000600 MachineBasicBlock::iterator MI,
601 unsigned DestReg, int FrameIdx,
602 const TargetRegisterClass *RC) const {
Dan Gohman221a4372008-07-07 23:14:23 +0000603 MachineFunction &MF = *MBB.getParent();
Owen Anderson81875432008-01-01 21:11:32 +0000604 SmallVector<MachineInstr*, 4> NewMIs;
Dan Gohman221a4372008-07-07 23:14:23 +0000605 LoadRegFromStackSlot(MF, DestReg, FrameIdx, RC, NewMIs);
Owen Anderson81875432008-01-01 21:11:32 +0000606 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
607 MBB.insert(MI, NewMIs[i]);
608}
609
610void PPCInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
Bill Wendlinga1877c52008-03-03 22:19:16 +0000611 SmallVectorImpl<MachineOperand> &Addr,
612 const TargetRegisterClass *RC,
613 SmallVectorImpl<MachineInstr*> &NewMIs)const{
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000614 if (Addr[0].isFI()) {
Dan Gohman221a4372008-07-07 23:14:23 +0000615 LoadRegFromStackSlot(MF, DestReg, Addr[0].getIndex(), RC, NewMIs);
Owen Anderson81875432008-01-01 21:11:32 +0000616 return;
617 }
618
619 unsigned Opc = 0;
620 if (RC == PPC::GPRCRegisterClass) {
621 assert(DestReg != PPC::LR && "Can't handle this yet!");
622 Opc = PPC::LWZ;
623 } else if (RC == PPC::G8RCRegisterClass) {
624 assert(DestReg != PPC::LR8 && "Can't handle this yet!");
625 Opc = PPC::LD;
626 } else if (RC == PPC::F8RCRegisterClass) {
627 Opc = PPC::LFD;
628 } else if (RC == PPC::F4RCRegisterClass) {
629 Opc = PPC::LFS;
630 } else if (RC == PPC::VRRCRegisterClass) {
631 Opc = PPC::LVX;
632 } else {
633 assert(0 && "Unknown regclass!");
634 abort();
635 }
Dan Gohman221a4372008-07-07 23:14:23 +0000636 MachineInstrBuilder MIB = BuildMI(MF, get(Opc), DestReg);
Owen Anderson81875432008-01-01 21:11:32 +0000637 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
638 MachineOperand &MO = Addr[i];
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000639 if (MO.isReg())
Owen Anderson81875432008-01-01 21:11:32 +0000640 MIB.addReg(MO.getReg());
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000641 else if (MO.isImm())
Owen Anderson81875432008-01-01 21:11:32 +0000642 MIB.addImm(MO.getImm());
643 else
644 MIB.addFrameIndex(MO.getIndex());
645 }
646 NewMIs.push_back(MIB);
647 return;
648}
649
Owen Anderson9a184ef2008-01-07 01:35:02 +0000650/// foldMemoryOperand - PowerPC (like most RISC's) can only fold spills into
651/// copy instructions, turning them into load/store instructions.
Dan Gohmanedc83d62008-12-03 18:43:12 +0000652MachineInstr *PPCInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
653 MachineInstr *MI,
654 const SmallVectorImpl<unsigned> &Ops,
655 int FrameIndex) const {
Owen Anderson9a184ef2008-01-07 01:35:02 +0000656 if (Ops.size() != 1) return NULL;
657
658 // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because
659 // it takes more than one instruction to store it.
660 unsigned Opc = MI->getOpcode();
661 unsigned OpNum = Ops[0];
662
663 MachineInstr *NewMI = NULL;
664 if ((Opc == PPC::OR &&
665 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
666 if (OpNum == 0) { // move -> store
667 unsigned InReg = MI->getOperand(1).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000668 bool isKill = MI->getOperand(1).isKill();
Dan Gohman221a4372008-07-07 23:14:23 +0000669 NewMI = addFrameReference(BuildMI(MF, get(PPC::STW))
Evan Chenge52c1912008-07-03 09:09:37 +0000670 .addReg(InReg, false, false, isKill),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000671 FrameIndex);
672 } else { // move -> load
673 unsigned OutReg = MI->getOperand(0).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000674 bool isDead = MI->getOperand(0).isDead();
Dan Gohman221a4372008-07-07 23:14:23 +0000675 NewMI = addFrameReference(BuildMI(MF, get(PPC::LWZ))
Evan Chenge52c1912008-07-03 09:09:37 +0000676 .addReg(OutReg, true, false, false, isDead),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000677 FrameIndex);
678 }
679 } else if ((Opc == PPC::OR8 &&
680 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
681 if (OpNum == 0) { // move -> store
682 unsigned InReg = MI->getOperand(1).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000683 bool isKill = MI->getOperand(1).isKill();
Dan Gohman221a4372008-07-07 23:14:23 +0000684 NewMI = addFrameReference(BuildMI(MF, get(PPC::STD))
Evan Chenge52c1912008-07-03 09:09:37 +0000685 .addReg(InReg, false, false, isKill),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000686 FrameIndex);
687 } else { // move -> load
688 unsigned OutReg = MI->getOperand(0).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000689 bool isDead = MI->getOperand(0).isDead();
Dan Gohman221a4372008-07-07 23:14:23 +0000690 NewMI = addFrameReference(BuildMI(MF, get(PPC::LD))
Evan Chenge52c1912008-07-03 09:09:37 +0000691 .addReg(OutReg, true, false, false, isDead),
692 FrameIndex);
Owen Anderson9a184ef2008-01-07 01:35:02 +0000693 }
694 } else if (Opc == PPC::FMRD) {
695 if (OpNum == 0) { // move -> store
696 unsigned InReg = MI->getOperand(1).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000697 bool isKill = MI->getOperand(1).isKill();
Dan Gohman221a4372008-07-07 23:14:23 +0000698 NewMI = addFrameReference(BuildMI(MF, get(PPC::STFD))
Evan Chenge52c1912008-07-03 09:09:37 +0000699 .addReg(InReg, false, false, isKill),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000700 FrameIndex);
701 } else { // move -> load
702 unsigned OutReg = MI->getOperand(0).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000703 bool isDead = MI->getOperand(0).isDead();
Dan Gohman221a4372008-07-07 23:14:23 +0000704 NewMI = addFrameReference(BuildMI(MF, get(PPC::LFD))
Evan Chenge52c1912008-07-03 09:09:37 +0000705 .addReg(OutReg, true, false, false, isDead),
706 FrameIndex);
Owen Anderson9a184ef2008-01-07 01:35:02 +0000707 }
708 } else if (Opc == PPC::FMRS) {
709 if (OpNum == 0) { // move -> store
710 unsigned InReg = MI->getOperand(1).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000711 bool isKill = MI->getOperand(1).isKill();
Dan Gohman221a4372008-07-07 23:14:23 +0000712 NewMI = addFrameReference(BuildMI(MF, get(PPC::STFS))
Evan Chenge52c1912008-07-03 09:09:37 +0000713 .addReg(InReg, false, false, isKill),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000714 FrameIndex);
715 } else { // move -> load
716 unsigned OutReg = MI->getOperand(0).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000717 bool isDead = MI->getOperand(0).isDead();
Dan Gohman221a4372008-07-07 23:14:23 +0000718 NewMI = addFrameReference(BuildMI(MF, get(PPC::LFS))
Evan Chenge52c1912008-07-03 09:09:37 +0000719 .addReg(OutReg, true, false, false, isDead),
720 FrameIndex);
Owen Anderson9a184ef2008-01-07 01:35:02 +0000721 }
722 }
723
Owen Anderson9a184ef2008-01-07 01:35:02 +0000724 return NewMI;
725}
726
Dan Gohman46b948e2008-10-16 01:49:15 +0000727bool PPCInstrInfo::canFoldMemoryOperand(const MachineInstr *MI,
728 const SmallVectorImpl<unsigned> &Ops) const {
Owen Anderson9a184ef2008-01-07 01:35:02 +0000729 if (Ops.size() != 1) return false;
730
731 // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because
732 // it takes more than one instruction to store it.
733 unsigned Opc = MI->getOpcode();
734
735 if ((Opc == PPC::OR &&
736 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
737 return true;
738 else if ((Opc == PPC::OR8 &&
739 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
740 return true;
741 else if (Opc == PPC::FMRD || Opc == PPC::FMRS)
742 return true;
743
744 return false;
745}
746
Owen Anderson81875432008-01-01 21:11:32 +0000747
Dan Gohman46b948e2008-10-16 01:49:15 +0000748bool PPCInstrInfo::BlockHasNoFallThrough(const MachineBasicBlock &MBB) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000749 if (MBB.empty()) return false;
750
751 switch (MBB.back().getOpcode()) {
752 case PPC::BLR: // Return.
753 case PPC::B: // Uncond branch.
754 case PPC::BCTR: // Indirect branch.
755 return true;
756 default: return false;
757 }
758}
759
760bool PPCInstrInfo::
Owen Andersond131b5b2008-08-14 22:49:33 +0000761ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000762 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
763 // Leave the CR# the same, but invert the condition.
764 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
765 return false;
766}
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000767
768/// GetInstSize - Return the number of bytes of code the specified
769/// instruction may be. This returns the maximum number of bytes.
770///
771unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
772 switch (MI->getOpcode()) {
773 case PPC::INLINEASM: { // Inline Asm: Variable size.
774 const MachineFunction *MF = MI->getParent()->getParent();
775 const char *AsmStr = MI->getOperand(0).getSymbolName();
776 return MF->getTarget().getTargetAsmInfo()->getInlineAsmLength(AsmStr);
777 }
Dan Gohmanfa607c92008-07-01 00:05:16 +0000778 case PPC::DBG_LABEL:
779 case PPC::EH_LABEL:
780 case PPC::GC_LABEL:
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000781 return 0;
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000782 default:
783 return 4; // PowerPC instructions are all 4 bytes
784 }
785}