blob: 3e0c7e73ac34d836789d908bf4abc295b45103fc [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,
Evan Chengeac31642009-02-09 07:14:22 +0000207 SmallVectorImpl<MachineOperand> &Cond,
208 bool AllowModify) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000209 // If the block has no terminators, it just falls into the block after it.
210 MachineBasicBlock::iterator I = MBB.end();
211 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
212 return false;
213
214 // Get the last instruction in the block.
215 MachineInstr *LastInst = I;
216
217 // If there is only one terminator instruction, process it.
218 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
219 if (LastInst->getOpcode() == PPC::B) {
Chris Lattner6017d482007-12-30 23:10:15 +0000220 TBB = LastInst->getOperand(0).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000221 return false;
222 } else if (LastInst->getOpcode() == PPC::BCC) {
223 // Block ends with fall-through condbranch.
Chris Lattner6017d482007-12-30 23:10:15 +0000224 TBB = LastInst->getOperand(2).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000225 Cond.push_back(LastInst->getOperand(0));
226 Cond.push_back(LastInst->getOperand(1));
227 return false;
228 }
229 // Otherwise, don't know what this is.
230 return true;
231 }
232
233 // Get the instruction before it if it's a terminator.
234 MachineInstr *SecondLastInst = I;
235
236 // If there are three terminators, we don't know what sort of block this is.
237 if (SecondLastInst && I != MBB.begin() &&
238 isUnpredicatedTerminator(--I))
239 return true;
240
241 // If the block ends with PPC::B and PPC:BCC, handle it.
242 if (SecondLastInst->getOpcode() == PPC::BCC &&
243 LastInst->getOpcode() == PPC::B) {
Chris Lattner6017d482007-12-30 23:10:15 +0000244 TBB = SecondLastInst->getOperand(2).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000245 Cond.push_back(SecondLastInst->getOperand(0));
246 Cond.push_back(SecondLastInst->getOperand(1));
Chris Lattner6017d482007-12-30 23:10:15 +0000247 FBB = LastInst->getOperand(0).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000248 return false;
249 }
250
251 // If the block ends with two PPC:Bs, handle it. The second one is not
252 // executed, so remove it.
253 if (SecondLastInst->getOpcode() == PPC::B &&
254 LastInst->getOpcode() == PPC::B) {
Chris Lattner6017d482007-12-30 23:10:15 +0000255 TBB = SecondLastInst->getOperand(0).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000256 I = LastInst;
Evan Chengeac31642009-02-09 07:14:22 +0000257 if (AllowModify)
258 I->eraseFromParent();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000259 return false;
260 }
261
262 // Otherwise, can't handle this.
263 return true;
264}
265
266unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
267 MachineBasicBlock::iterator I = MBB.end();
268 if (I == MBB.begin()) return 0;
269 --I;
270 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC)
271 return 0;
272
273 // Remove the branch.
274 I->eraseFromParent();
275
276 I = MBB.end();
277
278 if (I == MBB.begin()) return 1;
279 --I;
280 if (I->getOpcode() != PPC::BCC)
281 return 1;
282
283 // Remove the branch.
284 I->eraseFromParent();
285 return 2;
286}
287
288unsigned
289PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
290 MachineBasicBlock *FBB,
Owen Andersond131b5b2008-08-14 22:49:33 +0000291 const SmallVectorImpl<MachineOperand> &Cond) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000292 // Shouldn't be a fall through.
293 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
294 assert((Cond.size() == 2 || Cond.size() == 0) &&
295 "PPC branch conditions have two components!");
296
297 // One-way branch.
298 if (FBB == 0) {
299 if (Cond.empty()) // Unconditional branch
300 BuildMI(&MBB, get(PPC::B)).addMBB(TBB);
301 else // Conditional branch
302 BuildMI(&MBB, get(PPC::BCC))
303 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
304 return 1;
305 }
306
307 // Two-way Conditional Branch.
308 BuildMI(&MBB, get(PPC::BCC))
309 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
310 BuildMI(&MBB, get(PPC::B)).addMBB(FBB);
311 return 2;
312}
313
Owen Anderson9fa72d92008-08-26 18:03:31 +0000314bool PPCInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
Owen Anderson8f2c8932007-12-31 06:32:00 +0000315 MachineBasicBlock::iterator MI,
316 unsigned DestReg, unsigned SrcReg,
317 const TargetRegisterClass *DestRC,
318 const TargetRegisterClass *SrcRC) const {
319 if (DestRC != SrcRC) {
Owen Anderson9fa72d92008-08-26 18:03:31 +0000320 // Not yet supported!
321 return false;
Owen Anderson8f2c8932007-12-31 06:32:00 +0000322 }
323
324 if (DestRC == PPC::GPRCRegisterClass) {
325 BuildMI(MBB, MI, get(PPC::OR), DestReg).addReg(SrcReg).addReg(SrcReg);
326 } else if (DestRC == PPC::G8RCRegisterClass) {
327 BuildMI(MBB, MI, get(PPC::OR8), DestReg).addReg(SrcReg).addReg(SrcReg);
328 } else if (DestRC == PPC::F4RCRegisterClass) {
329 BuildMI(MBB, MI, get(PPC::FMRS), DestReg).addReg(SrcReg);
330 } else if (DestRC == PPC::F8RCRegisterClass) {
331 BuildMI(MBB, MI, get(PPC::FMRD), DestReg).addReg(SrcReg);
332 } else if (DestRC == PPC::CRRCRegisterClass) {
333 BuildMI(MBB, MI, get(PPC::MCRF), DestReg).addReg(SrcReg);
334 } else if (DestRC == PPC::VRRCRegisterClass) {
335 BuildMI(MBB, MI, get(PPC::VOR), DestReg).addReg(SrcReg).addReg(SrcReg);
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000336 } else if (DestRC == PPC::CRBITRCRegisterClass) {
337 BuildMI(MBB, MI, get(PPC::CROR), DestReg).addReg(SrcReg).addReg(SrcReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000338 } else {
Owen Anderson9fa72d92008-08-26 18:03:31 +0000339 // Attempt to copy register that is not GPR or FPR
340 return false;
Owen Anderson8f2c8932007-12-31 06:32:00 +0000341 }
Owen Anderson9fa72d92008-08-26 18:03:31 +0000342
343 return true;
Owen Anderson8f2c8932007-12-31 06:32:00 +0000344}
345
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000346bool
Dan Gohman221a4372008-07-07 23:14:23 +0000347PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
348 unsigned SrcReg, bool isKill,
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000349 int FrameIdx,
350 const TargetRegisterClass *RC,
351 SmallVectorImpl<MachineInstr*> &NewMIs) const{
Owen Anderson81875432008-01-01 21:11:32 +0000352 if (RC == PPC::GPRCRegisterClass) {
353 if (SrcReg != PPC::LR) {
Dan Gohman221a4372008-07-07 23:14:23 +0000354 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STW))
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000355 .addReg(SrcReg, false, false, isKill),
356 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000357 } else {
358 // FIXME: this spills LR immediately to memory in one step. To do this,
359 // we use R11, which we know cannot be used in the prolog/epilog. This is
360 // a hack.
Dan Gohman221a4372008-07-07 23:14:23 +0000361 NewMIs.push_back(BuildMI(MF, get(PPC::MFLR), PPC::R11));
362 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STW))
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000363 .addReg(PPC::R11, false, false, isKill),
364 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000365 }
366 } else if (RC == PPC::G8RCRegisterClass) {
367 if (SrcReg != PPC::LR8) {
Dan Gohman221a4372008-07-07 23:14:23 +0000368 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STD))
Chris Lattner7b7371c2008-03-10 18:55:53 +0000369 .addReg(SrcReg, false, false, isKill), FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000370 } else {
371 // FIXME: this spills LR immediately to memory in one step. To do this,
372 // we use R11, which we know cannot be used in the prolog/epilog. This is
373 // a hack.
Dan Gohman221a4372008-07-07 23:14:23 +0000374 NewMIs.push_back(BuildMI(MF, get(PPC::MFLR8), PPC::X11));
375 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STD))
Chris Lattner7b7371c2008-03-10 18:55:53 +0000376 .addReg(PPC::X11, false, false, isKill), FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000377 }
378 } else if (RC == PPC::F8RCRegisterClass) {
Dan Gohman221a4372008-07-07 23:14:23 +0000379 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STFD))
Chris Lattner7b7371c2008-03-10 18:55:53 +0000380 .addReg(SrcReg, false, false, isKill), FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000381 } else if (RC == PPC::F4RCRegisterClass) {
Dan Gohman221a4372008-07-07 23:14:23 +0000382 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STFS))
Chris Lattner7b7371c2008-03-10 18:55:53 +0000383 .addReg(SrcReg, false, false, isKill), FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000384 } else if (RC == PPC::CRRCRegisterClass) {
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000385 if ((EnablePPC32RS && !TM.getSubtargetImpl()->isPPC64()) ||
386 (EnablePPC64RS && TM.getSubtargetImpl()->isPPC64())) {
387 // FIXME (64-bit): Enable
Dan Gohman221a4372008-07-07 23:14:23 +0000388 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::SPILL_CR))
Bill Wendlinga1877c52008-03-03 22:19:16 +0000389 .addReg(SrcReg, false, false, isKill),
Chris Lattner6734c3a2008-03-20 01:22:40 +0000390 FrameIdx));
Bill Wendlinga1877c52008-03-03 22:19:16 +0000391 return true;
392 } else {
393 // FIXME: We use R0 here, because it isn't available for RA. We need to
394 // store the CR in the low 4-bits of the saved value. First, issue a MFCR
395 // to save all of the CRBits.
Dan Gohman221a4372008-07-07 23:14:23 +0000396 NewMIs.push_back(BuildMI(MF, get(PPC::MFCR), PPC::R0));
Owen Anderson81875432008-01-01 21:11:32 +0000397
Bill Wendlinga1877c52008-03-03 22:19:16 +0000398 // If the saved register wasn't CR0, shift the bits left so that they are
399 // in CR0's slot.
400 if (SrcReg != PPC::CR0) {
401 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(SrcReg)*4;
402 // rlwinm r0, r0, ShiftBits, 0, 31.
Dan Gohman221a4372008-07-07 23:14:23 +0000403 NewMIs.push_back(BuildMI(MF, get(PPC::RLWINM), PPC::R0)
Chris Lattner7b7371c2008-03-10 18:55:53 +0000404 .addReg(PPC::R0).addImm(ShiftBits).addImm(0).addImm(31));
Bill Wendlinga1877c52008-03-03 22:19:16 +0000405 }
406
Dan Gohman221a4372008-07-07 23:14:23 +0000407 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STW))
Bill Wendlinga1877c52008-03-03 22:19:16 +0000408 .addReg(PPC::R0, false, false, isKill),
409 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000410 }
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000411 } else if (RC == PPC::CRBITRCRegisterClass) {
412 // FIXME: We use CRi here because there is no mtcrf on a bit. Since the
413 // backend currently only uses CR1EQ as an individual bit, this should
414 // not cause any bug. If we need other uses of CR bits, the following
415 // code may be invalid.
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000416 unsigned Reg = 0;
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000417 if (SrcReg >= PPC::CR0LT || SrcReg <= PPC::CR0UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000418 Reg = PPC::CR0;
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000419 else if (SrcReg >= PPC::CR1LT || SrcReg <= PPC::CR1UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000420 Reg = PPC::CR1;
421 else if (SrcReg >= PPC::CR2LT || SrcReg <= PPC::CR2UN)
422 Reg = PPC::CR2;
423 else if (SrcReg >= PPC::CR3LT || SrcReg <= PPC::CR3UN)
424 Reg = PPC::CR3;
425 else if (SrcReg >= PPC::CR4LT || SrcReg <= PPC::CR4UN)
426 Reg = PPC::CR4;
427 else if (SrcReg >= PPC::CR5LT || SrcReg <= PPC::CR5UN)
428 Reg = PPC::CR5;
429 else if (SrcReg >= PPC::CR6LT || SrcReg <= PPC::CR6UN)
430 Reg = PPC::CR6;
431 else if (SrcReg >= PPC::CR7LT || SrcReg <= PPC::CR7UN)
432 Reg = PPC::CR7;
433
Dan Gohman221a4372008-07-07 23:14:23 +0000434 return StoreRegToStackSlot(MF, Reg, isKill, FrameIdx,
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000435 PPC::CRRCRegisterClass, NewMIs);
436
Owen Anderson81875432008-01-01 21:11:32 +0000437 } else if (RC == PPC::VRRCRegisterClass) {
438 // We don't have indexed addressing for vector loads. Emit:
439 // R0 = ADDI FI#
440 // STVX VAL, 0, R0
441 //
442 // FIXME: We use R0 here, because it isn't available for RA.
Dan Gohman221a4372008-07-07 23:14:23 +0000443 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::ADDI), PPC::R0),
Owen Anderson81875432008-01-01 21:11:32 +0000444 FrameIdx, 0, 0));
Dan Gohman221a4372008-07-07 23:14:23 +0000445 NewMIs.push_back(BuildMI(MF, get(PPC::STVX))
Chris Lattner7b7371c2008-03-10 18:55:53 +0000446 .addReg(SrcReg, false, false, isKill).addReg(PPC::R0).addReg(PPC::R0));
Owen Anderson81875432008-01-01 21:11:32 +0000447 } else {
448 assert(0 && "Unknown regclass!");
449 abort();
450 }
Bill Wendlinga1877c52008-03-03 22:19:16 +0000451
452 return false;
Owen Anderson81875432008-01-01 21:11:32 +0000453}
454
455void
456PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Bill Wendlinga1877c52008-03-03 22:19:16 +0000457 MachineBasicBlock::iterator MI,
458 unsigned SrcReg, bool isKill, int FrameIdx,
459 const TargetRegisterClass *RC) const {
Dan Gohman221a4372008-07-07 23:14:23 +0000460 MachineFunction &MF = *MBB.getParent();
Owen Anderson81875432008-01-01 21:11:32 +0000461 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendlinga1877c52008-03-03 22:19:16 +0000462
Dan Gohman221a4372008-07-07 23:14:23 +0000463 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs)) {
464 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Bill Wendlinga1877c52008-03-03 22:19:16 +0000465 FuncInfo->setSpillsCR();
466 }
467
Owen Anderson81875432008-01-01 21:11:32 +0000468 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
469 MBB.insert(MI, NewMIs[i]);
470}
471
472void PPCInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
Bill Wendlinga1877c52008-03-03 22:19:16 +0000473 bool isKill,
474 SmallVectorImpl<MachineOperand> &Addr,
475 const TargetRegisterClass *RC,
476 SmallVectorImpl<MachineInstr*> &NewMIs) const{
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000477 if (Addr[0].isFI()) {
Dan Gohman221a4372008-07-07 23:14:23 +0000478 if (StoreRegToStackSlot(MF, SrcReg, isKill,
479 Addr[0].getIndex(), RC, NewMIs)) {
Bill Wendlinga1877c52008-03-03 22:19:16 +0000480 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
481 FuncInfo->setSpillsCR();
482 }
483
Owen Anderson81875432008-01-01 21:11:32 +0000484 return;
485 }
486
487 unsigned Opc = 0;
488 if (RC == PPC::GPRCRegisterClass) {
489 Opc = PPC::STW;
490 } else if (RC == PPC::G8RCRegisterClass) {
491 Opc = PPC::STD;
492 } else if (RC == PPC::F8RCRegisterClass) {
493 Opc = PPC::STFD;
494 } else if (RC == PPC::F4RCRegisterClass) {
495 Opc = PPC::STFS;
496 } else if (RC == PPC::VRRCRegisterClass) {
497 Opc = PPC::STVX;
498 } else {
499 assert(0 && "Unknown regclass!");
500 abort();
501 }
Dan Gohman221a4372008-07-07 23:14:23 +0000502 MachineInstrBuilder MIB = BuildMI(MF, get(Opc))
Owen Anderson81875432008-01-01 21:11:32 +0000503 .addReg(SrcReg, false, false, isKill);
504 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
505 MachineOperand &MO = Addr[i];
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000506 if (MO.isReg())
Owen Anderson81875432008-01-01 21:11:32 +0000507 MIB.addReg(MO.getReg());
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000508 else if (MO.isImm())
Owen Anderson81875432008-01-01 21:11:32 +0000509 MIB.addImm(MO.getImm());
510 else
511 MIB.addFrameIndex(MO.getIndex());
512 }
513 NewMIs.push_back(MIB);
514 return;
515}
516
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000517void
Dan Gohman221a4372008-07-07 23:14:23 +0000518PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF,
519 unsigned DestReg, int FrameIdx,
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000520 const TargetRegisterClass *RC,
521 SmallVectorImpl<MachineInstr*> &NewMIs)const{
Owen Anderson81875432008-01-01 21:11:32 +0000522 if (RC == PPC::GPRCRegisterClass) {
523 if (DestReg != PPC::LR) {
Dan Gohman221a4372008-07-07 23:14:23 +0000524 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LWZ), DestReg),
Owen Anderson81875432008-01-01 21:11:32 +0000525 FrameIdx));
526 } else {
Dan Gohman221a4372008-07-07 23:14:23 +0000527 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LWZ), PPC::R11),
Owen Anderson81875432008-01-01 21:11:32 +0000528 FrameIdx));
Dan Gohman221a4372008-07-07 23:14:23 +0000529 NewMIs.push_back(BuildMI(MF, get(PPC::MTLR)).addReg(PPC::R11));
Owen Anderson81875432008-01-01 21:11:32 +0000530 }
531 } else if (RC == PPC::G8RCRegisterClass) {
532 if (DestReg != PPC::LR8) {
Dan Gohman221a4372008-07-07 23:14:23 +0000533 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LD), DestReg),
Owen Anderson81875432008-01-01 21:11:32 +0000534 FrameIdx));
535 } else {
Dan Gohman221a4372008-07-07 23:14:23 +0000536 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LD), PPC::R11),
Owen Anderson81875432008-01-01 21:11:32 +0000537 FrameIdx));
Dan Gohman221a4372008-07-07 23:14:23 +0000538 NewMIs.push_back(BuildMI(MF, get(PPC::MTLR8)).addReg(PPC::R11));
Owen Anderson81875432008-01-01 21:11:32 +0000539 }
540 } else if (RC == PPC::F8RCRegisterClass) {
Dan Gohman221a4372008-07-07 23:14:23 +0000541 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LFD), DestReg),
Owen Anderson81875432008-01-01 21:11:32 +0000542 FrameIdx));
543 } else if (RC == PPC::F4RCRegisterClass) {
Dan Gohman221a4372008-07-07 23:14:23 +0000544 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LFS), DestReg),
Owen Anderson81875432008-01-01 21:11:32 +0000545 FrameIdx));
546 } else if (RC == PPC::CRRCRegisterClass) {
547 // FIXME: We use R0 here, because it isn't available for RA.
Dan Gohman221a4372008-07-07 23:14:23 +0000548 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LWZ), PPC::R0),
Owen Anderson81875432008-01-01 21:11:32 +0000549 FrameIdx));
550
551 // If the reloaded register isn't CR0, shift the bits right so that they are
552 // in the right CR's slot.
553 if (DestReg != PPC::CR0) {
554 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(DestReg)*4;
555 // rlwinm r11, r11, 32-ShiftBits, 0, 31.
Dan Gohman221a4372008-07-07 23:14:23 +0000556 NewMIs.push_back(BuildMI(MF, get(PPC::RLWINM), PPC::R0)
Owen Anderson81875432008-01-01 21:11:32 +0000557 .addReg(PPC::R0).addImm(32-ShiftBits).addImm(0).addImm(31));
558 }
559
Dan Gohman221a4372008-07-07 23:14:23 +0000560 NewMIs.push_back(BuildMI(MF, get(PPC::MTCRF), DestReg).addReg(PPC::R0));
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000561 } else if (RC == PPC::CRBITRCRegisterClass) {
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000562
563 unsigned Reg = 0;
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000564 if (DestReg >= PPC::CR0LT || DestReg <= PPC::CR0UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000565 Reg = PPC::CR0;
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000566 else if (DestReg >= PPC::CR1LT || DestReg <= PPC::CR1UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000567 Reg = PPC::CR1;
568 else if (DestReg >= PPC::CR2LT || DestReg <= PPC::CR2UN)
569 Reg = PPC::CR2;
570 else if (DestReg >= PPC::CR3LT || DestReg <= PPC::CR3UN)
571 Reg = PPC::CR3;
572 else if (DestReg >= PPC::CR4LT || DestReg <= PPC::CR4UN)
573 Reg = PPC::CR4;
574 else if (DestReg >= PPC::CR5LT || DestReg <= PPC::CR5UN)
575 Reg = PPC::CR5;
576 else if (DestReg >= PPC::CR6LT || DestReg <= PPC::CR6UN)
577 Reg = PPC::CR6;
578 else if (DestReg >= PPC::CR7LT || DestReg <= PPC::CR7UN)
579 Reg = PPC::CR7;
580
Dan Gohman221a4372008-07-07 23:14:23 +0000581 return LoadRegFromStackSlot(MF, Reg, FrameIdx,
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000582 PPC::CRRCRegisterClass, NewMIs);
583
Owen Anderson81875432008-01-01 21:11:32 +0000584 } else if (RC == PPC::VRRCRegisterClass) {
585 // We don't have indexed addressing for vector loads. Emit:
586 // R0 = ADDI FI#
587 // Dest = LVX 0, R0
588 //
589 // FIXME: We use R0 here, because it isn't available for RA.
Dan Gohman221a4372008-07-07 23:14:23 +0000590 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::ADDI), PPC::R0),
Owen Anderson81875432008-01-01 21:11:32 +0000591 FrameIdx, 0, 0));
Dan Gohman221a4372008-07-07 23:14:23 +0000592 NewMIs.push_back(BuildMI(MF, get(PPC::LVX),DestReg).addReg(PPC::R0)
Owen Anderson81875432008-01-01 21:11:32 +0000593 .addReg(PPC::R0));
594 } else {
595 assert(0 && "Unknown regclass!");
596 abort();
597 }
598}
599
600void
601PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Bill Wendlinga1877c52008-03-03 22:19:16 +0000602 MachineBasicBlock::iterator MI,
603 unsigned DestReg, int FrameIdx,
604 const TargetRegisterClass *RC) const {
Dan Gohman221a4372008-07-07 23:14:23 +0000605 MachineFunction &MF = *MBB.getParent();
Owen Anderson81875432008-01-01 21:11:32 +0000606 SmallVector<MachineInstr*, 4> NewMIs;
Dan Gohman221a4372008-07-07 23:14:23 +0000607 LoadRegFromStackSlot(MF, DestReg, FrameIdx, RC, NewMIs);
Owen Anderson81875432008-01-01 21:11:32 +0000608 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
609 MBB.insert(MI, NewMIs[i]);
610}
611
612void PPCInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
Bill Wendlinga1877c52008-03-03 22:19:16 +0000613 SmallVectorImpl<MachineOperand> &Addr,
614 const TargetRegisterClass *RC,
615 SmallVectorImpl<MachineInstr*> &NewMIs)const{
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000616 if (Addr[0].isFI()) {
Dan Gohman221a4372008-07-07 23:14:23 +0000617 LoadRegFromStackSlot(MF, DestReg, Addr[0].getIndex(), RC, NewMIs);
Owen Anderson81875432008-01-01 21:11:32 +0000618 return;
619 }
620
621 unsigned Opc = 0;
622 if (RC == PPC::GPRCRegisterClass) {
623 assert(DestReg != PPC::LR && "Can't handle this yet!");
624 Opc = PPC::LWZ;
625 } else if (RC == PPC::G8RCRegisterClass) {
626 assert(DestReg != PPC::LR8 && "Can't handle this yet!");
627 Opc = PPC::LD;
628 } else if (RC == PPC::F8RCRegisterClass) {
629 Opc = PPC::LFD;
630 } else if (RC == PPC::F4RCRegisterClass) {
631 Opc = PPC::LFS;
632 } else if (RC == PPC::VRRCRegisterClass) {
633 Opc = PPC::LVX;
634 } else {
635 assert(0 && "Unknown regclass!");
636 abort();
637 }
Dan Gohman221a4372008-07-07 23:14:23 +0000638 MachineInstrBuilder MIB = BuildMI(MF, get(Opc), DestReg);
Owen Anderson81875432008-01-01 21:11:32 +0000639 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
640 MachineOperand &MO = Addr[i];
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000641 if (MO.isReg())
Owen Anderson81875432008-01-01 21:11:32 +0000642 MIB.addReg(MO.getReg());
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000643 else if (MO.isImm())
Owen Anderson81875432008-01-01 21:11:32 +0000644 MIB.addImm(MO.getImm());
645 else
646 MIB.addFrameIndex(MO.getIndex());
647 }
648 NewMIs.push_back(MIB);
649 return;
650}
651
Owen Anderson9a184ef2008-01-07 01:35:02 +0000652/// foldMemoryOperand - PowerPC (like most RISC's) can only fold spills into
653/// copy instructions, turning them into load/store instructions.
Dan Gohmanedc83d62008-12-03 18:43:12 +0000654MachineInstr *PPCInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
655 MachineInstr *MI,
656 const SmallVectorImpl<unsigned> &Ops,
657 int FrameIndex) const {
Owen Anderson9a184ef2008-01-07 01:35:02 +0000658 if (Ops.size() != 1) return NULL;
659
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];
664
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();
Evan Chenge52c1912008-07-03 09:09:37 +0000670 bool isKill = MI->getOperand(1).isKill();
Dan Gohman221a4372008-07-07 23:14:23 +0000671 NewMI = addFrameReference(BuildMI(MF, get(PPC::STW))
Evan Chenge52c1912008-07-03 09:09:37 +0000672 .addReg(InReg, false, false, isKill),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000673 FrameIndex);
674 } else { // move -> load
675 unsigned OutReg = MI->getOperand(0).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000676 bool isDead = MI->getOperand(0).isDead();
Dan Gohman221a4372008-07-07 23:14:23 +0000677 NewMI = addFrameReference(BuildMI(MF, get(PPC::LWZ))
Evan Chenge52c1912008-07-03 09:09:37 +0000678 .addReg(OutReg, true, false, false, isDead),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000679 FrameIndex);
680 }
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();
Evan Chenge52c1912008-07-03 09:09:37 +0000685 bool isKill = MI->getOperand(1).isKill();
Dan Gohman221a4372008-07-07 23:14:23 +0000686 NewMI = addFrameReference(BuildMI(MF, get(PPC::STD))
Evan Chenge52c1912008-07-03 09:09:37 +0000687 .addReg(InReg, false, false, isKill),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000688 FrameIndex);
689 } else { // move -> load
690 unsigned OutReg = MI->getOperand(0).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000691 bool isDead = MI->getOperand(0).isDead();
Dan Gohman221a4372008-07-07 23:14:23 +0000692 NewMI = addFrameReference(BuildMI(MF, get(PPC::LD))
Evan Chenge52c1912008-07-03 09:09:37 +0000693 .addReg(OutReg, true, false, false, isDead),
694 FrameIndex);
Owen Anderson9a184ef2008-01-07 01:35:02 +0000695 }
696 } else if (Opc == PPC::FMRD) {
697 if (OpNum == 0) { // move -> store
698 unsigned InReg = MI->getOperand(1).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000699 bool isKill = MI->getOperand(1).isKill();
Dan Gohman221a4372008-07-07 23:14:23 +0000700 NewMI = addFrameReference(BuildMI(MF, get(PPC::STFD))
Evan Chenge52c1912008-07-03 09:09:37 +0000701 .addReg(InReg, false, false, isKill),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000702 FrameIndex);
703 } else { // move -> load
704 unsigned OutReg = MI->getOperand(0).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000705 bool isDead = MI->getOperand(0).isDead();
Dan Gohman221a4372008-07-07 23:14:23 +0000706 NewMI = addFrameReference(BuildMI(MF, get(PPC::LFD))
Evan Chenge52c1912008-07-03 09:09:37 +0000707 .addReg(OutReg, true, false, false, isDead),
708 FrameIndex);
Owen Anderson9a184ef2008-01-07 01:35:02 +0000709 }
710 } else if (Opc == PPC::FMRS) {
711 if (OpNum == 0) { // move -> store
712 unsigned InReg = MI->getOperand(1).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000713 bool isKill = MI->getOperand(1).isKill();
Dan Gohman221a4372008-07-07 23:14:23 +0000714 NewMI = addFrameReference(BuildMI(MF, get(PPC::STFS))
Evan Chenge52c1912008-07-03 09:09:37 +0000715 .addReg(InReg, false, false, isKill),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000716 FrameIndex);
717 } else { // move -> load
718 unsigned OutReg = MI->getOperand(0).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000719 bool isDead = MI->getOperand(0).isDead();
Dan Gohman221a4372008-07-07 23:14:23 +0000720 NewMI = addFrameReference(BuildMI(MF, get(PPC::LFS))
Evan Chenge52c1912008-07-03 09:09:37 +0000721 .addReg(OutReg, true, false, false, isDead),
722 FrameIndex);
Owen Anderson9a184ef2008-01-07 01:35:02 +0000723 }
724 }
725
Owen Anderson9a184ef2008-01-07 01:35:02 +0000726 return NewMI;
727}
728
Dan Gohman46b948e2008-10-16 01:49:15 +0000729bool PPCInstrInfo::canFoldMemoryOperand(const MachineInstr *MI,
730 const SmallVectorImpl<unsigned> &Ops) const {
Owen Anderson9a184ef2008-01-07 01:35:02 +0000731 if (Ops.size() != 1) return false;
732
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();
736
737 if ((Opc == PPC::OR &&
738 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
739 return true;
740 else if ((Opc == PPC::OR8 &&
741 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
742 return true;
743 else if (Opc == PPC::FMRD || Opc == PPC::FMRS)
744 return true;
745
746 return false;
747}
748
Owen Anderson81875432008-01-01 21:11:32 +0000749
Dan Gohman46b948e2008-10-16 01:49:15 +0000750bool PPCInstrInfo::BlockHasNoFallThrough(const MachineBasicBlock &MBB) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000751 if (MBB.empty()) return false;
752
753 switch (MBB.back().getOpcode()) {
754 case PPC::BLR: // Return.
755 case PPC::B: // Uncond branch.
756 case PPC::BCTR: // Indirect branch.
757 return true;
758 default: return false;
759 }
760}
761
762bool PPCInstrInfo::
Owen Andersond131b5b2008-08-14 22:49:33 +0000763ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000764 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()));
767 return false;
768}
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000769
770/// GetInstSize - Return the number of bytes of code the specified
771/// instruction may be. This returns the maximum number of bytes.
772///
773unsigned 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);
779 }
Dan Gohmanfa607c92008-07-01 00:05:16 +0000780 case PPC::DBG_LABEL:
781 case PPC::EH_LABEL:
782 case PPC::GC_LABEL:
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000783 return 0;
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000784 default:
785 return 4; // PowerPC instructions are all 4 bytes
786 }
787}