blob: d88b2e9e82fa708dd17a3c93edfb55d006eb87e8 [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 Michel9bd7a372009-01-02 20:52:08 +000021#include "llvm/Support/Debug.h"
Scott Michel66377522007-12-04 22:35:58 +000022
23using namespace llvm;
24
Scott Michelaedc6372008-12-10 00:15:19 +000025namespace {
26 //! Predicate for an unconditional branch instruction
27 inline bool isUncondBranch(const MachineInstr *I) {
28 unsigned opc = I->getOpcode();
29
30 return (opc == SPU::BR
31 || opc == SPU::BRA
32 || opc == SPU::BI);
33 }
34
Scott Michel52d00012009-01-03 00:27:53 +000035 //! Predicate for a conditional branch instruction
Scott Michelaedc6372008-12-10 00:15:19 +000036 inline bool isCondBranch(const MachineInstr *I) {
37 unsigned opc = I->getOpcode();
38
Scott Michelf0569be2008-12-27 04:51:36 +000039 return (opc == SPU::BRNZr32
40 || opc == SPU::BRNZv4i32
41 || opc == SPU::BRZr32
42 || opc == SPU::BRZv4i32
43 || opc == SPU::BRHNZr16
44 || opc == SPU::BRHNZv8i16
45 || opc == SPU::BRHZr16
46 || opc == SPU::BRHZv8i16);
Scott Michelaedc6372008-12-10 00:15:19 +000047 }
48}
49
Scott Michel66377522007-12-04 22:35:58 +000050SPUInstrInfo::SPUInstrInfo(SPUTargetMachine &tm)
Chris Lattner64105522008-01-01 01:03:04 +000051 : TargetInstrInfoImpl(SPUInsts, sizeof(SPUInsts)/sizeof(SPUInsts[0])),
Scott Michel66377522007-12-04 22:35:58 +000052 TM(tm),
53 RI(*TM.getSubtargetImpl(), *this)
Scott Michel52d00012009-01-03 00:27:53 +000054{ /* NOP */ }
Scott Michel66377522007-12-04 22:35:58 +000055
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() &&
Scott Michel52d00012009-01-03 00:27:53 +0000137 "invalid SPU OR<type>_<vec> or LR instruction!");
Scott Michelf0569be2008-12-27 04:51:36 +0000138 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:
Scott Michel52d00012009-01-03 00:27:53 +0000148 case SPU::ORv2i64:
149 case SPU::ORr8:
150 case SPU::ORr16:
Scott Michel66377522007-12-04 22:35:58 +0000151 case SPU::ORr32:
152 case SPU::ORr64:
Scott Michel86c041f2007-12-20 00:44:13 +0000153 case SPU::ORf32:
154 case SPU::ORf64:
Scott Michel66377522007-12-04 22:35:58 +0000155 assert(MI.getNumOperands() == 3 &&
Dan Gohmand735b802008-10-03 15:45:36 +0000156 MI.getOperand(0).isReg() &&
157 MI.getOperand(1).isReg() &&
158 MI.getOperand(2).isReg() &&
Scott Michel66377522007-12-04 22:35:58 +0000159 "invalid SPU OR(vec|r32|r64|gprc) instruction!");
160 if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
161 sourceReg = MI.getOperand(1).getReg();
162 destReg = MI.getOperand(0).getReg();
163 return true;
164 }
165 break;
166 }
167
168 return false;
169}
170
171unsigned
Dan Gohmancbad42c2008-11-18 19:49:32 +0000172SPUInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
173 int &FrameIndex) const {
Scott Michel66377522007-12-04 22:35:58 +0000174 switch (MI->getOpcode()) {
175 default: break;
176 case SPU::LQDv16i8:
177 case SPU::LQDv8i16:
178 case SPU::LQDv4i32:
179 case SPU::LQDv4f32:
180 case SPU::LQDv2f64:
181 case SPU::LQDr128:
182 case SPU::LQDr64:
183 case SPU::LQDr32:
Scott Michelaedc6372008-12-10 00:15:19 +0000184 case SPU::LQDr16: {
185 const MachineOperand MOp1 = MI->getOperand(1);
186 const MachineOperand MOp2 = MI->getOperand(2);
Scott Michel52d00012009-01-03 00:27:53 +0000187 if (MOp1.isImm() && MOp2.isFI()) {
188 FrameIndex = MOp2.getIndex();
Scott Michelaedc6372008-12-10 00:15:19 +0000189 return MI->getOperand(0).getReg();
190 }
191 break;
192 }
Scott Michel66377522007-12-04 22:35:58 +0000193 }
194 return 0;
195}
196
197unsigned
Dan Gohmancbad42c2008-11-18 19:49:32 +0000198SPUInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
199 int &FrameIndex) const {
Scott Michel66377522007-12-04 22:35:58 +0000200 switch (MI->getOpcode()) {
201 default: break;
202 case SPU::STQDv16i8:
203 case SPU::STQDv8i16:
204 case SPU::STQDv4i32:
205 case SPU::STQDv4f32:
206 case SPU::STQDv2f64:
207 case SPU::STQDr128:
208 case SPU::STQDr64:
209 case SPU::STQDr32:
210 case SPU::STQDr16:
Scott Michelaedc6372008-12-10 00:15:19 +0000211 case SPU::STQDr8: {
212 const MachineOperand MOp1 = MI->getOperand(1);
213 const MachineOperand MOp2 = MI->getOperand(2);
Scott Michelf0569be2008-12-27 04:51:36 +0000214 if (MOp1.isImm() && MOp2.isFI()) {
215 FrameIndex = MOp2.getIndex();
Scott Michelaedc6372008-12-10 00:15:19 +0000216 return MI->getOperand(0).getReg();
217 }
218 break;
219 }
Scott Michel66377522007-12-04 22:35:58 +0000220 }
221 return 0;
222}
Owen Andersond10fd972007-12-31 06:32:00 +0000223
Owen Anderson940f83e2008-08-26 18:03:31 +0000224bool SPUInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
Owen Andersond10fd972007-12-31 06:32:00 +0000225 MachineBasicBlock::iterator MI,
226 unsigned DestReg, unsigned SrcReg,
227 const TargetRegisterClass *DestRC,
228 const TargetRegisterClass *SrcRC) const
229{
Chris Lattner5e09da22008-03-09 20:31:11 +0000230 // We support cross register class moves for our aliases, such as R3 in any
231 // reg class to any other reg class containing R3. This is required because
232 // we instruction select bitconvert i64 -> f64 as a noop for example, so our
233 // types have no specific meaning.
Scott Michel02d711b2008-12-30 23:28:25 +0000234
Owen Andersond10fd972007-12-31 06:32:00 +0000235 if (DestRC == SPU::R8CRegisterClass) {
Scott Michel9bd7a372009-01-02 20:52:08 +0000236 BuildMI(MBB, MI, get(SPU::LRr8), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000237 } else if (DestRC == SPU::R16CRegisterClass) {
Scott Michel9bd7a372009-01-02 20:52:08 +0000238 BuildMI(MBB, MI, get(SPU::LRr16), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000239 } else if (DestRC == SPU::R32CRegisterClass) {
Scott Michel9bd7a372009-01-02 20:52:08 +0000240 BuildMI(MBB, MI, get(SPU::LRr32), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000241 } else if (DestRC == SPU::R32FPRegisterClass) {
Scott Michel9bd7a372009-01-02 20:52:08 +0000242 BuildMI(MBB, MI, get(SPU::LRf32), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000243 } else if (DestRC == SPU::R64CRegisterClass) {
Scott Michel9bd7a372009-01-02 20:52:08 +0000244 BuildMI(MBB, MI, get(SPU::LRr64), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000245 } else if (DestRC == SPU::R64FPRegisterClass) {
Scott Michel9bd7a372009-01-02 20:52:08 +0000246 BuildMI(MBB, MI, get(SPU::LRf64), DestReg).addReg(SrcReg);
247 } else if (DestRC == SPU::GPRCRegisterClass) {
248 BuildMI(MBB, MI, get(SPU::LRr128), DestReg).addReg(SrcReg);
249 } else if (DestRC == SPU::VECREGRegisterClass) {
250 BuildMI(MBB, MI, get(SPU::LRv16i8), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000251 } else {
Owen Anderson940f83e2008-08-26 18:03:31 +0000252 // Attempt to copy unknown/unsupported register class!
253 return false;
Owen Andersond10fd972007-12-31 06:32:00 +0000254 }
Scott Michel02d711b2008-12-30 23:28:25 +0000255
Owen Anderson940f83e2008-08-26 18:03:31 +0000256 return true;
Owen Andersond10fd972007-12-31 06:32:00 +0000257}
Owen Andersonf6372aa2008-01-01 21:11:32 +0000258
259void
260SPUInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
261 MachineBasicBlock::iterator MI,
262 unsigned SrcReg, bool isKill, int FrameIdx,
263 const TargetRegisterClass *RC) const
264{
Chris Lattnercc8cd0c2008-01-07 02:48:55 +0000265 unsigned opc;
Scott Michelaedc6372008-12-10 00:15:19 +0000266 bool isValidFrameIdx = (FrameIdx < SPUFrameInfo::maxFrameOffset());
Owen Andersonf6372aa2008-01-01 21:11:32 +0000267 if (RC == SPU::GPRCRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000268 opc = (isValidFrameIdx ? SPU::STQDr128 : SPU::STQXr128);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000269 } else if (RC == SPU::R64CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000270 opc = (isValidFrameIdx ? SPU::STQDr64 : SPU::STQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000271 } else if (RC == SPU::R64FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000272 opc = (isValidFrameIdx ? SPU::STQDr64 : SPU::STQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000273 } else if (RC == SPU::R32CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000274 opc = (isValidFrameIdx ? SPU::STQDr32 : SPU::STQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000275 } else if (RC == SPU::R32FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000276 opc = (isValidFrameIdx ? SPU::STQDr32 : SPU::STQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000277 } else if (RC == SPU::R16CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000278 opc = (isValidFrameIdx ? SPU::STQDr16 : SPU::STQXr16);
279 } else if (RC == SPU::R8CRegisterClass) {
280 opc = (isValidFrameIdx ? SPU::STQDr8 : SPU::STQXr8);
Scott Michelf0569be2008-12-27 04:51:36 +0000281 } else if (RC == SPU::VECREGRegisterClass) {
282 opc = (isValidFrameIdx) ? SPU::STQDv16i8 : SPU::STQXv16i8;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000283 } else {
284 assert(0 && "Unknown regclass!");
285 abort();
286 }
287
288 addFrameReference(BuildMI(MBB, MI, get(opc))
289 .addReg(SrcReg, false, false, isKill), FrameIdx);
290}
291
292void SPUInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
293 bool isKill,
294 SmallVectorImpl<MachineOperand> &Addr,
295 const TargetRegisterClass *RC,
296 SmallVectorImpl<MachineInstr*> &NewMIs) const {
297 cerr << "storeRegToAddr() invoked!\n";
298 abort();
299
Dan Gohmand735b802008-10-03 15:45:36 +0000300 if (Addr[0].isFI()) {
Owen Andersonf6372aa2008-01-01 21:11:32 +0000301 /* do what storeRegToStackSlot does here */
302 } else {
303 unsigned Opc = 0;
304 if (RC == SPU::GPRCRegisterClass) {
305 /* Opc = PPC::STW; */
306 } else if (RC == SPU::R16CRegisterClass) {
307 /* Opc = PPC::STD; */
308 } else if (RC == SPU::R32CRegisterClass) {
309 /* Opc = PPC::STFD; */
310 } else if (RC == SPU::R32FPRegisterClass) {
311 /* Opc = PPC::STFD; */
312 } else if (RC == SPU::R64FPRegisterClass) {
313 /* Opc = PPC::STFS; */
314 } else if (RC == SPU::VECREGRegisterClass) {
315 /* Opc = PPC::STVX; */
316 } else {
317 assert(0 && "Unknown regclass!");
318 abort();
319 }
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000320 MachineInstrBuilder MIB = BuildMI(MF, get(Opc))
Owen Andersonf6372aa2008-01-01 21:11:32 +0000321 .addReg(SrcReg, false, false, isKill);
322 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
323 MachineOperand &MO = Addr[i];
Dan Gohmand735b802008-10-03 15:45:36 +0000324 if (MO.isReg())
Owen Andersonf6372aa2008-01-01 21:11:32 +0000325 MIB.addReg(MO.getReg());
Dan Gohmand735b802008-10-03 15:45:36 +0000326 else if (MO.isImm())
Owen Andersonf6372aa2008-01-01 21:11:32 +0000327 MIB.addImm(MO.getImm());
328 else
329 MIB.addFrameIndex(MO.getIndex());
330 }
331 NewMIs.push_back(MIB);
332 }
333}
334
335void
336SPUInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
337 MachineBasicBlock::iterator MI,
338 unsigned DestReg, int FrameIdx,
339 const TargetRegisterClass *RC) const
340{
Chris Lattnercc8cd0c2008-01-07 02:48:55 +0000341 unsigned opc;
Scott Michelaedc6372008-12-10 00:15:19 +0000342 bool isValidFrameIdx = (FrameIdx < SPUFrameInfo::maxFrameOffset());
Owen Andersonf6372aa2008-01-01 21:11:32 +0000343 if (RC == SPU::GPRCRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000344 opc = (isValidFrameIdx ? SPU::LQDr128 : SPU::LQXr128);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000345 } else if (RC == SPU::R64CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000346 opc = (isValidFrameIdx ? SPU::LQDr64 : SPU::LQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000347 } else if (RC == SPU::R64FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000348 opc = (isValidFrameIdx ? SPU::LQDr64 : SPU::LQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000349 } else if (RC == SPU::R32CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000350 opc = (isValidFrameIdx ? SPU::LQDr32 : SPU::LQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000351 } else if (RC == SPU::R32FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000352 opc = (isValidFrameIdx ? SPU::LQDr32 : SPU::LQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000353 } else if (RC == SPU::R16CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000354 opc = (isValidFrameIdx ? SPU::LQDr16 : SPU::LQXr16);
355 } else if (RC == SPU::R8CRegisterClass) {
356 opc = (isValidFrameIdx ? SPU::LQDr8 : SPU::LQXr8);
Scott Michelf0569be2008-12-27 04:51:36 +0000357 } else if (RC == SPU::VECREGRegisterClass) {
358 opc = (isValidFrameIdx) ? SPU::LQDv16i8 : SPU::LQXv16i8;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000359 } else {
360 assert(0 && "Unknown regclass in loadRegFromStackSlot!");
361 abort();
362 }
363
364 addFrameReference(BuildMI(MBB, MI, get(opc)).addReg(DestReg), FrameIdx);
365}
366
367/*!
368 \note We are really pessimistic here about what kind of a load we're doing.
369 */
370void SPUInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
Scott Michelaedc6372008-12-10 00:15:19 +0000371 SmallVectorImpl<MachineOperand> &Addr,
372 const TargetRegisterClass *RC,
373 SmallVectorImpl<MachineInstr*> &NewMIs)
Owen Andersonf6372aa2008-01-01 21:11:32 +0000374 const {
375 cerr << "loadRegToAddr() invoked!\n";
376 abort();
377
Dan Gohmand735b802008-10-03 15:45:36 +0000378 if (Addr[0].isFI()) {
Owen Andersonf6372aa2008-01-01 21:11:32 +0000379 /* do what loadRegFromStackSlot does here... */
380 } else {
381 unsigned Opc = 0;
382 if (RC == SPU::R8CRegisterClass) {
383 /* do brilliance here */
384 } else if (RC == SPU::R16CRegisterClass) {
385 /* Opc = PPC::LWZ; */
386 } else if (RC == SPU::R32CRegisterClass) {
387 /* Opc = PPC::LD; */
388 } else if (RC == SPU::R32FPRegisterClass) {
389 /* Opc = PPC::LFD; */
390 } else if (RC == SPU::R64FPRegisterClass) {
391 /* Opc = PPC::LFS; */
392 } else if (RC == SPU::VECREGRegisterClass) {
393 /* Opc = PPC::LVX; */
394 } else if (RC == SPU::GPRCRegisterClass) {
395 /* Opc = something else! */
396 } else {
397 assert(0 && "Unknown regclass!");
398 abort();
399 }
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000400 MachineInstrBuilder MIB = BuildMI(MF, get(Opc), DestReg);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000401 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
402 MachineOperand &MO = Addr[i];
Dan Gohmand735b802008-10-03 15:45:36 +0000403 if (MO.isReg())
Owen Andersonf6372aa2008-01-01 21:11:32 +0000404 MIB.addReg(MO.getReg());
Dan Gohmand735b802008-10-03 15:45:36 +0000405 else if (MO.isImm())
Owen Andersonf6372aa2008-01-01 21:11:32 +0000406 MIB.addImm(MO.getImm());
407 else
408 MIB.addFrameIndex(MO.getIndex());
409 }
410 NewMIs.push_back(MIB);
411 }
412}
413
Scott Michel52d00012009-01-03 00:27:53 +0000414//! Return true if the specified load or store can be folded
415bool
416SPUInstrInfo::canFoldMemoryOperand(const MachineInstr *MI,
417 const SmallVectorImpl<unsigned> &Ops) const {
418 if (Ops.size() != 1) return false;
419
420 // Make sure this is a reg-reg copy.
421 unsigned Opc = MI->getOpcode();
422
423 switch (Opc) {
424 case SPU::ORv16i8:
425 case SPU::ORv8i16:
426 case SPU::ORv4i32:
427 case SPU::ORv2i64:
428 case SPU::ORr8:
429 case SPU::ORr16:
430 case SPU::ORr32:
431 case SPU::ORr64:
432 case SPU::ORf32:
433 case SPU::ORf64:
434 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg())
435 return true;
436 break;
437 }
438
439 return false;
440}
441
Owen Anderson43dbe052008-01-07 01:35:02 +0000442/// foldMemoryOperand - SPU, like PPC, can only fold spills into
443/// copy instructions, turning them into load/store instructions.
444MachineInstr *
Dan Gohmanc54baa22008-12-03 18:43:12 +0000445SPUInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
446 MachineInstr *MI,
447 const SmallVectorImpl<unsigned> &Ops,
448 int FrameIndex) const
Owen Anderson43dbe052008-01-07 01:35:02 +0000449{
Scott Michel52d00012009-01-03 00:27:53 +0000450 if (Ops.size() != 1) return 0;
Owen Anderson43dbe052008-01-07 01:35:02 +0000451
452 unsigned OpNum = Ops[0];
453 unsigned Opc = MI->getOpcode();
454 MachineInstr *NewMI = 0;
Scott Michel02d711b2008-12-30 23:28:25 +0000455
Scott Michel52d00012009-01-03 00:27:53 +0000456 switch (Opc) {
457 case SPU::ORv16i8:
458 case SPU::ORv8i16:
459 case SPU::ORv4i32:
460 case SPU::ORv2i64:
461 case SPU::ORr8:
462 case SPU::ORr16:
463 case SPU::ORr32:
464 case SPU::ORr64:
465 case SPU::ORf32:
466 case SPU::ORf64:
Owen Anderson43dbe052008-01-07 01:35:02 +0000467 if (OpNum == 0) { // move -> store
468 unsigned InReg = MI->getOperand(1).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000469 bool isKill = MI->getOperand(1).isKill();
Owen Anderson43dbe052008-01-07 01:35:02 +0000470 if (FrameIndex < SPUFrameInfo::maxFrameOffset()) {
Scott Michel52d00012009-01-03 00:27:53 +0000471 MachineInstrBuilder MIB = BuildMI(MF, get(SPU::STQDr32));
472
473 MIB.addReg(InReg, false, false, isKill);
474 NewMI = addFrameReference(MIB, FrameIndex);
Owen Anderson43dbe052008-01-07 01:35:02 +0000475 }
476 } else { // move -> load
477 unsigned OutReg = MI->getOperand(0).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000478 bool isDead = MI->getOperand(0).isDead();
Scott Michel52d00012009-01-03 00:27:53 +0000479 MachineInstrBuilder MIB = BuildMI(MF, get(Opc));
480
481 MIB.addReg(OutReg, true, false, false, isDead);
Evan Cheng9f1c8312008-07-03 09:09:37 +0000482 Opc = (FrameIndex < SPUFrameInfo::maxFrameOffset())
483 ? SPU::STQDr32 : SPU::STQXr32;
Scott Michel52d00012009-01-03 00:27:53 +0000484 NewMI = addFrameReference(MIB, FrameIndex);
485 break;
486 }
Owen Anderson43dbe052008-01-07 01:35:02 +0000487 }
488
Owen Anderson43dbe052008-01-07 01:35:02 +0000489 return NewMI;
Owen Anderson43dbe052008-01-07 01:35:02 +0000490}
491
Scott Michelaedc6372008-12-10 00:15:19 +0000492//! Branch analysis
Scott Michel9bd7a372009-01-02 20:52:08 +0000493/*!
Scott Michelaedc6372008-12-10 00:15:19 +0000494 \note This code was kiped from PPC. There may be more branch analysis for
495 CellSPU than what's currently done here.
496 */
497bool
498SPUInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
499 MachineBasicBlock *&FBB,
500 SmallVectorImpl<MachineOperand> &Cond) const {
501 // If the block has no terminators, it just falls into the block after it.
502 MachineBasicBlock::iterator I = MBB.end();
503 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
504 return false;
505
506 // Get the last instruction in the block.
507 MachineInstr *LastInst = I;
Scott Michel02d711b2008-12-30 23:28:25 +0000508
Scott Michelaedc6372008-12-10 00:15:19 +0000509 // If there is only one terminator instruction, process it.
510 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
511 if (isUncondBranch(LastInst)) {
512 TBB = LastInst->getOperand(0).getMBB();
513 return false;
514 } else if (isCondBranch(LastInst)) {
515 // Block ends with fall-through condbranch.
516 TBB = LastInst->getOperand(1).getMBB();
Scott Michel9bd7a372009-01-02 20:52:08 +0000517 DEBUG(cerr << "Pushing LastInst: ");
518 DEBUG(LastInst->dump());
519 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Scott Michelaedc6372008-12-10 00:15:19 +0000520 Cond.push_back(LastInst->getOperand(0));
Scott Michelaedc6372008-12-10 00:15:19 +0000521 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();
Scott Michel9bd7a372009-01-02 20:52:08 +0000538 DEBUG(cerr << "Pushing SecondLastInst: ");
539 DEBUG(SecondLastInst->dump());
540 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Scott Michelaedc6372008-12-10 00:15:19 +0000541 Cond.push_back(SecondLastInst->getOperand(0));
Scott Michelaedc6372008-12-10 00:15:19 +0000542 FBB = LastInst->getOperand(0).getMBB();
543 return false;
544 }
Scott Michel02d711b2008-12-30 23:28:25 +0000545
Scott Michelaedc6372008-12-10 00:15:19 +0000546 // If the block ends with two unconditional branches, handle it. The second
547 // one is not executed, so remove it.
548 if (isUncondBranch(SecondLastInst) && isUncondBranch(LastInst)) {
549 TBB = SecondLastInst->getOperand(0).getMBB();
550 I = LastInst;
551 I->eraseFromParent();
552 return false;
553 }
554
555 // Otherwise, can't handle this.
556 return true;
557}
Scott Michel02d711b2008-12-30 23:28:25 +0000558
Scott Michelaedc6372008-12-10 00:15:19 +0000559unsigned
560SPUInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
561 MachineBasicBlock::iterator I = MBB.end();
562 if (I == MBB.begin())
563 return 0;
564 --I;
565 if (!isCondBranch(I) && !isUncondBranch(I))
566 return 0;
567
568 // Remove the first branch.
Scott Michel9bd7a372009-01-02 20:52:08 +0000569 DEBUG(cerr << "Removing branch: ");
570 DEBUG(I->dump());
Scott Michelaedc6372008-12-10 00:15:19 +0000571 I->eraseFromParent();
572 I = MBB.end();
573 if (I == MBB.begin())
574 return 1;
575
576 --I;
Scott Michel9bd7a372009-01-02 20:52:08 +0000577 if (!(isCondBranch(I) || isUncondBranch(I)))
Scott Michelaedc6372008-12-10 00:15:19 +0000578 return 1;
579
580 // Remove the second branch.
Scott Michel9bd7a372009-01-02 20:52:08 +0000581 DEBUG(cerr << "Removing second branch: ");
582 DEBUG(I->dump());
Scott Michelaedc6372008-12-10 00:15:19 +0000583 I->eraseFromParent();
584 return 2;
585}
Scott Michel02d711b2008-12-30 23:28:25 +0000586
Scott Michelaedc6372008-12-10 00:15:19 +0000587unsigned
588SPUInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
589 MachineBasicBlock *FBB,
590 const SmallVectorImpl<MachineOperand> &Cond) const {
591 // Shouldn't be a fall through.
592 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Scott Michel02d711b2008-12-30 23:28:25 +0000593 assert((Cond.size() == 2 || Cond.size() == 0) &&
Scott Michelaedc6372008-12-10 00:15:19 +0000594 "SPU branch conditions have two components!");
Scott Michel02d711b2008-12-30 23:28:25 +0000595
Scott Michelaedc6372008-12-10 00:15:19 +0000596 // One-way branch.
597 if (FBB == 0) {
Scott Michel9bd7a372009-01-02 20:52:08 +0000598 if (Cond.empty()) {
599 // Unconditional branch
600 MachineInstrBuilder MIB = BuildMI(&MBB, get(SPU::BR));
601 MIB.addMBB(TBB);
602
603 DEBUG(cerr << "Inserted one-way uncond branch: ");
604 DEBUG((*MIB).dump());
605 } else {
606 // Conditional branch
607 MachineInstrBuilder MIB = BuildMI(&MBB, get(Cond[0].getImm()));
608 MIB.addReg(Cond[1].getReg()).addMBB(TBB);
609
610 DEBUG(cerr << "Inserted one-way cond branch: ");
611 DEBUG((*MIB).dump());
Scott Michelaedc6372008-12-10 00:15:19 +0000612 }
613 return 1;
Scott Michel9bd7a372009-01-02 20:52:08 +0000614 } else {
615 MachineInstrBuilder MIB = BuildMI(&MBB, get(Cond[0].getImm()));
616 MachineInstrBuilder MIB2 = BuildMI(&MBB, get(SPU::BR));
617
618 // Two-way Conditional Branch.
619 MIB.addReg(Cond[1].getReg()).addMBB(TBB);
620 MIB2.addMBB(FBB);
621
622 DEBUG(cerr << "Inserted conditional branch: ");
623 DEBUG((*MIB).dump());
624 DEBUG(cerr << "part 2: ");
625 DEBUG((*MIB2).dump());
626 return 2;
Scott Michelaedc6372008-12-10 00:15:19 +0000627 }
Scott Michelaedc6372008-12-10 00:15:19 +0000628}
629
Scott Michel52d00012009-01-03 00:27:53 +0000630bool
631SPUInstrInfo::BlockHasNoFallThrough(const MachineBasicBlock &MBB) const {
632 return (!MBB.empty() && isUncondBranch(&MBB.back()));
633}
634//! Reverses a branch's condition, returning false on success.
635bool
636SPUInstrInfo::ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond)
637 const {
638 // Pretty brainless way of inverting the condition, but it works, considering
639 // there are only two conditions...
640 static struct {
641 unsigned Opc; //! The incoming opcode
642 unsigned RevCondOpc; //! The reversed condition opcode
643 } revconds[] = {
644 { SPU::BRNZr32, SPU::BRZr32 },
645 { SPU::BRNZv4i32, SPU::BRZv4i32 },
646 { SPU::BRZr32, SPU::BRNZr32 },
647 { SPU::BRZv4i32, SPU::BRNZv4i32 },
648 { SPU::BRHNZr16, SPU::BRHZr16 },
649 { SPU::BRHNZv8i16, SPU::BRHZv8i16 },
650 { SPU::BRHZr16, SPU::BRHNZr16 },
651 { SPU::BRHZv8i16, SPU::BRHNZv8i16 }
652 };
Scott Michelaedc6372008-12-10 00:15:19 +0000653
Scott Michel52d00012009-01-03 00:27:53 +0000654 unsigned Opc = unsigned(Cond[0].getImm());
655 // Pretty dull mapping between the two conditions that SPU can generate:
656 for (int i = sizeof(revconds)/sizeof(revconds[0]); i >= 0; --i) {
657 if (revconds[i].Opc == Opc) {
658 Cond[0].setImm(revconds[i].RevCondOpc);
659 return false;
660 }
661 }
662
663 return true;
664}