blob: 3c8165fbbd74ce8bc8dd283c48f4aa96d1191cb2 [file] [log] [blame]
Chris Lattner4ee451d2007-12-29 20:36:04 +00001//===- SPUInstrInfo.cpp - Cell SPU Instruction Information ----------------===//
Scott Michel66377522007-12-04 22:35:58 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Scott Michel66377522007-12-04 22:35:58 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the Cell SPU implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "SPURegisterNames.h"
15#include "SPUInstrInfo.h"
Owen Andersonf6372aa2008-01-01 21:11:32 +000016#include "SPUInstrBuilder.h"
Scott Michel66377522007-12-04 22:35:58 +000017#include "SPUTargetMachine.h"
18#include "SPUGenInstrInfo.inc"
19#include "llvm/CodeGen/MachineInstrBuilder.h"
Bill Wendlingeecfa362008-05-29 21:46:33 +000020#include "llvm/Support/Streams.h"
Scott Michel66377522007-12-04 22:35:58 +000021
22using namespace llvm;
23
Scott Michelaedc6372008-12-10 00:15:19 +000024namespace {
25 //! Predicate for an unconditional branch instruction
26 inline bool isUncondBranch(const MachineInstr *I) {
27 unsigned opc = I->getOpcode();
28
29 return (opc == SPU::BR
30 || opc == SPU::BRA
31 || opc == SPU::BI);
32 }
33
34 inline bool isCondBranch(const MachineInstr *I) {
35 unsigned opc = I->getOpcode();
36
Scott Michelf0569be2008-12-27 04:51:36 +000037 return (opc == SPU::BRNZr32
38 || opc == SPU::BRNZv4i32
39 || opc == SPU::BRZr32
40 || opc == SPU::BRZv4i32
41 || opc == SPU::BRHNZr16
42 || opc == SPU::BRHNZv8i16
43 || opc == SPU::BRHZr16
44 || opc == SPU::BRHZv8i16);
Scott Michelaedc6372008-12-10 00:15:19 +000045 }
46}
47
Scott Michel66377522007-12-04 22:35:58 +000048SPUInstrInfo::SPUInstrInfo(SPUTargetMachine &tm)
Chris Lattner64105522008-01-01 01:03:04 +000049 : TargetInstrInfoImpl(SPUInsts, sizeof(SPUInsts)/sizeof(SPUInsts[0])),
Scott Michel66377522007-12-04 22:35:58 +000050 TM(tm),
51 RI(*TM.getSubtargetImpl(), *this)
52{
53 /* NOP */
54}
55
56/// getPointerRegClass - Return the register class to use to hold pointers.
57/// This is used for addressing modes.
58const TargetRegisterClass *
59SPUInstrInfo::getPointerRegClass() const
60{
61 return &SPU::R32CRegClass;
62}
63
64bool
65SPUInstrInfo::isMoveInstr(const MachineInstr& MI,
66 unsigned& sourceReg,
67 unsigned& destReg) const {
68 // Primarily, ORI and OR are generated by copyRegToReg. But, there are other
69 // cases where we can safely say that what's being done is really a move
70 // (see how PowerPC does this -- it's the model for this code too.)
71 switch (MI.getOpcode()) {
72 default:
73 break;
74 case SPU::ORIv4i32:
75 case SPU::ORIr32:
Scott Michel66377522007-12-04 22:35:58 +000076 case SPU::ORHIv8i16:
77 case SPU::ORHIr16:
Scott Michela59d4692008-02-23 18:41:37 +000078 case SPU::ORHIi8i16:
Scott Michel66377522007-12-04 22:35:58 +000079 case SPU::ORBIv16i8:
Scott Michel504c3692007-12-17 22:32:34 +000080 case SPU::ORBIr8:
Scott Michela59d4692008-02-23 18:41:37 +000081 case SPU::ORIi16i32:
82 case SPU::ORIi8i32:
Scott Michel66377522007-12-04 22:35:58 +000083 case SPU::AHIvec:
84 case SPU::AHIr16:
Scott Michel02d711b2008-12-30 23:28:25 +000085 case SPU::AIv4i32:
Scott Michel66377522007-12-04 22:35:58 +000086 assert(MI.getNumOperands() == 3 &&
Dan Gohmand735b802008-10-03 15:45:36 +000087 MI.getOperand(0).isReg() &&
88 MI.getOperand(1).isReg() &&
89 MI.getOperand(2).isImm() &&
Scott Michel66377522007-12-04 22:35:58 +000090 "invalid SPU ORI/ORHI/ORBI/AHI/AI/SFI/SFHI instruction!");
Chris Lattner9a1ceae2007-12-30 20:49:49 +000091 if (MI.getOperand(2).getImm() == 0) {
Scott Michel66377522007-12-04 22:35:58 +000092 sourceReg = MI.getOperand(1).getReg();
93 destReg = MI.getOperand(0).getReg();
94 return true;
95 }
96 break;
Scott Michel9999e682007-12-19 07:35:06 +000097 case SPU::AIr32:
98 assert(MI.getNumOperands() == 3 &&
99 "wrong number of operands to AIr32");
Dan Gohmand735b802008-10-03 15:45:36 +0000100 if (MI.getOperand(0).isReg() &&
Scott Michel02d711b2008-12-30 23:28:25 +0000101 MI.getOperand(1).isReg() &&
Dan Gohmand735b802008-10-03 15:45:36 +0000102 (MI.getOperand(2).isImm() &&
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000103 MI.getOperand(2).getImm() == 0)) {
Scott Michel9999e682007-12-19 07:35:06 +0000104 sourceReg = MI.getOperand(1).getReg();
105 destReg = MI.getOperand(0).getReg();
106 return true;
107 }
108 break;
Scott Michelf0569be2008-12-27 04:51:36 +0000109 case SPU::LRr8:
110 case SPU::LRr16:
111 case SPU::LRr32:
112 case SPU::LRf32:
113 case SPU::LRr64:
114 case SPU::LRf64:
115 case SPU::LRr128:
116 case SPU::LRv16i8:
117 case SPU::LRv8i16:
118 case SPU::LRv4i32:
119 case SPU::LRv4f32:
120 case SPU::LRv2i64:
121 case SPU::LRv2f64:
Scott Michel170783a2007-12-19 20:15:47 +0000122 case SPU::ORv16i8_i8:
Scott Michel66377522007-12-04 22:35:58 +0000123 case SPU::ORv8i16_i16:
124 case SPU::ORv4i32_i32:
125 case SPU::ORv2i64_i64:
126 case SPU::ORv4f32_f32:
127 case SPU::ORv2f64_f64:
Scott Michel170783a2007-12-19 20:15:47 +0000128 case SPU::ORi8_v16i8:
Scott Michel66377522007-12-04 22:35:58 +0000129 case SPU::ORi16_v8i16:
130 case SPU::ORi32_v4i32:
131 case SPU::ORi64_v2i64:
132 case SPU::ORf32_v4f32:
Scott Michelf0569be2008-12-27 04:51:36 +0000133 case SPU::ORf64_v2f64: {
134 assert(MI.getNumOperands() == 2 &&
135 MI.getOperand(0).isReg() &&
136 MI.getOperand(1).isReg() &&
137 "invalid SPU OR<type>_<vec> instruction!");
138 if (MI.getOperand(0).getReg() == MI.getOperand(1).getReg()) {
139 sourceReg = MI.getOperand(0).getReg();
140 destReg = MI.getOperand(0).getReg();
141 return true;
142 }
143 break;
144 }
Scott Michel66377522007-12-04 22:35:58 +0000145 case SPU::ORv16i8:
146 case SPU::ORv8i16:
147 case SPU::ORv4i32:
148 case SPU::ORr32:
149 case SPU::ORr64:
Scott Michel86c041f2007-12-20 00:44:13 +0000150 case SPU::ORf32:
151 case SPU::ORf64:
Scott Michel66377522007-12-04 22:35:58 +0000152 assert(MI.getNumOperands() == 3 &&
Dan Gohmand735b802008-10-03 15:45:36 +0000153 MI.getOperand(0).isReg() &&
154 MI.getOperand(1).isReg() &&
155 MI.getOperand(2).isReg() &&
Scott Michel66377522007-12-04 22:35:58 +0000156 "invalid SPU OR(vec|r32|r64|gprc) instruction!");
157 if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
158 sourceReg = MI.getOperand(1).getReg();
159 destReg = MI.getOperand(0).getReg();
160 return true;
161 }
162 break;
163 }
164
165 return false;
166}
167
168unsigned
Dan Gohmancbad42c2008-11-18 19:49:32 +0000169SPUInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
170 int &FrameIndex) const {
Scott Michel66377522007-12-04 22:35:58 +0000171 switch (MI->getOpcode()) {
172 default: break;
173 case SPU::LQDv16i8:
174 case SPU::LQDv8i16:
175 case SPU::LQDv4i32:
176 case SPU::LQDv4f32:
177 case SPU::LQDv2f64:
178 case SPU::LQDr128:
179 case SPU::LQDr64:
180 case SPU::LQDr32:
Scott Michelaedc6372008-12-10 00:15:19 +0000181 case SPU::LQDr16: {
182 const MachineOperand MOp1 = MI->getOperand(1);
183 const MachineOperand MOp2 = MI->getOperand(2);
184 if (MOp1.isImm()
185 && (MOp2.isFI()
186 || (MOp2.isReg() && MOp2.getReg() == SPU::R1))) {
187 if (MOp2.isFI())
188 FrameIndex = MOp2.getIndex();
189 else
190 FrameIndex = MOp1.getImm() / SPUFrameInfo::stackSlotSize();
191 return MI->getOperand(0).getReg();
192 }
193 break;
194 }
Scott Michel66377522007-12-04 22:35:58 +0000195 case SPU::LQXv4i32:
196 case SPU::LQXr128:
197 case SPU::LQXr64:
198 case SPU::LQXr32:
199 case SPU::LQXr16:
Scott Michelaedc6372008-12-10 00:15:19 +0000200 if (MI->getOperand(1).isReg() && MI->getOperand(2).isReg()
201 && (MI->getOperand(2).getReg() == SPU::R1
202 || MI->getOperand(1).getReg() == SPU::R1)) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000203 FrameIndex = MI->getOperand(2).getIndex();
Scott Michel66377522007-12-04 22:35:58 +0000204 return MI->getOperand(0).getReg();
205 }
206 break;
207 }
208 return 0;
209}
210
211unsigned
Dan Gohmancbad42c2008-11-18 19:49:32 +0000212SPUInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
213 int &FrameIndex) const {
Scott Michel66377522007-12-04 22:35:58 +0000214 switch (MI->getOpcode()) {
215 default: break;
216 case SPU::STQDv16i8:
217 case SPU::STQDv8i16:
218 case SPU::STQDv4i32:
219 case SPU::STQDv4f32:
220 case SPU::STQDv2f64:
221 case SPU::STQDr128:
222 case SPU::STQDr64:
223 case SPU::STQDr32:
224 case SPU::STQDr16:
Scott Michelaedc6372008-12-10 00:15:19 +0000225 case SPU::STQDr8: {
226 const MachineOperand MOp1 = MI->getOperand(1);
227 const MachineOperand MOp2 = MI->getOperand(2);
Scott Michelf0569be2008-12-27 04:51:36 +0000228 if (MOp1.isImm() && MOp2.isFI()) {
229 FrameIndex = MOp2.getIndex();
Scott Michelaedc6372008-12-10 00:15:19 +0000230 return MI->getOperand(0).getReg();
231 }
232 break;
233 }
Scott Michelf0569be2008-12-27 04:51:36 +0000234#if 0
235 case SPU::STQXv16i8:
Scott Michel66377522007-12-04 22:35:58 +0000236 case SPU::STQXv8i16:
237 case SPU::STQXv4i32:
238 case SPU::STQXv4f32:
239 case SPU::STQXv2f64:
240 case SPU::STQXr128:
241 case SPU::STQXr64:
242 case SPU::STQXr32:
243 case SPU::STQXr16:
Scott Michel9c0c6b22008-11-21 02:56:16 +0000244 case SPU::STQXr8:
Scott Michelaedc6372008-12-10 00:15:19 +0000245 if (MI->getOperand(1).isReg() && MI->getOperand(2).isReg()
246 && (MI->getOperand(2).getReg() == SPU::R1
247 || MI->getOperand(1).getReg() == SPU::R1)) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000248 FrameIndex = MI->getOperand(2).getIndex();
Scott Michel66377522007-12-04 22:35:58 +0000249 return MI->getOperand(0).getReg();
250 }
251 break;
Scott Michelf0569be2008-12-27 04:51:36 +0000252#endif
Scott Michel66377522007-12-04 22:35:58 +0000253 }
254 return 0;
255}
Owen Andersond10fd972007-12-31 06:32:00 +0000256
Owen Anderson940f83e2008-08-26 18:03:31 +0000257bool SPUInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
Owen Andersond10fd972007-12-31 06:32:00 +0000258 MachineBasicBlock::iterator MI,
259 unsigned DestReg, unsigned SrcReg,
260 const TargetRegisterClass *DestRC,
261 const TargetRegisterClass *SrcRC) const
262{
Chris Lattner5e09da22008-03-09 20:31:11 +0000263 // We support cross register class moves for our aliases, such as R3 in any
264 // reg class to any other reg class containing R3. This is required because
265 // we instruction select bitconvert i64 -> f64 as a noop for example, so our
266 // types have no specific meaning.
Scott Michel02d711b2008-12-30 23:28:25 +0000267
Owen Andersond10fd972007-12-31 06:32:00 +0000268 if (DestRC == SPU::R8CRegisterClass) {
269 BuildMI(MBB, MI, get(SPU::ORBIr8), DestReg).addReg(SrcReg).addImm(0);
270 } else if (DestRC == SPU::R16CRegisterClass) {
271 BuildMI(MBB, MI, get(SPU::ORHIr16), DestReg).addReg(SrcReg).addImm(0);
272 } else if (DestRC == SPU::R32CRegisterClass) {
273 BuildMI(MBB, MI, get(SPU::ORIr32), DestReg).addReg(SrcReg).addImm(0);
274 } else if (DestRC == SPU::R32FPRegisterClass) {
275 BuildMI(MBB, MI, get(SPU::ORf32), DestReg).addReg(SrcReg)
276 .addReg(SrcReg);
277 } else if (DestRC == SPU::R64CRegisterClass) {
Scott Michela59d4692008-02-23 18:41:37 +0000278 BuildMI(MBB, MI, get(SPU::ORr64), DestReg).addReg(SrcReg)
279 .addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000280 } else if (DestRC == SPU::R64FPRegisterClass) {
281 BuildMI(MBB, MI, get(SPU::ORf64), DestReg).addReg(SrcReg)
282 .addReg(SrcReg);
Scott Michela59d4692008-02-23 18:41:37 +0000283 } /* else if (DestRC == SPU::GPRCRegisterClass) {
Owen Andersond10fd972007-12-31 06:32:00 +0000284 BuildMI(MBB, MI, get(SPU::ORgprc), DestReg).addReg(SrcReg)
285 .addReg(SrcReg);
Scott Michela59d4692008-02-23 18:41:37 +0000286 } */ else if (DestRC == SPU::VECREGRegisterClass) {
Owen Andersond10fd972007-12-31 06:32:00 +0000287 BuildMI(MBB, MI, get(SPU::ORv4i32), DestReg).addReg(SrcReg)
288 .addReg(SrcReg);
289 } else {
Owen Anderson940f83e2008-08-26 18:03:31 +0000290 // Attempt to copy unknown/unsupported register class!
291 return false;
Owen Andersond10fd972007-12-31 06:32:00 +0000292 }
Scott Michel02d711b2008-12-30 23:28:25 +0000293
Owen Anderson940f83e2008-08-26 18:03:31 +0000294 return true;
Owen Andersond10fd972007-12-31 06:32:00 +0000295}
Owen Andersonf6372aa2008-01-01 21:11:32 +0000296
297void
298SPUInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
299 MachineBasicBlock::iterator MI,
300 unsigned SrcReg, bool isKill, int FrameIdx,
301 const TargetRegisterClass *RC) const
302{
Chris Lattnercc8cd0c2008-01-07 02:48:55 +0000303 unsigned opc;
Scott Michelaedc6372008-12-10 00:15:19 +0000304 bool isValidFrameIdx = (FrameIdx < SPUFrameInfo::maxFrameOffset());
Owen Andersonf6372aa2008-01-01 21:11:32 +0000305 if (RC == SPU::GPRCRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000306 opc = (isValidFrameIdx ? SPU::STQDr128 : SPU::STQXr128);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000307 } else if (RC == SPU::R64CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000308 opc = (isValidFrameIdx ? SPU::STQDr64 : SPU::STQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000309 } else if (RC == SPU::R64FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000310 opc = (isValidFrameIdx ? SPU::STQDr64 : SPU::STQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000311 } else if (RC == SPU::R32CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000312 opc = (isValidFrameIdx ? SPU::STQDr32 : SPU::STQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000313 } else if (RC == SPU::R32FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000314 opc = (isValidFrameIdx ? SPU::STQDr32 : SPU::STQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000315 } else if (RC == SPU::R16CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000316 opc = (isValidFrameIdx ? SPU::STQDr16 : SPU::STQXr16);
317 } else if (RC == SPU::R8CRegisterClass) {
318 opc = (isValidFrameIdx ? SPU::STQDr8 : SPU::STQXr8);
Scott Michelf0569be2008-12-27 04:51:36 +0000319 } else if (RC == SPU::VECREGRegisterClass) {
320 opc = (isValidFrameIdx) ? SPU::STQDv16i8 : SPU::STQXv16i8;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000321 } else {
322 assert(0 && "Unknown regclass!");
323 abort();
324 }
325
326 addFrameReference(BuildMI(MBB, MI, get(opc))
327 .addReg(SrcReg, false, false, isKill), FrameIdx);
328}
329
330void SPUInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
331 bool isKill,
332 SmallVectorImpl<MachineOperand> &Addr,
333 const TargetRegisterClass *RC,
334 SmallVectorImpl<MachineInstr*> &NewMIs) const {
335 cerr << "storeRegToAddr() invoked!\n";
336 abort();
337
Dan Gohmand735b802008-10-03 15:45:36 +0000338 if (Addr[0].isFI()) {
Owen Andersonf6372aa2008-01-01 21:11:32 +0000339 /* do what storeRegToStackSlot does here */
340 } else {
341 unsigned Opc = 0;
342 if (RC == SPU::GPRCRegisterClass) {
343 /* Opc = PPC::STW; */
344 } else if (RC == SPU::R16CRegisterClass) {
345 /* Opc = PPC::STD; */
346 } else if (RC == SPU::R32CRegisterClass) {
347 /* Opc = PPC::STFD; */
348 } else if (RC == SPU::R32FPRegisterClass) {
349 /* Opc = PPC::STFD; */
350 } else if (RC == SPU::R64FPRegisterClass) {
351 /* Opc = PPC::STFS; */
352 } else if (RC == SPU::VECREGRegisterClass) {
353 /* Opc = PPC::STVX; */
354 } else {
355 assert(0 && "Unknown regclass!");
356 abort();
357 }
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000358 MachineInstrBuilder MIB = BuildMI(MF, get(Opc))
Owen Andersonf6372aa2008-01-01 21:11:32 +0000359 .addReg(SrcReg, false, false, isKill);
360 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
361 MachineOperand &MO = Addr[i];
Dan Gohmand735b802008-10-03 15:45:36 +0000362 if (MO.isReg())
Owen Andersonf6372aa2008-01-01 21:11:32 +0000363 MIB.addReg(MO.getReg());
Dan Gohmand735b802008-10-03 15:45:36 +0000364 else if (MO.isImm())
Owen Andersonf6372aa2008-01-01 21:11:32 +0000365 MIB.addImm(MO.getImm());
366 else
367 MIB.addFrameIndex(MO.getIndex());
368 }
369 NewMIs.push_back(MIB);
370 }
371}
372
373void
374SPUInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
375 MachineBasicBlock::iterator MI,
376 unsigned DestReg, int FrameIdx,
377 const TargetRegisterClass *RC) const
378{
Chris Lattnercc8cd0c2008-01-07 02:48:55 +0000379 unsigned opc;
Scott Michelaedc6372008-12-10 00:15:19 +0000380 bool isValidFrameIdx = (FrameIdx < SPUFrameInfo::maxFrameOffset());
Owen Andersonf6372aa2008-01-01 21:11:32 +0000381 if (RC == SPU::GPRCRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000382 opc = (isValidFrameIdx ? SPU::LQDr128 : SPU::LQXr128);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000383 } else if (RC == SPU::R64CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000384 opc = (isValidFrameIdx ? SPU::LQDr64 : SPU::LQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000385 } else if (RC == SPU::R64FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000386 opc = (isValidFrameIdx ? SPU::LQDr64 : SPU::LQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000387 } else if (RC == SPU::R32CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000388 opc = (isValidFrameIdx ? SPU::LQDr32 : SPU::LQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000389 } else if (RC == SPU::R32FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000390 opc = (isValidFrameIdx ? SPU::LQDr32 : SPU::LQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000391 } else if (RC == SPU::R16CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000392 opc = (isValidFrameIdx ? SPU::LQDr16 : SPU::LQXr16);
393 } else if (RC == SPU::R8CRegisterClass) {
394 opc = (isValidFrameIdx ? SPU::LQDr8 : SPU::LQXr8);
Scott Michelf0569be2008-12-27 04:51:36 +0000395 } else if (RC == SPU::VECREGRegisterClass) {
396 opc = (isValidFrameIdx) ? SPU::LQDv16i8 : SPU::LQXv16i8;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000397 } else {
398 assert(0 && "Unknown regclass in loadRegFromStackSlot!");
399 abort();
400 }
401
402 addFrameReference(BuildMI(MBB, MI, get(opc)).addReg(DestReg), FrameIdx);
403}
404
405/*!
406 \note We are really pessimistic here about what kind of a load we're doing.
407 */
408void SPUInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
Scott Michelaedc6372008-12-10 00:15:19 +0000409 SmallVectorImpl<MachineOperand> &Addr,
410 const TargetRegisterClass *RC,
411 SmallVectorImpl<MachineInstr*> &NewMIs)
Owen Andersonf6372aa2008-01-01 21:11:32 +0000412 const {
413 cerr << "loadRegToAddr() invoked!\n";
414 abort();
415
Dan Gohmand735b802008-10-03 15:45:36 +0000416 if (Addr[0].isFI()) {
Owen Andersonf6372aa2008-01-01 21:11:32 +0000417 /* do what loadRegFromStackSlot does here... */
418 } else {
419 unsigned Opc = 0;
420 if (RC == SPU::R8CRegisterClass) {
421 /* do brilliance here */
422 } else if (RC == SPU::R16CRegisterClass) {
423 /* Opc = PPC::LWZ; */
424 } else if (RC == SPU::R32CRegisterClass) {
425 /* Opc = PPC::LD; */
426 } else if (RC == SPU::R32FPRegisterClass) {
427 /* Opc = PPC::LFD; */
428 } else if (RC == SPU::R64FPRegisterClass) {
429 /* Opc = PPC::LFS; */
430 } else if (RC == SPU::VECREGRegisterClass) {
431 /* Opc = PPC::LVX; */
432 } else if (RC == SPU::GPRCRegisterClass) {
433 /* Opc = something else! */
434 } else {
435 assert(0 && "Unknown regclass!");
436 abort();
437 }
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000438 MachineInstrBuilder MIB = BuildMI(MF, get(Opc), DestReg);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000439 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
440 MachineOperand &MO = Addr[i];
Dan Gohmand735b802008-10-03 15:45:36 +0000441 if (MO.isReg())
Owen Andersonf6372aa2008-01-01 21:11:32 +0000442 MIB.addReg(MO.getReg());
Dan Gohmand735b802008-10-03 15:45:36 +0000443 else if (MO.isImm())
Owen Andersonf6372aa2008-01-01 21:11:32 +0000444 MIB.addImm(MO.getImm());
445 else
446 MIB.addFrameIndex(MO.getIndex());
447 }
448 NewMIs.push_back(MIB);
449 }
450}
451
Owen Anderson43dbe052008-01-07 01:35:02 +0000452/// foldMemoryOperand - SPU, like PPC, can only fold spills into
453/// copy instructions, turning them into load/store instructions.
454MachineInstr *
Dan Gohmanc54baa22008-12-03 18:43:12 +0000455SPUInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
456 MachineInstr *MI,
457 const SmallVectorImpl<unsigned> &Ops,
458 int FrameIndex) const
Owen Anderson43dbe052008-01-07 01:35:02 +0000459{
460#if SOMEDAY_SCOTT_LOOKS_AT_ME_AGAIN
461 if (Ops.size() != 1) return NULL;
462
463 unsigned OpNum = Ops[0];
464 unsigned Opc = MI->getOpcode();
465 MachineInstr *NewMI = 0;
Scott Michel02d711b2008-12-30 23:28:25 +0000466
Owen Anderson43dbe052008-01-07 01:35:02 +0000467 if ((Opc == SPU::ORr32
468 || Opc == SPU::ORv4i32)
469 && MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
470 if (OpNum == 0) { // move -> store
471 unsigned InReg = MI->getOperand(1).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000472 bool isKill = MI->getOperand(1).isKill();
Owen Anderson43dbe052008-01-07 01:35:02 +0000473 if (FrameIndex < SPUFrameInfo::maxFrameOffset()) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000474 NewMI = addFrameReference(BuildMI(MF, TII.get(SPU::STQDr32))
Evan Cheng9f1c8312008-07-03 09:09:37 +0000475 .addReg(InReg, false, false, isKill),
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000476 FrameIndex);
Owen Anderson43dbe052008-01-07 01:35:02 +0000477 }
478 } else { // move -> load
479 unsigned OutReg = MI->getOperand(0).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000480 bool isDead = MI->getOperand(0).isDead();
481 Opc = (FrameIndex < SPUFrameInfo::maxFrameOffset())
482 ? SPU::STQDr32 : SPU::STQXr32;
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000483 NewMI = addFrameReference(BuildMI(MF, TII.get(Opc))
Evan Cheng9f1c8312008-07-03 09:09:37 +0000484 .addReg(OutReg, true, false, false, isDead), FrameIndex);
Owen Anderson43dbe052008-01-07 01:35:02 +0000485 }
486 }
487
Owen Anderson43dbe052008-01-07 01:35:02 +0000488 return NewMI;
489#else
490 return 0;
491#endif
492}
493
Scott Michelaedc6372008-12-10 00:15:19 +0000494//! Branch analysis
495/*
496 \note This code was kiped from PPC. There may be more branch analysis for
497 CellSPU than what's currently done here.
498 */
499bool
500SPUInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
501 MachineBasicBlock *&FBB,
502 SmallVectorImpl<MachineOperand> &Cond) const {
503 // If the block has no terminators, it just falls into the block after it.
504 MachineBasicBlock::iterator I = MBB.end();
505 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
506 return false;
507
508 // Get the last instruction in the block.
509 MachineInstr *LastInst = I;
Scott Michel02d711b2008-12-30 23:28:25 +0000510
Scott Michelaedc6372008-12-10 00:15:19 +0000511 // If there is only one terminator instruction, process it.
512 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
513 if (isUncondBranch(LastInst)) {
514 TBB = LastInst->getOperand(0).getMBB();
515 return false;
516 } else if (isCondBranch(LastInst)) {
517 // Block ends with fall-through condbranch.
518 TBB = LastInst->getOperand(1).getMBB();
519 Cond.push_back(LastInst->getOperand(0));
520 Cond.push_back(LastInst->getOperand(1));
521 return false;
522 }
523 // Otherwise, don't know what this is.
524 return true;
525 }
Scott Michel02d711b2008-12-30 23:28:25 +0000526
Scott Michelaedc6372008-12-10 00:15:19 +0000527 // Get the instruction before it if it's a terminator.
528 MachineInstr *SecondLastInst = I;
529
530 // If there are three terminators, we don't know what sort of block this is.
531 if (SecondLastInst && I != MBB.begin() &&
532 isUnpredicatedTerminator(--I))
533 return true;
Scott Michel02d711b2008-12-30 23:28:25 +0000534
Scott Michelaedc6372008-12-10 00:15:19 +0000535 // If the block ends with a conditional and unconditional branch, handle it.
536 if (isCondBranch(SecondLastInst) && isUncondBranch(LastInst)) {
537 TBB = SecondLastInst->getOperand(1).getMBB();
538 Cond.push_back(SecondLastInst->getOperand(0));
539 Cond.push_back(SecondLastInst->getOperand(1));
540 FBB = LastInst->getOperand(0).getMBB();
541 return false;
542 }
Scott Michel02d711b2008-12-30 23:28:25 +0000543
Scott Michelaedc6372008-12-10 00:15:19 +0000544 // If the block ends with two unconditional branches, handle it. The second
545 // one is not executed, so remove it.
546 if (isUncondBranch(SecondLastInst) && isUncondBranch(LastInst)) {
547 TBB = SecondLastInst->getOperand(0).getMBB();
548 I = LastInst;
549 I->eraseFromParent();
550 return false;
551 }
552
553 // Otherwise, can't handle this.
554 return true;
555}
Scott Michel02d711b2008-12-30 23:28:25 +0000556
Scott Michelaedc6372008-12-10 00:15:19 +0000557unsigned
558SPUInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
559 MachineBasicBlock::iterator I = MBB.end();
560 if (I == MBB.begin())
561 return 0;
562 --I;
563 if (!isCondBranch(I) && !isUncondBranch(I))
564 return 0;
565
566 // Remove the first branch.
567 I->eraseFromParent();
568 I = MBB.end();
569 if (I == MBB.begin())
570 return 1;
571
572 --I;
573 if (isCondBranch(I))
574 return 1;
575
576 // Remove the second branch.
577 I->eraseFromParent();
578 return 2;
579}
Scott Michel02d711b2008-12-30 23:28:25 +0000580
Scott Michelaedc6372008-12-10 00:15:19 +0000581unsigned
582SPUInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
583 MachineBasicBlock *FBB,
584 const SmallVectorImpl<MachineOperand> &Cond) const {
585 // Shouldn't be a fall through.
586 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Scott Michel02d711b2008-12-30 23:28:25 +0000587 assert((Cond.size() == 2 || Cond.size() == 0) &&
Scott Michelaedc6372008-12-10 00:15:19 +0000588 "SPU branch conditions have two components!");
Scott Michel02d711b2008-12-30 23:28:25 +0000589
Scott Michelaedc6372008-12-10 00:15:19 +0000590 // One-way branch.
591 if (FBB == 0) {
592 if (Cond.empty()) // Unconditional branch
593 BuildMI(&MBB, get(SPU::BR)).addMBB(TBB);
594 else { // Conditional branch
595 /* BuildMI(&MBB, get(SPU::BRNZ))
596 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB); */
597 cerr << "SPUInstrInfo::InsertBranch conditional branch logic needed\n";
598 abort();
599 }
600 return 1;
601 }
Scott Michel02d711b2008-12-30 23:28:25 +0000602
Scott Michelaedc6372008-12-10 00:15:19 +0000603 // Two-way Conditional Branch.
604#if 0
605 BuildMI(&MBB, get(SPU::BRNZ))
606 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
607 BuildMI(&MBB, get(SPU::BR)).addMBB(FBB);
608#else
609 cerr << "SPUInstrInfo::InsertBranch conditional branch logic needed\n";
610 abort();
611#endif
612
613 return 2;
614}
615
616