blob: 87c612ab74e67997f05e465b7377c735a230a7ec [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- PPCInstrInfo.cpp - PowerPC32 Instruction Information -----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the PowerPC implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "PPCInstrInfo.h"
Owen Anderson81875432008-01-01 21:11:32 +000015#include "PPCInstrBuilder.h"
Bill Wendlinga1877c52008-03-03 22:19:16 +000016#include "PPCMachineFunctionInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000017#include "PPCPredicates.h"
18#include "PPCGenInstrInfo.inc"
19#include "PPCTargetMachine.h"
Owen Anderson1636de92007-09-07 04:06:50 +000020#include "llvm/ADT/STLExtras.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include "llvm/CodeGen/MachineInstrBuilder.h"
Bill Wendling03598502008-03-04 23:13:51 +000022#include "llvm/Support/CommandLine.h"
Nicolas Geoffraycb162a02008-04-16 20:10:13 +000023#include "llvm/Target/TargetAsmInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024using namespace llvm;
25
Bill Wendling4eaadfb2008-03-10 22:49:16 +000026extern cl::opt<bool> EnablePPC32RS; // FIXME (64-bit): See PPCRegisterInfo.cpp.
27extern cl::opt<bool> EnablePPC64RS; // FIXME (64-bit): See PPCRegisterInfo.cpp.
Bill Wendling03598502008-03-04 23:13:51 +000028
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029PPCInstrInfo::PPCInstrInfo(PPCTargetMachine &tm)
Chris Lattnerd2fd6db2008-01-01 01:03:04 +000030 : TargetInstrInfoImpl(PPCInsts, array_lengthof(PPCInsts)), TM(tm),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031 RI(*TM.getSubtargetImpl(), *this) {}
32
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033bool PPCInstrInfo::isMoveInstr(const MachineInstr& MI,
34 unsigned& sourceReg,
Evan Chengf97496a2009-01-20 19:12:24 +000035 unsigned& destReg,
36 unsigned& sourceSubIdx,
37 unsigned& destSubIdx) const {
38 sourceSubIdx = destSubIdx = 0; // No sub-registers.
39
Chris Lattner99aa3372008-01-07 02:48:55 +000040 unsigned oc = MI.getOpcode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041 if (oc == PPC::OR || oc == PPC::OR8 || oc == PPC::VOR ||
42 oc == PPC::OR4To8 || oc == PPC::OR8To4) { // or r1, r2, r2
43 assert(MI.getNumOperands() >= 3 &&
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000044 MI.getOperand(0).isReg() &&
45 MI.getOperand(1).isReg() &&
46 MI.getOperand(2).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047 "invalid PPC OR instruction!");
48 if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
49 sourceReg = MI.getOperand(1).getReg();
50 destReg = MI.getOperand(0).getReg();
51 return true;
52 }
53 } else if (oc == PPC::ADDI) { // addi r1, r2, 0
54 assert(MI.getNumOperands() >= 3 &&
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000055 MI.getOperand(0).isReg() &&
56 MI.getOperand(2).isImm() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057 "invalid PPC ADDI instruction!");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000058 if (MI.getOperand(1).isReg() && MI.getOperand(2).getImm() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059 sourceReg = MI.getOperand(1).getReg();
60 destReg = MI.getOperand(0).getReg();
61 return true;
62 }
63 } else if (oc == PPC::ORI) { // ori r1, r2, 0
64 assert(MI.getNumOperands() >= 3 &&
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000065 MI.getOperand(0).isReg() &&
66 MI.getOperand(1).isReg() &&
67 MI.getOperand(2).isImm() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068 "invalid PPC ORI instruction!");
Chris Lattnera96056a2007-12-30 20:49:49 +000069 if (MI.getOperand(2).getImm() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070 sourceReg = MI.getOperand(1).getReg();
71 destReg = MI.getOperand(0).getReg();
72 return true;
73 }
74 } else if (oc == PPC::FMRS || oc == PPC::FMRD ||
75 oc == PPC::FMRSD) { // fmr r1, r2
76 assert(MI.getNumOperands() >= 2 &&
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000077 MI.getOperand(0).isReg() &&
78 MI.getOperand(1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079 "invalid PPC FMR instruction");
80 sourceReg = MI.getOperand(1).getReg();
81 destReg = MI.getOperand(0).getReg();
82 return true;
83 } else if (oc == PPC::MCRF) { // mcrf cr1, cr2
84 assert(MI.getNumOperands() >= 2 &&
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000085 MI.getOperand(0).isReg() &&
86 MI.getOperand(1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087 "invalid PPC MCRF instruction");
88 sourceReg = MI.getOperand(1).getReg();
89 destReg = MI.getOperand(0).getReg();
90 return true;
91 }
92 return false;
93}
94
Dan Gohman90feee22008-11-18 19:49:32 +000095unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096 int &FrameIndex) const {
97 switch (MI->getOpcode()) {
98 default: break;
99 case PPC::LD:
100 case PPC::LWZ:
101 case PPC::LFS:
102 case PPC::LFD:
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000103 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
104 MI->getOperand(2).isFI()) {
Chris Lattner6017d482007-12-30 23:10:15 +0000105 FrameIndex = MI->getOperand(2).getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106 return MI->getOperand(0).getReg();
107 }
108 break;
109 }
110 return 0;
111}
112
Dan Gohman90feee22008-11-18 19:49:32 +0000113unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114 int &FrameIndex) const {
115 switch (MI->getOpcode()) {
116 default: break;
117 case PPC::STD:
118 case PPC::STW:
119 case PPC::STFS:
120 case PPC::STFD:
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000121 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
122 MI->getOperand(2).isFI()) {
Chris Lattner6017d482007-12-30 23:10:15 +0000123 FrameIndex = MI->getOperand(2).getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124 return MI->getOperand(0).getReg();
125 }
126 break;
127 }
128 return 0;
129}
130
131// commuteInstruction - We can commute rlwimi instructions, but only if the
132// rotate amt is zero. We also have to munge the immediates a bit.
Evan Cheng5de1aaf2008-06-16 07:33:11 +0000133MachineInstr *
134PPCInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
Dan Gohman221a4372008-07-07 23:14:23 +0000135 MachineFunction &MF = *MI->getParent()->getParent();
136
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137 // Normal instructions can be commuted the obvious way.
138 if (MI->getOpcode() != PPC::RLWIMI)
Evan Cheng5de1aaf2008-06-16 07:33:11 +0000139 return TargetInstrInfoImpl::commuteInstruction(MI, NewMI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140
141 // Cannot commute if it has a non-zero rotate count.
Chris Lattnera96056a2007-12-30 20:49:49 +0000142 if (MI->getOperand(3).getImm() != 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000143 return 0;
144
145 // If we have a zero rotate count, we have:
146 // M = mask(MB,ME)
147 // Op0 = (Op1 & ~M) | (Op2 & M)
148 // Change this to:
149 // M = mask((ME+1)&31, (MB-1)&31)
150 // Op0 = (Op2 & ~M) | (Op1 & M)
151
152 // Swap op1/op2
Evan Chengb554e532008-02-13 02:46:49 +0000153 unsigned Reg0 = MI->getOperand(0).getReg();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154 unsigned Reg1 = MI->getOperand(1).getReg();
155 unsigned Reg2 = MI->getOperand(2).getReg();
156 bool Reg1IsKill = MI->getOperand(1).isKill();
157 bool Reg2IsKill = MI->getOperand(2).isKill();
Evan Cheng5de1aaf2008-06-16 07:33:11 +0000158 bool ChangeReg0 = false;
Evan Chengb554e532008-02-13 02:46:49 +0000159 // If machine instrs are no longer in two-address forms, update
160 // destination register as well.
161 if (Reg0 == Reg1) {
162 // Must be two address instruction!
163 assert(MI->getDesc().getOperandConstraint(0, TOI::TIED_TO) &&
164 "Expecting a two-address instruction!");
Evan Chengb554e532008-02-13 02:46:49 +0000165 Reg2IsKill = false;
Evan Cheng5de1aaf2008-06-16 07:33:11 +0000166 ChangeReg0 = true;
Evan Chengb554e532008-02-13 02:46:49 +0000167 }
Evan Cheng5de1aaf2008-06-16 07:33:11 +0000168
169 // Masks.
170 unsigned MB = MI->getOperand(4).getImm();
171 unsigned ME = MI->getOperand(5).getImm();
172
173 if (NewMI) {
174 // Create a new instruction.
175 unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg();
176 bool Reg0IsDead = MI->getOperand(0).isDead();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000177 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
Bill Wendling2b739762009-05-13 21:33:08 +0000178 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
179 .addReg(Reg2, getKillRegState(Reg2IsKill))
180 .addReg(Reg1, getKillRegState(Reg1IsKill))
Evan Cheng5de1aaf2008-06-16 07:33:11 +0000181 .addImm((ME+1) & 31)
182 .addImm((MB-1) & 31);
183 }
184
185 if (ChangeReg0)
186 MI->getOperand(0).setReg(Reg2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 MI->getOperand(2).setReg(Reg1);
188 MI->getOperand(1).setReg(Reg2);
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000189 MI->getOperand(2).setIsKill(Reg1IsKill);
190 MI->getOperand(1).setIsKill(Reg2IsKill);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191
192 // Swap the mask around.
Chris Lattnera96056a2007-12-30 20:49:49 +0000193 MI->getOperand(4).setImm((ME+1) & 31);
194 MI->getOperand(5).setImm((MB-1) & 31);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195 return MI;
196}
197
198void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
199 MachineBasicBlock::iterator MI) const {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000200 DebugLoc DL = DebugLoc::getUnknownLoc();
201 if (MI != MBB.end()) DL = MI->getDebugLoc();
202
203 BuildMI(MBB, MI, DL, get(PPC::NOP));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000204}
205
206
207// Branch analysis.
208bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
209 MachineBasicBlock *&FBB,
Evan Chengeac31642009-02-09 07:14:22 +0000210 SmallVectorImpl<MachineOperand> &Cond,
211 bool AllowModify) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212 // If the block has no terminators, it just falls into the block after it.
213 MachineBasicBlock::iterator I = MBB.end();
214 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
215 return false;
216
217 // Get the last instruction in the block.
218 MachineInstr *LastInst = I;
219
220 // If there is only one terminator instruction, process it.
221 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
222 if (LastInst->getOpcode() == PPC::B) {
Evan Cheng6ed927b2009-05-08 23:09:25 +0000223 if (!LastInst->getOperand(0).isMBB())
224 return true;
Chris Lattner6017d482007-12-30 23:10:15 +0000225 TBB = LastInst->getOperand(0).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226 return false;
227 } else if (LastInst->getOpcode() == PPC::BCC) {
Evan Cheng6ed927b2009-05-08 23:09:25 +0000228 if (!LastInst->getOperand(2).isMBB())
229 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230 // Block ends with fall-through condbranch.
Chris Lattner6017d482007-12-30 23:10:15 +0000231 TBB = LastInst->getOperand(2).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 Cond.push_back(LastInst->getOperand(0));
233 Cond.push_back(LastInst->getOperand(1));
234 return false;
235 }
236 // Otherwise, don't know what this is.
237 return true;
238 }
239
240 // Get the instruction before it if it's a terminator.
241 MachineInstr *SecondLastInst = I;
242
243 // If there are three terminators, we don't know what sort of block this is.
244 if (SecondLastInst && I != MBB.begin() &&
245 isUnpredicatedTerminator(--I))
246 return true;
247
248 // If the block ends with PPC::B and PPC:BCC, handle it.
249 if (SecondLastInst->getOpcode() == PPC::BCC &&
250 LastInst->getOpcode() == PPC::B) {
Evan Cheng6ed927b2009-05-08 23:09:25 +0000251 if (!SecondLastInst->getOperand(2).isMBB() ||
252 !LastInst->getOperand(0).isMBB())
253 return true;
Chris Lattner6017d482007-12-30 23:10:15 +0000254 TBB = SecondLastInst->getOperand(2).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 Cond.push_back(SecondLastInst->getOperand(0));
256 Cond.push_back(SecondLastInst->getOperand(1));
Chris Lattner6017d482007-12-30 23:10:15 +0000257 FBB = LastInst->getOperand(0).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000258 return false;
259 }
260
261 // If the block ends with two PPC:Bs, handle it. The second one is not
262 // executed, so remove it.
263 if (SecondLastInst->getOpcode() == PPC::B &&
264 LastInst->getOpcode() == PPC::B) {
Evan Cheng6ed927b2009-05-08 23:09:25 +0000265 if (!SecondLastInst->getOperand(0).isMBB())
266 return true;
Chris Lattner6017d482007-12-30 23:10:15 +0000267 TBB = SecondLastInst->getOperand(0).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000268 I = LastInst;
Evan Chengeac31642009-02-09 07:14:22 +0000269 if (AllowModify)
270 I->eraseFromParent();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000271 return false;
272 }
273
274 // Otherwise, can't handle this.
275 return true;
276}
277
278unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
279 MachineBasicBlock::iterator I = MBB.end();
280 if (I == MBB.begin()) return 0;
281 --I;
282 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC)
283 return 0;
284
285 // Remove the branch.
286 I->eraseFromParent();
287
288 I = MBB.end();
289
290 if (I == MBB.begin()) return 1;
291 --I;
292 if (I->getOpcode() != PPC::BCC)
293 return 1;
294
295 // Remove the branch.
296 I->eraseFromParent();
297 return 2;
298}
299
300unsigned
301PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
302 MachineBasicBlock *FBB,
Owen Andersond131b5b2008-08-14 22:49:33 +0000303 const SmallVectorImpl<MachineOperand> &Cond) const {
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000304 // FIXME this should probably have a DebugLoc argument
305 DebugLoc dl = DebugLoc::getUnknownLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000306 // Shouldn't be a fall through.
307 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
308 assert((Cond.size() == 2 || Cond.size() == 0) &&
309 "PPC branch conditions have two components!");
310
311 // One-way branch.
312 if (FBB == 0) {
313 if (Cond.empty()) // Unconditional branch
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000314 BuildMI(&MBB, dl, get(PPC::B)).addMBB(TBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000315 else // Conditional branch
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000316 BuildMI(&MBB, dl, get(PPC::BCC))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000317 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
318 return 1;
319 }
320
321 // Two-way Conditional Branch.
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000322 BuildMI(&MBB, dl, get(PPC::BCC))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000323 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000324 BuildMI(&MBB, dl, get(PPC::B)).addMBB(FBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000325 return 2;
326}
327
Owen Anderson9fa72d92008-08-26 18:03:31 +0000328bool PPCInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
Owen Anderson8f2c8932007-12-31 06:32:00 +0000329 MachineBasicBlock::iterator MI,
330 unsigned DestReg, unsigned SrcReg,
331 const TargetRegisterClass *DestRC,
332 const TargetRegisterClass *SrcRC) const {
333 if (DestRC != SrcRC) {
Owen Anderson9fa72d92008-08-26 18:03:31 +0000334 // Not yet supported!
335 return false;
Owen Anderson8f2c8932007-12-31 06:32:00 +0000336 }
337
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000338 DebugLoc DL = DebugLoc::getUnknownLoc();
339 if (MI != MBB.end()) DL = MI->getDebugLoc();
340
Owen Anderson8f2c8932007-12-31 06:32:00 +0000341 if (DestRC == PPC::GPRCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000342 BuildMI(MBB, MI, DL, get(PPC::OR), DestReg).addReg(SrcReg).addReg(SrcReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000343 } else if (DestRC == PPC::G8RCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000344 BuildMI(MBB, MI, DL, get(PPC::OR8), DestReg).addReg(SrcReg).addReg(SrcReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000345 } else if (DestRC == PPC::F4RCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000346 BuildMI(MBB, MI, DL, get(PPC::FMRS), DestReg).addReg(SrcReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000347 } else if (DestRC == PPC::F8RCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000348 BuildMI(MBB, MI, DL, get(PPC::FMRD), DestReg).addReg(SrcReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000349 } else if (DestRC == PPC::CRRCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000350 BuildMI(MBB, MI, DL, get(PPC::MCRF), DestReg).addReg(SrcReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000351 } else if (DestRC == PPC::VRRCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000352 BuildMI(MBB, MI, DL, get(PPC::VOR), DestReg).addReg(SrcReg).addReg(SrcReg);
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000353 } else if (DestRC == PPC::CRBITRCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000354 BuildMI(MBB, MI, DL, get(PPC::CROR), DestReg).addReg(SrcReg).addReg(SrcReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000355 } else {
Owen Anderson9fa72d92008-08-26 18:03:31 +0000356 // Attempt to copy register that is not GPR or FPR
357 return false;
Owen Anderson8f2c8932007-12-31 06:32:00 +0000358 }
Owen Anderson9fa72d92008-08-26 18:03:31 +0000359
360 return true;
Owen Anderson8f2c8932007-12-31 06:32:00 +0000361}
362
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000363bool
Dan Gohman221a4372008-07-07 23:14:23 +0000364PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
365 unsigned SrcReg, bool isKill,
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000366 int FrameIdx,
367 const TargetRegisterClass *RC,
368 SmallVectorImpl<MachineInstr*> &NewMIs) const{
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000369 DebugLoc DL = DebugLoc::getUnknownLoc();
Owen Anderson81875432008-01-01 21:11:32 +0000370 if (RC == PPC::GPRCRegisterClass) {
371 if (SrcReg != PPC::LR) {
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000372 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
Bill Wendling2b739762009-05-13 21:33:08 +0000373 .addReg(SrcReg,
374 getKillRegState(isKill)),
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000375 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000376 } else {
377 // FIXME: this spills LR immediately to memory in one step. To do this,
378 // we use R11, which we know cannot be used in the prolog/epilog. This is
379 // a hack.
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000380 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFLR), PPC::R11));
381 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
Bill Wendling2b739762009-05-13 21:33:08 +0000382 .addReg(PPC::R11,
383 getKillRegState(isKill)),
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000384 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000385 }
386 } else if (RC == PPC::G8RCRegisterClass) {
387 if (SrcReg != PPC::LR8) {
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000388 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
Bill Wendling2b739762009-05-13 21:33:08 +0000389 .addReg(SrcReg,
390 getKillRegState(isKill)),
391 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000392 } else {
393 // FIXME: this spills LR immediately to memory in one step. To do this,
394 // we use R11, which we know cannot be used in the prolog/epilog. This is
395 // a hack.
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000396 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFLR8), PPC::X11));
397 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
Bill Wendling2b739762009-05-13 21:33:08 +0000398 .addReg(PPC::X11,
399 getKillRegState(isKill)),
400 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000401 }
402 } else if (RC == PPC::F8RCRegisterClass) {
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000403 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD))
Bill Wendling2b739762009-05-13 21:33:08 +0000404 .addReg(SrcReg,
405 getKillRegState(isKill)),
406 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000407 } else if (RC == PPC::F4RCRegisterClass) {
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000408 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS))
Bill Wendling2b739762009-05-13 21:33:08 +0000409 .addReg(SrcReg,
410 getKillRegState(isKill)),
411 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000412 } else if (RC == PPC::CRRCRegisterClass) {
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000413 if ((EnablePPC32RS && !TM.getSubtargetImpl()->isPPC64()) ||
414 (EnablePPC64RS && TM.getSubtargetImpl()->isPPC64())) {
415 // FIXME (64-bit): Enable
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000416 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR))
Bill Wendling2b739762009-05-13 21:33:08 +0000417 .addReg(SrcReg,
418 getKillRegState(isKill)),
Chris Lattner6734c3a2008-03-20 01:22:40 +0000419 FrameIdx));
Bill Wendlinga1877c52008-03-03 22:19:16 +0000420 return true;
421 } else {
422 // FIXME: We use R0 here, because it isn't available for RA. We need to
423 // store the CR in the low 4-bits of the saved value. First, issue a MFCR
424 // to save all of the CRBits.
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000425 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFCR), PPC::R0));
Owen Anderson81875432008-01-01 21:11:32 +0000426
Bill Wendlinga1877c52008-03-03 22:19:16 +0000427 // If the saved register wasn't CR0, shift the bits left so that they are
428 // in CR0's slot.
429 if (SrcReg != PPC::CR0) {
430 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(SrcReg)*4;
431 // rlwinm r0, r0, ShiftBits, 0, 31.
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000432 NewMIs.push_back(BuildMI(MF, DL, get(PPC::RLWINM), PPC::R0)
Chris Lattner7b7371c2008-03-10 18:55:53 +0000433 .addReg(PPC::R0).addImm(ShiftBits).addImm(0).addImm(31));
Bill Wendlinga1877c52008-03-03 22:19:16 +0000434 }
435
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000436 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
Bill Wendling2b739762009-05-13 21:33:08 +0000437 .addReg(PPC::R0,
438 getKillRegState(isKill)),
Bill Wendlinga1877c52008-03-03 22:19:16 +0000439 FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000440 }
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000441 } else if (RC == PPC::CRBITRCRegisterClass) {
442 // FIXME: We use CRi here because there is no mtcrf on a bit. Since the
443 // backend currently only uses CR1EQ as an individual bit, this should
444 // not cause any bug. If we need other uses of CR bits, the following
445 // code may be invalid.
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000446 unsigned Reg = 0;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000447 if (SrcReg == PPC::CR0LT || SrcReg == PPC::CR0GT ||
448 SrcReg == PPC::CR0EQ || SrcReg == PPC::CR0UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000449 Reg = PPC::CR0;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000450 else if (SrcReg == PPC::CR1LT || SrcReg == PPC::CR1GT ||
451 SrcReg == PPC::CR1EQ || SrcReg == PPC::CR1UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000452 Reg = PPC::CR1;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000453 else if (SrcReg == PPC::CR2LT || SrcReg == PPC::CR2GT ||
454 SrcReg == PPC::CR2EQ || SrcReg == PPC::CR2UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000455 Reg = PPC::CR2;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000456 else if (SrcReg == PPC::CR3LT || SrcReg == PPC::CR3GT ||
457 SrcReg == PPC::CR3EQ || SrcReg == PPC::CR3UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000458 Reg = PPC::CR3;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000459 else if (SrcReg == PPC::CR4LT || SrcReg == PPC::CR4GT ||
460 SrcReg == PPC::CR4EQ || SrcReg == PPC::CR4UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000461 Reg = PPC::CR4;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000462 else if (SrcReg == PPC::CR5LT || SrcReg == PPC::CR5GT ||
463 SrcReg == PPC::CR5EQ || SrcReg == PPC::CR5UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000464 Reg = PPC::CR5;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000465 else if (SrcReg == PPC::CR6LT || SrcReg == PPC::CR6GT ||
466 SrcReg == PPC::CR6EQ || SrcReg == PPC::CR6UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000467 Reg = PPC::CR6;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000468 else if (SrcReg == PPC::CR7LT || SrcReg == PPC::CR7GT ||
469 SrcReg == PPC::CR7EQ || SrcReg == PPC::CR7UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000470 Reg = PPC::CR7;
471
Dan Gohman221a4372008-07-07 23:14:23 +0000472 return StoreRegToStackSlot(MF, Reg, isKill, FrameIdx,
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000473 PPC::CRRCRegisterClass, NewMIs);
474
Owen Anderson81875432008-01-01 21:11:32 +0000475 } else if (RC == PPC::VRRCRegisterClass) {
476 // We don't have indexed addressing for vector loads. Emit:
477 // R0 = ADDI FI#
478 // STVX VAL, 0, R0
479 //
480 // FIXME: We use R0 here, because it isn't available for RA.
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000481 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::ADDI), PPC::R0),
Owen Anderson81875432008-01-01 21:11:32 +0000482 FrameIdx, 0, 0));
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000483 NewMIs.push_back(BuildMI(MF, DL, get(PPC::STVX))
Bill Wendling2b739762009-05-13 21:33:08 +0000484 .addReg(SrcReg, getKillRegState(isKill))
485 .addReg(PPC::R0)
486 .addReg(PPC::R0));
Owen Anderson81875432008-01-01 21:11:32 +0000487 } else {
488 assert(0 && "Unknown regclass!");
489 abort();
490 }
Bill Wendlinga1877c52008-03-03 22:19:16 +0000491
492 return false;
Owen Anderson81875432008-01-01 21:11:32 +0000493}
494
495void
496PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Bill Wendlinga1877c52008-03-03 22:19:16 +0000497 MachineBasicBlock::iterator MI,
498 unsigned SrcReg, bool isKill, int FrameIdx,
499 const TargetRegisterClass *RC) const {
Dan Gohman221a4372008-07-07 23:14:23 +0000500 MachineFunction &MF = *MBB.getParent();
Owen Anderson81875432008-01-01 21:11:32 +0000501 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendlinga1877c52008-03-03 22:19:16 +0000502
Dan Gohman221a4372008-07-07 23:14:23 +0000503 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs)) {
504 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Bill Wendlinga1877c52008-03-03 22:19:16 +0000505 FuncInfo->setSpillsCR();
506 }
507
Owen Anderson81875432008-01-01 21:11:32 +0000508 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
509 MBB.insert(MI, NewMIs[i]);
510}
511
512void PPCInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
Bill Wendlinga1877c52008-03-03 22:19:16 +0000513 bool isKill,
514 SmallVectorImpl<MachineOperand> &Addr,
515 const TargetRegisterClass *RC,
516 SmallVectorImpl<MachineInstr*> &NewMIs) const{
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000517 if (Addr[0].isFI()) {
Dan Gohman221a4372008-07-07 23:14:23 +0000518 if (StoreRegToStackSlot(MF, SrcReg, isKill,
519 Addr[0].getIndex(), RC, NewMIs)) {
Bill Wendlinga1877c52008-03-03 22:19:16 +0000520 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
521 FuncInfo->setSpillsCR();
522 }
523
Owen Anderson81875432008-01-01 21:11:32 +0000524 return;
525 }
526
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000527 DebugLoc DL = DebugLoc::getUnknownLoc();
Owen Anderson81875432008-01-01 21:11:32 +0000528 unsigned Opc = 0;
529 if (RC == PPC::GPRCRegisterClass) {
530 Opc = PPC::STW;
531 } else if (RC == PPC::G8RCRegisterClass) {
532 Opc = PPC::STD;
533 } else if (RC == PPC::F8RCRegisterClass) {
534 Opc = PPC::STFD;
535 } else if (RC == PPC::F4RCRegisterClass) {
536 Opc = PPC::STFS;
537 } else if (RC == PPC::VRRCRegisterClass) {
538 Opc = PPC::STVX;
539 } else {
540 assert(0 && "Unknown regclass!");
541 abort();
542 }
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000543 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Opc))
Bill Wendling2b739762009-05-13 21:33:08 +0000544 .addReg(SrcReg, getKillRegState(isKill));
Dan Gohmanc909bbb2009-02-18 05:45:50 +0000545 for (unsigned i = 0, e = Addr.size(); i != e; ++i)
546 MIB.addOperand(Addr[i]);
Owen Anderson81875432008-01-01 21:11:32 +0000547 NewMIs.push_back(MIB);
548 return;
549}
550
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000551void
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000552PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL,
Dan Gohman221a4372008-07-07 23:14:23 +0000553 unsigned DestReg, int FrameIdx,
Bill Wendling4eaadfb2008-03-10 22:49:16 +0000554 const TargetRegisterClass *RC,
555 SmallVectorImpl<MachineInstr*> &NewMIs)const{
Owen Anderson81875432008-01-01 21:11:32 +0000556 if (RC == PPC::GPRCRegisterClass) {
557 if (DestReg != PPC::LR) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000558 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
559 DestReg), FrameIdx));
Owen Anderson81875432008-01-01 21:11:32 +0000560 } else {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000561 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
562 PPC::R11), FrameIdx));
563 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTLR)).addReg(PPC::R11));
Owen Anderson81875432008-01-01 21:11:32 +0000564 }
565 } else if (RC == PPC::G8RCRegisterClass) {
566 if (DestReg != PPC::LR8) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000567 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg),
Owen Anderson81875432008-01-01 21:11:32 +0000568 FrameIdx));
569 } else {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000570 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD),
571 PPC::R11), FrameIdx));
572 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTLR8)).addReg(PPC::R11));
Owen Anderson81875432008-01-01 21:11:32 +0000573 }
574 } else if (RC == PPC::F8RCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000575 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg),
Owen Anderson81875432008-01-01 21:11:32 +0000576 FrameIdx));
577 } else if (RC == PPC::F4RCRegisterClass) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000578 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg),
Owen Anderson81875432008-01-01 21:11:32 +0000579 FrameIdx));
580 } else if (RC == PPC::CRRCRegisterClass) {
581 // FIXME: We use R0 here, because it isn't available for RA.
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000582 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ), PPC::R0),
Owen Anderson81875432008-01-01 21:11:32 +0000583 FrameIdx));
584
585 // If the reloaded register isn't CR0, shift the bits right so that they are
586 // in the right CR's slot.
587 if (DestReg != PPC::CR0) {
588 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(DestReg)*4;
589 // rlwinm r11, r11, 32-ShiftBits, 0, 31.
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000590 NewMIs.push_back(BuildMI(MF, DL, get(PPC::RLWINM), PPC::R0)
Owen Anderson81875432008-01-01 21:11:32 +0000591 .addReg(PPC::R0).addImm(32-ShiftBits).addImm(0).addImm(31));
592 }
593
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000594 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTCRF), DestReg).addReg(PPC::R0));
Nicolas Geoffrayd01feb22008-03-10 14:12:10 +0000595 } else if (RC == PPC::CRBITRCRegisterClass) {
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000596
597 unsigned Reg = 0;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000598 if (DestReg == PPC::CR0LT || DestReg == PPC::CR0GT ||
599 DestReg == PPC::CR0EQ || DestReg == PPC::CR0UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000600 Reg = PPC::CR0;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000601 else if (DestReg == PPC::CR1LT || DestReg == PPC::CR1GT ||
602 DestReg == PPC::CR1EQ || DestReg == PPC::CR1UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000603 Reg = PPC::CR1;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000604 else if (DestReg == PPC::CR2LT || DestReg == PPC::CR2GT ||
605 DestReg == PPC::CR2EQ || DestReg == PPC::CR2UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000606 Reg = PPC::CR2;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000607 else if (DestReg == PPC::CR3LT || DestReg == PPC::CR3GT ||
608 DestReg == PPC::CR3EQ || DestReg == PPC::CR3UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000609 Reg = PPC::CR3;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000610 else if (DestReg == PPC::CR4LT || DestReg == PPC::CR4GT ||
611 DestReg == PPC::CR4EQ || DestReg == PPC::CR4UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000612 Reg = PPC::CR4;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000613 else if (DestReg == PPC::CR5LT || DestReg == PPC::CR5GT ||
614 DestReg == PPC::CR5EQ || DestReg == PPC::CR5UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000615 Reg = PPC::CR5;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000616 else if (DestReg == PPC::CR6LT || DestReg == PPC::CR6GT ||
617 DestReg == PPC::CR6EQ || DestReg == PPC::CR6UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000618 Reg = PPC::CR6;
Tilmann Schelleradb669f2009-07-03 06:47:55 +0000619 else if (DestReg == PPC::CR7LT || DestReg == PPC::CR7GT ||
620 DestReg == PPC::CR7EQ || DestReg == PPC::CR7UN)
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000621 Reg = PPC::CR7;
622
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000623 return LoadRegFromStackSlot(MF, DL, Reg, FrameIdx,
Nicolas Geoffray51ed7a32008-03-10 17:46:45 +0000624 PPC::CRRCRegisterClass, NewMIs);
625
Owen Anderson81875432008-01-01 21:11:32 +0000626 } else if (RC == PPC::VRRCRegisterClass) {
627 // We don't have indexed addressing for vector loads. Emit:
628 // R0 = ADDI FI#
629 // Dest = LVX 0, R0
630 //
631 // FIXME: We use R0 here, because it isn't available for RA.
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000632 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::ADDI), PPC::R0),
Owen Anderson81875432008-01-01 21:11:32 +0000633 FrameIdx, 0, 0));
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000634 NewMIs.push_back(BuildMI(MF, DL, get(PPC::LVX),DestReg).addReg(PPC::R0)
Owen Anderson81875432008-01-01 21:11:32 +0000635 .addReg(PPC::R0));
636 } else {
637 assert(0 && "Unknown regclass!");
638 abort();
639 }
640}
641
642void
643PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Bill Wendlinga1877c52008-03-03 22:19:16 +0000644 MachineBasicBlock::iterator MI,
645 unsigned DestReg, int FrameIdx,
646 const TargetRegisterClass *RC) const {
Dan Gohman221a4372008-07-07 23:14:23 +0000647 MachineFunction &MF = *MBB.getParent();
Owen Anderson81875432008-01-01 21:11:32 +0000648 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000649 DebugLoc DL = DebugLoc::getUnknownLoc();
650 if (MI != MBB.end()) DL = MI->getDebugLoc();
651 LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs);
Owen Anderson81875432008-01-01 21:11:32 +0000652 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
653 MBB.insert(MI, NewMIs[i]);
654}
655
656void PPCInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
Bill Wendlinga1877c52008-03-03 22:19:16 +0000657 SmallVectorImpl<MachineOperand> &Addr,
658 const TargetRegisterClass *RC,
659 SmallVectorImpl<MachineInstr*> &NewMIs)const{
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000660 if (Addr[0].isFI()) {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000661 LoadRegFromStackSlot(MF, DebugLoc::getUnknownLoc(),
662 DestReg, Addr[0].getIndex(), RC, NewMIs);
Owen Anderson81875432008-01-01 21:11:32 +0000663 return;
664 }
665
666 unsigned Opc = 0;
667 if (RC == PPC::GPRCRegisterClass) {
668 assert(DestReg != PPC::LR && "Can't handle this yet!");
669 Opc = PPC::LWZ;
670 } else if (RC == PPC::G8RCRegisterClass) {
671 assert(DestReg != PPC::LR8 && "Can't handle this yet!");
672 Opc = PPC::LD;
673 } else if (RC == PPC::F8RCRegisterClass) {
674 Opc = PPC::LFD;
675 } else if (RC == PPC::F4RCRegisterClass) {
676 Opc = PPC::LFS;
677 } else if (RC == PPC::VRRCRegisterClass) {
678 Opc = PPC::LVX;
679 } else {
680 assert(0 && "Unknown regclass!");
681 abort();
682 }
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000683 DebugLoc DL = DebugLoc::getUnknownLoc();
684 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Opc), DestReg);
Dan Gohmanc909bbb2009-02-18 05:45:50 +0000685 for (unsigned i = 0, e = Addr.size(); i != e; ++i)
686 MIB.addOperand(Addr[i]);
Owen Anderson81875432008-01-01 21:11:32 +0000687 NewMIs.push_back(MIB);
688 return;
689}
690
Owen Anderson9a184ef2008-01-07 01:35:02 +0000691/// foldMemoryOperand - PowerPC (like most RISC's) can only fold spills into
692/// copy instructions, turning them into load/store instructions.
Dan Gohmanedc83d62008-12-03 18:43:12 +0000693MachineInstr *PPCInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
694 MachineInstr *MI,
695 const SmallVectorImpl<unsigned> &Ops,
696 int FrameIndex) const {
Owen Anderson9a184ef2008-01-07 01:35:02 +0000697 if (Ops.size() != 1) return NULL;
698
699 // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because
700 // it takes more than one instruction to store it.
701 unsigned Opc = MI->getOpcode();
702 unsigned OpNum = Ops[0];
703
704 MachineInstr *NewMI = NULL;
705 if ((Opc == PPC::OR &&
706 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
707 if (OpNum == 0) { // move -> store
708 unsigned InReg = MI->getOperand(1).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000709 bool isKill = MI->getOperand(1).isKill();
Evan Cheng65219822009-07-01 01:59:31 +0000710 bool isUndef = MI->getOperand(1).isUndef();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000711 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::STW))
Evan Cheng65219822009-07-01 01:59:31 +0000712 .addReg(InReg,
713 getKillRegState(isKill) |
714 getUndefRegState(isUndef)),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000715 FrameIndex);
716 } else { // move -> load
717 unsigned OutReg = MI->getOperand(0).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000718 bool isDead = MI->getOperand(0).isDead();
Evan Cheng65219822009-07-01 01:59:31 +0000719 bool isUndef = MI->getOperand(0).isUndef();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000720 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::LWZ))
Bill Wendling2b739762009-05-13 21:33:08 +0000721 .addReg(OutReg,
722 RegState::Define |
Evan Cheng65219822009-07-01 01:59:31 +0000723 getDeadRegState(isDead) |
724 getUndefRegState(isUndef)),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000725 FrameIndex);
726 }
727 } else if ((Opc == PPC::OR8 &&
728 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
729 if (OpNum == 0) { // move -> store
730 unsigned InReg = MI->getOperand(1).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000731 bool isKill = MI->getOperand(1).isKill();
Evan Cheng65219822009-07-01 01:59:31 +0000732 bool isUndef = MI->getOperand(1).isUndef();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000733 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::STD))
Evan Cheng65219822009-07-01 01:59:31 +0000734 .addReg(InReg,
735 getKillRegState(isKill) |
736 getUndefRegState(isUndef)),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000737 FrameIndex);
738 } else { // move -> load
739 unsigned OutReg = MI->getOperand(0).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000740 bool isDead = MI->getOperand(0).isDead();
Evan Cheng65219822009-07-01 01:59:31 +0000741 bool isUndef = MI->getOperand(0).isUndef();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000742 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::LD))
Bill Wendling2b739762009-05-13 21:33:08 +0000743 .addReg(OutReg,
744 RegState::Define |
Evan Cheng65219822009-07-01 01:59:31 +0000745 getDeadRegState(isDead) |
746 getUndefRegState(isUndef)),
Evan Chenge52c1912008-07-03 09:09:37 +0000747 FrameIndex);
Owen Anderson9a184ef2008-01-07 01:35:02 +0000748 }
749 } else if (Opc == PPC::FMRD) {
750 if (OpNum == 0) { // move -> store
751 unsigned InReg = MI->getOperand(1).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000752 bool isKill = MI->getOperand(1).isKill();
Evan Cheng65219822009-07-01 01:59:31 +0000753 bool isUndef = MI->getOperand(1).isUndef();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000754 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::STFD))
Evan Cheng65219822009-07-01 01:59:31 +0000755 .addReg(InReg,
756 getKillRegState(isKill) |
757 getUndefRegState(isUndef)),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000758 FrameIndex);
759 } else { // move -> load
760 unsigned OutReg = MI->getOperand(0).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000761 bool isDead = MI->getOperand(0).isDead();
Evan Cheng65219822009-07-01 01:59:31 +0000762 bool isUndef = MI->getOperand(0).isUndef();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000763 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::LFD))
Bill Wendling2b739762009-05-13 21:33:08 +0000764 .addReg(OutReg,
765 RegState::Define |
Evan Cheng65219822009-07-01 01:59:31 +0000766 getDeadRegState(isDead) |
767 getUndefRegState(isUndef)),
Evan Chenge52c1912008-07-03 09:09:37 +0000768 FrameIndex);
Owen Anderson9a184ef2008-01-07 01:35:02 +0000769 }
770 } else if (Opc == PPC::FMRS) {
771 if (OpNum == 0) { // move -> store
772 unsigned InReg = MI->getOperand(1).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000773 bool isKill = MI->getOperand(1).isKill();
Evan Cheng65219822009-07-01 01:59:31 +0000774 bool isUndef = MI->getOperand(1).isUndef();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000775 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::STFS))
Evan Cheng65219822009-07-01 01:59:31 +0000776 .addReg(InReg,
777 getKillRegState(isKill) |
778 getUndefRegState(isUndef)),
Owen Anderson9a184ef2008-01-07 01:35:02 +0000779 FrameIndex);
780 } else { // move -> load
781 unsigned OutReg = MI->getOperand(0).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000782 bool isDead = MI->getOperand(0).isDead();
Evan Cheng65219822009-07-01 01:59:31 +0000783 bool isUndef = MI->getOperand(0).isUndef();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000784 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::LFS))
Bill Wendling2b739762009-05-13 21:33:08 +0000785 .addReg(OutReg,
786 RegState::Define |
Evan Cheng65219822009-07-01 01:59:31 +0000787 getDeadRegState(isDead) |
788 getUndefRegState(isUndef)),
Evan Chenge52c1912008-07-03 09:09:37 +0000789 FrameIndex);
Owen Anderson9a184ef2008-01-07 01:35:02 +0000790 }
791 }
792
Owen Anderson9a184ef2008-01-07 01:35:02 +0000793 return NewMI;
794}
795
Dan Gohman46b948e2008-10-16 01:49:15 +0000796bool PPCInstrInfo::canFoldMemoryOperand(const MachineInstr *MI,
797 const SmallVectorImpl<unsigned> &Ops) const {
Owen Anderson9a184ef2008-01-07 01:35:02 +0000798 if (Ops.size() != 1) return false;
799
800 // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because
801 // it takes more than one instruction to store it.
802 unsigned Opc = MI->getOpcode();
803
804 if ((Opc == PPC::OR &&
805 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
806 return true;
807 else if ((Opc == PPC::OR8 &&
808 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
809 return true;
810 else if (Opc == PPC::FMRD || Opc == PPC::FMRS)
811 return true;
812
813 return false;
814}
815
Owen Anderson81875432008-01-01 21:11:32 +0000816
Dan Gohman46b948e2008-10-16 01:49:15 +0000817bool PPCInstrInfo::BlockHasNoFallThrough(const MachineBasicBlock &MBB) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000818 if (MBB.empty()) return false;
819
820 switch (MBB.back().getOpcode()) {
821 case PPC::BLR: // Return.
822 case PPC::B: // Uncond branch.
823 case PPC::BCTR: // Indirect branch.
824 return true;
825 default: return false;
826 }
827}
828
829bool PPCInstrInfo::
Owen Andersond131b5b2008-08-14 22:49:33 +0000830ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000831 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
832 // Leave the CR# the same, but invert the condition.
833 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
834 return false;
835}
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000836
837/// GetInstSize - Return the number of bytes of code the specified
838/// instruction may be. This returns the maximum number of bytes.
839///
840unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
841 switch (MI->getOpcode()) {
842 case PPC::INLINEASM: { // Inline Asm: Variable size.
843 const MachineFunction *MF = MI->getParent()->getParent();
844 const char *AsmStr = MI->getOperand(0).getSymbolName();
845 return MF->getTarget().getTargetAsmInfo()->getInlineAsmLength(AsmStr);
846 }
Dan Gohmanfa607c92008-07-01 00:05:16 +0000847 case PPC::DBG_LABEL:
848 case PPC::EH_LABEL:
849 case PPC::GC_LABEL:
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000850 return 0;
Nicolas Geoffraycb162a02008-04-16 20:10:13 +0000851 default:
852 return 4; // PowerPC instructions are all 4 bytes
853 }
854}