blob: 50c80c4ddd8b9a0d6dc66a184058d4fb756a0e3c [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();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000177 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
Dan Gohman221a4372008-07-07 23:14:23 +0000178 .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 {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000200 DebugLoc DL = DebugLoc::getUnknownLoc();
201 if (MI != MBB.end()) DL = MI->getDebugLoc();
202
203 BuildMI(MBB, MI, DL, get(PPC::NOP));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000204}
205
206
207// Branch analysis.
208bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
209 MachineBasicBlock *&FBB,
Evan Chengeac31642009-02-09 07:14:22 +0000210 SmallVectorImpl<MachineOperand> &Cond,
211 bool AllowModify) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212 // If the block has no terminators, it just falls into the block after it.
213 MachineBasicBlock::iterator I = MBB.end();
214 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
215 return false;
216
217 // Get the last instruction in the block.
218 MachineInstr *LastInst = I;
219
220 // If there is only one terminator instruction, process it.
221 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
222 if (LastInst->getOpcode() == PPC::B) {
Chris Lattner6017d482007-12-30 23:10:15 +0000223 TBB = LastInst->getOperand(0).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000224 return false;
225 } else if (LastInst->getOpcode() == PPC::BCC) {
226 // Block ends with fall-through condbranch.
Chris Lattner6017d482007-12-30 23:10:15 +0000227 TBB = LastInst->getOperand(2).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000228 Cond.push_back(LastInst->getOperand(0));
229 Cond.push_back(LastInst->getOperand(1));
230 return false;
231 }
232 // Otherwise, don't know what this is.
233 return true;
234 }
235
236 // Get the instruction before it if it's a terminator.
237 MachineInstr *SecondLastInst = I;
238
239 // If there are three terminators, we don't know what sort of block this is.
240 if (SecondLastInst && I != MBB.begin() &&
241 isUnpredicatedTerminator(--I))
242 return true;
243
244 // If the block ends with PPC::B and PPC:BCC, handle it.
245 if (SecondLastInst->getOpcode() == PPC::BCC &&
246 LastInst->getOpcode() == PPC::B) {
Chris Lattner6017d482007-12-30 23:10:15 +0000247 TBB = SecondLastInst->getOperand(2).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000248 Cond.push_back(SecondLastInst->getOperand(0));
249 Cond.push_back(SecondLastInst->getOperand(1));
Chris Lattner6017d482007-12-30 23:10:15 +0000250 FBB = LastInst->getOperand(0).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251 return false;
252 }
253
254 // If the block ends with two PPC:Bs, handle it. The second one is not
255 // executed, so remove it.
256 if (SecondLastInst->getOpcode() == PPC::B &&
257 LastInst->getOpcode() == PPC::B) {
Chris Lattner6017d482007-12-30 23:10:15 +0000258 TBB = SecondLastInst->getOperand(0).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000259 I = LastInst;
Evan Chengeac31642009-02-09 07:14:22 +0000260 if (AllowModify)
261 I->eraseFromParent();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262 return false;
263 }
264
265 // Otherwise, can't handle this.
266 return true;
267}
268
269unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
270 MachineBasicBlock::iterator I = MBB.end();
271 if (I == MBB.begin()) return 0;
272 --I;
273 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC)
274 return 0;
275
276 // Remove the branch.
277 I->eraseFromParent();
278
279 I = MBB.end();
280
281 if (I == MBB.begin()) return 1;
282 --I;
283 if (I->getOpcode() != PPC::BCC)
284 return 1;
285
286 // Remove the branch.
287 I->eraseFromParent();
288 return 2;
289}
290
291unsigned
292PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
293 MachineBasicBlock *FBB,
Owen Andersond131b5b2008-08-14 22:49:33 +0000294 const SmallVectorImpl<MachineOperand> &Cond) const {
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000295 // FIXME this should probably have a DebugLoc argument
296 DebugLoc dl = DebugLoc::getUnknownLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000297 // Shouldn't be a fall through.
298 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
299 assert((Cond.size() == 2 || Cond.size() == 0) &&
300 "PPC branch conditions have two components!");
301
302 // One-way branch.
303 if (FBB == 0) {
304 if (Cond.empty()) // Unconditional branch
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000305 BuildMI(&MBB, dl, get(PPC::B)).addMBB(TBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000306 else // Conditional branch
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000307 BuildMI(&MBB, dl, get(PPC::BCC))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000308 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
309 return 1;
310 }
311
312 // Two-way Conditional Branch.
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000313 BuildMI(&MBB, dl, get(PPC::BCC))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000314 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000315 BuildMI(&MBB, dl, get(PPC::B)).addMBB(FBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000316 return 2;
317}
318
Owen Anderson9fa72d92008-08-26 18:03:31 +0000319bool PPCInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
Owen Anderson8f2c8932007-12-31 06:32:00 +0000320 MachineBasicBlock::iterator MI,
321 unsigned DestReg, unsigned SrcReg,
322 const TargetRegisterClass *DestRC,
323 const TargetRegisterClass *SrcRC) const {
324 if (DestRC != SrcRC) {
Owen Anderson9fa72d92008-08-26 18:03:31 +0000325 // Not yet supported!
326 return false;
Owen Anderson8f2c8932007-12-31 06:32:00 +0000327 }
328
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000329 DebugLoc DL = DebugLoc::getUnknownLoc();
330 if (MI != MBB.end()) DL = MI->getDebugLoc();
331
Owen Anderson8f2c8932007-12-31 06:32:00 +0000332 if (DestRC == PPC::GPRCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000333 BuildMI(MBB, MI, DL, get(PPC::OR), DestReg).addReg(SrcReg).addReg(SrcReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000334 } else if (DestRC == PPC::G8RCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000335 BuildMI(MBB, MI, DL, get(PPC::OR8), DestReg).addReg(SrcReg).addReg(SrcReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000336 } else if (DestRC == PPC::F4RCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000337 BuildMI(MBB, MI, DL, get(PPC::FMRS), DestReg).addReg(SrcReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000338 } else if (DestRC == PPC::F8RCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000339 BuildMI(MBB, MI, DL, get(PPC::FMRD), DestReg).addReg(SrcReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000340 } else if (DestRC == PPC::CRRCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000341 BuildMI(MBB, MI, DL, get(PPC::MCRF), DestReg).addReg(SrcReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000342 } else if (DestRC == PPC::VRRCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000343 BuildMI(MBB, MI, DL, get(PPC::VOR), DestReg).addReg(SrcReg).addReg(SrcReg);
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000344 } else if (DestRC == PPC::CRBITRCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000345 BuildMI(MBB, MI, DL, get(PPC::CROR), DestReg).addReg(SrcReg).addReg(SrcReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000346 } else {
Owen Anderson9fa72d92008-08-26 18:03:31 +0000347 // Attempt to copy register that is not GPR or FPR
348 return false;
Owen Anderson8f2c8932007-12-31 06:32:00 +0000349 }
Owen Anderson9fa72d92008-08-26 18:03:31 +0000350
351 return true;
Owen Anderson8f2c8932007-12-31 06:32:00 +0000352}
353
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000354bool
Dan Gohman221a4372008-07-07 23:14:23 +0000355PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
356 unsigned SrcReg, bool isKill,
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000357 int FrameIdx,
358 const TargetRegisterClass *RC,
359 SmallVectorImpl<MachineInstr*> &NewMIs) const{
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000360 DebugLoc DL = DebugLoc::getUnknownLoc();
Owen Anderson81875432008-01-01 21:11:32 +0000361 if (RC == PPC::GPRCRegisterClass) {
362 if (SrcReg != PPC::LR) {
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000363 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000364 .addReg(SrcReg, false, false, isKill),
365 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000366 } else {
367 // FIXME: this spills LR immediately to memory in one step. To do this,
368 // we use R11, which we know cannot be used in the prolog/epilog. This is
369 // a hack.
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000370 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFLR), PPC::R11));
371 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000372 .addReg(PPC::R11, false, false, isKill),
373 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000374 }
375 } else if (RC == PPC::G8RCRegisterClass) {
376 if (SrcReg != PPC::LR8) {
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000377 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
Chris Lattner7b7371c2008-03-10 18:55:53 +0000378 .addReg(SrcReg, false, false, isKill), FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000379 } else {
380 // FIXME: this spills LR immediately to memory in one step. To do this,
381 // we use R11, which we know cannot be used in the prolog/epilog. This is
382 // a hack.
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000383 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFLR8), PPC::X11));
384 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
Chris Lattner7b7371c2008-03-10 18:55:53 +0000385 .addReg(PPC::X11, false, false, isKill), FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000386 }
387 } else if (RC == PPC::F8RCRegisterClass) {
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000388 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD))
Chris Lattner7b7371c2008-03-10 18:55:53 +0000389 .addReg(SrcReg, false, false, isKill), FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000390 } else if (RC == PPC::F4RCRegisterClass) {
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000391 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS))
Chris Lattner7b7371c2008-03-10 18:55:53 +0000392 .addReg(SrcReg, false, false, isKill), FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000393 } else if (RC == PPC::CRRCRegisterClass) {
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000394 if ((EnablePPC32RS && !TM.getSubtargetImpl()->isPPC64()) ||
395 (EnablePPC64RS && TM.getSubtargetImpl()->isPPC64())) {
396 // FIXME (64-bit): Enable
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000397 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR))
Bill Wendlinga1877c52008-03-03 22:19:16 +0000398 .addReg(SrcReg, false, false, isKill),
Chris Lattner6734c3a2008-03-20 01:22:40 +0000399 FrameIdx));
Bill Wendlinga1877c52008-03-03 22:19:16 +0000400 return true;
401 } else {
402 // FIXME: We use R0 here, because it isn't available for RA. We need to
403 // store the CR in the low 4-bits of the saved value. First, issue a MFCR
404 // to save all of the CRBits.
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000405 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFCR), PPC::R0));
Owen Anderson81875432008-01-01 21:11:32 +0000406
Bill Wendlinga1877c52008-03-03 22:19:16 +0000407 // If the saved register wasn't CR0, shift the bits left so that they are
408 // in CR0's slot.
409 if (SrcReg != PPC::CR0) {
410 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(SrcReg)*4;
411 // rlwinm r0, r0, ShiftBits, 0, 31.
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000412 NewMIs.push_back(BuildMI(MF, DL, get(PPC::RLWINM), PPC::R0)
Chris Lattner7b7371c2008-03-10 18:55:53 +0000413 .addReg(PPC::R0).addImm(ShiftBits).addImm(0).addImm(31));
Bill Wendlinga1877c52008-03-03 22:19:16 +0000414 }
415
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000416 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
Bill Wendlinga1877c52008-03-03 22:19:16 +0000417 .addReg(PPC::R0, false, false, isKill),
418 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000419 }
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000420 } else if (RC == PPC::CRBITRCRegisterClass) {
421 // FIXME: We use CRi here because there is no mtcrf on a bit. Since the
422 // backend currently only uses CR1EQ as an individual bit, this should
423 // not cause any bug. If we need other uses of CR bits, the following
424 // code may be invalid.
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000425 unsigned Reg = 0;
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000426 if (SrcReg >= PPC::CR0LT || SrcReg <= PPC::CR0UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000427 Reg = PPC::CR0;
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000428 else if (SrcReg >= PPC::CR1LT || SrcReg <= PPC::CR1UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000429 Reg = PPC::CR1;
430 else if (SrcReg >= PPC::CR2LT || SrcReg <= PPC::CR2UN)
431 Reg = PPC::CR2;
432 else if (SrcReg >= PPC::CR3LT || SrcReg <= PPC::CR3UN)
433 Reg = PPC::CR3;
434 else if (SrcReg >= PPC::CR4LT || SrcReg <= PPC::CR4UN)
435 Reg = PPC::CR4;
436 else if (SrcReg >= PPC::CR5LT || SrcReg <= PPC::CR5UN)
437 Reg = PPC::CR5;
438 else if (SrcReg >= PPC::CR6LT || SrcReg <= PPC::CR6UN)
439 Reg = PPC::CR6;
440 else if (SrcReg >= PPC::CR7LT || SrcReg <= PPC::CR7UN)
441 Reg = PPC::CR7;
442
Dan Gohman221a4372008-07-07 23:14:23 +0000443 return StoreRegToStackSlot(MF, Reg, isKill, FrameIdx,
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000444 PPC::CRRCRegisterClass, NewMIs);
445
Owen Anderson81875432008-01-01 21:11:32 +0000446 } else if (RC == PPC::VRRCRegisterClass) {
447 // We don't have indexed addressing for vector loads. Emit:
448 // R0 = ADDI FI#
449 // STVX VAL, 0, R0
450 //
451 // FIXME: We use R0 here, because it isn't available for RA.
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000452 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::ADDI), PPC::R0),
Owen Anderson81875432008-01-01 21:11:32 +0000453 FrameIdx, 0, 0));
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000454 NewMIs.push_back(BuildMI(MF, DL, get(PPC::STVX))
Chris Lattner7b7371c2008-03-10 18:55:53 +0000455 .addReg(SrcReg, false, false, isKill).addReg(PPC::R0).addReg(PPC::R0));
Owen Anderson81875432008-01-01 21:11:32 +0000456 } else {
457 assert(0 && "Unknown regclass!");
458 abort();
459 }
Bill Wendlinga1877c52008-03-03 22:19:16 +0000460
461 return false;
Owen Anderson81875432008-01-01 21:11:32 +0000462}
463
464void
465PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Bill Wendlinga1877c52008-03-03 22:19:16 +0000466 MachineBasicBlock::iterator MI,
467 unsigned SrcReg, bool isKill, int FrameIdx,
468 const TargetRegisterClass *RC) const {
Dan Gohman221a4372008-07-07 23:14:23 +0000469 MachineFunction &MF = *MBB.getParent();
Owen Anderson81875432008-01-01 21:11:32 +0000470 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendlinga1877c52008-03-03 22:19:16 +0000471
Dan Gohman221a4372008-07-07 23:14:23 +0000472 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs)) {
473 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Bill Wendlinga1877c52008-03-03 22:19:16 +0000474 FuncInfo->setSpillsCR();
475 }
476
Owen Anderson81875432008-01-01 21:11:32 +0000477 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
478 MBB.insert(MI, NewMIs[i]);
479}
480
481void PPCInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
Bill Wendlinga1877c52008-03-03 22:19:16 +0000482 bool isKill,
483 SmallVectorImpl<MachineOperand> &Addr,
484 const TargetRegisterClass *RC,
485 SmallVectorImpl<MachineInstr*> &NewMIs) const{
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000486 if (Addr[0].isFI()) {
Dan Gohman221a4372008-07-07 23:14:23 +0000487 if (StoreRegToStackSlot(MF, SrcReg, isKill,
488 Addr[0].getIndex(), RC, NewMIs)) {
Bill Wendlinga1877c52008-03-03 22:19:16 +0000489 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
490 FuncInfo->setSpillsCR();
491 }
492
Owen Anderson81875432008-01-01 21:11:32 +0000493 return;
494 }
495
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000496 DebugLoc DL = DebugLoc::getUnknownLoc();
Owen Anderson81875432008-01-01 21:11:32 +0000497 unsigned Opc = 0;
498 if (RC == PPC::GPRCRegisterClass) {
499 Opc = PPC::STW;
500 } else if (RC == PPC::G8RCRegisterClass) {
501 Opc = PPC::STD;
502 } else if (RC == PPC::F8RCRegisterClass) {
503 Opc = PPC::STFD;
504 } else if (RC == PPC::F4RCRegisterClass) {
505 Opc = PPC::STFS;
506 } else if (RC == PPC::VRRCRegisterClass) {
507 Opc = PPC::STVX;
508 } else {
509 assert(0 && "Unknown regclass!");
510 abort();
511 }
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000512 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Opc))
Owen Anderson81875432008-01-01 21:11:32 +0000513 .addReg(SrcReg, false, false, isKill);
514 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
515 MachineOperand &MO = Addr[i];
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000516 if (MO.isReg())
Owen Anderson81875432008-01-01 21:11:32 +0000517 MIB.addReg(MO.getReg());
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000518 else if (MO.isImm())
Owen Anderson81875432008-01-01 21:11:32 +0000519 MIB.addImm(MO.getImm());
520 else
521 MIB.addFrameIndex(MO.getIndex());
522 }
523 NewMIs.push_back(MIB);
524 return;
525}
526
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000527void
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000528PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL,
Dan Gohman221a4372008-07-07 23:14:23 +0000529 unsigned DestReg, int FrameIdx,
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000530 const TargetRegisterClass *RC,
531 SmallVectorImpl<MachineInstr*> &NewMIs)const{
Owen Anderson81875432008-01-01 21:11:32 +0000532 if (RC == PPC::GPRCRegisterClass) {
533 if (DestReg != PPC::LR) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000534 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
535 DestReg), FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000536 } else {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000537 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
538 PPC::R11), FrameIdx));
539 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTLR)).addReg(PPC::R11));
Owen Anderson81875432008-01-01 21:11:32 +0000540 }
541 } else if (RC == PPC::G8RCRegisterClass) {
542 if (DestReg != PPC::LR8) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000543 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg),
Owen Anderson81875432008-01-01 21:11:32 +0000544 FrameIdx));
545 } else {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000546 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD),
547 PPC::R11), FrameIdx));
548 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTLR8)).addReg(PPC::R11));
Owen Anderson81875432008-01-01 21:11:32 +0000549 }
550 } else if (RC == PPC::F8RCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000551 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg),
Owen Anderson81875432008-01-01 21:11:32 +0000552 FrameIdx));
553 } else if (RC == PPC::F4RCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000554 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg),
Owen Anderson81875432008-01-01 21:11:32 +0000555 FrameIdx));
556 } else if (RC == PPC::CRRCRegisterClass) {
557 // FIXME: We use R0 here, because it isn't available for RA.
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000558 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ), PPC::R0),
Owen Anderson81875432008-01-01 21:11:32 +0000559 FrameIdx));
560
561 // If the reloaded register isn't CR0, shift the bits right so that they are
562 // in the right CR's slot.
563 if (DestReg != PPC::CR0) {
564 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(DestReg)*4;
565 // rlwinm r11, r11, 32-ShiftBits, 0, 31.
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000566 NewMIs.push_back(BuildMI(MF, DL, get(PPC::RLWINM), PPC::R0)
Owen Anderson81875432008-01-01 21:11:32 +0000567 .addReg(PPC::R0).addImm(32-ShiftBits).addImm(0).addImm(31));
568 }
569
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000570 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTCRF), DestReg).addReg(PPC::R0));
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000571 } else if (RC == PPC::CRBITRCRegisterClass) {
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000572
573 unsigned Reg = 0;
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000574 if (DestReg >= PPC::CR0LT || DestReg <= PPC::CR0UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000575 Reg = PPC::CR0;
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000576 else if (DestReg >= PPC::CR1LT || DestReg <= PPC::CR1UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000577 Reg = PPC::CR1;
578 else if (DestReg >= PPC::CR2LT || DestReg <= PPC::CR2UN)
579 Reg = PPC::CR2;
580 else if (DestReg >= PPC::CR3LT || DestReg <= PPC::CR3UN)
581 Reg = PPC::CR3;
582 else if (DestReg >= PPC::CR4LT || DestReg <= PPC::CR4UN)
583 Reg = PPC::CR4;
584 else if (DestReg >= PPC::CR5LT || DestReg <= PPC::CR5UN)
585 Reg = PPC::CR5;
586 else if (DestReg >= PPC::CR6LT || DestReg <= PPC::CR6UN)
587 Reg = PPC::CR6;
588 else if (DestReg >= PPC::CR7LT || DestReg <= PPC::CR7UN)
589 Reg = PPC::CR7;
590
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000591 return LoadRegFromStackSlot(MF, DL, Reg, FrameIdx,
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000592 PPC::CRRCRegisterClass, NewMIs);
593
Owen Anderson81875432008-01-01 21:11:32 +0000594 } else if (RC == PPC::VRRCRegisterClass) {
595 // We don't have indexed addressing for vector loads. Emit:
596 // R0 = ADDI FI#
597 // Dest = LVX 0, R0
598 //
599 // FIXME: We use R0 here, because it isn't available for RA.
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000600 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::ADDI), PPC::R0),
Owen Anderson81875432008-01-01 21:11:32 +0000601 FrameIdx, 0, 0));
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000602 NewMIs.push_back(BuildMI(MF, DL, get(PPC::LVX),DestReg).addReg(PPC::R0)
Owen Anderson81875432008-01-01 21:11:32 +0000603 .addReg(PPC::R0));
604 } else {
605 assert(0 && "Unknown regclass!");
606 abort();
607 }
608}
609
610void
611PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Bill Wendlinga1877c52008-03-03 22:19:16 +0000612 MachineBasicBlock::iterator MI,
613 unsigned DestReg, int FrameIdx,
614 const TargetRegisterClass *RC) const {
Dan Gohman221a4372008-07-07 23:14:23 +0000615 MachineFunction &MF = *MBB.getParent();
Owen Anderson81875432008-01-01 21:11:32 +0000616 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000617 DebugLoc DL = DebugLoc::getUnknownLoc();
618 if (MI != MBB.end()) DL = MI->getDebugLoc();
619 LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs);
Owen Anderson81875432008-01-01 21:11:32 +0000620 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
621 MBB.insert(MI, NewMIs[i]);
622}
623
624void PPCInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
Bill Wendlinga1877c52008-03-03 22:19:16 +0000625 SmallVectorImpl<MachineOperand> &Addr,
626 const TargetRegisterClass *RC,
627 SmallVectorImpl<MachineInstr*> &NewMIs)const{
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000628 if (Addr[0].isFI()) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000629 LoadRegFromStackSlot(MF, DebugLoc::getUnknownLoc(),
630 DestReg, Addr[0].getIndex(), RC, NewMIs);
Owen Anderson81875432008-01-01 21:11:32 +0000631 return;
632 }
633
634 unsigned Opc = 0;
635 if (RC == PPC::GPRCRegisterClass) {
636 assert(DestReg != PPC::LR && "Can't handle this yet!");
637 Opc = PPC::LWZ;
638 } else if (RC == PPC::G8RCRegisterClass) {
639 assert(DestReg != PPC::LR8 && "Can't handle this yet!");
640 Opc = PPC::LD;
641 } else if (RC == PPC::F8RCRegisterClass) {
642 Opc = PPC::LFD;
643 } else if (RC == PPC::F4RCRegisterClass) {
644 Opc = PPC::LFS;
645 } else if (RC == PPC::VRRCRegisterClass) {
646 Opc = PPC::LVX;
647 } else {
648 assert(0 && "Unknown regclass!");
649 abort();
650 }
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000651 DebugLoc DL = DebugLoc::getUnknownLoc();
652 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Opc), DestReg);
Owen Anderson81875432008-01-01 21:11:32 +0000653 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
654 MachineOperand &MO = Addr[i];
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000655 if (MO.isReg())
Owen Anderson81875432008-01-01 21:11:32 +0000656 MIB.addReg(MO.getReg());
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000657 else if (MO.isImm())
Owen Anderson81875432008-01-01 21:11:32 +0000658 MIB.addImm(MO.getImm());
659 else
660 MIB.addFrameIndex(MO.getIndex());
661 }
662 NewMIs.push_back(MIB);
663 return;
664}
665
Owen Anderson9a184ef2008-01-07 01:35:02 +0000666/// foldMemoryOperand - PowerPC (like most RISC's) can only fold spills into
667/// copy instructions, turning them into load/store instructions.
Dan Gohmanedc83d62008-12-03 18:43:12 +0000668MachineInstr *PPCInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
669 MachineInstr *MI,
670 const SmallVectorImpl<unsigned> &Ops,
671 int FrameIndex) const {
Owen Anderson9a184ef2008-01-07 01:35:02 +0000672 if (Ops.size() != 1) return NULL;
673
674 // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because
675 // it takes more than one instruction to store it.
676 unsigned Opc = MI->getOpcode();
677 unsigned OpNum = Ops[0];
678
679 MachineInstr *NewMI = NULL;
680 if ((Opc == PPC::OR &&
681 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
682 if (OpNum == 0) { // move -> store
683 unsigned InReg = MI->getOperand(1).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000684 bool isKill = MI->getOperand(1).isKill();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000685 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::STW))
Evan Chenge52c1912008-07-03 09:09:37 +0000686 .addReg(InReg, false, false, isKill),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000687 FrameIndex);
688 } else { // move -> load
689 unsigned OutReg = MI->getOperand(0).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000690 bool isDead = MI->getOperand(0).isDead();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000691 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::LWZ))
Evan Chenge52c1912008-07-03 09:09:37 +0000692 .addReg(OutReg, true, false, false, isDead),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000693 FrameIndex);
694 }
695 } else if ((Opc == PPC::OR8 &&
696 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
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();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000700 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::STD))
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();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000706 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::LD))
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::FMRD) {
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();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000714 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::STFD))
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();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000720 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::LFD))
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 } else if (Opc == PPC::FMRS) {
725 if (OpNum == 0) { // move -> store
726 unsigned InReg = MI->getOperand(1).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000727 bool isKill = MI->getOperand(1).isKill();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000728 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::STFS))
Evan Chenge52c1912008-07-03 09:09:37 +0000729 .addReg(InReg, false, false, isKill),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000730 FrameIndex);
731 } else { // move -> load
732 unsigned OutReg = MI->getOperand(0).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000733 bool isDead = MI->getOperand(0).isDead();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000734 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::LFS))
Evan Chenge52c1912008-07-03 09:09:37 +0000735 .addReg(OutReg, true, false, false, isDead),
736 FrameIndex);
Owen Anderson9a184ef2008-01-07 01:35:02 +0000737 }
738 }
739
Owen Anderson9a184ef2008-01-07 01:35:02 +0000740 return NewMI;
741}
742
Dan Gohman46b948e2008-10-16 01:49:15 +0000743bool PPCInstrInfo::canFoldMemoryOperand(const MachineInstr *MI,
744 const SmallVectorImpl<unsigned> &Ops) const {
Owen Anderson9a184ef2008-01-07 01:35:02 +0000745 if (Ops.size() != 1) return false;
746
747 // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because
748 // it takes more than one instruction to store it.
749 unsigned Opc = MI->getOpcode();
750
751 if ((Opc == PPC::OR &&
752 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
753 return true;
754 else if ((Opc == PPC::OR8 &&
755 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
756 return true;
757 else if (Opc == PPC::FMRD || Opc == PPC::FMRS)
758 return true;
759
760 return false;
761}
762
Owen Anderson81875432008-01-01 21:11:32 +0000763
Dan Gohman46b948e2008-10-16 01:49:15 +0000764bool PPCInstrInfo::BlockHasNoFallThrough(const MachineBasicBlock &MBB) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000765 if (MBB.empty()) return false;
766
767 switch (MBB.back().getOpcode()) {
768 case PPC::BLR: // Return.
769 case PPC::B: // Uncond branch.
770 case PPC::BCTR: // Indirect branch.
771 return true;
772 default: return false;
773 }
774}
775
776bool PPCInstrInfo::
Owen Andersond131b5b2008-08-14 22:49:33 +0000777ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000778 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
779 // Leave the CR# the same, but invert the condition.
780 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
781 return false;
782}
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000783
784/// GetInstSize - Return the number of bytes of code the specified
785/// instruction may be. This returns the maximum number of bytes.
786///
787unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
788 switch (MI->getOpcode()) {
789 case PPC::INLINEASM: { // Inline Asm: Variable size.
790 const MachineFunction *MF = MI->getParent()->getParent();
791 const char *AsmStr = MI->getOperand(0).getSymbolName();
792 return MF->getTarget().getTargetAsmInfo()->getInlineAsmLength(AsmStr);
793 }
Dan Gohmanfa607c92008-07-01 00:05:16 +0000794 case PPC::DBG_LABEL:
795 case PPC::EH_LABEL:
796 case PPC::GC_LABEL:
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000797 return 0;
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000798 default:
799 return 4; // PowerPC instructions are all 4 bytes
800 }
801}