blob: 674b36c5a38e9c4f6090339143b1003edb9eed38 [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
33/// getPointerRegClass - Return the register class to use to hold pointers.
34/// This is used for addressing modes.
35const TargetRegisterClass *PPCInstrInfo::getPointerRegClass() const {
36 if (TM.getSubtargetImpl()->isPPC64())
37 return &PPC::G8RCRegClass;
38 else
39 return &PPC::GPRCRegClass;
40}
41
42
43bool PPCInstrInfo::isMoveInstr(const MachineInstr& MI,
44 unsigned& sourceReg,
45 unsigned& destReg) const {
Chris Lattner99aa3372008-01-07 02:48:55 +000046 unsigned oc = MI.getOpcode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047 if (oc == PPC::OR || oc == PPC::OR8 || oc == PPC::VOR ||
48 oc == PPC::OR4To8 || oc == PPC::OR8To4) { // or r1, r2, r2
49 assert(MI.getNumOperands() >= 3 &&
50 MI.getOperand(0).isRegister() &&
51 MI.getOperand(1).isRegister() &&
52 MI.getOperand(2).isRegister() &&
53 "invalid PPC OR instruction!");
54 if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
55 sourceReg = MI.getOperand(1).getReg();
56 destReg = MI.getOperand(0).getReg();
57 return true;
58 }
59 } else if (oc == PPC::ADDI) { // addi r1, r2, 0
60 assert(MI.getNumOperands() >= 3 &&
61 MI.getOperand(0).isRegister() &&
62 MI.getOperand(2).isImmediate() &&
63 "invalid PPC ADDI instruction!");
Chris Lattnera96056a2007-12-30 20:49:49 +000064 if (MI.getOperand(1).isRegister() && MI.getOperand(2).getImm() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065 sourceReg = MI.getOperand(1).getReg();
66 destReg = MI.getOperand(0).getReg();
67 return true;
68 }
69 } else if (oc == PPC::ORI) { // ori r1, r2, 0
70 assert(MI.getNumOperands() >= 3 &&
71 MI.getOperand(0).isRegister() &&
72 MI.getOperand(1).isRegister() &&
73 MI.getOperand(2).isImmediate() &&
74 "invalid PPC ORI instruction!");
Chris Lattnera96056a2007-12-30 20:49:49 +000075 if (MI.getOperand(2).getImm() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076 sourceReg = MI.getOperand(1).getReg();
77 destReg = MI.getOperand(0).getReg();
78 return true;
79 }
80 } else if (oc == PPC::FMRS || oc == PPC::FMRD ||
81 oc == PPC::FMRSD) { // fmr r1, r2
82 assert(MI.getNumOperands() >= 2 &&
83 MI.getOperand(0).isRegister() &&
84 MI.getOperand(1).isRegister() &&
85 "invalid PPC FMR instruction");
86 sourceReg = MI.getOperand(1).getReg();
87 destReg = MI.getOperand(0).getReg();
88 return true;
89 } else if (oc == PPC::MCRF) { // mcrf cr1, cr2
90 assert(MI.getNumOperands() >= 2 &&
91 MI.getOperand(0).isRegister() &&
92 MI.getOperand(1).isRegister() &&
93 "invalid PPC MCRF instruction");
94 sourceReg = MI.getOperand(1).getReg();
95 destReg = MI.getOperand(0).getReg();
96 return true;
97 }
98 return false;
99}
100
101unsigned PPCInstrInfo::isLoadFromStackSlot(MachineInstr *MI,
102 int &FrameIndex) const {
103 switch (MI->getOpcode()) {
104 default: break;
105 case PPC::LD:
106 case PPC::LWZ:
107 case PPC::LFS:
108 case PPC::LFD:
Chris Lattner6017d482007-12-30 23:10:15 +0000109 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
110 MI->getOperand(2).isFI()) {
111 FrameIndex = MI->getOperand(2).getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112 return MI->getOperand(0).getReg();
113 }
114 break;
115 }
116 return 0;
117}
118
119unsigned PPCInstrInfo::isStoreToStackSlot(MachineInstr *MI,
120 int &FrameIndex) const {
121 switch (MI->getOpcode()) {
122 default: break;
123 case PPC::STD:
124 case PPC::STW:
125 case PPC::STFS:
126 case PPC::STFD:
Chris Lattner6017d482007-12-30 23:10:15 +0000127 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
128 MI->getOperand(2).isFI()) {
129 FrameIndex = MI->getOperand(2).getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000130 return MI->getOperand(0).getReg();
131 }
132 break;
133 }
134 return 0;
135}
136
137// commuteInstruction - We can commute rlwimi instructions, but only if the
138// rotate amt is zero. We also have to munge the immediates a bit.
Evan Cheng5de1aaf2008-06-16 07:33:11 +0000139MachineInstr *
140PPCInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
Dan Gohman221a4372008-07-07 23:14:23 +0000141 MachineFunction &MF = *MI->getParent()->getParent();
142
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000143 // Normal instructions can be commuted the obvious way.
144 if (MI->getOpcode() != PPC::RLWIMI)
Evan Cheng5de1aaf2008-06-16 07:33:11 +0000145 return TargetInstrInfoImpl::commuteInstruction(MI, NewMI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146
147 // Cannot commute if it has a non-zero rotate count.
Chris Lattnera96056a2007-12-30 20:49:49 +0000148 if (MI->getOperand(3).getImm() != 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149 return 0;
150
151 // If we have a zero rotate count, we have:
152 // M = mask(MB,ME)
153 // Op0 = (Op1 & ~M) | (Op2 & M)
154 // Change this to:
155 // M = mask((ME+1)&31, (MB-1)&31)
156 // Op0 = (Op2 & ~M) | (Op1 & M)
157
158 // Swap op1/op2
Evan Chengb554e532008-02-13 02:46:49 +0000159 unsigned Reg0 = MI->getOperand(0).getReg();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 unsigned Reg1 = MI->getOperand(1).getReg();
161 unsigned Reg2 = MI->getOperand(2).getReg();
162 bool Reg1IsKill = MI->getOperand(1).isKill();
163 bool Reg2IsKill = MI->getOperand(2).isKill();
Evan Cheng5de1aaf2008-06-16 07:33:11 +0000164 bool ChangeReg0 = false;
Evan Chengb554e532008-02-13 02:46:49 +0000165 // If machine instrs are no longer in two-address forms, update
166 // destination register as well.
167 if (Reg0 == Reg1) {
168 // Must be two address instruction!
169 assert(MI->getDesc().getOperandConstraint(0, TOI::TIED_TO) &&
170 "Expecting a two-address instruction!");
Evan Chengb554e532008-02-13 02:46:49 +0000171 Reg2IsKill = false;
Evan Cheng5de1aaf2008-06-16 07:33:11 +0000172 ChangeReg0 = true;
Evan Chengb554e532008-02-13 02:46:49 +0000173 }
Evan Cheng5de1aaf2008-06-16 07:33:11 +0000174
175 // Masks.
176 unsigned MB = MI->getOperand(4).getImm();
177 unsigned ME = MI->getOperand(5).getImm();
178
179 if (NewMI) {
180 // Create a new instruction.
181 unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg();
182 bool Reg0IsDead = MI->getOperand(0).isDead();
Dan Gohman221a4372008-07-07 23:14:23 +0000183 return BuildMI(MF, MI->getDesc())
184 .addReg(Reg0, true, false, false, Reg0IsDead)
Evan Cheng5de1aaf2008-06-16 07:33:11 +0000185 .addReg(Reg2, false, false, Reg2IsKill)
186 .addReg(Reg1, false, false, Reg1IsKill)
187 .addImm((ME+1) & 31)
188 .addImm((MB-1) & 31);
189 }
190
191 if (ChangeReg0)
192 MI->getOperand(0).setReg(Reg2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000193 MI->getOperand(2).setReg(Reg1);
194 MI->getOperand(1).setReg(Reg2);
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000195 MI->getOperand(2).setIsKill(Reg1IsKill);
196 MI->getOperand(1).setIsKill(Reg2IsKill);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197
198 // Swap the mask around.
Chris Lattnera96056a2007-12-30 20:49:49 +0000199 MI->getOperand(4).setImm((ME+1) & 31);
200 MI->getOperand(5).setImm((MB-1) & 31);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201 return MI;
202}
203
204void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
205 MachineBasicBlock::iterator MI) const {
206 BuildMI(MBB, MI, get(PPC::NOP));
207}
208
209
210// Branch analysis.
211bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
212 MachineBasicBlock *&FBB,
Owen Andersond131b5b2008-08-14 22:49:33 +0000213 SmallVectorImpl<MachineOperand> &Cond) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000214 // If the block has no terminators, it just falls into the block after it.
215 MachineBasicBlock::iterator I = MBB.end();
216 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
217 return false;
218
219 // Get the last instruction in the block.
220 MachineInstr *LastInst = I;
221
222 // If there is only one terminator instruction, process it.
223 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
224 if (LastInst->getOpcode() == PPC::B) {
Chris Lattner6017d482007-12-30 23:10:15 +0000225 TBB = LastInst->getOperand(0).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226 return false;
227 } else if (LastInst->getOpcode() == PPC::BCC) {
228 // Block ends with fall-through condbranch.
Chris Lattner6017d482007-12-30 23:10:15 +0000229 TBB = LastInst->getOperand(2).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230 Cond.push_back(LastInst->getOperand(0));
231 Cond.push_back(LastInst->getOperand(1));
232 return false;
233 }
234 // Otherwise, don't know what this is.
235 return true;
236 }
237
238 // Get the instruction before it if it's a terminator.
239 MachineInstr *SecondLastInst = I;
240
241 // If there are three terminators, we don't know what sort of block this is.
242 if (SecondLastInst && I != MBB.begin() &&
243 isUnpredicatedTerminator(--I))
244 return true;
245
246 // If the block ends with PPC::B and PPC:BCC, handle it.
247 if (SecondLastInst->getOpcode() == PPC::BCC &&
248 LastInst->getOpcode() == PPC::B) {
Chris Lattner6017d482007-12-30 23:10:15 +0000249 TBB = SecondLastInst->getOperand(2).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 Cond.push_back(SecondLastInst->getOperand(0));
251 Cond.push_back(SecondLastInst->getOperand(1));
Chris Lattner6017d482007-12-30 23:10:15 +0000252 FBB = LastInst->getOperand(0).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253 return false;
254 }
255
256 // If the block ends with two PPC:Bs, handle it. The second one is not
257 // executed, so remove it.
258 if (SecondLastInst->getOpcode() == PPC::B &&
259 LastInst->getOpcode() == PPC::B) {
Chris Lattner6017d482007-12-30 23:10:15 +0000260 TBB = SecondLastInst->getOperand(0).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261 I = LastInst;
262 I->eraseFromParent();
263 return false;
264 }
265
266 // Otherwise, can't handle this.
267 return true;
268}
269
270unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
271 MachineBasicBlock::iterator I = MBB.end();
272 if (I == MBB.begin()) return 0;
273 --I;
274 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC)
275 return 0;
276
277 // Remove the branch.
278 I->eraseFromParent();
279
280 I = MBB.end();
281
282 if (I == MBB.begin()) return 1;
283 --I;
284 if (I->getOpcode() != PPC::BCC)
285 return 1;
286
287 // Remove the branch.
288 I->eraseFromParent();
289 return 2;
290}
291
292unsigned
293PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
294 MachineBasicBlock *FBB,
Owen Andersond131b5b2008-08-14 22:49:33 +0000295 const SmallVectorImpl<MachineOperand> &Cond) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000296 // Shouldn't be a fall through.
297 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
298 assert((Cond.size() == 2 || Cond.size() == 0) &&
299 "PPC branch conditions have two components!");
300
301 // One-way branch.
302 if (FBB == 0) {
303 if (Cond.empty()) // Unconditional branch
304 BuildMI(&MBB, get(PPC::B)).addMBB(TBB);
305 else // Conditional branch
306 BuildMI(&MBB, get(PPC::BCC))
307 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
308 return 1;
309 }
310
311 // Two-way Conditional Branch.
312 BuildMI(&MBB, get(PPC::BCC))
313 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
314 BuildMI(&MBB, get(PPC::B)).addMBB(FBB);
315 return 2;
316}
317
Owen Anderson8f2c8932007-12-31 06:32:00 +0000318void PPCInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
319 MachineBasicBlock::iterator MI,
320 unsigned DestReg, unsigned SrcReg,
321 const TargetRegisterClass *DestRC,
322 const TargetRegisterClass *SrcRC) const {
323 if (DestRC != SrcRC) {
324 cerr << "Not yet supported!";
325 abort();
326 }
327
328 if (DestRC == PPC::GPRCRegisterClass) {
329 BuildMI(MBB, MI, get(PPC::OR), DestReg).addReg(SrcReg).addReg(SrcReg);
330 } else if (DestRC == PPC::G8RCRegisterClass) {
331 BuildMI(MBB, MI, get(PPC::OR8), DestReg).addReg(SrcReg).addReg(SrcReg);
332 } else if (DestRC == PPC::F4RCRegisterClass) {
333 BuildMI(MBB, MI, get(PPC::FMRS), DestReg).addReg(SrcReg);
334 } else if (DestRC == PPC::F8RCRegisterClass) {
335 BuildMI(MBB, MI, get(PPC::FMRD), DestReg).addReg(SrcReg);
336 } else if (DestRC == PPC::CRRCRegisterClass) {
337 BuildMI(MBB, MI, get(PPC::MCRF), DestReg).addReg(SrcReg);
338 } else if (DestRC == PPC::VRRCRegisterClass) {
339 BuildMI(MBB, MI, get(PPC::VOR), DestReg).addReg(SrcReg).addReg(SrcReg);
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000340 } else if (DestRC == PPC::CRBITRCRegisterClass) {
341 BuildMI(MBB, MI, get(PPC::CROR), DestReg).addReg(SrcReg).addReg(SrcReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000342 } else {
343 cerr << "Attempt to copy register that is not GPR or FPR";
344 abort();
345 }
346}
347
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000348bool
Dan Gohman221a4372008-07-07 23:14:23 +0000349PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
350 unsigned SrcReg, bool isKill,
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000351 int FrameIdx,
352 const TargetRegisterClass *RC,
353 SmallVectorImpl<MachineInstr*> &NewMIs) const{
Owen Anderson81875432008-01-01 21:11:32 +0000354 if (RC == PPC::GPRCRegisterClass) {
355 if (SrcReg != PPC::LR) {
Dan Gohman221a4372008-07-07 23:14:23 +0000356 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STW))
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000357 .addReg(SrcReg, false, false, isKill),
358 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000359 } else {
360 // FIXME: this spills LR immediately to memory in one step. To do this,
361 // we use R11, which we know cannot be used in the prolog/epilog. This is
362 // a hack.
Dan Gohman221a4372008-07-07 23:14:23 +0000363 NewMIs.push_back(BuildMI(MF, get(PPC::MFLR), PPC::R11));
364 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STW))
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000365 .addReg(PPC::R11, false, false, isKill),
366 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000367 }
368 } else if (RC == PPC::G8RCRegisterClass) {
369 if (SrcReg != PPC::LR8) {
Dan Gohman221a4372008-07-07 23:14:23 +0000370 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STD))
Chris Lattner7b7371c2008-03-10 18:55:53 +0000371 .addReg(SrcReg, false, false, isKill), FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000372 } else {
373 // FIXME: this spills LR immediately to memory in one step. To do this,
374 // we use R11, which we know cannot be used in the prolog/epilog. This is
375 // a hack.
Dan Gohman221a4372008-07-07 23:14:23 +0000376 NewMIs.push_back(BuildMI(MF, get(PPC::MFLR8), PPC::X11));
377 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STD))
Chris Lattner7b7371c2008-03-10 18:55:53 +0000378 .addReg(PPC::X11, false, false, isKill), FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000379 }
380 } else if (RC == PPC::F8RCRegisterClass) {
Dan Gohman221a4372008-07-07 23:14:23 +0000381 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STFD))
Chris Lattner7b7371c2008-03-10 18:55:53 +0000382 .addReg(SrcReg, false, false, isKill), FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000383 } else if (RC == PPC::F4RCRegisterClass) {
Dan Gohman221a4372008-07-07 23:14:23 +0000384 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STFS))
Chris Lattner7b7371c2008-03-10 18:55:53 +0000385 .addReg(SrcReg, false, false, isKill), FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000386 } else if (RC == PPC::CRRCRegisterClass) {
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000387 if ((EnablePPC32RS && !TM.getSubtargetImpl()->isPPC64()) ||
388 (EnablePPC64RS && TM.getSubtargetImpl()->isPPC64())) {
389 // FIXME (64-bit): Enable
Dan Gohman221a4372008-07-07 23:14:23 +0000390 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::SPILL_CR))
Bill Wendlinga1877c52008-03-03 22:19:16 +0000391 .addReg(SrcReg, false, false, isKill),
Chris Lattner6734c3a2008-03-20 01:22:40 +0000392 FrameIdx));
Bill Wendlinga1877c52008-03-03 22:19:16 +0000393 return true;
394 } else {
395 // FIXME: We use R0 here, because it isn't available for RA. We need to
396 // store the CR in the low 4-bits of the saved value. First, issue a MFCR
397 // to save all of the CRBits.
Dan Gohman221a4372008-07-07 23:14:23 +0000398 NewMIs.push_back(BuildMI(MF, get(PPC::MFCR), PPC::R0));
Owen Anderson81875432008-01-01 21:11:32 +0000399
Bill Wendlinga1877c52008-03-03 22:19:16 +0000400 // If the saved register wasn't CR0, shift the bits left so that they are
401 // in CR0's slot.
402 if (SrcReg != PPC::CR0) {
403 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(SrcReg)*4;
404 // rlwinm r0, r0, ShiftBits, 0, 31.
Dan Gohman221a4372008-07-07 23:14:23 +0000405 NewMIs.push_back(BuildMI(MF, get(PPC::RLWINM), PPC::R0)
Chris Lattner7b7371c2008-03-10 18:55:53 +0000406 .addReg(PPC::R0).addImm(ShiftBits).addImm(0).addImm(31));
Bill Wendlinga1877c52008-03-03 22:19:16 +0000407 }
408
Dan Gohman221a4372008-07-07 23:14:23 +0000409 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STW))
Bill Wendlinga1877c52008-03-03 22:19:16 +0000410 .addReg(PPC::R0, false, false, isKill),
411 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000412 }
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000413 } else if (RC == PPC::CRBITRCRegisterClass) {
414 // FIXME: We use CRi here because there is no mtcrf on a bit. Since the
415 // backend currently only uses CR1EQ as an individual bit, this should
416 // not cause any bug. If we need other uses of CR bits, the following
417 // code may be invalid.
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000418 unsigned Reg = 0;
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000419 if (SrcReg >= PPC::CR0LT || SrcReg <= PPC::CR0UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000420 Reg = PPC::CR0;
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000421 else if (SrcReg >= PPC::CR1LT || SrcReg <= PPC::CR1UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000422 Reg = PPC::CR1;
423 else if (SrcReg >= PPC::CR2LT || SrcReg <= PPC::CR2UN)
424 Reg = PPC::CR2;
425 else if (SrcReg >= PPC::CR3LT || SrcReg <= PPC::CR3UN)
426 Reg = PPC::CR3;
427 else if (SrcReg >= PPC::CR4LT || SrcReg <= PPC::CR4UN)
428 Reg = PPC::CR4;
429 else if (SrcReg >= PPC::CR5LT || SrcReg <= PPC::CR5UN)
430 Reg = PPC::CR5;
431 else if (SrcReg >= PPC::CR6LT || SrcReg <= PPC::CR6UN)
432 Reg = PPC::CR6;
433 else if (SrcReg >= PPC::CR7LT || SrcReg <= PPC::CR7UN)
434 Reg = PPC::CR7;
435
Dan Gohman221a4372008-07-07 23:14:23 +0000436 return StoreRegToStackSlot(MF, Reg, isKill, FrameIdx,
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000437 PPC::CRRCRegisterClass, NewMIs);
438
Owen Anderson81875432008-01-01 21:11:32 +0000439 } else if (RC == PPC::VRRCRegisterClass) {
440 // We don't have indexed addressing for vector loads. Emit:
441 // R0 = ADDI FI#
442 // STVX VAL, 0, R0
443 //
444 // FIXME: We use R0 here, because it isn't available for RA.
Dan Gohman221a4372008-07-07 23:14:23 +0000445 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::ADDI), PPC::R0),
Owen Anderson81875432008-01-01 21:11:32 +0000446 FrameIdx, 0, 0));
Dan Gohman221a4372008-07-07 23:14:23 +0000447 NewMIs.push_back(BuildMI(MF, get(PPC::STVX))
Chris Lattner7b7371c2008-03-10 18:55:53 +0000448 .addReg(SrcReg, false, false, isKill).addReg(PPC::R0).addReg(PPC::R0));
Owen Anderson81875432008-01-01 21:11:32 +0000449 } else {
450 assert(0 && "Unknown regclass!");
451 abort();
452 }
Bill Wendlinga1877c52008-03-03 22:19:16 +0000453
454 return false;
Owen Anderson81875432008-01-01 21:11:32 +0000455}
456
457void
458PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Bill Wendlinga1877c52008-03-03 22:19:16 +0000459 MachineBasicBlock::iterator MI,
460 unsigned SrcReg, bool isKill, int FrameIdx,
461 const TargetRegisterClass *RC) const {
Dan Gohman221a4372008-07-07 23:14:23 +0000462 MachineFunction &MF = *MBB.getParent();
Owen Anderson81875432008-01-01 21:11:32 +0000463 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendlinga1877c52008-03-03 22:19:16 +0000464
Dan Gohman221a4372008-07-07 23:14:23 +0000465 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs)) {
466 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Bill Wendlinga1877c52008-03-03 22:19:16 +0000467 FuncInfo->setSpillsCR();
468 }
469
Owen Anderson81875432008-01-01 21:11:32 +0000470 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
471 MBB.insert(MI, NewMIs[i]);
472}
473
474void PPCInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
Bill Wendlinga1877c52008-03-03 22:19:16 +0000475 bool isKill,
476 SmallVectorImpl<MachineOperand> &Addr,
477 const TargetRegisterClass *RC,
478 SmallVectorImpl<MachineInstr*> &NewMIs) const{
Owen Anderson81875432008-01-01 21:11:32 +0000479 if (Addr[0].isFrameIndex()) {
Dan Gohman221a4372008-07-07 23:14:23 +0000480 if (StoreRegToStackSlot(MF, SrcReg, isKill,
481 Addr[0].getIndex(), RC, NewMIs)) {
Bill Wendlinga1877c52008-03-03 22:19:16 +0000482 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
483 FuncInfo->setSpillsCR();
484 }
485
Owen Anderson81875432008-01-01 21:11:32 +0000486 return;
487 }
488
489 unsigned Opc = 0;
490 if (RC == PPC::GPRCRegisterClass) {
491 Opc = PPC::STW;
492 } else if (RC == PPC::G8RCRegisterClass) {
493 Opc = PPC::STD;
494 } else if (RC == PPC::F8RCRegisterClass) {
495 Opc = PPC::STFD;
496 } else if (RC == PPC::F4RCRegisterClass) {
497 Opc = PPC::STFS;
498 } else if (RC == PPC::VRRCRegisterClass) {
499 Opc = PPC::STVX;
500 } else {
501 assert(0 && "Unknown regclass!");
502 abort();
503 }
Dan Gohman221a4372008-07-07 23:14:23 +0000504 MachineInstrBuilder MIB = BuildMI(MF, get(Opc))
Owen Anderson81875432008-01-01 21:11:32 +0000505 .addReg(SrcReg, false, false, isKill);
506 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
507 MachineOperand &MO = Addr[i];
508 if (MO.isRegister())
509 MIB.addReg(MO.getReg());
510 else if (MO.isImmediate())
511 MIB.addImm(MO.getImm());
512 else
513 MIB.addFrameIndex(MO.getIndex());
514 }
515 NewMIs.push_back(MIB);
516 return;
517}
518
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000519void
Dan Gohman221a4372008-07-07 23:14:23 +0000520PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF,
521 unsigned DestReg, int FrameIdx,
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000522 const TargetRegisterClass *RC,
523 SmallVectorImpl<MachineInstr*> &NewMIs)const{
Owen Anderson81875432008-01-01 21:11:32 +0000524 if (RC == PPC::GPRCRegisterClass) {
525 if (DestReg != PPC::LR) {
Dan Gohman221a4372008-07-07 23:14:23 +0000526 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LWZ), DestReg),
Owen Anderson81875432008-01-01 21:11:32 +0000527 FrameIdx));
528 } else {
Dan Gohman221a4372008-07-07 23:14:23 +0000529 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LWZ), PPC::R11),
Owen Anderson81875432008-01-01 21:11:32 +0000530 FrameIdx));
Dan Gohman221a4372008-07-07 23:14:23 +0000531 NewMIs.push_back(BuildMI(MF, get(PPC::MTLR)).addReg(PPC::R11));
Owen Anderson81875432008-01-01 21:11:32 +0000532 }
533 } else if (RC == PPC::G8RCRegisterClass) {
534 if (DestReg != PPC::LR8) {
Dan Gohman221a4372008-07-07 23:14:23 +0000535 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LD), DestReg),
Owen Anderson81875432008-01-01 21:11:32 +0000536 FrameIdx));
537 } else {
Dan Gohman221a4372008-07-07 23:14:23 +0000538 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LD), PPC::R11),
Owen Anderson81875432008-01-01 21:11:32 +0000539 FrameIdx));
Dan Gohman221a4372008-07-07 23:14:23 +0000540 NewMIs.push_back(BuildMI(MF, get(PPC::MTLR8)).addReg(PPC::R11));
Owen Anderson81875432008-01-01 21:11:32 +0000541 }
542 } else if (RC == PPC::F8RCRegisterClass) {
Dan Gohman221a4372008-07-07 23:14:23 +0000543 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LFD), DestReg),
Owen Anderson81875432008-01-01 21:11:32 +0000544 FrameIdx));
545 } else if (RC == PPC::F4RCRegisterClass) {
Dan Gohman221a4372008-07-07 23:14:23 +0000546 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LFS), DestReg),
Owen Anderson81875432008-01-01 21:11:32 +0000547 FrameIdx));
548 } else if (RC == PPC::CRRCRegisterClass) {
549 // FIXME: We use R0 here, because it isn't available for RA.
Dan Gohman221a4372008-07-07 23:14:23 +0000550 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LWZ), PPC::R0),
Owen Anderson81875432008-01-01 21:11:32 +0000551 FrameIdx));
552
553 // If the reloaded register isn't CR0, shift the bits right so that they are
554 // in the right CR's slot.
555 if (DestReg != PPC::CR0) {
556 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(DestReg)*4;
557 // rlwinm r11, r11, 32-ShiftBits, 0, 31.
Dan Gohman221a4372008-07-07 23:14:23 +0000558 NewMIs.push_back(BuildMI(MF, get(PPC::RLWINM), PPC::R0)
Owen Anderson81875432008-01-01 21:11:32 +0000559 .addReg(PPC::R0).addImm(32-ShiftBits).addImm(0).addImm(31));
560 }
561
Dan Gohman221a4372008-07-07 23:14:23 +0000562 NewMIs.push_back(BuildMI(MF, get(PPC::MTCRF), DestReg).addReg(PPC::R0));
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000563 } else if (RC == PPC::CRBITRCRegisterClass) {
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000564
565 unsigned Reg = 0;
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000566 if (DestReg >= PPC::CR0LT || DestReg <= PPC::CR0UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000567 Reg = PPC::CR0;
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000568 else if (DestReg >= PPC::CR1LT || DestReg <= PPC::CR1UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000569 Reg = PPC::CR1;
570 else if (DestReg >= PPC::CR2LT || DestReg <= PPC::CR2UN)
571 Reg = PPC::CR2;
572 else if (DestReg >= PPC::CR3LT || DestReg <= PPC::CR3UN)
573 Reg = PPC::CR3;
574 else if (DestReg >= PPC::CR4LT || DestReg <= PPC::CR4UN)
575 Reg = PPC::CR4;
576 else if (DestReg >= PPC::CR5LT || DestReg <= PPC::CR5UN)
577 Reg = PPC::CR5;
578 else if (DestReg >= PPC::CR6LT || DestReg <= PPC::CR6UN)
579 Reg = PPC::CR6;
580 else if (DestReg >= PPC::CR7LT || DestReg <= PPC::CR7UN)
581 Reg = PPC::CR7;
582
Dan Gohman221a4372008-07-07 23:14:23 +0000583 return LoadRegFromStackSlot(MF, Reg, FrameIdx,
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000584 PPC::CRRCRegisterClass, NewMIs);
585
Owen Anderson81875432008-01-01 21:11:32 +0000586 } else if (RC == PPC::VRRCRegisterClass) {
587 // We don't have indexed addressing for vector loads. Emit:
588 // R0 = ADDI FI#
589 // Dest = LVX 0, R0
590 //
591 // FIXME: We use R0 here, because it isn't available for RA.
Dan Gohman221a4372008-07-07 23:14:23 +0000592 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::ADDI), PPC::R0),
Owen Anderson81875432008-01-01 21:11:32 +0000593 FrameIdx, 0, 0));
Dan Gohman221a4372008-07-07 23:14:23 +0000594 NewMIs.push_back(BuildMI(MF, get(PPC::LVX),DestReg).addReg(PPC::R0)
Owen Anderson81875432008-01-01 21:11:32 +0000595 .addReg(PPC::R0));
596 } else {
597 assert(0 && "Unknown regclass!");
598 abort();
599 }
600}
601
602void
603PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Bill Wendlinga1877c52008-03-03 22:19:16 +0000604 MachineBasicBlock::iterator MI,
605 unsigned DestReg, int FrameIdx,
606 const TargetRegisterClass *RC) const {
Dan Gohman221a4372008-07-07 23:14:23 +0000607 MachineFunction &MF = *MBB.getParent();
Owen Anderson81875432008-01-01 21:11:32 +0000608 SmallVector<MachineInstr*, 4> NewMIs;
Dan Gohman221a4372008-07-07 23:14:23 +0000609 LoadRegFromStackSlot(MF, DestReg, FrameIdx, RC, NewMIs);
Owen Anderson81875432008-01-01 21:11:32 +0000610 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
611 MBB.insert(MI, NewMIs[i]);
612}
613
614void PPCInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
Bill Wendlinga1877c52008-03-03 22:19:16 +0000615 SmallVectorImpl<MachineOperand> &Addr,
616 const TargetRegisterClass *RC,
617 SmallVectorImpl<MachineInstr*> &NewMIs)const{
Owen Anderson81875432008-01-01 21:11:32 +0000618 if (Addr[0].isFrameIndex()) {
Dan Gohman221a4372008-07-07 23:14:23 +0000619 LoadRegFromStackSlot(MF, DestReg, Addr[0].getIndex(), RC, NewMIs);
Owen Anderson81875432008-01-01 21:11:32 +0000620 return;
621 }
622
623 unsigned Opc = 0;
624 if (RC == PPC::GPRCRegisterClass) {
625 assert(DestReg != PPC::LR && "Can't handle this yet!");
626 Opc = PPC::LWZ;
627 } else if (RC == PPC::G8RCRegisterClass) {
628 assert(DestReg != PPC::LR8 && "Can't handle this yet!");
629 Opc = PPC::LD;
630 } else if (RC == PPC::F8RCRegisterClass) {
631 Opc = PPC::LFD;
632 } else if (RC == PPC::F4RCRegisterClass) {
633 Opc = PPC::LFS;
634 } else if (RC == PPC::VRRCRegisterClass) {
635 Opc = PPC::LVX;
636 } else {
637 assert(0 && "Unknown regclass!");
638 abort();
639 }
Dan Gohman221a4372008-07-07 23:14:23 +0000640 MachineInstrBuilder MIB = BuildMI(MF, get(Opc), DestReg);
Owen Anderson81875432008-01-01 21:11:32 +0000641 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
642 MachineOperand &MO = Addr[i];
643 if (MO.isRegister())
644 MIB.addReg(MO.getReg());
645 else if (MO.isImmediate())
646 MIB.addImm(MO.getImm());
647 else
648 MIB.addFrameIndex(MO.getIndex());
649 }
650 NewMIs.push_back(MIB);
651 return;
652}
653
Owen Anderson9a184ef2008-01-07 01:35:02 +0000654/// foldMemoryOperand - PowerPC (like most RISC's) can only fold spills into
655/// copy instructions, turning them into load/store instructions.
Evan Cheng4f2f3f62008-02-08 21:20:40 +0000656MachineInstr *PPCInstrInfo::foldMemoryOperand(MachineFunction &MF,
657 MachineInstr *MI,
Owen Anderson9a184ef2008-01-07 01:35:02 +0000658 SmallVectorImpl<unsigned> &Ops,
659 int FrameIndex) const {
660 if (Ops.size() != 1) return NULL;
661
662 // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because
663 // it takes more than one instruction to store it.
664 unsigned Opc = MI->getOpcode();
665 unsigned OpNum = Ops[0];
666
667 MachineInstr *NewMI = NULL;
668 if ((Opc == PPC::OR &&
669 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
670 if (OpNum == 0) { // move -> store
671 unsigned InReg = MI->getOperand(1).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000672 bool isKill = MI->getOperand(1).isKill();
Dan Gohman221a4372008-07-07 23:14:23 +0000673 NewMI = addFrameReference(BuildMI(MF, get(PPC::STW))
Evan Chenge52c1912008-07-03 09:09:37 +0000674 .addReg(InReg, false, false, isKill),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000675 FrameIndex);
676 } else { // move -> load
677 unsigned OutReg = MI->getOperand(0).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000678 bool isDead = MI->getOperand(0).isDead();
Dan Gohman221a4372008-07-07 23:14:23 +0000679 NewMI = addFrameReference(BuildMI(MF, get(PPC::LWZ))
Evan Chenge52c1912008-07-03 09:09:37 +0000680 .addReg(OutReg, true, false, false, isDead),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000681 FrameIndex);
682 }
683 } else if ((Opc == PPC::OR8 &&
684 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
685 if (OpNum == 0) { // move -> store
686 unsigned InReg = MI->getOperand(1).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000687 bool isKill = MI->getOperand(1).isKill();
Dan Gohman221a4372008-07-07 23:14:23 +0000688 NewMI = addFrameReference(BuildMI(MF, get(PPC::STD))
Evan Chenge52c1912008-07-03 09:09:37 +0000689 .addReg(InReg, false, false, isKill),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000690 FrameIndex);
691 } else { // move -> load
692 unsigned OutReg = MI->getOperand(0).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000693 bool isDead = MI->getOperand(0).isDead();
Dan Gohman221a4372008-07-07 23:14:23 +0000694 NewMI = addFrameReference(BuildMI(MF, get(PPC::LD))
Evan Chenge52c1912008-07-03 09:09:37 +0000695 .addReg(OutReg, true, false, false, isDead),
696 FrameIndex);
Owen Anderson9a184ef2008-01-07 01:35:02 +0000697 }
698 } else if (Opc == PPC::FMRD) {
699 if (OpNum == 0) { // move -> store
700 unsigned InReg = MI->getOperand(1).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000701 bool isKill = MI->getOperand(1).isKill();
Dan Gohman221a4372008-07-07 23:14:23 +0000702 NewMI = addFrameReference(BuildMI(MF, get(PPC::STFD))
Evan Chenge52c1912008-07-03 09:09:37 +0000703 .addReg(InReg, false, false, isKill),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000704 FrameIndex);
705 } else { // move -> load
706 unsigned OutReg = MI->getOperand(0).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000707 bool isDead = MI->getOperand(0).isDead();
Dan Gohman221a4372008-07-07 23:14:23 +0000708 NewMI = addFrameReference(BuildMI(MF, get(PPC::LFD))
Evan Chenge52c1912008-07-03 09:09:37 +0000709 .addReg(OutReg, true, false, false, isDead),
710 FrameIndex);
Owen Anderson9a184ef2008-01-07 01:35:02 +0000711 }
712 } else if (Opc == PPC::FMRS) {
713 if (OpNum == 0) { // move -> store
714 unsigned InReg = MI->getOperand(1).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000715 bool isKill = MI->getOperand(1).isKill();
Dan Gohman221a4372008-07-07 23:14:23 +0000716 NewMI = addFrameReference(BuildMI(MF, get(PPC::STFS))
Evan Chenge52c1912008-07-03 09:09:37 +0000717 .addReg(InReg, false, false, isKill),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000718 FrameIndex);
719 } else { // move -> load
720 unsigned OutReg = MI->getOperand(0).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000721 bool isDead = MI->getOperand(0).isDead();
Dan Gohman221a4372008-07-07 23:14:23 +0000722 NewMI = addFrameReference(BuildMI(MF, get(PPC::LFS))
Evan Chenge52c1912008-07-03 09:09:37 +0000723 .addReg(OutReg, true, false, false, isDead),
724 FrameIndex);
Owen Anderson9a184ef2008-01-07 01:35:02 +0000725 }
726 }
727
Owen Anderson9a184ef2008-01-07 01:35:02 +0000728 return NewMI;
729}
730
731bool PPCInstrInfo::canFoldMemoryOperand(MachineInstr *MI,
Evan Cheng4f2f3f62008-02-08 21:20:40 +0000732 SmallVectorImpl<unsigned> &Ops) const {
Owen Anderson9a184ef2008-01-07 01:35:02 +0000733 if (Ops.size() != 1) return false;
734
735 // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because
736 // it takes more than one instruction to store it.
737 unsigned Opc = MI->getOpcode();
738
739 if ((Opc == PPC::OR &&
740 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
741 return true;
742 else if ((Opc == PPC::OR8 &&
743 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
744 return true;
745 else if (Opc == PPC::FMRD || Opc == PPC::FMRS)
746 return true;
747
748 return false;
749}
750
Owen Anderson81875432008-01-01 21:11:32 +0000751
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000752bool PPCInstrInfo::BlockHasNoFallThrough(MachineBasicBlock &MBB) const {
753 if (MBB.empty()) return false;
754
755 switch (MBB.back().getOpcode()) {
756 case PPC::BLR: // Return.
757 case PPC::B: // Uncond branch.
758 case PPC::BCTR: // Indirect branch.
759 return true;
760 default: return false;
761 }
762}
763
764bool PPCInstrInfo::
Owen Andersond131b5b2008-08-14 22:49:33 +0000765ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000766 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
767 // Leave the CR# the same, but invert the condition.
768 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
769 return false;
770}
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000771
772/// GetInstSize - Return the number of bytes of code the specified
773/// instruction may be. This returns the maximum number of bytes.
774///
775unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
776 switch (MI->getOpcode()) {
777 case PPC::INLINEASM: { // Inline Asm: Variable size.
778 const MachineFunction *MF = MI->getParent()->getParent();
779 const char *AsmStr = MI->getOperand(0).getSymbolName();
780 return MF->getTarget().getTargetAsmInfo()->getInlineAsmLength(AsmStr);
781 }
Dan Gohmanfa607c92008-07-01 00:05:16 +0000782 case PPC::DBG_LABEL:
783 case PPC::EH_LABEL:
784 case PPC::GC_LABEL:
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000785 return 0;
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000786 default:
787 return 4; // PowerPC instructions are all 4 bytes
788 }
789}