blob: 01d684bf48033d729199ec5db00cc75affbb5c4b [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"
Jakob Stoklund Olesen98172ee2010-02-26 21:09:24 +000022#include "llvm/CodeGen/MachineRegisterInfo.h"
Bill Wendling03598502008-03-04 23:13:51 +000023#include "llvm/Support/CommandLine.h"
Edwin Török4d9756a2009-07-08 20:53:28 +000024#include "llvm/Support/ErrorHandling.h"
25#include "llvm/Support/raw_ostream.h"
Chris Lattner621c44d2009-08-22 20:48:53 +000026#include "llvm/MC/MCAsmInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027using namespace llvm;
28
Bill Wendling4eaadfb2008-03-10 22:49:16 +000029extern cl::opt<bool> EnablePPC32RS; // FIXME (64-bit): See PPCRegisterInfo.cpp.
30extern cl::opt<bool> EnablePPC64RS; // FIXME (64-bit): See PPCRegisterInfo.cpp.
Bill Wendling03598502008-03-04 23:13:51 +000031
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032PPCInstrInfo::PPCInstrInfo(PPCTargetMachine &tm)
Chris Lattnerd2fd6db2008-01-01 01:03:04 +000033 : TargetInstrInfoImpl(PPCInsts, array_lengthof(PPCInsts)), TM(tm),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034 RI(*TM.getSubtargetImpl(), *this) {}
35
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036bool PPCInstrInfo::isMoveInstr(const MachineInstr& MI,
37 unsigned& sourceReg,
Evan Chengf97496a2009-01-20 19:12:24 +000038 unsigned& destReg,
39 unsigned& sourceSubIdx,
40 unsigned& destSubIdx) const {
41 sourceSubIdx = destSubIdx = 0; // No sub-registers.
42
Chris Lattner99aa3372008-01-07 02:48:55 +000043 unsigned oc = MI.getOpcode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044 if (oc == PPC::OR || oc == PPC::OR8 || oc == PPC::VOR ||
45 oc == PPC::OR4To8 || oc == PPC::OR8To4) { // or r1, r2, r2
46 assert(MI.getNumOperands() >= 3 &&
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000047 MI.getOperand(0).isReg() &&
48 MI.getOperand(1).isReg() &&
49 MI.getOperand(2).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050 "invalid PPC OR instruction!");
51 if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
52 sourceReg = MI.getOperand(1).getReg();
53 destReg = MI.getOperand(0).getReg();
54 return true;
55 }
56 } else if (oc == PPC::ADDI) { // addi r1, r2, 0
57 assert(MI.getNumOperands() >= 3 &&
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000058 MI.getOperand(0).isReg() &&
59 MI.getOperand(2).isImm() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060 "invalid PPC ADDI instruction!");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000061 if (MI.getOperand(1).isReg() && MI.getOperand(2).getImm() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062 sourceReg = MI.getOperand(1).getReg();
63 destReg = MI.getOperand(0).getReg();
64 return true;
65 }
66 } else if (oc == PPC::ORI) { // ori r1, r2, 0
67 assert(MI.getNumOperands() >= 3 &&
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000068 MI.getOperand(0).isReg() &&
69 MI.getOperand(1).isReg() &&
70 MI.getOperand(2).isImm() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071 "invalid PPC ORI instruction!");
Chris Lattnera96056a2007-12-30 20:49:49 +000072 if (MI.getOperand(2).getImm() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073 sourceReg = MI.getOperand(1).getReg();
74 destReg = MI.getOperand(0).getReg();
75 return true;
76 }
77 } else if (oc == PPC::FMRS || oc == PPC::FMRD ||
78 oc == PPC::FMRSD) { // fmr r1, r2
79 assert(MI.getNumOperands() >= 2 &&
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000080 MI.getOperand(0).isReg() &&
81 MI.getOperand(1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082 "invalid PPC FMR instruction");
83 sourceReg = MI.getOperand(1).getReg();
84 destReg = MI.getOperand(0).getReg();
85 return true;
86 } else if (oc == PPC::MCRF) { // mcrf cr1, cr2
87 assert(MI.getNumOperands() >= 2 &&
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000088 MI.getOperand(0).isReg() &&
89 MI.getOperand(1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090 "invalid PPC MCRF instruction");
91 sourceReg = MI.getOperand(1).getReg();
92 destReg = MI.getOperand(0).getReg();
93 return true;
94 }
95 return false;
96}
97
Dan Gohman90feee22008-11-18 19:49:32 +000098unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099 int &FrameIndex) const {
100 switch (MI->getOpcode()) {
101 default: break;
102 case PPC::LD:
103 case PPC::LWZ:
104 case PPC::LFS:
105 case PPC::LFD:
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000106 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
107 MI->getOperand(2).isFI()) {
Chris Lattner6017d482007-12-30 23:10:15 +0000108 FrameIndex = MI->getOperand(2).getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109 return MI->getOperand(0).getReg();
110 }
111 break;
112 }
113 return 0;
114}
115
Dan Gohman90feee22008-11-18 19:49:32 +0000116unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117 int &FrameIndex) const {
118 switch (MI->getOpcode()) {
119 default: break;
120 case PPC::STD:
121 case PPC::STW:
122 case PPC::STFS:
123 case PPC::STFD:
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000124 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
125 MI->getOperand(2).isFI()) {
Chris Lattner6017d482007-12-30 23:10:15 +0000126 FrameIndex = MI->getOperand(2).getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000127 return MI->getOperand(0).getReg();
128 }
129 break;
130 }
131 return 0;
132}
133
134// commuteInstruction - We can commute rlwimi instructions, but only if the
135// rotate amt is zero. We also have to munge the immediates a bit.
Evan Cheng5de1aaf2008-06-16 07:33:11 +0000136MachineInstr *
137PPCInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
Dan Gohman221a4372008-07-07 23:14:23 +0000138 MachineFunction &MF = *MI->getParent()->getParent();
139
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140 // Normal instructions can be commuted the obvious way.
141 if (MI->getOpcode() != PPC::RLWIMI)
Evan Cheng5de1aaf2008-06-16 07:33:11 +0000142 return TargetInstrInfoImpl::commuteInstruction(MI, NewMI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000143
144 // Cannot commute if it has a non-zero rotate count.
Chris Lattnera96056a2007-12-30 20:49:49 +0000145 if (MI->getOperand(3).getImm() != 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146 return 0;
147
148 // If we have a zero rotate count, we have:
149 // M = mask(MB,ME)
150 // Op0 = (Op1 & ~M) | (Op2 & M)
151 // Change this to:
152 // M = mask((ME+1)&31, (MB-1)&31)
153 // Op0 = (Op2 & ~M) | (Op1 & M)
154
155 // Swap op1/op2
Evan Chengb554e532008-02-13 02:46:49 +0000156 unsigned Reg0 = MI->getOperand(0).getReg();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 unsigned Reg1 = MI->getOperand(1).getReg();
158 unsigned Reg2 = MI->getOperand(2).getReg();
159 bool Reg1IsKill = MI->getOperand(1).isKill();
160 bool Reg2IsKill = MI->getOperand(2).isKill();
Evan Cheng5de1aaf2008-06-16 07:33:11 +0000161 bool ChangeReg0 = false;
Evan Chengb554e532008-02-13 02:46:49 +0000162 // If machine instrs are no longer in two-address forms, update
163 // destination register as well.
164 if (Reg0 == Reg1) {
165 // Must be two address instruction!
166 assert(MI->getDesc().getOperandConstraint(0, TOI::TIED_TO) &&
167 "Expecting a two-address instruction!");
Evan Chengb554e532008-02-13 02:46:49 +0000168 Reg2IsKill = false;
Evan Cheng5de1aaf2008-06-16 07:33:11 +0000169 ChangeReg0 = true;
Evan Chengb554e532008-02-13 02:46:49 +0000170 }
Evan Cheng5de1aaf2008-06-16 07:33:11 +0000171
172 // Masks.
173 unsigned MB = MI->getOperand(4).getImm();
174 unsigned ME = MI->getOperand(5).getImm();
175
176 if (NewMI) {
177 // Create a new instruction.
178 unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg();
179 bool Reg0IsDead = MI->getOperand(0).isDead();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000180 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
Bill Wendling2b739762009-05-13 21:33:08 +0000181 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
182 .addReg(Reg2, getKillRegState(Reg2IsKill))
183 .addReg(Reg1, getKillRegState(Reg1IsKill))
Evan Cheng5de1aaf2008-06-16 07:33:11 +0000184 .addImm((ME+1) & 31)
185 .addImm((MB-1) & 31);
186 }
187
188 if (ChangeReg0)
189 MI->getOperand(0).setReg(Reg2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000190 MI->getOperand(2).setReg(Reg1);
191 MI->getOperand(1).setReg(Reg2);
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000192 MI->getOperand(2).setIsKill(Reg1IsKill);
193 MI->getOperand(1).setIsKill(Reg2IsKill);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000194
195 // Swap the mask around.
Chris Lattnera96056a2007-12-30 20:49:49 +0000196 MI->getOperand(4).setImm((ME+1) & 31);
197 MI->getOperand(5).setImm((MB-1) & 31);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000198 return MI;
199}
200
201void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
202 MachineBasicBlock::iterator MI) const {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000203 DebugLoc DL = DebugLoc::getUnknownLoc();
204 if (MI != MBB.end()) DL = MI->getDebugLoc();
205
206 BuildMI(MBB, MI, DL, get(PPC::NOP));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207}
208
209
210// Branch analysis.
211bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
212 MachineBasicBlock *&FBB,
Evan Chengeac31642009-02-09 07:14:22 +0000213 SmallVectorImpl<MachineOperand> &Cond,
214 bool AllowModify) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000215 // If the block has no terminators, it just falls into the block after it.
216 MachineBasicBlock::iterator I = MBB.end();
217 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
218 return false;
219
220 // Get the last instruction in the block.
221 MachineInstr *LastInst = I;
222
223 // If there is only one terminator instruction, process it.
224 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
225 if (LastInst->getOpcode() == PPC::B) {
Evan Cheng6ed927b2009-05-08 23:09:25 +0000226 if (!LastInst->getOperand(0).isMBB())
227 return true;
Chris Lattner6017d482007-12-30 23:10:15 +0000228 TBB = LastInst->getOperand(0).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229 return false;
230 } else if (LastInst->getOpcode() == PPC::BCC) {
Evan Cheng6ed927b2009-05-08 23:09:25 +0000231 if (!LastInst->getOperand(2).isMBB())
232 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233 // Block ends with fall-through condbranch.
Chris Lattner6017d482007-12-30 23:10:15 +0000234 TBB = LastInst->getOperand(2).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235 Cond.push_back(LastInst->getOperand(0));
236 Cond.push_back(LastInst->getOperand(1));
237 return false;
238 }
239 // Otherwise, don't know what this is.
240 return true;
241 }
242
243 // Get the instruction before it if it's a terminator.
244 MachineInstr *SecondLastInst = I;
245
246 // If there are three terminators, we don't know what sort of block this is.
247 if (SecondLastInst && I != MBB.begin() &&
248 isUnpredicatedTerminator(--I))
249 return true;
250
251 // If the block ends with PPC::B and PPC:BCC, handle it.
252 if (SecondLastInst->getOpcode() == PPC::BCC &&
253 LastInst->getOpcode() == PPC::B) {
Evan Cheng6ed927b2009-05-08 23:09:25 +0000254 if (!SecondLastInst->getOperand(2).isMBB() ||
255 !LastInst->getOperand(0).isMBB())
256 return true;
Chris Lattner6017d482007-12-30 23:10:15 +0000257 TBB = SecondLastInst->getOperand(2).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000258 Cond.push_back(SecondLastInst->getOperand(0));
259 Cond.push_back(SecondLastInst->getOperand(1));
Chris Lattner6017d482007-12-30 23:10:15 +0000260 FBB = LastInst->getOperand(0).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261 return false;
262 }
263
264 // If the block ends with two PPC:Bs, handle it. The second one is not
265 // executed, so remove it.
266 if (SecondLastInst->getOpcode() == PPC::B &&
267 LastInst->getOpcode() == PPC::B) {
Evan Cheng6ed927b2009-05-08 23:09:25 +0000268 if (!SecondLastInst->getOperand(0).isMBB())
269 return true;
Chris Lattner6017d482007-12-30 23:10:15 +0000270 TBB = SecondLastInst->getOperand(0).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000271 I = LastInst;
Evan Chengeac31642009-02-09 07:14:22 +0000272 if (AllowModify)
273 I->eraseFromParent();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274 return false;
275 }
276
277 // Otherwise, can't handle this.
278 return true;
279}
280
281unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
282 MachineBasicBlock::iterator I = MBB.end();
283 if (I == MBB.begin()) return 0;
284 --I;
285 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC)
286 return 0;
287
288 // Remove the branch.
289 I->eraseFromParent();
290
291 I = MBB.end();
292
293 if (I == MBB.begin()) return 1;
294 --I;
295 if (I->getOpcode() != PPC::BCC)
296 return 1;
297
298 // Remove the branch.
299 I->eraseFromParent();
300 return 2;
301}
302
303unsigned
304PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
305 MachineBasicBlock *FBB,
Owen Andersond131b5b2008-08-14 22:49:33 +0000306 const SmallVectorImpl<MachineOperand> &Cond) const {
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000307 // FIXME this should probably have a DebugLoc argument
308 DebugLoc dl = DebugLoc::getUnknownLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000309 // Shouldn't be a fall through.
310 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
311 assert((Cond.size() == 2 || Cond.size() == 0) &&
312 "PPC branch conditions have two components!");
313
314 // One-way branch.
315 if (FBB == 0) {
316 if (Cond.empty()) // Unconditional branch
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000317 BuildMI(&MBB, dl, get(PPC::B)).addMBB(TBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000318 else // Conditional branch
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000319 BuildMI(&MBB, dl, get(PPC::BCC))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000320 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
321 return 1;
322 }
323
324 // Two-way Conditional Branch.
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000325 BuildMI(&MBB, dl, get(PPC::BCC))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000326 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000327 BuildMI(&MBB, dl, get(PPC::B)).addMBB(FBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000328 return 2;
329}
330
Owen Anderson9fa72d92008-08-26 18:03:31 +0000331bool PPCInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
Owen Anderson8f2c8932007-12-31 06:32:00 +0000332 MachineBasicBlock::iterator MI,
333 unsigned DestReg, unsigned SrcReg,
334 const TargetRegisterClass *DestRC,
335 const TargetRegisterClass *SrcRC) const {
336 if (DestRC != SrcRC) {
Owen Anderson9fa72d92008-08-26 18:03:31 +0000337 // Not yet supported!
338 return false;
Owen Anderson8f2c8932007-12-31 06:32:00 +0000339 }
340
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000341 DebugLoc DL = DebugLoc::getUnknownLoc();
342 if (MI != MBB.end()) DL = MI->getDebugLoc();
343
Owen Anderson8f2c8932007-12-31 06:32:00 +0000344 if (DestRC == PPC::GPRCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000345 BuildMI(MBB, MI, DL, get(PPC::OR), DestReg).addReg(SrcReg).addReg(SrcReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000346 } else if (DestRC == PPC::G8RCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000347 BuildMI(MBB, MI, DL, get(PPC::OR8), DestReg).addReg(SrcReg).addReg(SrcReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000348 } else if (DestRC == PPC::F4RCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000349 BuildMI(MBB, MI, DL, get(PPC::FMRS), DestReg).addReg(SrcReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000350 } else if (DestRC == PPC::F8RCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000351 BuildMI(MBB, MI, DL, get(PPC::FMRD), DestReg).addReg(SrcReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000352 } else if (DestRC == PPC::CRRCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000353 BuildMI(MBB, MI, DL, get(PPC::MCRF), DestReg).addReg(SrcReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000354 } else if (DestRC == PPC::VRRCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000355 BuildMI(MBB, MI, DL, get(PPC::VOR), DestReg).addReg(SrcReg).addReg(SrcReg);
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000356 } else if (DestRC == PPC::CRBITRCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000357 BuildMI(MBB, MI, DL, get(PPC::CROR), DestReg).addReg(SrcReg).addReg(SrcReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000358 } else {
Owen Anderson9fa72d92008-08-26 18:03:31 +0000359 // Attempt to copy register that is not GPR or FPR
360 return false;
Owen Anderson8f2c8932007-12-31 06:32:00 +0000361 }
Owen Anderson9fa72d92008-08-26 18:03:31 +0000362
363 return true;
Owen Anderson8f2c8932007-12-31 06:32:00 +0000364}
365
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000366bool
Dan Gohman221a4372008-07-07 23:14:23 +0000367PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
368 unsigned SrcReg, bool isKill,
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000369 int FrameIdx,
370 const TargetRegisterClass *RC,
371 SmallVectorImpl<MachineInstr*> &NewMIs) const{
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000372 DebugLoc DL = DebugLoc::getUnknownLoc();
Owen Anderson81875432008-01-01 21:11:32 +0000373 if (RC == PPC::GPRCRegisterClass) {
374 if (SrcReg != PPC::LR) {
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000375 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
Bill Wendling2b739762009-05-13 21:33:08 +0000376 .addReg(SrcReg,
377 getKillRegState(isKill)),
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000378 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::MFLR), PPC::R11));
384 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
Bill Wendling2b739762009-05-13 21:33:08 +0000385 .addReg(PPC::R11,
386 getKillRegState(isKill)),
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000387 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000388 }
389 } else if (RC == PPC::G8RCRegisterClass) {
390 if (SrcReg != PPC::LR8) {
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000391 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
Bill Wendling2b739762009-05-13 21:33:08 +0000392 .addReg(SrcReg,
393 getKillRegState(isKill)),
394 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000395 } else {
396 // FIXME: this spills LR immediately to memory in one step. To do this,
397 // we use R11, which we know cannot be used in the prolog/epilog. This is
398 // a hack.
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000399 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFLR8), PPC::X11));
400 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
Bill Wendling2b739762009-05-13 21:33:08 +0000401 .addReg(PPC::X11,
402 getKillRegState(isKill)),
403 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000404 }
405 } else if (RC == PPC::F8RCRegisterClass) {
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000406 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD))
Bill Wendling2b739762009-05-13 21:33:08 +0000407 .addReg(SrcReg,
408 getKillRegState(isKill)),
409 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000410 } else if (RC == PPC::F4RCRegisterClass) {
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000411 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS))
Bill Wendling2b739762009-05-13 21:33:08 +0000412 .addReg(SrcReg,
413 getKillRegState(isKill)),
414 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000415 } else if (RC == PPC::CRRCRegisterClass) {
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000416 if ((EnablePPC32RS && !TM.getSubtargetImpl()->isPPC64()) ||
417 (EnablePPC64RS && TM.getSubtargetImpl()->isPPC64())) {
418 // FIXME (64-bit): Enable
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000419 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR))
Bill Wendling2b739762009-05-13 21:33:08 +0000420 .addReg(SrcReg,
421 getKillRegState(isKill)),
Chris Lattner6734c3a2008-03-20 01:22:40 +0000422 FrameIdx));
Bill Wendlinga1877c52008-03-03 22:19:16 +0000423 return true;
424 } else {
Dale Johannesenb000c482010-02-12 21:35:34 +0000425 // FIXME: We need a scatch reg here. The trouble with using R0 is that
426 // it's possible for the stack frame to be so big the save location is
427 // out of range of immediate offsets, necessitating another register.
428 // We hack this on Darwin by reserving R2. It's probably broken on Linux
429 // at the moment.
430
431 // We need to store the CR in the low 4-bits of the saved value. First,
432 // issue a MFCR to save all of the CRBits.
433 unsigned ScratchReg = TM.getSubtargetImpl()->isDarwinABI() ?
434 PPC::R2 : PPC::R0;
435 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFCR), ScratchReg));
Owen Anderson81875432008-01-01 21:11:32 +0000436
Bill Wendlinga1877c52008-03-03 22:19:16 +0000437 // If the saved register wasn't CR0, shift the bits left so that they are
438 // in CR0's slot.
439 if (SrcReg != PPC::CR0) {
440 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(SrcReg)*4;
Dale Johannesenb000c482010-02-12 21:35:34 +0000441 // rlwinm scratch, scratch, ShiftBits, 0, 31.
442 NewMIs.push_back(BuildMI(MF, DL, get(PPC::RLWINM), ScratchReg)
443 .addReg(ScratchReg).addImm(ShiftBits)
444 .addImm(0).addImm(31));
Bill Wendlinga1877c52008-03-03 22:19:16 +0000445 }
446
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000447 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
Dale Johannesenb000c482010-02-12 21:35:34 +0000448 .addReg(ScratchReg,
Bill Wendling2b739762009-05-13 21:33:08 +0000449 getKillRegState(isKill)),
Bill Wendlinga1877c52008-03-03 22:19:16 +0000450 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000451 }
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000452 } else if (RC == PPC::CRBITRCRegisterClass) {
453 // FIXME: We use CRi here because there is no mtcrf on a bit. Since the
454 // backend currently only uses CR1EQ as an individual bit, this should
455 // not cause any bug. If we need other uses of CR bits, the following
456 // code may be invalid.
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000457 unsigned Reg = 0;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000458 if (SrcReg == PPC::CR0LT || SrcReg == PPC::CR0GT ||
459 SrcReg == PPC::CR0EQ || SrcReg == PPC::CR0UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000460 Reg = PPC::CR0;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000461 else if (SrcReg == PPC::CR1LT || SrcReg == PPC::CR1GT ||
462 SrcReg == PPC::CR1EQ || SrcReg == PPC::CR1UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000463 Reg = PPC::CR1;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000464 else if (SrcReg == PPC::CR2LT || SrcReg == PPC::CR2GT ||
465 SrcReg == PPC::CR2EQ || SrcReg == PPC::CR2UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000466 Reg = PPC::CR2;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000467 else if (SrcReg == PPC::CR3LT || SrcReg == PPC::CR3GT ||
468 SrcReg == PPC::CR3EQ || SrcReg == PPC::CR3UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000469 Reg = PPC::CR3;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000470 else if (SrcReg == PPC::CR4LT || SrcReg == PPC::CR4GT ||
471 SrcReg == PPC::CR4EQ || SrcReg == PPC::CR4UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000472 Reg = PPC::CR4;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000473 else if (SrcReg == PPC::CR5LT || SrcReg == PPC::CR5GT ||
474 SrcReg == PPC::CR5EQ || SrcReg == PPC::CR5UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000475 Reg = PPC::CR5;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000476 else if (SrcReg == PPC::CR6LT || SrcReg == PPC::CR6GT ||
477 SrcReg == PPC::CR6EQ || SrcReg == PPC::CR6UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000478 Reg = PPC::CR6;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000479 else if (SrcReg == PPC::CR7LT || SrcReg == PPC::CR7GT ||
480 SrcReg == PPC::CR7EQ || SrcReg == PPC::CR7UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000481 Reg = PPC::CR7;
482
Dan Gohman221a4372008-07-07 23:14:23 +0000483 return StoreRegToStackSlot(MF, Reg, isKill, FrameIdx,
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000484 PPC::CRRCRegisterClass, NewMIs);
485
Owen Anderson81875432008-01-01 21:11:32 +0000486 } else if (RC == PPC::VRRCRegisterClass) {
487 // We don't have indexed addressing for vector loads. Emit:
488 // R0 = ADDI FI#
489 // STVX VAL, 0, R0
490 //
491 // FIXME: We use R0 here, because it isn't available for RA.
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000492 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::ADDI), PPC::R0),
Owen Anderson81875432008-01-01 21:11:32 +0000493 FrameIdx, 0, 0));
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000494 NewMIs.push_back(BuildMI(MF, DL, get(PPC::STVX))
Bill Wendling2b739762009-05-13 21:33:08 +0000495 .addReg(SrcReg, getKillRegState(isKill))
496 .addReg(PPC::R0)
497 .addReg(PPC::R0));
Owen Anderson81875432008-01-01 21:11:32 +0000498 } else {
Edwin Törökbd448e32009-07-14 16:55:14 +0000499 llvm_unreachable("Unknown regclass!");
Owen Anderson81875432008-01-01 21:11:32 +0000500 }
Bill Wendlinga1877c52008-03-03 22:19:16 +0000501
502 return false;
Owen Anderson81875432008-01-01 21:11:32 +0000503}
504
505void
506PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Bill Wendlinga1877c52008-03-03 22:19:16 +0000507 MachineBasicBlock::iterator MI,
508 unsigned SrcReg, bool isKill, int FrameIdx,
509 const TargetRegisterClass *RC) const {
Dan Gohman221a4372008-07-07 23:14:23 +0000510 MachineFunction &MF = *MBB.getParent();
Owen Anderson81875432008-01-01 21:11:32 +0000511 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendlinga1877c52008-03-03 22:19:16 +0000512
Dan Gohman221a4372008-07-07 23:14:23 +0000513 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs)) {
514 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Bill Wendlinga1877c52008-03-03 22:19:16 +0000515 FuncInfo->setSpillsCR();
516 }
517
Owen Anderson81875432008-01-01 21:11:32 +0000518 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
519 MBB.insert(MI, NewMIs[i]);
520}
521
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000522void
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000523PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL,
Dan Gohman221a4372008-07-07 23:14:23 +0000524 unsigned DestReg, int FrameIdx,
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000525 const TargetRegisterClass *RC,
526 SmallVectorImpl<MachineInstr*> &NewMIs)const{
Owen Anderson81875432008-01-01 21:11:32 +0000527 if (RC == PPC::GPRCRegisterClass) {
528 if (DestReg != PPC::LR) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000529 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
530 DestReg), FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000531 } else {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000532 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
533 PPC::R11), FrameIdx));
534 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTLR)).addReg(PPC::R11));
Owen Anderson81875432008-01-01 21:11:32 +0000535 }
536 } else if (RC == PPC::G8RCRegisterClass) {
537 if (DestReg != PPC::LR8) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000538 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg),
Owen Anderson81875432008-01-01 21:11:32 +0000539 FrameIdx));
540 } else {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000541 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD),
542 PPC::R11), FrameIdx));
543 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTLR8)).addReg(PPC::R11));
Owen Anderson81875432008-01-01 21:11:32 +0000544 }
545 } else if (RC == PPC::F8RCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000546 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg),
Owen Anderson81875432008-01-01 21:11:32 +0000547 FrameIdx));
548 } else if (RC == PPC::F4RCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000549 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg),
Owen Anderson81875432008-01-01 21:11:32 +0000550 FrameIdx));
551 } else if (RC == PPC::CRRCRegisterClass) {
Dale Johannesenb000c482010-02-12 21:35:34 +0000552 // FIXME: We need a scatch reg here. The trouble with using R0 is that
553 // it's possible for the stack frame to be so big the save location is
554 // out of range of immediate offsets, necessitating another register.
555 // We hack this on Darwin by reserving R2. It's probably broken on Linux
556 // at the moment.
557 unsigned ScratchReg = TM.getSubtargetImpl()->isDarwinABI() ?
558 PPC::R2 : PPC::R0;
559 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
560 ScratchReg), FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000561
562 // If the reloaded register isn't CR0, shift the bits right so that they are
563 // in the right CR's slot.
564 if (DestReg != PPC::CR0) {
565 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(DestReg)*4;
566 // rlwinm r11, r11, 32-ShiftBits, 0, 31.
Dale Johannesenb000c482010-02-12 21:35:34 +0000567 NewMIs.push_back(BuildMI(MF, DL, get(PPC::RLWINM), ScratchReg)
568 .addReg(ScratchReg).addImm(32-ShiftBits).addImm(0)
569 .addImm(31));
Owen Anderson81875432008-01-01 21:11:32 +0000570 }
571
Dale Johannesenb000c482010-02-12 21:35:34 +0000572 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTCRF), DestReg)
573 .addReg(ScratchReg));
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000574 } else if (RC == PPC::CRBITRCRegisterClass) {
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000575
576 unsigned Reg = 0;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000577 if (DestReg == PPC::CR0LT || DestReg == PPC::CR0GT ||
578 DestReg == PPC::CR0EQ || DestReg == PPC::CR0UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000579 Reg = PPC::CR0;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000580 else if (DestReg == PPC::CR1LT || DestReg == PPC::CR1GT ||
581 DestReg == PPC::CR1EQ || DestReg == PPC::CR1UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000582 Reg = PPC::CR1;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000583 else if (DestReg == PPC::CR2LT || DestReg == PPC::CR2GT ||
584 DestReg == PPC::CR2EQ || DestReg == PPC::CR2UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000585 Reg = PPC::CR2;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000586 else if (DestReg == PPC::CR3LT || DestReg == PPC::CR3GT ||
587 DestReg == PPC::CR3EQ || DestReg == PPC::CR3UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000588 Reg = PPC::CR3;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000589 else if (DestReg == PPC::CR4LT || DestReg == PPC::CR4GT ||
590 DestReg == PPC::CR4EQ || DestReg == PPC::CR4UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000591 Reg = PPC::CR4;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000592 else if (DestReg == PPC::CR5LT || DestReg == PPC::CR5GT ||
593 DestReg == PPC::CR5EQ || DestReg == PPC::CR5UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000594 Reg = PPC::CR5;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000595 else if (DestReg == PPC::CR6LT || DestReg == PPC::CR6GT ||
596 DestReg == PPC::CR6EQ || DestReg == PPC::CR6UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000597 Reg = PPC::CR6;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000598 else if (DestReg == PPC::CR7LT || DestReg == PPC::CR7GT ||
599 DestReg == PPC::CR7EQ || DestReg == PPC::CR7UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000600 Reg = PPC::CR7;
601
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000602 return LoadRegFromStackSlot(MF, DL, Reg, FrameIdx,
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000603 PPC::CRRCRegisterClass, NewMIs);
604
Owen Anderson81875432008-01-01 21:11:32 +0000605 } else if (RC == PPC::VRRCRegisterClass) {
606 // We don't have indexed addressing for vector loads. Emit:
607 // R0 = ADDI FI#
608 // Dest = LVX 0, R0
609 //
610 // FIXME: We use R0 here, because it isn't available for RA.
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000611 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::ADDI), PPC::R0),
Owen Anderson81875432008-01-01 21:11:32 +0000612 FrameIdx, 0, 0));
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000613 NewMIs.push_back(BuildMI(MF, DL, get(PPC::LVX),DestReg).addReg(PPC::R0)
Owen Anderson81875432008-01-01 21:11:32 +0000614 .addReg(PPC::R0));
615 } else {
Edwin Törökbd448e32009-07-14 16:55:14 +0000616 llvm_unreachable("Unknown regclass!");
Owen Anderson81875432008-01-01 21:11:32 +0000617 }
618}
619
620void
621PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Bill Wendlinga1877c52008-03-03 22:19:16 +0000622 MachineBasicBlock::iterator MI,
623 unsigned DestReg, int FrameIdx,
624 const TargetRegisterClass *RC) const {
Dan Gohman221a4372008-07-07 23:14:23 +0000625 MachineFunction &MF = *MBB.getParent();
Owen Anderson81875432008-01-01 21:11:32 +0000626 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000627 DebugLoc DL = DebugLoc::getUnknownLoc();
628 if (MI != MBB.end()) DL = MI->getDebugLoc();
629 LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs);
Owen Anderson81875432008-01-01 21:11:32 +0000630 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
631 MBB.insert(MI, NewMIs[i]);
632}
633
Owen Anderson9a184ef2008-01-07 01:35:02 +0000634/// foldMemoryOperand - PowerPC (like most RISC's) can only fold spills into
635/// copy instructions, turning them into load/store instructions.
Dan Gohmanedc83d62008-12-03 18:43:12 +0000636MachineInstr *PPCInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
637 MachineInstr *MI,
638 const SmallVectorImpl<unsigned> &Ops,
639 int FrameIndex) const {
Owen Anderson9a184ef2008-01-07 01:35:02 +0000640 if (Ops.size() != 1) return NULL;
641
642 // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because
643 // it takes more than one instruction to store it.
644 unsigned Opc = MI->getOpcode();
645 unsigned OpNum = Ops[0];
646
647 MachineInstr *NewMI = NULL;
648 if ((Opc == PPC::OR &&
649 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
650 if (OpNum == 0) { // move -> store
651 unsigned InReg = MI->getOperand(1).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000652 bool isKill = MI->getOperand(1).isKill();
Evan Cheng65219822009-07-01 01:59:31 +0000653 bool isUndef = MI->getOperand(1).isUndef();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000654 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::STW))
Evan Cheng65219822009-07-01 01:59:31 +0000655 .addReg(InReg,
656 getKillRegState(isKill) |
657 getUndefRegState(isUndef)),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000658 FrameIndex);
659 } else { // move -> load
660 unsigned OutReg = MI->getOperand(0).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000661 bool isDead = MI->getOperand(0).isDead();
Evan Cheng65219822009-07-01 01:59:31 +0000662 bool isUndef = MI->getOperand(0).isUndef();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000663 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::LWZ))
Bill Wendling2b739762009-05-13 21:33:08 +0000664 .addReg(OutReg,
665 RegState::Define |
Evan Cheng65219822009-07-01 01:59:31 +0000666 getDeadRegState(isDead) |
667 getUndefRegState(isUndef)),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000668 FrameIndex);
669 }
670 } else if ((Opc == PPC::OR8 &&
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();
Evan Cheng65219822009-07-01 01:59:31 +0000675 bool isUndef = MI->getOperand(1).isUndef();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000676 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::STD))
Evan Cheng65219822009-07-01 01:59:31 +0000677 .addReg(InReg,
678 getKillRegState(isKill) |
679 getUndefRegState(isUndef)),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000680 FrameIndex);
681 } else { // move -> load
682 unsigned OutReg = MI->getOperand(0).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000683 bool isDead = MI->getOperand(0).isDead();
Evan Cheng65219822009-07-01 01:59:31 +0000684 bool isUndef = MI->getOperand(0).isUndef();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000685 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::LD))
Bill Wendling2b739762009-05-13 21:33:08 +0000686 .addReg(OutReg,
687 RegState::Define |
Evan Cheng65219822009-07-01 01:59:31 +0000688 getDeadRegState(isDead) |
689 getUndefRegState(isUndef)),
Evan Chenge52c1912008-07-03 09:09:37 +0000690 FrameIndex);
Owen Anderson9a184ef2008-01-07 01:35:02 +0000691 }
Jakob Stoklund Olesen98172ee2010-02-26 21:09:24 +0000692 } else if (Opc == PPC::FMRD || Opc == PPC::FMRS || Opc == PPC::FMRSD) {
693 // The register may be F4RC or F8RC, and that determines the memory op.
694 unsigned OrigReg = MI->getOperand(OpNum).getReg();
695 // We cannot tell the register class from a physreg alone.
696 if (TargetRegisterInfo::isPhysicalRegister(OrigReg))
697 return NULL;
698 const TargetRegisterClass *RC = MF.getRegInfo().getRegClass(OrigReg);
699 const bool is64 = RC == PPC::F8RCRegisterClass;
700
Owen Anderson9a184ef2008-01-07 01:35:02 +0000701 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();
Evan Cheng65219822009-07-01 01:59:31 +0000704 bool isUndef = MI->getOperand(1).isUndef();
Jakob Stoklund Olesen98172ee2010-02-26 21:09:24 +0000705 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(),
706 get(is64 ? PPC::STFD : PPC::STFS))
Evan Cheng65219822009-07-01 01:59:31 +0000707 .addReg(InReg,
708 getKillRegState(isKill) |
709 getUndefRegState(isUndef)),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000710 FrameIndex);
711 } else { // move -> load
712 unsigned OutReg = MI->getOperand(0).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000713 bool isDead = MI->getOperand(0).isDead();
Evan Cheng65219822009-07-01 01:59:31 +0000714 bool isUndef = MI->getOperand(0).isUndef();
Jakob Stoklund Olesen98172ee2010-02-26 21:09:24 +0000715 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(),
716 get(is64 ? PPC::LFD : PPC::LFS))
Bill Wendling2b739762009-05-13 21:33:08 +0000717 .addReg(OutReg,
718 RegState::Define |
Evan Cheng65219822009-07-01 01:59:31 +0000719 getDeadRegState(isDead) |
720 getUndefRegState(isUndef)),
Evan Chenge52c1912008-07-03 09:09:37 +0000721 FrameIndex);
Owen Anderson9a184ef2008-01-07 01:35:02 +0000722 }
723 }
724
Owen Anderson9a184ef2008-01-07 01:35:02 +0000725 return NewMI;
726}
727
Dan Gohman46b948e2008-10-16 01:49:15 +0000728bool PPCInstrInfo::canFoldMemoryOperand(const MachineInstr *MI,
729 const SmallVectorImpl<unsigned> &Ops) const {
Owen Anderson9a184ef2008-01-07 01:35:02 +0000730 if (Ops.size() != 1) return false;
731
732 // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because
733 // it takes more than one instruction to store it.
734 unsigned Opc = MI->getOpcode();
735
736 if ((Opc == PPC::OR &&
737 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
738 return true;
739 else if ((Opc == PPC::OR8 &&
740 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
741 return true;
742 else if (Opc == PPC::FMRD || Opc == PPC::FMRS)
743 return true;
744
745 return false;
746}
747
Owen Anderson81875432008-01-01 21:11:32 +0000748
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000749bool PPCInstrInfo::
Owen Andersond131b5b2008-08-14 22:49:33 +0000750ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000751 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
752 // Leave the CR# the same, but invert the condition.
753 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
754 return false;
755}
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000756
757/// GetInstSize - Return the number of bytes of code the specified
758/// instruction may be. This returns the maximum number of bytes.
759///
760unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
761 switch (MI->getOpcode()) {
762 case PPC::INLINEASM: { // Inline Asm: Variable size.
763 const MachineFunction *MF = MI->getParent()->getParent();
764 const char *AsmStr = MI->getOperand(0).getSymbolName();
Chris Lattner621c44d2009-08-22 20:48:53 +0000765 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000766 }
Dan Gohmanfa607c92008-07-01 00:05:16 +0000767 case PPC::DBG_LABEL:
768 case PPC::EH_LABEL:
769 case PPC::GC_LABEL:
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000770 return 0;
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000771 default:
772 return 4; // PowerPC instructions are all 4 bytes
773 }
774}