blob: 2553a447168d7a3f68d7b1b8d3f5a578cc05c017 [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"
Edwin Török4d9756a2009-07-08 20:53:28 +000023#include "llvm/Support/ErrorHandling.h"
24#include "llvm/Support/raw_ostream.h"
Nicolas Geoffraycb162a02008-04-16 20:10:13 +000025#include "llvm/Target/TargetAsmInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026using namespace llvm;
27
Bill Wendling4eaadfb2008-03-10 22:49:16 +000028extern cl::opt<bool> EnablePPC32RS; // FIXME (64-bit): See PPCRegisterInfo.cpp.
29extern cl::opt<bool> EnablePPC64RS; // FIXME (64-bit): See PPCRegisterInfo.cpp.
Bill Wendling03598502008-03-04 23:13:51 +000030
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031PPCInstrInfo::PPCInstrInfo(PPCTargetMachine &tm)
Chris Lattnerd2fd6db2008-01-01 01:03:04 +000032 : TargetInstrInfoImpl(PPCInsts, array_lengthof(PPCInsts)), TM(tm),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033 RI(*TM.getSubtargetImpl(), *this) {}
34
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035bool PPCInstrInfo::isMoveInstr(const MachineInstr& MI,
36 unsigned& sourceReg,
Evan Chengf97496a2009-01-20 19:12:24 +000037 unsigned& destReg,
38 unsigned& sourceSubIdx,
39 unsigned& destSubIdx) const {
40 sourceSubIdx = destSubIdx = 0; // No sub-registers.
41
Chris Lattner99aa3372008-01-07 02:48:55 +000042 unsigned oc = MI.getOpcode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043 if (oc == PPC::OR || oc == PPC::OR8 || oc == PPC::VOR ||
44 oc == PPC::OR4To8 || oc == PPC::OR8To4) { // or r1, r2, r2
45 assert(MI.getNumOperands() >= 3 &&
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000046 MI.getOperand(0).isReg() &&
47 MI.getOperand(1).isReg() &&
48 MI.getOperand(2).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049 "invalid PPC OR instruction!");
50 if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
51 sourceReg = MI.getOperand(1).getReg();
52 destReg = MI.getOperand(0).getReg();
53 return true;
54 }
55 } else if (oc == PPC::ADDI) { // addi r1, r2, 0
56 assert(MI.getNumOperands() >= 3 &&
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000057 MI.getOperand(0).isReg() &&
58 MI.getOperand(2).isImm() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059 "invalid PPC ADDI instruction!");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000060 if (MI.getOperand(1).isReg() && MI.getOperand(2).getImm() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061 sourceReg = MI.getOperand(1).getReg();
62 destReg = MI.getOperand(0).getReg();
63 return true;
64 }
65 } else if (oc == PPC::ORI) { // ori r1, r2, 0
66 assert(MI.getNumOperands() >= 3 &&
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000067 MI.getOperand(0).isReg() &&
68 MI.getOperand(1).isReg() &&
69 MI.getOperand(2).isImm() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070 "invalid PPC ORI instruction!");
Chris Lattnera96056a2007-12-30 20:49:49 +000071 if (MI.getOperand(2).getImm() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072 sourceReg = MI.getOperand(1).getReg();
73 destReg = MI.getOperand(0).getReg();
74 return true;
75 }
76 } else if (oc == PPC::FMRS || oc == PPC::FMRD ||
77 oc == PPC::FMRSD) { // fmr r1, r2
78 assert(MI.getNumOperands() >= 2 &&
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000079 MI.getOperand(0).isReg() &&
80 MI.getOperand(1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081 "invalid PPC FMR instruction");
82 sourceReg = MI.getOperand(1).getReg();
83 destReg = MI.getOperand(0).getReg();
84 return true;
85 } else if (oc == PPC::MCRF) { // mcrf cr1, cr2
86 assert(MI.getNumOperands() >= 2 &&
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000087 MI.getOperand(0).isReg() &&
88 MI.getOperand(1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089 "invalid PPC MCRF instruction");
90 sourceReg = MI.getOperand(1).getReg();
91 destReg = MI.getOperand(0).getReg();
92 return true;
93 }
94 return false;
95}
96
Dan Gohman90feee22008-11-18 19:49:32 +000097unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098 int &FrameIndex) const {
99 switch (MI->getOpcode()) {
100 default: break;
101 case PPC::LD:
102 case PPC::LWZ:
103 case PPC::LFS:
104 case PPC::LFD:
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000105 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
106 MI->getOperand(2).isFI()) {
Chris Lattner6017d482007-12-30 23:10:15 +0000107 FrameIndex = MI->getOperand(2).getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000108 return MI->getOperand(0).getReg();
109 }
110 break;
111 }
112 return 0;
113}
114
Dan Gohman90feee22008-11-18 19:49:32 +0000115unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116 int &FrameIndex) const {
117 switch (MI->getOpcode()) {
118 default: break;
119 case PPC::STD:
120 case PPC::STW:
121 case PPC::STFS:
122 case PPC::STFD:
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000123 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
124 MI->getOperand(2).isFI()) {
Chris Lattner6017d482007-12-30 23:10:15 +0000125 FrameIndex = MI->getOperand(2).getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126 return MI->getOperand(0).getReg();
127 }
128 break;
129 }
130 return 0;
131}
132
133// commuteInstruction - We can commute rlwimi instructions, but only if the
134// rotate amt is zero. We also have to munge the immediates a bit.
Evan Cheng5de1aaf2008-06-16 07:33:11 +0000135MachineInstr *
136PPCInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
Dan Gohman221a4372008-07-07 23:14:23 +0000137 MachineFunction &MF = *MI->getParent()->getParent();
138
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139 // Normal instructions can be commuted the obvious way.
140 if (MI->getOpcode() != PPC::RLWIMI)
Evan Cheng5de1aaf2008-06-16 07:33:11 +0000141 return TargetInstrInfoImpl::commuteInstruction(MI, NewMI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142
143 // Cannot commute if it has a non-zero rotate count.
Chris Lattnera96056a2007-12-30 20:49:49 +0000144 if (MI->getOperand(3).getImm() != 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145 return 0;
146
147 // If we have a zero rotate count, we have:
148 // M = mask(MB,ME)
149 // Op0 = (Op1 & ~M) | (Op2 & M)
150 // Change this to:
151 // M = mask((ME+1)&31, (MB-1)&31)
152 // Op0 = (Op2 & ~M) | (Op1 & M)
153
154 // Swap op1/op2
Evan Chengb554e532008-02-13 02:46:49 +0000155 unsigned Reg0 = MI->getOperand(0).getReg();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156 unsigned Reg1 = MI->getOperand(1).getReg();
157 unsigned Reg2 = MI->getOperand(2).getReg();
158 bool Reg1IsKill = MI->getOperand(1).isKill();
159 bool Reg2IsKill = MI->getOperand(2).isKill();
Evan Cheng5de1aaf2008-06-16 07:33:11 +0000160 bool ChangeReg0 = false;
Evan Chengb554e532008-02-13 02:46:49 +0000161 // If machine instrs are no longer in two-address forms, update
162 // destination register as well.
163 if (Reg0 == Reg1) {
164 // Must be two address instruction!
165 assert(MI->getDesc().getOperandConstraint(0, TOI::TIED_TO) &&
166 "Expecting a two-address instruction!");
Evan Chengb554e532008-02-13 02:46:49 +0000167 Reg2IsKill = false;
Evan Cheng5de1aaf2008-06-16 07:33:11 +0000168 ChangeReg0 = true;
Evan Chengb554e532008-02-13 02:46:49 +0000169 }
Evan Cheng5de1aaf2008-06-16 07:33:11 +0000170
171 // Masks.
172 unsigned MB = MI->getOperand(4).getImm();
173 unsigned ME = MI->getOperand(5).getImm();
174
175 if (NewMI) {
176 // Create a new instruction.
177 unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg();
178 bool Reg0IsDead = MI->getOperand(0).isDead();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000179 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
Bill Wendling2b739762009-05-13 21:33:08 +0000180 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
181 .addReg(Reg2, getKillRegState(Reg2IsKill))
182 .addReg(Reg1, getKillRegState(Reg1IsKill))
Evan Cheng5de1aaf2008-06-16 07:33:11 +0000183 .addImm((ME+1) & 31)
184 .addImm((MB-1) & 31);
185 }
186
187 if (ChangeReg0)
188 MI->getOperand(0).setReg(Reg2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 MI->getOperand(2).setReg(Reg1);
190 MI->getOperand(1).setReg(Reg2);
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000191 MI->getOperand(2).setIsKill(Reg1IsKill);
192 MI->getOperand(1).setIsKill(Reg2IsKill);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000193
194 // Swap the mask around.
Chris Lattnera96056a2007-12-30 20:49:49 +0000195 MI->getOperand(4).setImm((ME+1) & 31);
196 MI->getOperand(5).setImm((MB-1) & 31);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197 return MI;
198}
199
200void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
201 MachineBasicBlock::iterator MI) const {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000202 DebugLoc DL = DebugLoc::getUnknownLoc();
203 if (MI != MBB.end()) DL = MI->getDebugLoc();
204
205 BuildMI(MBB, MI, DL, get(PPC::NOP));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206}
207
208
209// Branch analysis.
210bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
211 MachineBasicBlock *&FBB,
Evan Chengeac31642009-02-09 07:14:22 +0000212 SmallVectorImpl<MachineOperand> &Cond,
213 bool AllowModify) 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) {
Evan Cheng6ed927b2009-05-08 23:09:25 +0000225 if (!LastInst->getOperand(0).isMBB())
226 return true;
Chris Lattner6017d482007-12-30 23:10:15 +0000227 TBB = LastInst->getOperand(0).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000228 return false;
229 } else if (LastInst->getOpcode() == PPC::BCC) {
Evan Cheng6ed927b2009-05-08 23:09:25 +0000230 if (!LastInst->getOperand(2).isMBB())
231 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 // Block ends with fall-through condbranch.
Chris Lattner6017d482007-12-30 23:10:15 +0000233 TBB = LastInst->getOperand(2).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000234 Cond.push_back(LastInst->getOperand(0));
235 Cond.push_back(LastInst->getOperand(1));
236 return false;
237 }
238 // Otherwise, don't know what this is.
239 return true;
240 }
241
242 // Get the instruction before it if it's a terminator.
243 MachineInstr *SecondLastInst = I;
244
245 // If there are three terminators, we don't know what sort of block this is.
246 if (SecondLastInst && I != MBB.begin() &&
247 isUnpredicatedTerminator(--I))
248 return true;
249
250 // If the block ends with PPC::B and PPC:BCC, handle it.
251 if (SecondLastInst->getOpcode() == PPC::BCC &&
252 LastInst->getOpcode() == PPC::B) {
Evan Cheng6ed927b2009-05-08 23:09:25 +0000253 if (!SecondLastInst->getOperand(2).isMBB() ||
254 !LastInst->getOperand(0).isMBB())
255 return true;
Chris Lattner6017d482007-12-30 23:10:15 +0000256 TBB = SecondLastInst->getOperand(2).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257 Cond.push_back(SecondLastInst->getOperand(0));
258 Cond.push_back(SecondLastInst->getOperand(1));
Chris Lattner6017d482007-12-30 23:10:15 +0000259 FBB = LastInst->getOperand(0).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000260 return false;
261 }
262
263 // If the block ends with two PPC:Bs, handle it. The second one is not
264 // executed, so remove it.
265 if (SecondLastInst->getOpcode() == PPC::B &&
266 LastInst->getOpcode() == PPC::B) {
Evan Cheng6ed927b2009-05-08 23:09:25 +0000267 if (!SecondLastInst->getOperand(0).isMBB())
268 return true;
Chris Lattner6017d482007-12-30 23:10:15 +0000269 TBB = SecondLastInst->getOperand(0).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000270 I = LastInst;
Evan Chengeac31642009-02-09 07:14:22 +0000271 if (AllowModify)
272 I->eraseFromParent();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000273 return false;
274 }
275
276 // Otherwise, can't handle this.
277 return true;
278}
279
280unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
281 MachineBasicBlock::iterator I = MBB.end();
282 if (I == MBB.begin()) return 0;
283 --I;
284 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC)
285 return 0;
286
287 // Remove the branch.
288 I->eraseFromParent();
289
290 I = MBB.end();
291
292 if (I == MBB.begin()) return 1;
293 --I;
294 if (I->getOpcode() != PPC::BCC)
295 return 1;
296
297 // Remove the branch.
298 I->eraseFromParent();
299 return 2;
300}
301
302unsigned
303PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
304 MachineBasicBlock *FBB,
Owen Andersond131b5b2008-08-14 22:49:33 +0000305 const SmallVectorImpl<MachineOperand> &Cond) const {
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000306 // FIXME this should probably have a DebugLoc argument
307 DebugLoc dl = DebugLoc::getUnknownLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000308 // Shouldn't be a fall through.
309 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
310 assert((Cond.size() == 2 || Cond.size() == 0) &&
311 "PPC branch conditions have two components!");
312
313 // One-way branch.
314 if (FBB == 0) {
315 if (Cond.empty()) // Unconditional branch
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000316 BuildMI(&MBB, dl, get(PPC::B)).addMBB(TBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000317 else // Conditional branch
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000318 BuildMI(&MBB, dl, get(PPC::BCC))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000319 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
320 return 1;
321 }
322
323 // Two-way Conditional Branch.
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000324 BuildMI(&MBB, dl, get(PPC::BCC))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000325 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000326 BuildMI(&MBB, dl, get(PPC::B)).addMBB(FBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000327 return 2;
328}
329
Owen Anderson9fa72d92008-08-26 18:03:31 +0000330bool PPCInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
Owen Anderson8f2c8932007-12-31 06:32:00 +0000331 MachineBasicBlock::iterator MI,
332 unsigned DestReg, unsigned SrcReg,
333 const TargetRegisterClass *DestRC,
334 const TargetRegisterClass *SrcRC) const {
335 if (DestRC != SrcRC) {
Owen Anderson9fa72d92008-08-26 18:03:31 +0000336 // Not yet supported!
337 return false;
Owen Anderson8f2c8932007-12-31 06:32:00 +0000338 }
339
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000340 DebugLoc DL = DebugLoc::getUnknownLoc();
341 if (MI != MBB.end()) DL = MI->getDebugLoc();
342
Owen Anderson8f2c8932007-12-31 06:32:00 +0000343 if (DestRC == PPC::GPRCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000344 BuildMI(MBB, MI, DL, get(PPC::OR), DestReg).addReg(SrcReg).addReg(SrcReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000345 } else if (DestRC == PPC::G8RCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000346 BuildMI(MBB, MI, DL, get(PPC::OR8), DestReg).addReg(SrcReg).addReg(SrcReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000347 } else if (DestRC == PPC::F4RCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000348 BuildMI(MBB, MI, DL, get(PPC::FMRS), DestReg).addReg(SrcReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000349 } else if (DestRC == PPC::F8RCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000350 BuildMI(MBB, MI, DL, get(PPC::FMRD), DestReg).addReg(SrcReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000351 } else if (DestRC == PPC::CRRCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000352 BuildMI(MBB, MI, DL, get(PPC::MCRF), DestReg).addReg(SrcReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000353 } else if (DestRC == PPC::VRRCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000354 BuildMI(MBB, MI, DL, get(PPC::VOR), DestReg).addReg(SrcReg).addReg(SrcReg);
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000355 } else if (DestRC == PPC::CRBITRCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000356 BuildMI(MBB, MI, DL, get(PPC::CROR), DestReg).addReg(SrcReg).addReg(SrcReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000357 } else {
Owen Anderson9fa72d92008-08-26 18:03:31 +0000358 // Attempt to copy register that is not GPR or FPR
359 return false;
Owen Anderson8f2c8932007-12-31 06:32:00 +0000360 }
Owen Anderson9fa72d92008-08-26 18:03:31 +0000361
362 return true;
Owen Anderson8f2c8932007-12-31 06:32:00 +0000363}
364
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000365bool
Dan Gohman221a4372008-07-07 23:14:23 +0000366PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
367 unsigned SrcReg, bool isKill,
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000368 int FrameIdx,
369 const TargetRegisterClass *RC,
370 SmallVectorImpl<MachineInstr*> &NewMIs) const{
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000371 DebugLoc DL = DebugLoc::getUnknownLoc();
Owen Anderson81875432008-01-01 21:11:32 +0000372 if (RC == PPC::GPRCRegisterClass) {
373 if (SrcReg != PPC::LR) {
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000374 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
Bill Wendling2b739762009-05-13 21:33:08 +0000375 .addReg(SrcReg,
376 getKillRegState(isKill)),
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000377 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000378 } else {
379 // FIXME: this spills LR immediately to memory in one step. To do this,
380 // we use R11, which we know cannot be used in the prolog/epilog. This is
381 // a hack.
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000382 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFLR), PPC::R11));
383 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
Bill Wendling2b739762009-05-13 21:33:08 +0000384 .addReg(PPC::R11,
385 getKillRegState(isKill)),
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000386 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000387 }
388 } else if (RC == PPC::G8RCRegisterClass) {
389 if (SrcReg != PPC::LR8) {
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000390 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
Bill Wendling2b739762009-05-13 21:33:08 +0000391 .addReg(SrcReg,
392 getKillRegState(isKill)),
393 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000394 } else {
395 // FIXME: this spills LR immediately to memory in one step. To do this,
396 // we use R11, which we know cannot be used in the prolog/epilog. This is
397 // a hack.
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000398 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFLR8), PPC::X11));
399 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
Bill Wendling2b739762009-05-13 21:33:08 +0000400 .addReg(PPC::X11,
401 getKillRegState(isKill)),
402 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000403 }
404 } else if (RC == PPC::F8RCRegisterClass) {
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000405 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD))
Bill Wendling2b739762009-05-13 21:33:08 +0000406 .addReg(SrcReg,
407 getKillRegState(isKill)),
408 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000409 } else if (RC == PPC::F4RCRegisterClass) {
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000410 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS))
Bill Wendling2b739762009-05-13 21:33:08 +0000411 .addReg(SrcReg,
412 getKillRegState(isKill)),
413 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000414 } else if (RC == PPC::CRRCRegisterClass) {
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000415 if ((EnablePPC32RS && !TM.getSubtargetImpl()->isPPC64()) ||
416 (EnablePPC64RS && TM.getSubtargetImpl()->isPPC64())) {
417 // FIXME (64-bit): Enable
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000418 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR))
Bill Wendling2b739762009-05-13 21:33:08 +0000419 .addReg(SrcReg,
420 getKillRegState(isKill)),
Chris Lattner6734c3a2008-03-20 01:22:40 +0000421 FrameIdx));
Bill Wendlinga1877c52008-03-03 22:19:16 +0000422 return true;
423 } else {
424 // FIXME: We use R0 here, because it isn't available for RA. We need to
425 // store the CR in the low 4-bits of the saved value. First, issue a MFCR
426 // to save all of the CRBits.
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000427 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFCR), PPC::R0));
Owen Anderson81875432008-01-01 21:11:32 +0000428
Bill Wendlinga1877c52008-03-03 22:19:16 +0000429 // If the saved register wasn't CR0, shift the bits left so that they are
430 // in CR0's slot.
431 if (SrcReg != PPC::CR0) {
432 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(SrcReg)*4;
433 // rlwinm r0, r0, ShiftBits, 0, 31.
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000434 NewMIs.push_back(BuildMI(MF, DL, get(PPC::RLWINM), PPC::R0)
Chris Lattner7b7371c2008-03-10 18:55:53 +0000435 .addReg(PPC::R0).addImm(ShiftBits).addImm(0).addImm(31));
Bill Wendlinga1877c52008-03-03 22:19:16 +0000436 }
437
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000438 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
Bill Wendling2b739762009-05-13 21:33:08 +0000439 .addReg(PPC::R0,
440 getKillRegState(isKill)),
Bill Wendlinga1877c52008-03-03 22:19:16 +0000441 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000442 }
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000443 } else if (RC == PPC::CRBITRCRegisterClass) {
444 // FIXME: We use CRi here because there is no mtcrf on a bit. Since the
445 // backend currently only uses CR1EQ as an individual bit, this should
446 // not cause any bug. If we need other uses of CR bits, the following
447 // code may be invalid.
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000448 unsigned Reg = 0;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000449 if (SrcReg == PPC::CR0LT || SrcReg == PPC::CR0GT ||
450 SrcReg == PPC::CR0EQ || SrcReg == PPC::CR0UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000451 Reg = PPC::CR0;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000452 else if (SrcReg == PPC::CR1LT || SrcReg == PPC::CR1GT ||
453 SrcReg == PPC::CR1EQ || SrcReg == PPC::CR1UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000454 Reg = PPC::CR1;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000455 else if (SrcReg == PPC::CR2LT || SrcReg == PPC::CR2GT ||
456 SrcReg == PPC::CR2EQ || SrcReg == PPC::CR2UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000457 Reg = PPC::CR2;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000458 else if (SrcReg == PPC::CR3LT || SrcReg == PPC::CR3GT ||
459 SrcReg == PPC::CR3EQ || SrcReg == PPC::CR3UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000460 Reg = PPC::CR3;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000461 else if (SrcReg == PPC::CR4LT || SrcReg == PPC::CR4GT ||
462 SrcReg == PPC::CR4EQ || SrcReg == PPC::CR4UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000463 Reg = PPC::CR4;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000464 else if (SrcReg == PPC::CR5LT || SrcReg == PPC::CR5GT ||
465 SrcReg == PPC::CR5EQ || SrcReg == PPC::CR5UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000466 Reg = PPC::CR5;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000467 else if (SrcReg == PPC::CR6LT || SrcReg == PPC::CR6GT ||
468 SrcReg == PPC::CR6EQ || SrcReg == PPC::CR6UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000469 Reg = PPC::CR6;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000470 else if (SrcReg == PPC::CR7LT || SrcReg == PPC::CR7GT ||
471 SrcReg == PPC::CR7EQ || SrcReg == PPC::CR7UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000472 Reg = PPC::CR7;
473
Dan Gohman221a4372008-07-07 23:14:23 +0000474 return StoreRegToStackSlot(MF, Reg, isKill, FrameIdx,
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000475 PPC::CRRCRegisterClass, NewMIs);
476
Owen Anderson81875432008-01-01 21:11:32 +0000477 } else if (RC == PPC::VRRCRegisterClass) {
478 // We don't have indexed addressing for vector loads. Emit:
479 // R0 = ADDI FI#
480 // STVX VAL, 0, R0
481 //
482 // FIXME: We use R0 here, because it isn't available for RA.
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000483 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::ADDI), PPC::R0),
Owen Anderson81875432008-01-01 21:11:32 +0000484 FrameIdx, 0, 0));
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000485 NewMIs.push_back(BuildMI(MF, DL, get(PPC::STVX))
Bill Wendling2b739762009-05-13 21:33:08 +0000486 .addReg(SrcReg, getKillRegState(isKill))
487 .addReg(PPC::R0)
488 .addReg(PPC::R0));
Owen Anderson81875432008-01-01 21:11:32 +0000489 } else {
Edwin Törökbd448e32009-07-14 16:55:14 +0000490 llvm_unreachable("Unknown regclass!");
Owen Anderson81875432008-01-01 21:11:32 +0000491 }
Bill Wendlinga1877c52008-03-03 22:19:16 +0000492
493 return false;
Owen Anderson81875432008-01-01 21:11:32 +0000494}
495
496void
497PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Bill Wendlinga1877c52008-03-03 22:19:16 +0000498 MachineBasicBlock::iterator MI,
499 unsigned SrcReg, bool isKill, int FrameIdx,
500 const TargetRegisterClass *RC) const {
Dan Gohman221a4372008-07-07 23:14:23 +0000501 MachineFunction &MF = *MBB.getParent();
Owen Anderson81875432008-01-01 21:11:32 +0000502 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendlinga1877c52008-03-03 22:19:16 +0000503
Dan Gohman221a4372008-07-07 23:14:23 +0000504 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs)) {
505 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Bill Wendlinga1877c52008-03-03 22:19:16 +0000506 FuncInfo->setSpillsCR();
507 }
508
Owen Anderson81875432008-01-01 21:11:32 +0000509 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
510 MBB.insert(MI, NewMIs[i]);
511}
512
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000513void
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000514PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL,
Dan Gohman221a4372008-07-07 23:14:23 +0000515 unsigned DestReg, int FrameIdx,
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000516 const TargetRegisterClass *RC,
517 SmallVectorImpl<MachineInstr*> &NewMIs)const{
Owen Anderson81875432008-01-01 21:11:32 +0000518 if (RC == PPC::GPRCRegisterClass) {
519 if (DestReg != PPC::LR) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000520 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
521 DestReg), FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000522 } else {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000523 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
524 PPC::R11), FrameIdx));
525 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTLR)).addReg(PPC::R11));
Owen Anderson81875432008-01-01 21:11:32 +0000526 }
527 } else if (RC == PPC::G8RCRegisterClass) {
528 if (DestReg != PPC::LR8) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000529 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg),
Owen Anderson81875432008-01-01 21:11:32 +0000530 FrameIdx));
531 } else {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000532 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD),
533 PPC::R11), FrameIdx));
534 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTLR8)).addReg(PPC::R11));
Owen Anderson81875432008-01-01 21:11:32 +0000535 }
536 } else if (RC == PPC::F8RCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000537 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg),
Owen Anderson81875432008-01-01 21:11:32 +0000538 FrameIdx));
539 } else if (RC == PPC::F4RCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000540 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg),
Owen Anderson81875432008-01-01 21:11:32 +0000541 FrameIdx));
542 } else if (RC == PPC::CRRCRegisterClass) {
543 // FIXME: We use R0 here, because it isn't available for RA.
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000544 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ), PPC::R0),
Owen Anderson81875432008-01-01 21:11:32 +0000545 FrameIdx));
546
547 // If the reloaded register isn't CR0, shift the bits right so that they are
548 // in the right CR's slot.
549 if (DestReg != PPC::CR0) {
550 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(DestReg)*4;
551 // rlwinm r11, r11, 32-ShiftBits, 0, 31.
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000552 NewMIs.push_back(BuildMI(MF, DL, get(PPC::RLWINM), PPC::R0)
Owen Anderson81875432008-01-01 21:11:32 +0000553 .addReg(PPC::R0).addImm(32-ShiftBits).addImm(0).addImm(31));
554 }
555
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000556 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTCRF), DestReg).addReg(PPC::R0));
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000557 } else if (RC == PPC::CRBITRCRegisterClass) {
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000558
559 unsigned Reg = 0;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000560 if (DestReg == PPC::CR0LT || DestReg == PPC::CR0GT ||
561 DestReg == PPC::CR0EQ || DestReg == PPC::CR0UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000562 Reg = PPC::CR0;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000563 else if (DestReg == PPC::CR1LT || DestReg == PPC::CR1GT ||
564 DestReg == PPC::CR1EQ || DestReg == PPC::CR1UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000565 Reg = PPC::CR1;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000566 else if (DestReg == PPC::CR2LT || DestReg == PPC::CR2GT ||
567 DestReg == PPC::CR2EQ || DestReg == PPC::CR2UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000568 Reg = PPC::CR2;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000569 else if (DestReg == PPC::CR3LT || DestReg == PPC::CR3GT ||
570 DestReg == PPC::CR3EQ || DestReg == PPC::CR3UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000571 Reg = PPC::CR3;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000572 else if (DestReg == PPC::CR4LT || DestReg == PPC::CR4GT ||
573 DestReg == PPC::CR4EQ || DestReg == PPC::CR4UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000574 Reg = PPC::CR4;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000575 else if (DestReg == PPC::CR5LT || DestReg == PPC::CR5GT ||
576 DestReg == PPC::CR5EQ || DestReg == PPC::CR5UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000577 Reg = PPC::CR5;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000578 else if (DestReg == PPC::CR6LT || DestReg == PPC::CR6GT ||
579 DestReg == PPC::CR6EQ || DestReg == PPC::CR6UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000580 Reg = PPC::CR6;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000581 else if (DestReg == PPC::CR7LT || DestReg == PPC::CR7GT ||
582 DestReg == PPC::CR7EQ || DestReg == PPC::CR7UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000583 Reg = PPC::CR7;
584
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000585 return LoadRegFromStackSlot(MF, DL, 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.
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000594 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::ADDI), PPC::R0),
Owen Anderson81875432008-01-01 21:11:32 +0000595 FrameIdx, 0, 0));
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000596 NewMIs.push_back(BuildMI(MF, DL, get(PPC::LVX),DestReg).addReg(PPC::R0)
Owen Anderson81875432008-01-01 21:11:32 +0000597 .addReg(PPC::R0));
598 } else {
Edwin Törökbd448e32009-07-14 16:55:14 +0000599 llvm_unreachable("Unknown regclass!");
Owen Anderson81875432008-01-01 21:11:32 +0000600 }
601}
602
603void
604PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Bill Wendlinga1877c52008-03-03 22:19:16 +0000605 MachineBasicBlock::iterator MI,
606 unsigned DestReg, int FrameIdx,
607 const TargetRegisterClass *RC) const {
Dan Gohman221a4372008-07-07 23:14:23 +0000608 MachineFunction &MF = *MBB.getParent();
Owen Anderson81875432008-01-01 21:11:32 +0000609 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000610 DebugLoc DL = DebugLoc::getUnknownLoc();
611 if (MI != MBB.end()) DL = MI->getDebugLoc();
612 LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs);
Owen Anderson81875432008-01-01 21:11:32 +0000613 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
614 MBB.insert(MI, NewMIs[i]);
615}
616
Owen Anderson9a184ef2008-01-07 01:35:02 +0000617/// foldMemoryOperand - PowerPC (like most RISC's) can only fold spills into
618/// copy instructions, turning them into load/store instructions.
Dan Gohmanedc83d62008-12-03 18:43:12 +0000619MachineInstr *PPCInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
620 MachineInstr *MI,
621 const SmallVectorImpl<unsigned> &Ops,
622 int FrameIndex) const {
Owen Anderson9a184ef2008-01-07 01:35:02 +0000623 if (Ops.size() != 1) return NULL;
624
625 // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because
626 // it takes more than one instruction to store it.
627 unsigned Opc = MI->getOpcode();
628 unsigned OpNum = Ops[0];
629
630 MachineInstr *NewMI = NULL;
631 if ((Opc == PPC::OR &&
632 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
633 if (OpNum == 0) { // move -> store
634 unsigned InReg = MI->getOperand(1).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000635 bool isKill = MI->getOperand(1).isKill();
Evan Cheng65219822009-07-01 01:59:31 +0000636 bool isUndef = MI->getOperand(1).isUndef();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000637 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::STW))
Evan Cheng65219822009-07-01 01:59:31 +0000638 .addReg(InReg,
639 getKillRegState(isKill) |
640 getUndefRegState(isUndef)),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000641 FrameIndex);
642 } else { // move -> load
643 unsigned OutReg = MI->getOperand(0).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000644 bool isDead = MI->getOperand(0).isDead();
Evan Cheng65219822009-07-01 01:59:31 +0000645 bool isUndef = MI->getOperand(0).isUndef();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000646 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::LWZ))
Bill Wendling2b739762009-05-13 21:33:08 +0000647 .addReg(OutReg,
648 RegState::Define |
Evan Cheng65219822009-07-01 01:59:31 +0000649 getDeadRegState(isDead) |
650 getUndefRegState(isUndef)),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000651 FrameIndex);
652 }
653 } else if ((Opc == PPC::OR8 &&
654 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
655 if (OpNum == 0) { // move -> store
656 unsigned InReg = MI->getOperand(1).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000657 bool isKill = MI->getOperand(1).isKill();
Evan Cheng65219822009-07-01 01:59:31 +0000658 bool isUndef = MI->getOperand(1).isUndef();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000659 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::STD))
Evan Cheng65219822009-07-01 01:59:31 +0000660 .addReg(InReg,
661 getKillRegState(isKill) |
662 getUndefRegState(isUndef)),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000663 FrameIndex);
664 } else { // move -> load
665 unsigned OutReg = MI->getOperand(0).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000666 bool isDead = MI->getOperand(0).isDead();
Evan Cheng65219822009-07-01 01:59:31 +0000667 bool isUndef = MI->getOperand(0).isUndef();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000668 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::LD))
Bill Wendling2b739762009-05-13 21:33:08 +0000669 .addReg(OutReg,
670 RegState::Define |
Evan Cheng65219822009-07-01 01:59:31 +0000671 getDeadRegState(isDead) |
672 getUndefRegState(isUndef)),
Evan Chenge52c1912008-07-03 09:09:37 +0000673 FrameIndex);
Owen Anderson9a184ef2008-01-07 01:35:02 +0000674 }
675 } else if (Opc == PPC::FMRD) {
676 if (OpNum == 0) { // move -> store
677 unsigned InReg = MI->getOperand(1).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000678 bool isKill = MI->getOperand(1).isKill();
Evan Cheng65219822009-07-01 01:59:31 +0000679 bool isUndef = MI->getOperand(1).isUndef();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000680 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::STFD))
Evan Cheng65219822009-07-01 01:59:31 +0000681 .addReg(InReg,
682 getKillRegState(isKill) |
683 getUndefRegState(isUndef)),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000684 FrameIndex);
685 } else { // move -> load
686 unsigned OutReg = MI->getOperand(0).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000687 bool isDead = MI->getOperand(0).isDead();
Evan Cheng65219822009-07-01 01:59:31 +0000688 bool isUndef = MI->getOperand(0).isUndef();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000689 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::LFD))
Bill Wendling2b739762009-05-13 21:33:08 +0000690 .addReg(OutReg,
691 RegState::Define |
Evan Cheng65219822009-07-01 01:59:31 +0000692 getDeadRegState(isDead) |
693 getUndefRegState(isUndef)),
Evan Chenge52c1912008-07-03 09:09:37 +0000694 FrameIndex);
Owen Anderson9a184ef2008-01-07 01:35:02 +0000695 }
696 } else if (Opc == PPC::FMRS) {
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();
Evan Cheng65219822009-07-01 01:59:31 +0000700 bool isUndef = MI->getOperand(1).isUndef();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000701 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::STFS))
Evan Cheng65219822009-07-01 01:59:31 +0000702 .addReg(InReg,
703 getKillRegState(isKill) |
704 getUndefRegState(isUndef)),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000705 FrameIndex);
706 } else { // move -> load
707 unsigned OutReg = MI->getOperand(0).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000708 bool isDead = MI->getOperand(0).isDead();
Evan Cheng65219822009-07-01 01:59:31 +0000709 bool isUndef = MI->getOperand(0).isUndef();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000710 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::LFS))
Bill Wendling2b739762009-05-13 21:33:08 +0000711 .addReg(OutReg,
712 RegState::Define |
Evan Cheng65219822009-07-01 01:59:31 +0000713 getDeadRegState(isDead) |
714 getUndefRegState(isUndef)),
Evan Chenge52c1912008-07-03 09:09:37 +0000715 FrameIndex);
Owen Anderson9a184ef2008-01-07 01:35:02 +0000716 }
717 }
718
Owen Anderson9a184ef2008-01-07 01:35:02 +0000719 return NewMI;
720}
721
Dan Gohman46b948e2008-10-16 01:49:15 +0000722bool PPCInstrInfo::canFoldMemoryOperand(const MachineInstr *MI,
723 const SmallVectorImpl<unsigned> &Ops) const {
Owen Anderson9a184ef2008-01-07 01:35:02 +0000724 if (Ops.size() != 1) return false;
725
726 // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because
727 // it takes more than one instruction to store it.
728 unsigned Opc = MI->getOpcode();
729
730 if ((Opc == PPC::OR &&
731 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
732 return true;
733 else if ((Opc == PPC::OR8 &&
734 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
735 return true;
736 else if (Opc == PPC::FMRD || Opc == PPC::FMRS)
737 return true;
738
739 return false;
740}
741
Owen Anderson81875432008-01-01 21:11:32 +0000742
Dan Gohman46b948e2008-10-16 01:49:15 +0000743bool PPCInstrInfo::BlockHasNoFallThrough(const MachineBasicBlock &MBB) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000744 if (MBB.empty()) return false;
745
746 switch (MBB.back().getOpcode()) {
747 case PPC::BLR: // Return.
748 case PPC::B: // Uncond branch.
749 case PPC::BCTR: // Indirect branch.
750 return true;
751 default: return false;
752 }
753}
754
755bool PPCInstrInfo::
Owen Andersond131b5b2008-08-14 22:49:33 +0000756ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000757 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
758 // Leave the CR# the same, but invert the condition.
759 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
760 return false;
761}
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000762
763/// GetInstSize - Return the number of bytes of code the specified
764/// instruction may be. This returns the maximum number of bytes.
765///
766unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
767 switch (MI->getOpcode()) {
768 case PPC::INLINEASM: { // Inline Asm: Variable size.
769 const MachineFunction *MF = MI->getParent()->getParent();
770 const char *AsmStr = MI->getOperand(0).getSymbolName();
771 return MF->getTarget().getTargetAsmInfo()->getInlineAsmLength(AsmStr);
772 }
Dan Gohmanfa607c92008-07-01 00:05:16 +0000773 case PPC::DBG_LABEL:
774 case PPC::EH_LABEL:
775 case PPC::GC_LABEL:
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000776 return 0;
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000777 default:
778 return 4; // PowerPC instructions are all 4 bytes
779 }
780}