blob: af88b44d6a2e3925630785acbfc907e09595f7ed [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,
348 const TargetRegisterClass *SrcRC) const {
349 if (DestRC != SrcRC) {
Owen Anderson9fa72d92008-08-26 18:03:31 +0000350 // Not yet supported!
351 return false;
Owen Anderson8f2c8932007-12-31 06:32:00 +0000352 }
353
Chris Lattnerd2c680b2010-04-02 20:16:16 +0000354 DebugLoc DL;
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000355 if (MI != MBB.end()) DL = MI->getDebugLoc();
356
Owen Anderson8f2c8932007-12-31 06:32:00 +0000357 if (DestRC == PPC::GPRCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000358 BuildMI(MBB, MI, DL, get(PPC::OR), DestReg).addReg(SrcReg).addReg(SrcReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000359 } else if (DestRC == PPC::G8RCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000360 BuildMI(MBB, MI, DL, get(PPC::OR8), DestReg).addReg(SrcReg).addReg(SrcReg);
Jakob Stoklund Olesen00da1ea2010-02-26 21:53:24 +0000361 } else if (DestRC == PPC::F4RCRegisterClass ||
362 DestRC == PPC::F8RCRegisterClass) {
363 BuildMI(MBB, MI, DL, get(PPC::FMR), DestReg).addReg(SrcReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000364 } else if (DestRC == PPC::CRRCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000365 BuildMI(MBB, MI, DL, get(PPC::MCRF), DestReg).addReg(SrcReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000366 } else if (DestRC == PPC::VRRCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000367 BuildMI(MBB, MI, DL, get(PPC::VOR), DestReg).addReg(SrcReg).addReg(SrcReg);
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000368 } else if (DestRC == PPC::CRBITRCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000369 BuildMI(MBB, MI, DL, get(PPC::CROR), DestReg).addReg(SrcReg).addReg(SrcReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000370 } else {
Owen Anderson9fa72d92008-08-26 18:03:31 +0000371 // Attempt to copy register that is not GPR or FPR
372 return false;
Owen Anderson8f2c8932007-12-31 06:32:00 +0000373 }
Owen Anderson9fa72d92008-08-26 18:03:31 +0000374
375 return true;
Owen Anderson8f2c8932007-12-31 06:32:00 +0000376}
377
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000378bool
Dan Gohman221a4372008-07-07 23:14:23 +0000379PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
380 unsigned SrcReg, bool isKill,
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000381 int FrameIdx,
382 const TargetRegisterClass *RC,
383 SmallVectorImpl<MachineInstr*> &NewMIs) const{
Chris Lattnerd2c680b2010-04-02 20:16:16 +0000384 DebugLoc DL;
Owen Anderson81875432008-01-01 21:11:32 +0000385 if (RC == PPC::GPRCRegisterClass) {
386 if (SrcReg != PPC::LR) {
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000387 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
Bill Wendling2b739762009-05-13 21:33:08 +0000388 .addReg(SrcReg,
389 getKillRegState(isKill)),
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000390 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000391 } else {
392 // FIXME: this spills LR immediately to memory in one step. To do this,
393 // we use R11, which we know cannot be used in the prolog/epilog. This is
394 // a hack.
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000395 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFLR), PPC::R11));
396 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
Bill Wendling2b739762009-05-13 21:33:08 +0000397 .addReg(PPC::R11,
398 getKillRegState(isKill)),
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000399 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000400 }
401 } else if (RC == PPC::G8RCRegisterClass) {
402 if (SrcReg != PPC::LR8) {
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000403 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
Bill Wendling2b739762009-05-13 21:33:08 +0000404 .addReg(SrcReg,
405 getKillRegState(isKill)),
406 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000407 } else {
408 // FIXME: this spills LR immediately to memory in one step. To do this,
409 // we use R11, which we know cannot be used in the prolog/epilog. This is
410 // a hack.
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000411 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFLR8), PPC::X11));
412 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
Bill Wendling2b739762009-05-13 21:33:08 +0000413 .addReg(PPC::X11,
414 getKillRegState(isKill)),
415 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000416 }
417 } else if (RC == PPC::F8RCRegisterClass) {
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000418 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD))
Bill Wendling2b739762009-05-13 21:33:08 +0000419 .addReg(SrcReg,
420 getKillRegState(isKill)),
421 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000422 } else if (RC == PPC::F4RCRegisterClass) {
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000423 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS))
Bill Wendling2b739762009-05-13 21:33:08 +0000424 .addReg(SrcReg,
425 getKillRegState(isKill)),
426 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000427 } else if (RC == PPC::CRRCRegisterClass) {
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000428 if ((EnablePPC32RS && !TM.getSubtargetImpl()->isPPC64()) ||
429 (EnablePPC64RS && TM.getSubtargetImpl()->isPPC64())) {
430 // FIXME (64-bit): Enable
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000431 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR))
Bill Wendling2b739762009-05-13 21:33:08 +0000432 .addReg(SrcReg,
433 getKillRegState(isKill)),
Chris Lattner6734c3a2008-03-20 01:22:40 +0000434 FrameIdx));
Bill Wendlinga1877c52008-03-03 22:19:16 +0000435 return true;
436 } else {
Dale Johannesenb000c482010-02-12 21:35:34 +0000437 // FIXME: We need a scatch reg here. The trouble with using R0 is that
438 // it's possible for the stack frame to be so big the save location is
439 // out of range of immediate offsets, necessitating another register.
440 // We hack this on Darwin by reserving R2. It's probably broken on Linux
441 // at the moment.
442
443 // We need to store the CR in the low 4-bits of the saved value. First,
444 // issue a MFCR to save all of the CRBits.
445 unsigned ScratchReg = TM.getSubtargetImpl()->isDarwinABI() ?
446 PPC::R2 : PPC::R0;
447 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFCR), ScratchReg));
Owen Anderson81875432008-01-01 21:11:32 +0000448
Bill Wendlinga1877c52008-03-03 22:19:16 +0000449 // If the saved register wasn't CR0, shift the bits left so that they are
450 // in CR0's slot.
451 if (SrcReg != PPC::CR0) {
452 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(SrcReg)*4;
Dale Johannesenb000c482010-02-12 21:35:34 +0000453 // rlwinm scratch, scratch, ShiftBits, 0, 31.
454 NewMIs.push_back(BuildMI(MF, DL, get(PPC::RLWINM), ScratchReg)
455 .addReg(ScratchReg).addImm(ShiftBits)
456 .addImm(0).addImm(31));
Bill Wendlinga1877c52008-03-03 22:19:16 +0000457 }
458
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000459 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
Dale Johannesenb000c482010-02-12 21:35:34 +0000460 .addReg(ScratchReg,
Bill Wendling2b739762009-05-13 21:33:08 +0000461 getKillRegState(isKill)),
Bill Wendlinga1877c52008-03-03 22:19:16 +0000462 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000463 }
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000464 } else if (RC == PPC::CRBITRCRegisterClass) {
465 // FIXME: We use CRi here because there is no mtcrf on a bit. Since the
466 // backend currently only uses CR1EQ as an individual bit, this should
467 // not cause any bug. If we need other uses of CR bits, the following
468 // code may be invalid.
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000469 unsigned Reg = 0;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000470 if (SrcReg == PPC::CR0LT || SrcReg == PPC::CR0GT ||
471 SrcReg == PPC::CR0EQ || SrcReg == PPC::CR0UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000472 Reg = PPC::CR0;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000473 else if (SrcReg == PPC::CR1LT || SrcReg == PPC::CR1GT ||
474 SrcReg == PPC::CR1EQ || SrcReg == PPC::CR1UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000475 Reg = PPC::CR1;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000476 else if (SrcReg == PPC::CR2LT || SrcReg == PPC::CR2GT ||
477 SrcReg == PPC::CR2EQ || SrcReg == PPC::CR2UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000478 Reg = PPC::CR2;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000479 else if (SrcReg == PPC::CR3LT || SrcReg == PPC::CR3GT ||
480 SrcReg == PPC::CR3EQ || SrcReg == PPC::CR3UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000481 Reg = PPC::CR3;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000482 else if (SrcReg == PPC::CR4LT || SrcReg == PPC::CR4GT ||
483 SrcReg == PPC::CR4EQ || SrcReg == PPC::CR4UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000484 Reg = PPC::CR4;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000485 else if (SrcReg == PPC::CR5LT || SrcReg == PPC::CR5GT ||
486 SrcReg == PPC::CR5EQ || SrcReg == PPC::CR5UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000487 Reg = PPC::CR5;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000488 else if (SrcReg == PPC::CR6LT || SrcReg == PPC::CR6GT ||
489 SrcReg == PPC::CR6EQ || SrcReg == PPC::CR6UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000490 Reg = PPC::CR6;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000491 else if (SrcReg == PPC::CR7LT || SrcReg == PPC::CR7GT ||
492 SrcReg == PPC::CR7EQ || SrcReg == PPC::CR7UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000493 Reg = PPC::CR7;
494
Dan Gohman221a4372008-07-07 23:14:23 +0000495 return StoreRegToStackSlot(MF, Reg, isKill, FrameIdx,
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000496 PPC::CRRCRegisterClass, NewMIs);
497
Owen Anderson81875432008-01-01 21:11:32 +0000498 } else if (RC == PPC::VRRCRegisterClass) {
499 // We don't have indexed addressing for vector loads. Emit:
500 // R0 = ADDI FI#
501 // STVX VAL, 0, R0
502 //
503 // FIXME: We use R0 here, because it isn't available for RA.
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000504 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::ADDI), PPC::R0),
Owen Anderson81875432008-01-01 21:11:32 +0000505 FrameIdx, 0, 0));
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000506 NewMIs.push_back(BuildMI(MF, DL, get(PPC::STVX))
Bill Wendling2b739762009-05-13 21:33:08 +0000507 .addReg(SrcReg, getKillRegState(isKill))
508 .addReg(PPC::R0)
509 .addReg(PPC::R0));
Owen Anderson81875432008-01-01 21:11:32 +0000510 } else {
Edwin Törökbd448e32009-07-14 16:55:14 +0000511 llvm_unreachable("Unknown regclass!");
Owen Anderson81875432008-01-01 21:11:32 +0000512 }
Bill Wendlinga1877c52008-03-03 22:19:16 +0000513
514 return false;
Owen Anderson81875432008-01-01 21:11:32 +0000515}
516
517void
518PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Bill Wendlinga1877c52008-03-03 22:19:16 +0000519 MachineBasicBlock::iterator MI,
520 unsigned SrcReg, bool isKill, int FrameIdx,
521 const TargetRegisterClass *RC) 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,
636 const TargetRegisterClass *RC) const {
Dan Gohman221a4372008-07-07 23:14:23 +0000637 MachineFunction &MF = *MBB.getParent();
Owen Anderson81875432008-01-01 21:11:32 +0000638 SmallVector<MachineInstr*, 4> NewMIs;
Chris Lattnerd2c680b2010-04-02 20:16:16 +0000639 DebugLoc DL;
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000640 if (MI != MBB.end()) DL = MI->getDebugLoc();
641 LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs);
Owen Anderson81875432008-01-01 21:11:32 +0000642 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
643 MBB.insert(MI, NewMIs[i]);
644}
645
Evan Cheng284da772010-04-26 07:39:36 +0000646MachineInstr*
647PPCInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF,
Evan Chengf9c420a2010-04-29 01:13:30 +0000648 int FrameIx, uint64_t Offset,
Evan Cheng284da772010-04-26 07:39:36 +0000649 const MDNode *MDPtr,
650 DebugLoc DL) const {
651 MachineInstrBuilder MIB = BuildMI(MF, DL, get(PPC::DBG_VALUE));
652 addFrameReference(MIB, FrameIx, 0, false).addImm(Offset).addMetadata(MDPtr);
653 return &*MIB;
654}
655
Owen Anderson9a184ef2008-01-07 01:35:02 +0000656/// foldMemoryOperand - PowerPC (like most RISC's) can only fold spills into
657/// copy instructions, turning them into load/store instructions.
Dan Gohmanedc83d62008-12-03 18:43:12 +0000658MachineInstr *PPCInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
659 MachineInstr *MI,
660 const SmallVectorImpl<unsigned> &Ops,
661 int FrameIndex) const {
Owen Anderson9a184ef2008-01-07 01:35:02 +0000662 if (Ops.size() != 1) return NULL;
663
664 // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because
665 // it takes more than one instruction to store it.
666 unsigned Opc = MI->getOpcode();
667 unsigned OpNum = Ops[0];
668
669 MachineInstr *NewMI = NULL;
670 if ((Opc == PPC::OR &&
671 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
672 if (OpNum == 0) { // move -> store
673 unsigned InReg = MI->getOperand(1).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000674 bool isKill = MI->getOperand(1).isKill();
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::STW))
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::LWZ))
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)),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000690 FrameIndex);
691 }
692 } else if ((Opc == PPC::OR8 &&
693 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
694 if (OpNum == 0) { // move -> store
695 unsigned InReg = MI->getOperand(1).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000696 bool isKill = MI->getOperand(1).isKill();
Evan Cheng65219822009-07-01 01:59:31 +0000697 bool isUndef = MI->getOperand(1).isUndef();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000698 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::STD))
Evan Cheng65219822009-07-01 01:59:31 +0000699 .addReg(InReg,
700 getKillRegState(isKill) |
701 getUndefRegState(isUndef)),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000702 FrameIndex);
703 } else { // move -> load
704 unsigned OutReg = MI->getOperand(0).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000705 bool isDead = MI->getOperand(0).isDead();
Evan Cheng65219822009-07-01 01:59:31 +0000706 bool isUndef = MI->getOperand(0).isUndef();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000707 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::LD))
Bill Wendling2b739762009-05-13 21:33:08 +0000708 .addReg(OutReg,
709 RegState::Define |
Evan Cheng65219822009-07-01 01:59:31 +0000710 getDeadRegState(isDead) |
711 getUndefRegState(isUndef)),
Evan Chenge52c1912008-07-03 09:09:37 +0000712 FrameIndex);
Owen Anderson9a184ef2008-01-07 01:35:02 +0000713 }
Jakob Stoklund Olesen00da1ea2010-02-26 21:53:24 +0000714 } else if (Opc == PPC::FMR || Opc == PPC::FMRSD) {
Jakob Stoklund Olesen98172ee2010-02-26 21:09:24 +0000715 // The register may be F4RC or F8RC, and that determines the memory op.
716 unsigned OrigReg = MI->getOperand(OpNum).getReg();
717 // We cannot tell the register class from a physreg alone.
718 if (TargetRegisterInfo::isPhysicalRegister(OrigReg))
719 return NULL;
720 const TargetRegisterClass *RC = MF.getRegInfo().getRegClass(OrigReg);
721 const bool is64 = RC == PPC::F8RCRegisterClass;
722
Owen Anderson9a184ef2008-01-07 01:35:02 +0000723 if (OpNum == 0) { // move -> store
724 unsigned InReg = MI->getOperand(1).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000725 bool isKill = MI->getOperand(1).isKill();
Evan Cheng65219822009-07-01 01:59:31 +0000726 bool isUndef = MI->getOperand(1).isUndef();
Jakob Stoklund Olesen98172ee2010-02-26 21:09:24 +0000727 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(),
728 get(is64 ? PPC::STFD : PPC::STFS))
Evan Cheng65219822009-07-01 01:59:31 +0000729 .addReg(InReg,
730 getKillRegState(isKill) |
731 getUndefRegState(isUndef)),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000732 FrameIndex);
733 } else { // move -> load
734 unsigned OutReg = MI->getOperand(0).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000735 bool isDead = MI->getOperand(0).isDead();
Evan Cheng65219822009-07-01 01:59:31 +0000736 bool isUndef = MI->getOperand(0).isUndef();
Jakob Stoklund Olesen98172ee2010-02-26 21:09:24 +0000737 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(),
738 get(is64 ? PPC::LFD : PPC::LFS))
Bill Wendling2b739762009-05-13 21:33:08 +0000739 .addReg(OutReg,
740 RegState::Define |
Evan Cheng65219822009-07-01 01:59:31 +0000741 getDeadRegState(isDead) |
742 getUndefRegState(isUndef)),
Evan Chenge52c1912008-07-03 09:09:37 +0000743 FrameIndex);
Owen Anderson9a184ef2008-01-07 01:35:02 +0000744 }
745 }
746
Owen Anderson9a184ef2008-01-07 01:35:02 +0000747 return NewMI;
748}
749
Dan Gohman46b948e2008-10-16 01:49:15 +0000750bool PPCInstrInfo::canFoldMemoryOperand(const MachineInstr *MI,
751 const SmallVectorImpl<unsigned> &Ops) const {
Owen Anderson9a184ef2008-01-07 01:35:02 +0000752 if (Ops.size() != 1) return false;
753
754 // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because
755 // it takes more than one instruction to store it.
756 unsigned Opc = MI->getOpcode();
757
758 if ((Opc == PPC::OR &&
759 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
760 return true;
761 else if ((Opc == PPC::OR8 &&
762 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
763 return true;
Jakob Stoklund Olesen00da1ea2010-02-26 21:53:24 +0000764 else if (Opc == PPC::FMR || Opc == PPC::FMRSD)
Owen Anderson9a184ef2008-01-07 01:35:02 +0000765 return true;
766
767 return false;
768}
769
Owen Anderson81875432008-01-01 21:11:32 +0000770
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000771bool PPCInstrInfo::
Owen Andersond131b5b2008-08-14 22:49:33 +0000772ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000773 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
774 // Leave the CR# the same, but invert the condition.
775 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
776 return false;
777}
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000778
779/// GetInstSize - Return the number of bytes of code the specified
780/// instruction may be. This returns the maximum number of bytes.
781///
782unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
783 switch (MI->getOpcode()) {
784 case PPC::INLINEASM: { // Inline Asm: Variable size.
785 const MachineFunction *MF = MI->getParent()->getParent();
786 const char *AsmStr = MI->getOperand(0).getSymbolName();
Chris Lattner621c44d2009-08-22 20:48:53 +0000787 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000788 }
Dan Gohmanfa607c92008-07-01 00:05:16 +0000789 case PPC::DBG_LABEL:
790 case PPC::EH_LABEL:
791 case PPC::GC_LABEL:
Dale Johannesenac548972010-04-07 19:51:44 +0000792 case PPC::DBG_VALUE:
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000793 return 0;
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000794 default:
795 return 4; // PowerPC instructions are all 4 bytes
796 }
797}