blob: 2ec6dcb7d8e8b8d782c691859b7fb2a16c4d9788 [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 Anderson9fa72d92008-08-26 18:03:31 +0000318bool PPCInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
Owen Anderson8f2c8932007-12-31 06:32:00 +0000319 MachineBasicBlock::iterator MI,
320 unsigned DestReg, unsigned SrcReg,
321 const TargetRegisterClass *DestRC,
322 const TargetRegisterClass *SrcRC) const {
323 if (DestRC != SrcRC) {
Owen Anderson9fa72d92008-08-26 18:03:31 +0000324 // Not yet supported!
325 return false;
Owen Anderson8f2c8932007-12-31 06:32:00 +0000326 }
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 {
Owen Anderson9fa72d92008-08-26 18:03:31 +0000343 // Attempt to copy register that is not GPR or FPR
344 return false;
Owen Anderson8f2c8932007-12-31 06:32:00 +0000345 }
Owen Anderson9fa72d92008-08-26 18:03:31 +0000346
347 return true;
Owen Anderson8f2c8932007-12-31 06:32:00 +0000348}
349
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000350bool
Dan Gohman221a4372008-07-07 23:14:23 +0000351PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
352 unsigned SrcReg, bool isKill,
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000353 int FrameIdx,
354 const TargetRegisterClass *RC,
355 SmallVectorImpl<MachineInstr*> &NewMIs) const{
Owen Anderson81875432008-01-01 21:11:32 +0000356 if (RC == PPC::GPRCRegisterClass) {
357 if (SrcReg != PPC::LR) {
Dan Gohman221a4372008-07-07 23:14:23 +0000358 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STW))
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000359 .addReg(SrcReg, false, false, isKill),
360 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000361 } else {
362 // FIXME: this spills LR immediately to memory in one step. To do this,
363 // we use R11, which we know cannot be used in the prolog/epilog. This is
364 // a hack.
Dan Gohman221a4372008-07-07 23:14:23 +0000365 NewMIs.push_back(BuildMI(MF, get(PPC::MFLR), PPC::R11));
366 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STW))
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000367 .addReg(PPC::R11, false, false, isKill),
368 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000369 }
370 } else if (RC == PPC::G8RCRegisterClass) {
371 if (SrcReg != PPC::LR8) {
Dan Gohman221a4372008-07-07 23:14:23 +0000372 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STD))
Chris Lattner7b7371c2008-03-10 18:55:53 +0000373 .addReg(SrcReg, false, false, isKill), FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000374 } else {
375 // FIXME: this spills LR immediately to memory in one step. To do this,
376 // we use R11, which we know cannot be used in the prolog/epilog. This is
377 // a hack.
Dan Gohman221a4372008-07-07 23:14:23 +0000378 NewMIs.push_back(BuildMI(MF, get(PPC::MFLR8), PPC::X11));
379 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STD))
Chris Lattner7b7371c2008-03-10 18:55:53 +0000380 .addReg(PPC::X11, false, false, isKill), FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000381 }
382 } else if (RC == PPC::F8RCRegisterClass) {
Dan Gohman221a4372008-07-07 23:14:23 +0000383 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STFD))
Chris Lattner7b7371c2008-03-10 18:55:53 +0000384 .addReg(SrcReg, false, false, isKill), FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000385 } else if (RC == PPC::F4RCRegisterClass) {
Dan Gohman221a4372008-07-07 23:14:23 +0000386 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STFS))
Chris Lattner7b7371c2008-03-10 18:55:53 +0000387 .addReg(SrcReg, false, false, isKill), FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000388 } else if (RC == PPC::CRRCRegisterClass) {
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000389 if ((EnablePPC32RS && !TM.getSubtargetImpl()->isPPC64()) ||
390 (EnablePPC64RS && TM.getSubtargetImpl()->isPPC64())) {
391 // FIXME (64-bit): Enable
Dan Gohman221a4372008-07-07 23:14:23 +0000392 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::SPILL_CR))
Bill Wendlinga1877c52008-03-03 22:19:16 +0000393 .addReg(SrcReg, false, false, isKill),
Chris Lattner6734c3a2008-03-20 01:22:40 +0000394 FrameIdx));
Bill Wendlinga1877c52008-03-03 22:19:16 +0000395 return true;
396 } else {
397 // FIXME: We use R0 here, because it isn't available for RA. We need to
398 // store the CR in the low 4-bits of the saved value. First, issue a MFCR
399 // to save all of the CRBits.
Dan Gohman221a4372008-07-07 23:14:23 +0000400 NewMIs.push_back(BuildMI(MF, get(PPC::MFCR), PPC::R0));
Owen Anderson81875432008-01-01 21:11:32 +0000401
Bill Wendlinga1877c52008-03-03 22:19:16 +0000402 // If the saved register wasn't CR0, shift the bits left so that they are
403 // in CR0's slot.
404 if (SrcReg != PPC::CR0) {
405 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(SrcReg)*4;
406 // rlwinm r0, r0, ShiftBits, 0, 31.
Dan Gohman221a4372008-07-07 23:14:23 +0000407 NewMIs.push_back(BuildMI(MF, get(PPC::RLWINM), PPC::R0)
Chris Lattner7b7371c2008-03-10 18:55:53 +0000408 .addReg(PPC::R0).addImm(ShiftBits).addImm(0).addImm(31));
Bill Wendlinga1877c52008-03-03 22:19:16 +0000409 }
410
Dan Gohman221a4372008-07-07 23:14:23 +0000411 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STW))
Bill Wendlinga1877c52008-03-03 22:19:16 +0000412 .addReg(PPC::R0, false, false, isKill),
413 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000414 }
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000415 } else if (RC == PPC::CRBITRCRegisterClass) {
416 // FIXME: We use CRi here because there is no mtcrf on a bit. Since the
417 // backend currently only uses CR1EQ as an individual bit, this should
418 // not cause any bug. If we need other uses of CR bits, the following
419 // code may be invalid.
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000420 unsigned Reg = 0;
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000421 if (SrcReg >= PPC::CR0LT || SrcReg <= PPC::CR0UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000422 Reg = PPC::CR0;
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000423 else if (SrcReg >= PPC::CR1LT || SrcReg <= PPC::CR1UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000424 Reg = PPC::CR1;
425 else if (SrcReg >= PPC::CR2LT || SrcReg <= PPC::CR2UN)
426 Reg = PPC::CR2;
427 else if (SrcReg >= PPC::CR3LT || SrcReg <= PPC::CR3UN)
428 Reg = PPC::CR3;
429 else if (SrcReg >= PPC::CR4LT || SrcReg <= PPC::CR4UN)
430 Reg = PPC::CR4;
431 else if (SrcReg >= PPC::CR5LT || SrcReg <= PPC::CR5UN)
432 Reg = PPC::CR5;
433 else if (SrcReg >= PPC::CR6LT || SrcReg <= PPC::CR6UN)
434 Reg = PPC::CR6;
435 else if (SrcReg >= PPC::CR7LT || SrcReg <= PPC::CR7UN)
436 Reg = PPC::CR7;
437
Dan Gohman221a4372008-07-07 23:14:23 +0000438 return StoreRegToStackSlot(MF, Reg, isKill, FrameIdx,
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000439 PPC::CRRCRegisterClass, NewMIs);
440
Owen Anderson81875432008-01-01 21:11:32 +0000441 } else if (RC == PPC::VRRCRegisterClass) {
442 // We don't have indexed addressing for vector loads. Emit:
443 // R0 = ADDI FI#
444 // STVX VAL, 0, R0
445 //
446 // FIXME: We use R0 here, because it isn't available for RA.
Dan Gohman221a4372008-07-07 23:14:23 +0000447 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::ADDI), PPC::R0),
Owen Anderson81875432008-01-01 21:11:32 +0000448 FrameIdx, 0, 0));
Dan Gohman221a4372008-07-07 23:14:23 +0000449 NewMIs.push_back(BuildMI(MF, get(PPC::STVX))
Chris Lattner7b7371c2008-03-10 18:55:53 +0000450 .addReg(SrcReg, false, false, isKill).addReg(PPC::R0).addReg(PPC::R0));
Owen Anderson81875432008-01-01 21:11:32 +0000451 } else {
452 assert(0 && "Unknown regclass!");
453 abort();
454 }
Bill Wendlinga1877c52008-03-03 22:19:16 +0000455
456 return false;
Owen Anderson81875432008-01-01 21:11:32 +0000457}
458
459void
460PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Bill Wendlinga1877c52008-03-03 22:19:16 +0000461 MachineBasicBlock::iterator MI,
462 unsigned SrcReg, bool isKill, int FrameIdx,
463 const TargetRegisterClass *RC) const {
Dan Gohman221a4372008-07-07 23:14:23 +0000464 MachineFunction &MF = *MBB.getParent();
Owen Anderson81875432008-01-01 21:11:32 +0000465 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendlinga1877c52008-03-03 22:19:16 +0000466
Dan Gohman221a4372008-07-07 23:14:23 +0000467 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs)) {
468 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Bill Wendlinga1877c52008-03-03 22:19:16 +0000469 FuncInfo->setSpillsCR();
470 }
471
Owen Anderson81875432008-01-01 21:11:32 +0000472 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
473 MBB.insert(MI, NewMIs[i]);
474}
475
476void PPCInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
Bill Wendlinga1877c52008-03-03 22:19:16 +0000477 bool isKill,
478 SmallVectorImpl<MachineOperand> &Addr,
479 const TargetRegisterClass *RC,
480 SmallVectorImpl<MachineInstr*> &NewMIs) const{
Owen Anderson81875432008-01-01 21:11:32 +0000481 if (Addr[0].isFrameIndex()) {
Dan Gohman221a4372008-07-07 23:14:23 +0000482 if (StoreRegToStackSlot(MF, SrcReg, isKill,
483 Addr[0].getIndex(), RC, NewMIs)) {
Bill Wendlinga1877c52008-03-03 22:19:16 +0000484 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
485 FuncInfo->setSpillsCR();
486 }
487
Owen Anderson81875432008-01-01 21:11:32 +0000488 return;
489 }
490
491 unsigned Opc = 0;
492 if (RC == PPC::GPRCRegisterClass) {
493 Opc = PPC::STW;
494 } else if (RC == PPC::G8RCRegisterClass) {
495 Opc = PPC::STD;
496 } else if (RC == PPC::F8RCRegisterClass) {
497 Opc = PPC::STFD;
498 } else if (RC == PPC::F4RCRegisterClass) {
499 Opc = PPC::STFS;
500 } else if (RC == PPC::VRRCRegisterClass) {
501 Opc = PPC::STVX;
502 } else {
503 assert(0 && "Unknown regclass!");
504 abort();
505 }
Dan Gohman221a4372008-07-07 23:14:23 +0000506 MachineInstrBuilder MIB = BuildMI(MF, get(Opc))
Owen Anderson81875432008-01-01 21:11:32 +0000507 .addReg(SrcReg, false, false, isKill);
508 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
509 MachineOperand &MO = Addr[i];
510 if (MO.isRegister())
511 MIB.addReg(MO.getReg());
512 else if (MO.isImmediate())
513 MIB.addImm(MO.getImm());
514 else
515 MIB.addFrameIndex(MO.getIndex());
516 }
517 NewMIs.push_back(MIB);
518 return;
519}
520
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000521void
Dan Gohman221a4372008-07-07 23:14:23 +0000522PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF,
523 unsigned DestReg, int FrameIdx,
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000524 const TargetRegisterClass *RC,
525 SmallVectorImpl<MachineInstr*> &NewMIs)const{
Owen Anderson81875432008-01-01 21:11:32 +0000526 if (RC == PPC::GPRCRegisterClass) {
527 if (DestReg != PPC::LR) {
Dan Gohman221a4372008-07-07 23:14:23 +0000528 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LWZ), DestReg),
Owen Anderson81875432008-01-01 21:11:32 +0000529 FrameIdx));
530 } else {
Dan Gohman221a4372008-07-07 23:14:23 +0000531 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LWZ), PPC::R11),
Owen Anderson81875432008-01-01 21:11:32 +0000532 FrameIdx));
Dan Gohman221a4372008-07-07 23:14:23 +0000533 NewMIs.push_back(BuildMI(MF, get(PPC::MTLR)).addReg(PPC::R11));
Owen Anderson81875432008-01-01 21:11:32 +0000534 }
535 } else if (RC == PPC::G8RCRegisterClass) {
536 if (DestReg != PPC::LR8) {
Dan Gohman221a4372008-07-07 23:14:23 +0000537 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LD), DestReg),
Owen Anderson81875432008-01-01 21:11:32 +0000538 FrameIdx));
539 } else {
Dan Gohman221a4372008-07-07 23:14:23 +0000540 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LD), PPC::R11),
Owen Anderson81875432008-01-01 21:11:32 +0000541 FrameIdx));
Dan Gohman221a4372008-07-07 23:14:23 +0000542 NewMIs.push_back(BuildMI(MF, get(PPC::MTLR8)).addReg(PPC::R11));
Owen Anderson81875432008-01-01 21:11:32 +0000543 }
544 } else if (RC == PPC::F8RCRegisterClass) {
Dan Gohman221a4372008-07-07 23:14:23 +0000545 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LFD), DestReg),
Owen Anderson81875432008-01-01 21:11:32 +0000546 FrameIdx));
547 } else if (RC == PPC::F4RCRegisterClass) {
Dan Gohman221a4372008-07-07 23:14:23 +0000548 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LFS), DestReg),
Owen Anderson81875432008-01-01 21:11:32 +0000549 FrameIdx));
550 } else if (RC == PPC::CRRCRegisterClass) {
551 // FIXME: We use R0 here, because it isn't available for RA.
Dan Gohman221a4372008-07-07 23:14:23 +0000552 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LWZ), PPC::R0),
Owen Anderson81875432008-01-01 21:11:32 +0000553 FrameIdx));
554
555 // If the reloaded register isn't CR0, shift the bits right so that they are
556 // in the right CR's slot.
557 if (DestReg != PPC::CR0) {
558 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(DestReg)*4;
559 // rlwinm r11, r11, 32-ShiftBits, 0, 31.
Dan Gohman221a4372008-07-07 23:14:23 +0000560 NewMIs.push_back(BuildMI(MF, get(PPC::RLWINM), PPC::R0)
Owen Anderson81875432008-01-01 21:11:32 +0000561 .addReg(PPC::R0).addImm(32-ShiftBits).addImm(0).addImm(31));
562 }
563
Dan Gohman221a4372008-07-07 23:14:23 +0000564 NewMIs.push_back(BuildMI(MF, get(PPC::MTCRF), DestReg).addReg(PPC::R0));
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000565 } else if (RC == PPC::CRBITRCRegisterClass) {
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000566
567 unsigned Reg = 0;
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000568 if (DestReg >= PPC::CR0LT || DestReg <= PPC::CR0UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000569 Reg = PPC::CR0;
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000570 else if (DestReg >= PPC::CR1LT || DestReg <= PPC::CR1UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000571 Reg = PPC::CR1;
572 else if (DestReg >= PPC::CR2LT || DestReg <= PPC::CR2UN)
573 Reg = PPC::CR2;
574 else if (DestReg >= PPC::CR3LT || DestReg <= PPC::CR3UN)
575 Reg = PPC::CR3;
576 else if (DestReg >= PPC::CR4LT || DestReg <= PPC::CR4UN)
577 Reg = PPC::CR4;
578 else if (DestReg >= PPC::CR5LT || DestReg <= PPC::CR5UN)
579 Reg = PPC::CR5;
580 else if (DestReg >= PPC::CR6LT || DestReg <= PPC::CR6UN)
581 Reg = PPC::CR6;
582 else if (DestReg >= PPC::CR7LT || DestReg <= PPC::CR7UN)
583 Reg = PPC::CR7;
584
Dan Gohman221a4372008-07-07 23:14:23 +0000585 return LoadRegFromStackSlot(MF, Reg, FrameIdx,
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000586 PPC::CRRCRegisterClass, NewMIs);
587
Owen Anderson81875432008-01-01 21:11:32 +0000588 } else if (RC == PPC::VRRCRegisterClass) {
589 // We don't have indexed addressing for vector loads. Emit:
590 // R0 = ADDI FI#
591 // Dest = LVX 0, R0
592 //
593 // FIXME: We use R0 here, because it isn't available for RA.
Dan Gohman221a4372008-07-07 23:14:23 +0000594 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::ADDI), PPC::R0),
Owen Anderson81875432008-01-01 21:11:32 +0000595 FrameIdx, 0, 0));
Dan Gohman221a4372008-07-07 23:14:23 +0000596 NewMIs.push_back(BuildMI(MF, get(PPC::LVX),DestReg).addReg(PPC::R0)
Owen Anderson81875432008-01-01 21:11:32 +0000597 .addReg(PPC::R0));
598 } else {
599 assert(0 && "Unknown regclass!");
600 abort();
601 }
602}
603
604void
605PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Bill Wendlinga1877c52008-03-03 22:19:16 +0000606 MachineBasicBlock::iterator MI,
607 unsigned DestReg, int FrameIdx,
608 const TargetRegisterClass *RC) const {
Dan Gohman221a4372008-07-07 23:14:23 +0000609 MachineFunction &MF = *MBB.getParent();
Owen Anderson81875432008-01-01 21:11:32 +0000610 SmallVector<MachineInstr*, 4> NewMIs;
Dan Gohman221a4372008-07-07 23:14:23 +0000611 LoadRegFromStackSlot(MF, DestReg, FrameIdx, RC, NewMIs);
Owen Anderson81875432008-01-01 21:11:32 +0000612 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
613 MBB.insert(MI, NewMIs[i]);
614}
615
616void PPCInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
Bill Wendlinga1877c52008-03-03 22:19:16 +0000617 SmallVectorImpl<MachineOperand> &Addr,
618 const TargetRegisterClass *RC,
619 SmallVectorImpl<MachineInstr*> &NewMIs)const{
Owen Anderson81875432008-01-01 21:11:32 +0000620 if (Addr[0].isFrameIndex()) {
Dan Gohman221a4372008-07-07 23:14:23 +0000621 LoadRegFromStackSlot(MF, DestReg, Addr[0].getIndex(), RC, NewMIs);
Owen Anderson81875432008-01-01 21:11:32 +0000622 return;
623 }
624
625 unsigned Opc = 0;
626 if (RC == PPC::GPRCRegisterClass) {
627 assert(DestReg != PPC::LR && "Can't handle this yet!");
628 Opc = PPC::LWZ;
629 } else if (RC == PPC::G8RCRegisterClass) {
630 assert(DestReg != PPC::LR8 && "Can't handle this yet!");
631 Opc = PPC::LD;
632 } else if (RC == PPC::F8RCRegisterClass) {
633 Opc = PPC::LFD;
634 } else if (RC == PPC::F4RCRegisterClass) {
635 Opc = PPC::LFS;
636 } else if (RC == PPC::VRRCRegisterClass) {
637 Opc = PPC::LVX;
638 } else {
639 assert(0 && "Unknown regclass!");
640 abort();
641 }
Dan Gohman221a4372008-07-07 23:14:23 +0000642 MachineInstrBuilder MIB = BuildMI(MF, get(Opc), DestReg);
Owen Anderson81875432008-01-01 21:11:32 +0000643 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
644 MachineOperand &MO = Addr[i];
645 if (MO.isRegister())
646 MIB.addReg(MO.getReg());
647 else if (MO.isImmediate())
648 MIB.addImm(MO.getImm());
649 else
650 MIB.addFrameIndex(MO.getIndex());
651 }
652 NewMIs.push_back(MIB);
653 return;
654}
655
Owen Anderson9a184ef2008-01-07 01:35:02 +0000656/// foldMemoryOperand - PowerPC (like most RISC's) can only fold spills into
657/// copy instructions, turning them into load/store instructions.
Evan Cheng4f2f3f62008-02-08 21:20:40 +0000658MachineInstr *PPCInstrInfo::foldMemoryOperand(MachineFunction &MF,
659 MachineInstr *MI,
Owen Anderson9a184ef2008-01-07 01:35:02 +0000660 SmallVectorImpl<unsigned> &Ops,
661 int FrameIndex) const {
662 if (Ops.size() != 1) return NULL;
663
664 // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because
665 // it takes more than one instruction to store it.
666 unsigned Opc = MI->getOpcode();
667 unsigned OpNum = Ops[0];
668
669 MachineInstr *NewMI = NULL;
670 if ((Opc == PPC::OR &&
671 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
672 if (OpNum == 0) { // move -> store
673 unsigned InReg = MI->getOperand(1).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000674 bool isKill = MI->getOperand(1).isKill();
Dan Gohman221a4372008-07-07 23:14:23 +0000675 NewMI = addFrameReference(BuildMI(MF, get(PPC::STW))
Evan Chenge52c1912008-07-03 09:09:37 +0000676 .addReg(InReg, false, false, isKill),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000677 FrameIndex);
678 } else { // move -> load
679 unsigned OutReg = MI->getOperand(0).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000680 bool isDead = MI->getOperand(0).isDead();
Dan Gohman221a4372008-07-07 23:14:23 +0000681 NewMI = addFrameReference(BuildMI(MF, get(PPC::LWZ))
Evan Chenge52c1912008-07-03 09:09:37 +0000682 .addReg(OutReg, true, false, false, isDead),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000683 FrameIndex);
684 }
685 } else if ((Opc == PPC::OR8 &&
686 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
687 if (OpNum == 0) { // move -> store
688 unsigned InReg = MI->getOperand(1).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000689 bool isKill = MI->getOperand(1).isKill();
Dan Gohman221a4372008-07-07 23:14:23 +0000690 NewMI = addFrameReference(BuildMI(MF, get(PPC::STD))
Evan Chenge52c1912008-07-03 09:09:37 +0000691 .addReg(InReg, false, false, isKill),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000692 FrameIndex);
693 } else { // move -> load
694 unsigned OutReg = MI->getOperand(0).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000695 bool isDead = MI->getOperand(0).isDead();
Dan Gohman221a4372008-07-07 23:14:23 +0000696 NewMI = addFrameReference(BuildMI(MF, get(PPC::LD))
Evan Chenge52c1912008-07-03 09:09:37 +0000697 .addReg(OutReg, true, false, false, isDead),
698 FrameIndex);
Owen Anderson9a184ef2008-01-07 01:35:02 +0000699 }
700 } else if (Opc == PPC::FMRD) {
701 if (OpNum == 0) { // move -> store
702 unsigned InReg = MI->getOperand(1).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000703 bool isKill = MI->getOperand(1).isKill();
Dan Gohman221a4372008-07-07 23:14:23 +0000704 NewMI = addFrameReference(BuildMI(MF, get(PPC::STFD))
Evan Chenge52c1912008-07-03 09:09:37 +0000705 .addReg(InReg, false, false, isKill),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000706 FrameIndex);
707 } else { // move -> load
708 unsigned OutReg = MI->getOperand(0).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000709 bool isDead = MI->getOperand(0).isDead();
Dan Gohman221a4372008-07-07 23:14:23 +0000710 NewMI = addFrameReference(BuildMI(MF, get(PPC::LFD))
Evan Chenge52c1912008-07-03 09:09:37 +0000711 .addReg(OutReg, true, false, false, isDead),
712 FrameIndex);
Owen Anderson9a184ef2008-01-07 01:35:02 +0000713 }
714 } else if (Opc == PPC::FMRS) {
715 if (OpNum == 0) { // move -> store
716 unsigned InReg = MI->getOperand(1).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000717 bool isKill = MI->getOperand(1).isKill();
Dan Gohman221a4372008-07-07 23:14:23 +0000718 NewMI = addFrameReference(BuildMI(MF, get(PPC::STFS))
Evan Chenge52c1912008-07-03 09:09:37 +0000719 .addReg(InReg, false, false, isKill),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000720 FrameIndex);
721 } else { // move -> load
722 unsigned OutReg = MI->getOperand(0).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000723 bool isDead = MI->getOperand(0).isDead();
Dan Gohman221a4372008-07-07 23:14:23 +0000724 NewMI = addFrameReference(BuildMI(MF, get(PPC::LFS))
Evan Chenge52c1912008-07-03 09:09:37 +0000725 .addReg(OutReg, true, false, false, isDead),
726 FrameIndex);
Owen Anderson9a184ef2008-01-07 01:35:02 +0000727 }
728 }
729
Owen Anderson9a184ef2008-01-07 01:35:02 +0000730 return NewMI;
731}
732
733bool PPCInstrInfo::canFoldMemoryOperand(MachineInstr *MI,
Evan Cheng4f2f3f62008-02-08 21:20:40 +0000734 SmallVectorImpl<unsigned> &Ops) const {
Owen Anderson9a184ef2008-01-07 01:35:02 +0000735 if (Ops.size() != 1) return false;
736
737 // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because
738 // it takes more than one instruction to store it.
739 unsigned Opc = MI->getOpcode();
740
741 if ((Opc == PPC::OR &&
742 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
743 return true;
744 else if ((Opc == PPC::OR8 &&
745 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
746 return true;
747 else if (Opc == PPC::FMRD || Opc == PPC::FMRS)
748 return true;
749
750 return false;
751}
752
Owen Anderson81875432008-01-01 21:11:32 +0000753
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000754bool PPCInstrInfo::BlockHasNoFallThrough(MachineBasicBlock &MBB) const {
755 if (MBB.empty()) return false;
756
757 switch (MBB.back().getOpcode()) {
758 case PPC::BLR: // Return.
759 case PPC::B: // Uncond branch.
760 case PPC::BCTR: // Indirect branch.
761 return true;
762 default: return false;
763 }
764}
765
766bool PPCInstrInfo::
Owen Andersond131b5b2008-08-14 22:49:33 +0000767ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000768 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
769 // Leave the CR# the same, but invert the condition.
770 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
771 return false;
772}
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000773
774/// GetInstSize - Return the number of bytes of code the specified
775/// instruction may be. This returns the maximum number of bytes.
776///
777unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
778 switch (MI->getOpcode()) {
779 case PPC::INLINEASM: { // Inline Asm: Variable size.
780 const MachineFunction *MF = MI->getParent()->getParent();
781 const char *AsmStr = MI->getOperand(0).getSymbolName();
782 return MF->getTarget().getTargetAsmInfo()->getInlineAsmLength(AsmStr);
783 }
Dan Gohmanfa607c92008-07-01 00:05:16 +0000784 case PPC::DBG_LABEL:
785 case PPC::EH_LABEL:
786 case PPC::GC_LABEL:
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000787 return 0;
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000788 default:
789 return 4; // PowerPC instructions are all 4 bytes
790 }
791}