blob: 1b7a7783b23cd452647ccaff09b2db90705fc351 [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 +000027
Dan Gohmanca9ec762010-04-15 17:20:57 +000028namespace llvm {
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.
Dan Gohmanca9ec762010-04-15 17:20:57 +000031}
32
33using namespace llvm;
Bill Wendling03598502008-03-04 23:13:51 +000034
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035PPCInstrInfo::PPCInstrInfo(PPCTargetMachine &tm)
Chris Lattnerd2fd6db2008-01-01 01:03:04 +000036 : TargetInstrInfoImpl(PPCInsts, array_lengthof(PPCInsts)), TM(tm),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037 RI(*TM.getSubtargetImpl(), *this) {}
38
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039bool PPCInstrInfo::isMoveInstr(const MachineInstr& MI,
40 unsigned& sourceReg,
Evan Chengf97496a2009-01-20 19:12:24 +000041 unsigned& destReg,
42 unsigned& sourceSubIdx,
43 unsigned& destSubIdx) const {
44 sourceSubIdx = destSubIdx = 0; // No sub-registers.
45
Chris Lattner99aa3372008-01-07 02:48:55 +000046 unsigned oc = MI.getOpcode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047 if (oc == PPC::OR || oc == PPC::OR8 || oc == PPC::VOR ||
48 oc == PPC::OR4To8 || oc == PPC::OR8To4) { // or r1, r2, r2
49 assert(MI.getNumOperands() >= 3 &&
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000050 MI.getOperand(0).isReg() &&
51 MI.getOperand(1).isReg() &&
52 MI.getOperand(2).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053 "invalid PPC OR instruction!");
54 if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
55 sourceReg = MI.getOperand(1).getReg();
56 destReg = MI.getOperand(0).getReg();
57 return true;
58 }
59 } else if (oc == PPC::ADDI) { // addi r1, r2, 0
60 assert(MI.getNumOperands() >= 3 &&
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000061 MI.getOperand(0).isReg() &&
62 MI.getOperand(2).isImm() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +000063 "invalid PPC ADDI instruction!");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000064 if (MI.getOperand(1).isReg() && MI.getOperand(2).getImm() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065 sourceReg = MI.getOperand(1).getReg();
66 destReg = MI.getOperand(0).getReg();
67 return true;
68 }
69 } else if (oc == PPC::ORI) { // ori r1, r2, 0
70 assert(MI.getNumOperands() >= 3 &&
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000071 MI.getOperand(0).isReg() &&
72 MI.getOperand(1).isReg() &&
73 MI.getOperand(2).isImm() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074 "invalid PPC ORI instruction!");
Chris Lattnera96056a2007-12-30 20:49:49 +000075 if (MI.getOperand(2).getImm() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076 sourceReg = MI.getOperand(1).getReg();
77 destReg = MI.getOperand(0).getReg();
78 return true;
79 }
Jakob Stoklund Olesen00da1ea2010-02-26 21:53:24 +000080 } else if (oc == PPC::FMR || oc == PPC::FMRSD) { // fmr r1, r2
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081 assert(MI.getNumOperands() >= 2 &&
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000082 MI.getOperand(0).isReg() &&
83 MI.getOperand(1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +000084 "invalid PPC FMR instruction");
85 sourceReg = MI.getOperand(1).getReg();
86 destReg = MI.getOperand(0).getReg();
87 return true;
88 } else if (oc == PPC::MCRF) { // mcrf cr1, cr2
89 assert(MI.getNumOperands() >= 2 &&
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000090 MI.getOperand(0).isReg() &&
91 MI.getOperand(1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092 "invalid PPC MCRF instruction");
93 sourceReg = MI.getOperand(1).getReg();
94 destReg = MI.getOperand(0).getReg();
95 return true;
96 }
97 return false;
98}
99
Dan Gohman90feee22008-11-18 19:49:32 +0000100unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101 int &FrameIndex) const {
102 switch (MI->getOpcode()) {
103 default: break;
104 case PPC::LD:
105 case PPC::LWZ:
106 case PPC::LFS:
107 case PPC::LFD:
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000108 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
109 MI->getOperand(2).isFI()) {
Chris Lattner6017d482007-12-30 23:10:15 +0000110 FrameIndex = MI->getOperand(2).getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111 return MI->getOperand(0).getReg();
112 }
113 break;
114 }
115 return 0;
116}
117
Dan Gohman90feee22008-11-18 19:49:32 +0000118unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119 int &FrameIndex) const {
120 switch (MI->getOpcode()) {
121 default: break;
122 case PPC::STD:
123 case PPC::STW:
124 case PPC::STFS:
125 case PPC::STFD:
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000126 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
127 MI->getOperand(2).isFI()) {
Chris Lattner6017d482007-12-30 23:10:15 +0000128 FrameIndex = MI->getOperand(2).getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129 return MI->getOperand(0).getReg();
130 }
131 break;
132 }
133 return 0;
134}
135
136// commuteInstruction - We can commute rlwimi instructions, but only if the
137// rotate amt is zero. We also have to munge the immediates a bit.
Evan Cheng5de1aaf2008-06-16 07:33:11 +0000138MachineInstr *
139PPCInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
Dan Gohman221a4372008-07-07 23:14:23 +0000140 MachineFunction &MF = *MI->getParent()->getParent();
141
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142 // Normal instructions can be commuted the obvious way.
143 if (MI->getOpcode() != PPC::RLWIMI)
Evan Cheng5de1aaf2008-06-16 07:33:11 +0000144 return TargetInstrInfoImpl::commuteInstruction(MI, NewMI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145
146 // Cannot commute if it has a non-zero rotate count.
Chris Lattnera96056a2007-12-30 20:49:49 +0000147 if (MI->getOperand(3).getImm() != 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000148 return 0;
149
150 // If we have a zero rotate count, we have:
151 // M = mask(MB,ME)
152 // Op0 = (Op1 & ~M) | (Op2 & M)
153 // Change this to:
154 // M = mask((ME+1)&31, (MB-1)&31)
155 // Op0 = (Op2 & ~M) | (Op1 & M)
156
157 // Swap op1/op2
Evan Chengb554e532008-02-13 02:46:49 +0000158 unsigned Reg0 = MI->getOperand(0).getReg();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159 unsigned Reg1 = MI->getOperand(1).getReg();
160 unsigned Reg2 = MI->getOperand(2).getReg();
161 bool Reg1IsKill = MI->getOperand(1).isKill();
162 bool Reg2IsKill = MI->getOperand(2).isKill();
Evan Cheng5de1aaf2008-06-16 07:33:11 +0000163 bool ChangeReg0 = false;
Evan Chengb554e532008-02-13 02:46:49 +0000164 // If machine instrs are no longer in two-address forms, update
165 // destination register as well.
166 if (Reg0 == Reg1) {
167 // Must be two address instruction!
168 assert(MI->getDesc().getOperandConstraint(0, TOI::TIED_TO) &&
169 "Expecting a two-address instruction!");
Evan Chengb554e532008-02-13 02:46:49 +0000170 Reg2IsKill = false;
Evan Cheng5de1aaf2008-06-16 07:33:11 +0000171 ChangeReg0 = true;
Evan Chengb554e532008-02-13 02:46:49 +0000172 }
Evan Cheng5de1aaf2008-06-16 07:33:11 +0000173
174 // Masks.
175 unsigned MB = MI->getOperand(4).getImm();
176 unsigned ME = MI->getOperand(5).getImm();
177
178 if (NewMI) {
179 // Create a new instruction.
180 unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg();
181 bool Reg0IsDead = MI->getOperand(0).isDead();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000182 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
Bill Wendling2b739762009-05-13 21:33:08 +0000183 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
184 .addReg(Reg2, getKillRegState(Reg2IsKill))
185 .addReg(Reg1, getKillRegState(Reg1IsKill))
Evan Cheng5de1aaf2008-06-16 07:33:11 +0000186 .addImm((ME+1) & 31)
187 .addImm((MB-1) & 31);
188 }
189
190 if (ChangeReg0)
191 MI->getOperand(0).setReg(Reg2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192 MI->getOperand(2).setReg(Reg1);
193 MI->getOperand(1).setReg(Reg2);
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000194 MI->getOperand(2).setIsKill(Reg1IsKill);
195 MI->getOperand(1).setIsKill(Reg2IsKill);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196
197 // Swap the mask around.
Chris Lattnera96056a2007-12-30 20:49:49 +0000198 MI->getOperand(4).setImm((ME+1) & 31);
199 MI->getOperand(5).setImm((MB-1) & 31);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200 return MI;
201}
202
203void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
204 MachineBasicBlock::iterator MI) const {
Chris Lattnerd2c680b2010-04-02 20:16:16 +0000205 DebugLoc DL;
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000206 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();
Dale Johannesen139dcd72010-04-02 01:38:09 +0000217 if (I == MBB.begin())
218 return false;
219 --I;
220 while (I->isDebugValue()) {
221 if (I == MBB.begin())
222 return false;
223 --I;
224 }
225 if (!isUnpredicatedTerminator(I))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226 return false;
227
228 // Get the last instruction in the block.
229 MachineInstr *LastInst = I;
230
231 // If there is only one terminator instruction, process it.
232 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
233 if (LastInst->getOpcode() == PPC::B) {
Evan Cheng6ed927b2009-05-08 23:09:25 +0000234 if (!LastInst->getOperand(0).isMBB())
235 return true;
Chris Lattner6017d482007-12-30 23:10:15 +0000236 TBB = LastInst->getOperand(0).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237 return false;
238 } else if (LastInst->getOpcode() == PPC::BCC) {
Evan Cheng6ed927b2009-05-08 23:09:25 +0000239 if (!LastInst->getOperand(2).isMBB())
240 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000241 // Block ends with fall-through condbranch.
Chris Lattner6017d482007-12-30 23:10:15 +0000242 TBB = LastInst->getOperand(2).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243 Cond.push_back(LastInst->getOperand(0));
244 Cond.push_back(LastInst->getOperand(1));
245 return false;
246 }
247 // Otherwise, don't know what this is.
248 return true;
249 }
250
251 // Get the instruction before it if it's a terminator.
252 MachineInstr *SecondLastInst = I;
253
254 // If there are three terminators, we don't know what sort of block this is.
255 if (SecondLastInst && I != MBB.begin() &&
256 isUnpredicatedTerminator(--I))
257 return true;
258
259 // If the block ends with PPC::B and PPC:BCC, handle it.
260 if (SecondLastInst->getOpcode() == PPC::BCC &&
261 LastInst->getOpcode() == PPC::B) {
Evan Cheng6ed927b2009-05-08 23:09:25 +0000262 if (!SecondLastInst->getOperand(2).isMBB() ||
263 !LastInst->getOperand(0).isMBB())
264 return true;
Chris Lattner6017d482007-12-30 23:10:15 +0000265 TBB = SecondLastInst->getOperand(2).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000266 Cond.push_back(SecondLastInst->getOperand(0));
267 Cond.push_back(SecondLastInst->getOperand(1));
Chris Lattner6017d482007-12-30 23:10:15 +0000268 FBB = LastInst->getOperand(0).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000269 return false;
270 }
271
272 // If the block ends with two PPC:Bs, handle it. The second one is not
273 // executed, so remove it.
274 if (SecondLastInst->getOpcode() == PPC::B &&
275 LastInst->getOpcode() == PPC::B) {
Evan Cheng6ed927b2009-05-08 23:09:25 +0000276 if (!SecondLastInst->getOperand(0).isMBB())
277 return true;
Chris Lattner6017d482007-12-30 23:10:15 +0000278 TBB = SecondLastInst->getOperand(0).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000279 I = LastInst;
Evan Chengeac31642009-02-09 07:14:22 +0000280 if (AllowModify)
281 I->eraseFromParent();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000282 return false;
283 }
284
285 // Otherwise, can't handle this.
286 return true;
287}
288
289unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
290 MachineBasicBlock::iterator I = MBB.end();
291 if (I == MBB.begin()) return 0;
292 --I;
Dale Johannesen139dcd72010-04-02 01:38:09 +0000293 while (I->isDebugValue()) {
294 if (I == MBB.begin())
295 return 0;
296 --I;
297 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000298 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC)
299 return 0;
300
301 // Remove the branch.
302 I->eraseFromParent();
303
304 I = MBB.end();
305
306 if (I == MBB.begin()) return 1;
307 --I;
308 if (I->getOpcode() != PPC::BCC)
309 return 1;
310
311 // Remove the branch.
312 I->eraseFromParent();
313 return 2;
314}
315
316unsigned
317PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
318 MachineBasicBlock *FBB,
Owen Andersond131b5b2008-08-14 22:49:33 +0000319 const SmallVectorImpl<MachineOperand> &Cond) const {
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000320 // FIXME this should probably have a DebugLoc argument
Chris Lattnerd2c680b2010-04-02 20:16:16 +0000321 DebugLoc dl;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000322 // Shouldn't be a fall through.
323 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
324 assert((Cond.size() == 2 || Cond.size() == 0) &&
325 "PPC branch conditions have two components!");
326
327 // One-way branch.
328 if (FBB == 0) {
329 if (Cond.empty()) // Unconditional branch
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000330 BuildMI(&MBB, dl, get(PPC::B)).addMBB(TBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000331 else // Conditional branch
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000332 BuildMI(&MBB, dl, get(PPC::BCC))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000333 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
334 return 1;
335 }
336
337 // Two-way Conditional Branch.
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000338 BuildMI(&MBB, dl, get(PPC::BCC))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000339 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000340 BuildMI(&MBB, dl, get(PPC::B)).addMBB(FBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000341 return 2;
342}
343
Owen Anderson9fa72d92008-08-26 18:03:31 +0000344bool PPCInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
Owen Anderson8f2c8932007-12-31 06:32:00 +0000345 MachineBasicBlock::iterator MI,
346 unsigned DestReg, unsigned SrcReg,
347 const TargetRegisterClass *DestRC,
Dan Gohman75a44ec2010-05-06 20:33:48 +0000348 const TargetRegisterClass *SrcRC,
349 DebugLoc DL) const {
Owen Anderson8f2c8932007-12-31 06:32:00 +0000350 if (DestRC != SrcRC) {
Owen Anderson9fa72d92008-08-26 18:03:31 +0000351 // Not yet supported!
352 return false;
Owen Anderson8f2c8932007-12-31 06:32:00 +0000353 }
354
355 if (DestRC == PPC::GPRCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000356 BuildMI(MBB, MI, DL, get(PPC::OR), DestReg).addReg(SrcReg).addReg(SrcReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000357 } else if (DestRC == PPC::G8RCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000358 BuildMI(MBB, MI, DL, get(PPC::OR8), DestReg).addReg(SrcReg).addReg(SrcReg);
Jakob Stoklund Olesen00da1ea2010-02-26 21:53:24 +0000359 } else if (DestRC == PPC::F4RCRegisterClass ||
360 DestRC == PPC::F8RCRegisterClass) {
361 BuildMI(MBB, MI, DL, get(PPC::FMR), DestReg).addReg(SrcReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000362 } else if (DestRC == PPC::CRRCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000363 BuildMI(MBB, MI, DL, get(PPC::MCRF), DestReg).addReg(SrcReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000364 } else if (DestRC == PPC::VRRCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000365 BuildMI(MBB, MI, DL, get(PPC::VOR), DestReg).addReg(SrcReg).addReg(SrcReg);
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000366 } else if (DestRC == PPC::CRBITRCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000367 BuildMI(MBB, MI, DL, get(PPC::CROR), DestReg).addReg(SrcReg).addReg(SrcReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000368 } else {
Owen Anderson9fa72d92008-08-26 18:03:31 +0000369 // Attempt to copy register that is not GPR or FPR
370 return false;
Owen Anderson8f2c8932007-12-31 06:32:00 +0000371 }
Owen Anderson9fa72d92008-08-26 18:03:31 +0000372
373 return true;
Owen Anderson8f2c8932007-12-31 06:32:00 +0000374}
375
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000376bool
Dan Gohman221a4372008-07-07 23:14:23 +0000377PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
378 unsigned SrcReg, bool isKill,
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000379 int FrameIdx,
380 const TargetRegisterClass *RC,
381 SmallVectorImpl<MachineInstr*> &NewMIs) const{
Chris Lattnerd2c680b2010-04-02 20:16:16 +0000382 DebugLoc DL;
Owen Anderson81875432008-01-01 21:11:32 +0000383 if (RC == PPC::GPRCRegisterClass) {
384 if (SrcReg != PPC::LR) {
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000385 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
Bill Wendling2b739762009-05-13 21:33:08 +0000386 .addReg(SrcReg,
387 getKillRegState(isKill)),
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000388 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000389 } else {
390 // FIXME: this spills LR immediately to memory in one step. To do this,
391 // we use R11, which we know cannot be used in the prolog/epilog. This is
392 // a hack.
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000393 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFLR), PPC::R11));
394 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
Bill Wendling2b739762009-05-13 21:33:08 +0000395 .addReg(PPC::R11,
396 getKillRegState(isKill)),
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000397 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000398 }
399 } else if (RC == PPC::G8RCRegisterClass) {
400 if (SrcReg != PPC::LR8) {
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000401 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
Bill Wendling2b739762009-05-13 21:33:08 +0000402 .addReg(SrcReg,
403 getKillRegState(isKill)),
404 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000405 } else {
406 // FIXME: this spills LR immediately to memory in one step. To do this,
407 // we use R11, which we know cannot be used in the prolog/epilog. This is
408 // a hack.
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000409 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFLR8), PPC::X11));
410 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
Bill Wendling2b739762009-05-13 21:33:08 +0000411 .addReg(PPC::X11,
412 getKillRegState(isKill)),
413 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000414 }
415 } else if (RC == PPC::F8RCRegisterClass) {
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000416 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD))
Bill Wendling2b739762009-05-13 21:33:08 +0000417 .addReg(SrcReg,
418 getKillRegState(isKill)),
419 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000420 } else if (RC == PPC::F4RCRegisterClass) {
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000421 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS))
Bill Wendling2b739762009-05-13 21:33:08 +0000422 .addReg(SrcReg,
423 getKillRegState(isKill)),
424 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000425 } else if (RC == PPC::CRRCRegisterClass) {
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000426 if ((EnablePPC32RS && !TM.getSubtargetImpl()->isPPC64()) ||
427 (EnablePPC64RS && TM.getSubtargetImpl()->isPPC64())) {
428 // FIXME (64-bit): Enable
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000429 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR))
Bill Wendling2b739762009-05-13 21:33:08 +0000430 .addReg(SrcReg,
431 getKillRegState(isKill)),
Chris Lattner6734c3a2008-03-20 01:22:40 +0000432 FrameIdx));
Bill Wendlinga1877c52008-03-03 22:19:16 +0000433 return true;
434 } else {
Dale Johannesenb000c482010-02-12 21:35:34 +0000435 // FIXME: We need a scatch reg here. The trouble with using R0 is that
436 // it's possible for the stack frame to be so big the save location is
437 // out of range of immediate offsets, necessitating another register.
438 // We hack this on Darwin by reserving R2. It's probably broken on Linux
439 // at the moment.
440
441 // We need to store the CR in the low 4-bits of the saved value. First,
442 // issue a MFCR to save all of the CRBits.
443 unsigned ScratchReg = TM.getSubtargetImpl()->isDarwinABI() ?
444 PPC::R2 : PPC::R0;
Dale Johannesen8052a182010-05-20 17:48:26 +0000445 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFCRpseud), ScratchReg)
446 .addReg(SrcReg, getKillRegState(isKill)));
Owen Anderson81875432008-01-01 21:11:32 +0000447
Bill Wendlinga1877c52008-03-03 22:19:16 +0000448 // If the saved register wasn't CR0, shift the bits left so that they are
449 // in CR0's slot.
450 if (SrcReg != PPC::CR0) {
451 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(SrcReg)*4;
Dale Johannesenb000c482010-02-12 21:35:34 +0000452 // rlwinm scratch, scratch, ShiftBits, 0, 31.
453 NewMIs.push_back(BuildMI(MF, DL, get(PPC::RLWINM), ScratchReg)
454 .addReg(ScratchReg).addImm(ShiftBits)
455 .addImm(0).addImm(31));
Bill Wendlinga1877c52008-03-03 22:19:16 +0000456 }
457
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000458 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
Dale Johannesenb000c482010-02-12 21:35:34 +0000459 .addReg(ScratchReg,
Bill Wendling2b739762009-05-13 21:33:08 +0000460 getKillRegState(isKill)),
Bill Wendlinga1877c52008-03-03 22:19:16 +0000461 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000462 }
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000463 } else if (RC == PPC::CRBITRCRegisterClass) {
464 // FIXME: We use CRi here because there is no mtcrf on a bit. Since the
465 // backend currently only uses CR1EQ as an individual bit, this should
466 // not cause any bug. If we need other uses of CR bits, the following
467 // code may be invalid.
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000468 unsigned Reg = 0;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000469 if (SrcReg == PPC::CR0LT || SrcReg == PPC::CR0GT ||
470 SrcReg == PPC::CR0EQ || SrcReg == PPC::CR0UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000471 Reg = PPC::CR0;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000472 else if (SrcReg == PPC::CR1LT || SrcReg == PPC::CR1GT ||
473 SrcReg == PPC::CR1EQ || SrcReg == PPC::CR1UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000474 Reg = PPC::CR1;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000475 else if (SrcReg == PPC::CR2LT || SrcReg == PPC::CR2GT ||
476 SrcReg == PPC::CR2EQ || SrcReg == PPC::CR2UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000477 Reg = PPC::CR2;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000478 else if (SrcReg == PPC::CR3LT || SrcReg == PPC::CR3GT ||
479 SrcReg == PPC::CR3EQ || SrcReg == PPC::CR3UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000480 Reg = PPC::CR3;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000481 else if (SrcReg == PPC::CR4LT || SrcReg == PPC::CR4GT ||
482 SrcReg == PPC::CR4EQ || SrcReg == PPC::CR4UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000483 Reg = PPC::CR4;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000484 else if (SrcReg == PPC::CR5LT || SrcReg == PPC::CR5GT ||
485 SrcReg == PPC::CR5EQ || SrcReg == PPC::CR5UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000486 Reg = PPC::CR5;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000487 else if (SrcReg == PPC::CR6LT || SrcReg == PPC::CR6GT ||
488 SrcReg == PPC::CR6EQ || SrcReg == PPC::CR6UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000489 Reg = PPC::CR6;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000490 else if (SrcReg == PPC::CR7LT || SrcReg == PPC::CR7GT ||
491 SrcReg == PPC::CR7EQ || SrcReg == PPC::CR7UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000492 Reg = PPC::CR7;
493
Dan Gohman221a4372008-07-07 23:14:23 +0000494 return StoreRegToStackSlot(MF, Reg, isKill, FrameIdx,
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000495 PPC::CRRCRegisterClass, NewMIs);
496
Owen Anderson81875432008-01-01 21:11:32 +0000497 } else if (RC == PPC::VRRCRegisterClass) {
498 // We don't have indexed addressing for vector loads. Emit:
499 // R0 = ADDI FI#
500 // STVX VAL, 0, R0
501 //
502 // FIXME: We use R0 here, because it isn't available for RA.
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000503 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::ADDI), PPC::R0),
Owen Anderson81875432008-01-01 21:11:32 +0000504 FrameIdx, 0, 0));
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000505 NewMIs.push_back(BuildMI(MF, DL, get(PPC::STVX))
Bill Wendling2b739762009-05-13 21:33:08 +0000506 .addReg(SrcReg, getKillRegState(isKill))
507 .addReg(PPC::R0)
508 .addReg(PPC::R0));
Owen Anderson81875432008-01-01 21:11:32 +0000509 } else {
Edwin Törökbd448e32009-07-14 16:55:14 +0000510 llvm_unreachable("Unknown regclass!");
Owen Anderson81875432008-01-01 21:11:32 +0000511 }
Bill Wendlinga1877c52008-03-03 22:19:16 +0000512
513 return false;
Owen Anderson81875432008-01-01 21:11:32 +0000514}
515
516void
517PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Bill Wendlinga1877c52008-03-03 22:19:16 +0000518 MachineBasicBlock::iterator MI,
519 unsigned SrcReg, bool isKill, int FrameIdx,
Evan Cheng1f8534d2010-05-06 19:06:44 +0000520 const TargetRegisterClass *RC,
521 const TargetRegisterInfo *TRI) const {
Dan Gohman221a4372008-07-07 23:14:23 +0000522 MachineFunction &MF = *MBB.getParent();
Owen Anderson81875432008-01-01 21:11:32 +0000523 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendlinga1877c52008-03-03 22:19:16 +0000524
Dan Gohman221a4372008-07-07 23:14:23 +0000525 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs)) {
526 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Bill Wendlinga1877c52008-03-03 22:19:16 +0000527 FuncInfo->setSpillsCR();
528 }
529
Owen Anderson81875432008-01-01 21:11:32 +0000530 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
531 MBB.insert(MI, NewMIs[i]);
532}
533
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000534void
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000535PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL,
Dan Gohman221a4372008-07-07 23:14:23 +0000536 unsigned DestReg, int FrameIdx,
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000537 const TargetRegisterClass *RC,
538 SmallVectorImpl<MachineInstr*> &NewMIs)const{
Owen Anderson81875432008-01-01 21:11:32 +0000539 if (RC == PPC::GPRCRegisterClass) {
540 if (DestReg != PPC::LR) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000541 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
542 DestReg), FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000543 } else {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000544 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
545 PPC::R11), FrameIdx));
546 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTLR)).addReg(PPC::R11));
Owen Anderson81875432008-01-01 21:11:32 +0000547 }
548 } else if (RC == PPC::G8RCRegisterClass) {
549 if (DestReg != PPC::LR8) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000550 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg),
Owen Anderson81875432008-01-01 21:11:32 +0000551 FrameIdx));
552 } else {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000553 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD),
554 PPC::R11), FrameIdx));
555 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTLR8)).addReg(PPC::R11));
Owen Anderson81875432008-01-01 21:11:32 +0000556 }
557 } else if (RC == PPC::F8RCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000558 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg),
Owen Anderson81875432008-01-01 21:11:32 +0000559 FrameIdx));
560 } else if (RC == PPC::F4RCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000561 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg),
Owen Anderson81875432008-01-01 21:11:32 +0000562 FrameIdx));
563 } else if (RC == PPC::CRRCRegisterClass) {
Dale Johannesenb000c482010-02-12 21:35:34 +0000564 // FIXME: We need a scatch reg here. The trouble with using R0 is that
565 // it's possible for the stack frame to be so big the save location is
566 // out of range of immediate offsets, necessitating another register.
567 // We hack this on Darwin by reserving R2. It's probably broken on Linux
568 // at the moment.
569 unsigned ScratchReg = TM.getSubtargetImpl()->isDarwinABI() ?
570 PPC::R2 : PPC::R0;
571 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
572 ScratchReg), FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000573
574 // If the reloaded register isn't CR0, shift the bits right so that they are
575 // in the right CR's slot.
576 if (DestReg != PPC::CR0) {
577 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(DestReg)*4;
578 // rlwinm r11, r11, 32-ShiftBits, 0, 31.
Dale Johannesenb000c482010-02-12 21:35:34 +0000579 NewMIs.push_back(BuildMI(MF, DL, get(PPC::RLWINM), ScratchReg)
580 .addReg(ScratchReg).addImm(32-ShiftBits).addImm(0)
581 .addImm(31));
Owen Anderson81875432008-01-01 21:11:32 +0000582 }
583
Dale Johannesenb000c482010-02-12 21:35:34 +0000584 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTCRF), DestReg)
585 .addReg(ScratchReg));
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000586 } else if (RC == PPC::CRBITRCRegisterClass) {
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000587
588 unsigned Reg = 0;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000589 if (DestReg == PPC::CR0LT || DestReg == PPC::CR0GT ||
590 DestReg == PPC::CR0EQ || DestReg == PPC::CR0UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000591 Reg = PPC::CR0;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000592 else if (DestReg == PPC::CR1LT || DestReg == PPC::CR1GT ||
593 DestReg == PPC::CR1EQ || DestReg == PPC::CR1UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000594 Reg = PPC::CR1;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000595 else if (DestReg == PPC::CR2LT || DestReg == PPC::CR2GT ||
596 DestReg == PPC::CR2EQ || DestReg == PPC::CR2UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000597 Reg = PPC::CR2;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000598 else if (DestReg == PPC::CR3LT || DestReg == PPC::CR3GT ||
599 DestReg == PPC::CR3EQ || DestReg == PPC::CR3UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000600 Reg = PPC::CR3;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000601 else if (DestReg == PPC::CR4LT || DestReg == PPC::CR4GT ||
602 DestReg == PPC::CR4EQ || DestReg == PPC::CR4UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000603 Reg = PPC::CR4;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000604 else if (DestReg == PPC::CR5LT || DestReg == PPC::CR5GT ||
605 DestReg == PPC::CR5EQ || DestReg == PPC::CR5UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000606 Reg = PPC::CR5;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000607 else if (DestReg == PPC::CR6LT || DestReg == PPC::CR6GT ||
608 DestReg == PPC::CR6EQ || DestReg == PPC::CR6UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000609 Reg = PPC::CR6;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000610 else if (DestReg == PPC::CR7LT || DestReg == PPC::CR7GT ||
611 DestReg == PPC::CR7EQ || DestReg == PPC::CR7UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000612 Reg = PPC::CR7;
613
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000614 return LoadRegFromStackSlot(MF, DL, Reg, FrameIdx,
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000615 PPC::CRRCRegisterClass, NewMIs);
616
Owen Anderson81875432008-01-01 21:11:32 +0000617 } else if (RC == PPC::VRRCRegisterClass) {
618 // We don't have indexed addressing for vector loads. Emit:
619 // R0 = ADDI FI#
620 // Dest = LVX 0, R0
621 //
622 // FIXME: We use R0 here, because it isn't available for RA.
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000623 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::ADDI), PPC::R0),
Owen Anderson81875432008-01-01 21:11:32 +0000624 FrameIdx, 0, 0));
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000625 NewMIs.push_back(BuildMI(MF, DL, get(PPC::LVX),DestReg).addReg(PPC::R0)
Owen Anderson81875432008-01-01 21:11:32 +0000626 .addReg(PPC::R0));
627 } else {
Edwin Törökbd448e32009-07-14 16:55:14 +0000628 llvm_unreachable("Unknown regclass!");
Owen Anderson81875432008-01-01 21:11:32 +0000629 }
630}
631
632void
633PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Bill Wendlinga1877c52008-03-03 22:19:16 +0000634 MachineBasicBlock::iterator MI,
635 unsigned DestReg, int FrameIdx,
Evan Cheng1f8534d2010-05-06 19:06:44 +0000636 const TargetRegisterClass *RC,
637 const TargetRegisterInfo *TRI) const {
Dan Gohman221a4372008-07-07 23:14:23 +0000638 MachineFunction &MF = *MBB.getParent();
Owen Anderson81875432008-01-01 21:11:32 +0000639 SmallVector<MachineInstr*, 4> NewMIs;
Chris Lattnerd2c680b2010-04-02 20:16:16 +0000640 DebugLoc DL;
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000641 if (MI != MBB.end()) DL = MI->getDebugLoc();
642 LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs);
Owen Anderson81875432008-01-01 21:11:32 +0000643 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
644 MBB.insert(MI, NewMIs[i]);
645}
646
Evan Cheng284da772010-04-26 07:39:36 +0000647MachineInstr*
648PPCInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF,
Evan Chengf9c420a2010-04-29 01:13:30 +0000649 int FrameIx, uint64_t Offset,
Evan Cheng284da772010-04-26 07:39:36 +0000650 const MDNode *MDPtr,
651 DebugLoc DL) const {
652 MachineInstrBuilder MIB = BuildMI(MF, DL, get(PPC::DBG_VALUE));
653 addFrameReference(MIB, FrameIx, 0, false).addImm(Offset).addMetadata(MDPtr);
654 return &*MIB;
655}
656
Owen Anderson9a184ef2008-01-07 01:35:02 +0000657/// foldMemoryOperand - PowerPC (like most RISC's) can only fold spills into
658/// copy instructions, turning them into load/store instructions.
Dan Gohmanedc83d62008-12-03 18:43:12 +0000659MachineInstr *PPCInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
660 MachineInstr *MI,
661 const SmallVectorImpl<unsigned> &Ops,
662 int FrameIndex) const {
Owen Anderson9a184ef2008-01-07 01:35:02 +0000663 if (Ops.size() != 1) return NULL;
664
665 // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because
666 // it takes more than one instruction to store it.
667 unsigned Opc = MI->getOpcode();
668 unsigned OpNum = Ops[0];
669
670 MachineInstr *NewMI = NULL;
671 if ((Opc == PPC::OR &&
672 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
673 if (OpNum == 0) { // move -> store
674 unsigned InReg = MI->getOperand(1).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000675 bool isKill = MI->getOperand(1).isKill();
Evan Cheng65219822009-07-01 01:59:31 +0000676 bool isUndef = MI->getOperand(1).isUndef();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000677 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::STW))
Evan Cheng65219822009-07-01 01:59:31 +0000678 .addReg(InReg,
679 getKillRegState(isKill) |
680 getUndefRegState(isUndef)),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000681 FrameIndex);
682 } else { // move -> load
683 unsigned OutReg = MI->getOperand(0).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000684 bool isDead = MI->getOperand(0).isDead();
Evan Cheng65219822009-07-01 01:59:31 +0000685 bool isUndef = MI->getOperand(0).isUndef();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000686 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::LWZ))
Bill Wendling2b739762009-05-13 21:33:08 +0000687 .addReg(OutReg,
688 RegState::Define |
Evan Cheng65219822009-07-01 01:59:31 +0000689 getDeadRegState(isDead) |
690 getUndefRegState(isUndef)),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000691 FrameIndex);
692 }
693 } else if ((Opc == PPC::OR8 &&
694 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
695 if (OpNum == 0) { // move -> store
696 unsigned InReg = MI->getOperand(1).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000697 bool isKill = MI->getOperand(1).isKill();
Evan Cheng65219822009-07-01 01:59:31 +0000698 bool isUndef = MI->getOperand(1).isUndef();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000699 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::STD))
Evan Cheng65219822009-07-01 01:59:31 +0000700 .addReg(InReg,
701 getKillRegState(isKill) |
702 getUndefRegState(isUndef)),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000703 FrameIndex);
704 } else { // move -> load
705 unsigned OutReg = MI->getOperand(0).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000706 bool isDead = MI->getOperand(0).isDead();
Evan Cheng65219822009-07-01 01:59:31 +0000707 bool isUndef = MI->getOperand(0).isUndef();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000708 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::LD))
Bill Wendling2b739762009-05-13 21:33:08 +0000709 .addReg(OutReg,
710 RegState::Define |
Evan Cheng65219822009-07-01 01:59:31 +0000711 getDeadRegState(isDead) |
712 getUndefRegState(isUndef)),
Evan Chenge52c1912008-07-03 09:09:37 +0000713 FrameIndex);
Owen Anderson9a184ef2008-01-07 01:35:02 +0000714 }
Jakob Stoklund Olesen00da1ea2010-02-26 21:53:24 +0000715 } else if (Opc == PPC::FMR || Opc == PPC::FMRSD) {
Jakob Stoklund Olesen98172ee2010-02-26 21:09:24 +0000716 // The register may be F4RC or F8RC, and that determines the memory op.
717 unsigned OrigReg = MI->getOperand(OpNum).getReg();
718 // We cannot tell the register class from a physreg alone.
719 if (TargetRegisterInfo::isPhysicalRegister(OrigReg))
720 return NULL;
721 const TargetRegisterClass *RC = MF.getRegInfo().getRegClass(OrigReg);
722 const bool is64 = RC == PPC::F8RCRegisterClass;
723
Owen Anderson9a184ef2008-01-07 01:35:02 +0000724 if (OpNum == 0) { // move -> store
725 unsigned InReg = MI->getOperand(1).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000726 bool isKill = MI->getOperand(1).isKill();
Evan Cheng65219822009-07-01 01:59:31 +0000727 bool isUndef = MI->getOperand(1).isUndef();
Jakob Stoklund Olesen98172ee2010-02-26 21:09:24 +0000728 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(),
729 get(is64 ? PPC::STFD : PPC::STFS))
Evan Cheng65219822009-07-01 01:59:31 +0000730 .addReg(InReg,
731 getKillRegState(isKill) |
732 getUndefRegState(isUndef)),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000733 FrameIndex);
734 } else { // move -> load
735 unsigned OutReg = MI->getOperand(0).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000736 bool isDead = MI->getOperand(0).isDead();
Evan Cheng65219822009-07-01 01:59:31 +0000737 bool isUndef = MI->getOperand(0).isUndef();
Jakob Stoklund Olesen98172ee2010-02-26 21:09:24 +0000738 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(),
739 get(is64 ? PPC::LFD : PPC::LFS))
Bill Wendling2b739762009-05-13 21:33:08 +0000740 .addReg(OutReg,
741 RegState::Define |
Evan Cheng65219822009-07-01 01:59:31 +0000742 getDeadRegState(isDead) |
743 getUndefRegState(isUndef)),
Evan Chenge52c1912008-07-03 09:09:37 +0000744 FrameIndex);
Owen Anderson9a184ef2008-01-07 01:35:02 +0000745 }
746 }
747
Owen Anderson9a184ef2008-01-07 01:35:02 +0000748 return NewMI;
749}
750
Dan Gohman46b948e2008-10-16 01:49:15 +0000751bool PPCInstrInfo::canFoldMemoryOperand(const MachineInstr *MI,
752 const SmallVectorImpl<unsigned> &Ops) const {
Owen Anderson9a184ef2008-01-07 01:35:02 +0000753 if (Ops.size() != 1) return false;
754
755 // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because
756 // it takes more than one instruction to store it.
757 unsigned Opc = MI->getOpcode();
758
759 if ((Opc == PPC::OR &&
760 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
761 return true;
762 else if ((Opc == PPC::OR8 &&
763 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
764 return true;
Jakob Stoklund Olesen00da1ea2010-02-26 21:53:24 +0000765 else if (Opc == PPC::FMR || Opc == PPC::FMRSD)
Owen Anderson9a184ef2008-01-07 01:35:02 +0000766 return true;
767
768 return false;
769}
770
Owen Anderson81875432008-01-01 21:11:32 +0000771
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000772bool PPCInstrInfo::
Owen Andersond131b5b2008-08-14 22:49:33 +0000773ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000774 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
775 // Leave the CR# the same, but invert the condition.
776 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
777 return false;
778}
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000779
780/// GetInstSize - Return the number of bytes of code the specified
781/// instruction may be. This returns the maximum number of bytes.
782///
783unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
784 switch (MI->getOpcode()) {
785 case PPC::INLINEASM: { // Inline Asm: Variable size.
786 const MachineFunction *MF = MI->getParent()->getParent();
787 const char *AsmStr = MI->getOperand(0).getSymbolName();
Chris Lattner621c44d2009-08-22 20:48:53 +0000788 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000789 }
Dan Gohmanfa607c92008-07-01 00:05:16 +0000790 case PPC::DBG_LABEL:
791 case PPC::EH_LABEL:
792 case PPC::GC_LABEL:
Dale Johannesenac548972010-04-07 19:51:44 +0000793 case PPC::DBG_VALUE:
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000794 return 0;
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000795 default:
796 return 4; // PowerPC instructions are all 4 bytes
797 }
798}