blob: ae95ae47712540526d4a5ebd01d9ac55c5c9235c [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;
445 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFCR), ScratchReg));
Owen Anderson81875432008-01-01 21:11:32 +0000446
Bill Wendlinga1877c52008-03-03 22:19:16 +0000447 // If the saved register wasn't CR0, shift the bits left so that they are
448 // in CR0's slot.
449 if (SrcReg != PPC::CR0) {
450 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(SrcReg)*4;
Dale Johannesenb000c482010-02-12 21:35:34 +0000451 // rlwinm scratch, scratch, ShiftBits, 0, 31.
452 NewMIs.push_back(BuildMI(MF, DL, get(PPC::RLWINM), ScratchReg)
453 .addReg(ScratchReg).addImm(ShiftBits)
454 .addImm(0).addImm(31));
Bill Wendlinga1877c52008-03-03 22:19:16 +0000455 }
456
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000457 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
Dale Johannesenb000c482010-02-12 21:35:34 +0000458 .addReg(ScratchReg,
Bill Wendling2b739762009-05-13 21:33:08 +0000459 getKillRegState(isKill)),
Bill Wendlinga1877c52008-03-03 22:19:16 +0000460 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000461 }
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000462 } else if (RC == PPC::CRBITRCRegisterClass) {
463 // FIXME: We use CRi here because there is no mtcrf on a bit. Since the
464 // backend currently only uses CR1EQ as an individual bit, this should
465 // not cause any bug. If we need other uses of CR bits, the following
466 // code may be invalid.
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000467 unsigned Reg = 0;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000468 if (SrcReg == PPC::CR0LT || SrcReg == PPC::CR0GT ||
469 SrcReg == PPC::CR0EQ || SrcReg == PPC::CR0UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000470 Reg = PPC::CR0;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000471 else if (SrcReg == PPC::CR1LT || SrcReg == PPC::CR1GT ||
472 SrcReg == PPC::CR1EQ || SrcReg == PPC::CR1UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000473 Reg = PPC::CR1;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000474 else if (SrcReg == PPC::CR2LT || SrcReg == PPC::CR2GT ||
475 SrcReg == PPC::CR2EQ || SrcReg == PPC::CR2UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000476 Reg = PPC::CR2;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000477 else if (SrcReg == PPC::CR3LT || SrcReg == PPC::CR3GT ||
478 SrcReg == PPC::CR3EQ || SrcReg == PPC::CR3UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000479 Reg = PPC::CR3;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000480 else if (SrcReg == PPC::CR4LT || SrcReg == PPC::CR4GT ||
481 SrcReg == PPC::CR4EQ || SrcReg == PPC::CR4UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000482 Reg = PPC::CR4;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000483 else if (SrcReg == PPC::CR5LT || SrcReg == PPC::CR5GT ||
484 SrcReg == PPC::CR5EQ || SrcReg == PPC::CR5UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000485 Reg = PPC::CR5;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000486 else if (SrcReg == PPC::CR6LT || SrcReg == PPC::CR6GT ||
487 SrcReg == PPC::CR6EQ || SrcReg == PPC::CR6UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000488 Reg = PPC::CR6;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000489 else if (SrcReg == PPC::CR7LT || SrcReg == PPC::CR7GT ||
490 SrcReg == PPC::CR7EQ || SrcReg == PPC::CR7UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000491 Reg = PPC::CR7;
492
Dan Gohman221a4372008-07-07 23:14:23 +0000493 return StoreRegToStackSlot(MF, Reg, isKill, FrameIdx,
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000494 PPC::CRRCRegisterClass, NewMIs);
495
Owen Anderson81875432008-01-01 21:11:32 +0000496 } else if (RC == PPC::VRRCRegisterClass) {
497 // We don't have indexed addressing for vector loads. Emit:
498 // R0 = ADDI FI#
499 // STVX VAL, 0, R0
500 //
501 // FIXME: We use R0 here, because it isn't available for RA.
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000502 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::ADDI), PPC::R0),
Owen Anderson81875432008-01-01 21:11:32 +0000503 FrameIdx, 0, 0));
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000504 NewMIs.push_back(BuildMI(MF, DL, get(PPC::STVX))
Bill Wendling2b739762009-05-13 21:33:08 +0000505 .addReg(SrcReg, getKillRegState(isKill))
506 .addReg(PPC::R0)
507 .addReg(PPC::R0));
Owen Anderson81875432008-01-01 21:11:32 +0000508 } else {
Edwin Törökbd448e32009-07-14 16:55:14 +0000509 llvm_unreachable("Unknown regclass!");
Owen Anderson81875432008-01-01 21:11:32 +0000510 }
Bill Wendlinga1877c52008-03-03 22:19:16 +0000511
512 return false;
Owen Anderson81875432008-01-01 21:11:32 +0000513}
514
515void
516PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Bill Wendlinga1877c52008-03-03 22:19:16 +0000517 MachineBasicBlock::iterator MI,
518 unsigned SrcReg, bool isKill, int FrameIdx,
Evan Cheng1f8534d2010-05-06 19:06:44 +0000519 const TargetRegisterClass *RC,
520 const TargetRegisterInfo *TRI) const {
Dan Gohman221a4372008-07-07 23:14:23 +0000521 MachineFunction &MF = *MBB.getParent();
Owen Anderson81875432008-01-01 21:11:32 +0000522 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendlinga1877c52008-03-03 22:19:16 +0000523
Dan Gohman221a4372008-07-07 23:14:23 +0000524 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs)) {
525 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Bill Wendlinga1877c52008-03-03 22:19:16 +0000526 FuncInfo->setSpillsCR();
527 }
528
Owen Anderson81875432008-01-01 21:11:32 +0000529 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
530 MBB.insert(MI, NewMIs[i]);
531}
532
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000533void
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000534PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL,
Dan Gohman221a4372008-07-07 23:14:23 +0000535 unsigned DestReg, int FrameIdx,
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000536 const TargetRegisterClass *RC,
537 SmallVectorImpl<MachineInstr*> &NewMIs)const{
Owen Anderson81875432008-01-01 21:11:32 +0000538 if (RC == PPC::GPRCRegisterClass) {
539 if (DestReg != PPC::LR) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000540 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
541 DestReg), FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000542 } else {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000543 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
544 PPC::R11), FrameIdx));
545 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTLR)).addReg(PPC::R11));
Owen Anderson81875432008-01-01 21:11:32 +0000546 }
547 } else if (RC == PPC::G8RCRegisterClass) {
548 if (DestReg != PPC::LR8) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000549 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg),
Owen Anderson81875432008-01-01 21:11:32 +0000550 FrameIdx));
551 } else {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000552 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD),
553 PPC::R11), FrameIdx));
554 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTLR8)).addReg(PPC::R11));
Owen Anderson81875432008-01-01 21:11:32 +0000555 }
556 } else if (RC == PPC::F8RCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000557 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg),
Owen Anderson81875432008-01-01 21:11:32 +0000558 FrameIdx));
559 } else if (RC == PPC::F4RCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000560 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg),
Owen Anderson81875432008-01-01 21:11:32 +0000561 FrameIdx));
562 } else if (RC == PPC::CRRCRegisterClass) {
Dale Johannesenb000c482010-02-12 21:35:34 +0000563 // FIXME: We need a scatch reg here. The trouble with using R0 is that
564 // it's possible for the stack frame to be so big the save location is
565 // out of range of immediate offsets, necessitating another register.
566 // We hack this on Darwin by reserving R2. It's probably broken on Linux
567 // at the moment.
568 unsigned ScratchReg = TM.getSubtargetImpl()->isDarwinABI() ?
569 PPC::R2 : PPC::R0;
570 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
571 ScratchReg), FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000572
573 // If the reloaded register isn't CR0, shift the bits right so that they are
574 // in the right CR's slot.
575 if (DestReg != PPC::CR0) {
576 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(DestReg)*4;
577 // rlwinm r11, r11, 32-ShiftBits, 0, 31.
Dale Johannesenb000c482010-02-12 21:35:34 +0000578 NewMIs.push_back(BuildMI(MF, DL, get(PPC::RLWINM), ScratchReg)
579 .addReg(ScratchReg).addImm(32-ShiftBits).addImm(0)
580 .addImm(31));
Owen Anderson81875432008-01-01 21:11:32 +0000581 }
582
Dale Johannesenb000c482010-02-12 21:35:34 +0000583 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTCRF), DestReg)
584 .addReg(ScratchReg));
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000585 } else if (RC == PPC::CRBITRCRegisterClass) {
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000586
587 unsigned Reg = 0;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000588 if (DestReg == PPC::CR0LT || DestReg == PPC::CR0GT ||
589 DestReg == PPC::CR0EQ || DestReg == PPC::CR0UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000590 Reg = PPC::CR0;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000591 else if (DestReg == PPC::CR1LT || DestReg == PPC::CR1GT ||
592 DestReg == PPC::CR1EQ || DestReg == PPC::CR1UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000593 Reg = PPC::CR1;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000594 else if (DestReg == PPC::CR2LT || DestReg == PPC::CR2GT ||
595 DestReg == PPC::CR2EQ || DestReg == PPC::CR2UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000596 Reg = PPC::CR2;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000597 else if (DestReg == PPC::CR3LT || DestReg == PPC::CR3GT ||
598 DestReg == PPC::CR3EQ || DestReg == PPC::CR3UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000599 Reg = PPC::CR3;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000600 else if (DestReg == PPC::CR4LT || DestReg == PPC::CR4GT ||
601 DestReg == PPC::CR4EQ || DestReg == PPC::CR4UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000602 Reg = PPC::CR4;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000603 else if (DestReg == PPC::CR5LT || DestReg == PPC::CR5GT ||
604 DestReg == PPC::CR5EQ || DestReg == PPC::CR5UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000605 Reg = PPC::CR5;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000606 else if (DestReg == PPC::CR6LT || DestReg == PPC::CR6GT ||
607 DestReg == PPC::CR6EQ || DestReg == PPC::CR6UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000608 Reg = PPC::CR6;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000609 else if (DestReg == PPC::CR7LT || DestReg == PPC::CR7GT ||
610 DestReg == PPC::CR7EQ || DestReg == PPC::CR7UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000611 Reg = PPC::CR7;
612
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000613 return LoadRegFromStackSlot(MF, DL, Reg, FrameIdx,
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000614 PPC::CRRCRegisterClass, NewMIs);
615
Owen Anderson81875432008-01-01 21:11:32 +0000616 } else if (RC == PPC::VRRCRegisterClass) {
617 // We don't have indexed addressing for vector loads. Emit:
618 // R0 = ADDI FI#
619 // Dest = LVX 0, R0
620 //
621 // FIXME: We use R0 here, because it isn't available for RA.
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000622 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::ADDI), PPC::R0),
Owen Anderson81875432008-01-01 21:11:32 +0000623 FrameIdx, 0, 0));
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000624 NewMIs.push_back(BuildMI(MF, DL, get(PPC::LVX),DestReg).addReg(PPC::R0)
Owen Anderson81875432008-01-01 21:11:32 +0000625 .addReg(PPC::R0));
626 } else {
Edwin Törökbd448e32009-07-14 16:55:14 +0000627 llvm_unreachable("Unknown regclass!");
Owen Anderson81875432008-01-01 21:11:32 +0000628 }
629}
630
631void
632PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Bill Wendlinga1877c52008-03-03 22:19:16 +0000633 MachineBasicBlock::iterator MI,
634 unsigned DestReg, int FrameIdx,
Evan Cheng1f8534d2010-05-06 19:06:44 +0000635 const TargetRegisterClass *RC,
636 const TargetRegisterInfo *TRI) 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}