blob: 5802eb68feec7637782b721fb843b36531bf501c [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,
Evan Cheng04ee5a12009-01-20 19:12:24 +000067 unsigned& destReg,
68 unsigned& SrcSR, unsigned& DstSR) const {
69 SrcSR = DstSR = 0; // No sub-registers.
70
Scott Michel66377522007-12-04 22:35:58 +000071 // Primarily, ORI and OR are generated by copyRegToReg. But, there are other
72 // cases where we can safely say that what's being done is really a move
73 // (see how PowerPC does this -- it's the model for this code too.)
74 switch (MI.getOpcode()) {
75 default:
76 break;
77 case SPU::ORIv4i32:
78 case SPU::ORIr32:
Scott Michel66377522007-12-04 22:35:58 +000079 case SPU::ORHIv8i16:
80 case SPU::ORHIr16:
Scott Michela59d4692008-02-23 18:41:37 +000081 case SPU::ORHIi8i16:
Scott Michel66377522007-12-04 22:35:58 +000082 case SPU::ORBIv16i8:
Scott Michel504c3692007-12-17 22:32:34 +000083 case SPU::ORBIr8:
Scott Michela59d4692008-02-23 18:41:37 +000084 case SPU::ORIi16i32:
85 case SPU::ORIi8i32:
Scott Michel66377522007-12-04 22:35:58 +000086 case SPU::AHIvec:
87 case SPU::AHIr16:
Scott Michel02d711b2008-12-30 23:28:25 +000088 case SPU::AIv4i32:
Scott Michel66377522007-12-04 22:35:58 +000089 assert(MI.getNumOperands() == 3 &&
Dan Gohmand735b802008-10-03 15:45:36 +000090 MI.getOperand(0).isReg() &&
91 MI.getOperand(1).isReg() &&
92 MI.getOperand(2).isImm() &&
Scott Michel66377522007-12-04 22:35:58 +000093 "invalid SPU ORI/ORHI/ORBI/AHI/AI/SFI/SFHI instruction!");
Chris Lattner9a1ceae2007-12-30 20:49:49 +000094 if (MI.getOperand(2).getImm() == 0) {
Scott Michel66377522007-12-04 22:35:58 +000095 sourceReg = MI.getOperand(1).getReg();
96 destReg = MI.getOperand(0).getReg();
97 return true;
98 }
99 break;
Scott Michel9999e682007-12-19 07:35:06 +0000100 case SPU::AIr32:
101 assert(MI.getNumOperands() == 3 &&
102 "wrong number of operands to AIr32");
Dan Gohmand735b802008-10-03 15:45:36 +0000103 if (MI.getOperand(0).isReg() &&
Scott Michel02d711b2008-12-30 23:28:25 +0000104 MI.getOperand(1).isReg() &&
Dan Gohmand735b802008-10-03 15:45:36 +0000105 (MI.getOperand(2).isImm() &&
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000106 MI.getOperand(2).getImm() == 0)) {
Scott Michel9999e682007-12-19 07:35:06 +0000107 sourceReg = MI.getOperand(1).getReg();
108 destReg = MI.getOperand(0).getReg();
109 return true;
110 }
111 break;
Scott Michelf0569be2008-12-27 04:51:36 +0000112 case SPU::LRr8:
113 case SPU::LRr16:
114 case SPU::LRr32:
115 case SPU::LRf32:
116 case SPU::LRr64:
117 case SPU::LRf64:
118 case SPU::LRr128:
119 case SPU::LRv16i8:
120 case SPU::LRv8i16:
121 case SPU::LRv4i32:
122 case SPU::LRv4f32:
123 case SPU::LRv2i64:
124 case SPU::LRv2f64:
Scott Michel170783a2007-12-19 20:15:47 +0000125 case SPU::ORv16i8_i8:
Scott Michel66377522007-12-04 22:35:58 +0000126 case SPU::ORv8i16_i16:
127 case SPU::ORv4i32_i32:
128 case SPU::ORv2i64_i64:
129 case SPU::ORv4f32_f32:
130 case SPU::ORv2f64_f64:
Scott Michel170783a2007-12-19 20:15:47 +0000131 case SPU::ORi8_v16i8:
Scott Michel66377522007-12-04 22:35:58 +0000132 case SPU::ORi16_v8i16:
133 case SPU::ORi32_v4i32:
134 case SPU::ORi64_v2i64:
135 case SPU::ORf32_v4f32:
Scott Micheldd950092009-01-06 03:36:14 +0000136 case SPU::ORf64_v2f64:
137 case SPU::ORi128_r64:
138 case SPU::ORi128_f64:
139 case SPU::ORi128_r32:
140 case SPU::ORi128_f32:
141 case SPU::ORi128_r16:
142 case SPU::ORi128_r8:
143 case SPU::ORi128_vec:
144 case SPU::ORr64_i128:
145 case SPU::ORf64_i128:
146 case SPU::ORr32_i128:
147 case SPU::ORf32_i128:
148 case SPU::ORr16_i128:
149 case SPU::ORr8_i128:
150 case SPU::ORvec_i128:
151 case SPU::ORr16_r32:
152 case SPU::ORr8_r32:
153 case SPU::ORr32_r16:
154 case SPU::ORr32_r8:
155 case SPU::ORr32_r64:
156 case SPU::ORr16_r64:
157 case SPU::ORr8_r64:
158 case SPU::ORr64_r32:
159 case SPU::ORr64_r16:
160 case SPU::ORr64_r8:
161 {
Scott Michelf0569be2008-12-27 04:51:36 +0000162 assert(MI.getNumOperands() == 2 &&
163 MI.getOperand(0).isReg() &&
164 MI.getOperand(1).isReg() &&
Scott Michel52d00012009-01-03 00:27:53 +0000165 "invalid SPU OR<type>_<vec> or LR instruction!");
Scott Michelf0569be2008-12-27 04:51:36 +0000166 if (MI.getOperand(0).getReg() == MI.getOperand(1).getReg()) {
167 sourceReg = MI.getOperand(0).getReg();
168 destReg = MI.getOperand(0).getReg();
169 return true;
170 }
171 break;
172 }
Scott Michel66377522007-12-04 22:35:58 +0000173 case SPU::ORv16i8:
174 case SPU::ORv8i16:
175 case SPU::ORv4i32:
Scott Michel52d00012009-01-03 00:27:53 +0000176 case SPU::ORv2i64:
177 case SPU::ORr8:
178 case SPU::ORr16:
Scott Michel66377522007-12-04 22:35:58 +0000179 case SPU::ORr32:
180 case SPU::ORr64:
Scott Michel86c041f2007-12-20 00:44:13 +0000181 case SPU::ORf32:
182 case SPU::ORf64:
Scott Michel66377522007-12-04 22:35:58 +0000183 assert(MI.getNumOperands() == 3 &&
Dan Gohmand735b802008-10-03 15:45:36 +0000184 MI.getOperand(0).isReg() &&
185 MI.getOperand(1).isReg() &&
186 MI.getOperand(2).isReg() &&
Scott Michel66377522007-12-04 22:35:58 +0000187 "invalid SPU OR(vec|r32|r64|gprc) instruction!");
188 if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
189 sourceReg = MI.getOperand(1).getReg();
190 destReg = MI.getOperand(0).getReg();
191 return true;
192 }
193 break;
194 }
195
196 return false;
197}
198
199unsigned
Dan Gohmancbad42c2008-11-18 19:49:32 +0000200SPUInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
201 int &FrameIndex) const {
Scott Michel66377522007-12-04 22:35:58 +0000202 switch (MI->getOpcode()) {
203 default: break;
204 case SPU::LQDv16i8:
205 case SPU::LQDv8i16:
206 case SPU::LQDv4i32:
207 case SPU::LQDv4f32:
208 case SPU::LQDv2f64:
209 case SPU::LQDr128:
210 case SPU::LQDr64:
211 case SPU::LQDr32:
Scott Michelaedc6372008-12-10 00:15:19 +0000212 case SPU::LQDr16: {
213 const MachineOperand MOp1 = MI->getOperand(1);
214 const MachineOperand MOp2 = MI->getOperand(2);
Scott Michel52d00012009-01-03 00:27:53 +0000215 if (MOp1.isImm() && MOp2.isFI()) {
216 FrameIndex = MOp2.getIndex();
Scott Michelaedc6372008-12-10 00:15:19 +0000217 return MI->getOperand(0).getReg();
218 }
219 break;
220 }
Scott Michel66377522007-12-04 22:35:58 +0000221 }
222 return 0;
223}
224
225unsigned
Dan Gohmancbad42c2008-11-18 19:49:32 +0000226SPUInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
227 int &FrameIndex) const {
Scott Michel66377522007-12-04 22:35:58 +0000228 switch (MI->getOpcode()) {
229 default: break;
230 case SPU::STQDv16i8:
231 case SPU::STQDv8i16:
232 case SPU::STQDv4i32:
233 case SPU::STQDv4f32:
234 case SPU::STQDv2f64:
235 case SPU::STQDr128:
236 case SPU::STQDr64:
237 case SPU::STQDr32:
238 case SPU::STQDr16:
Scott Michelaedc6372008-12-10 00:15:19 +0000239 case SPU::STQDr8: {
240 const MachineOperand MOp1 = MI->getOperand(1);
241 const MachineOperand MOp2 = MI->getOperand(2);
Scott Michelf0569be2008-12-27 04:51:36 +0000242 if (MOp1.isImm() && MOp2.isFI()) {
243 FrameIndex = MOp2.getIndex();
Scott Michelaedc6372008-12-10 00:15:19 +0000244 return MI->getOperand(0).getReg();
245 }
246 break;
247 }
Scott Michel66377522007-12-04 22:35:58 +0000248 }
249 return 0;
250}
Owen Andersond10fd972007-12-31 06:32:00 +0000251
Owen Anderson940f83e2008-08-26 18:03:31 +0000252bool SPUInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
Owen Andersond10fd972007-12-31 06:32:00 +0000253 MachineBasicBlock::iterator MI,
254 unsigned DestReg, unsigned SrcReg,
255 const TargetRegisterClass *DestRC,
256 const TargetRegisterClass *SrcRC) const
257{
Chris Lattner5e09da22008-03-09 20:31:11 +0000258 // We support cross register class moves for our aliases, such as R3 in any
259 // reg class to any other reg class containing R3. This is required because
260 // we instruction select bitconvert i64 -> f64 as a noop for example, so our
261 // types have no specific meaning.
Scott Michel02d711b2008-12-30 23:28:25 +0000262
Owen Andersond10fd972007-12-31 06:32:00 +0000263 if (DestRC == SPU::R8CRegisterClass) {
Scott Michel9bd7a372009-01-02 20:52:08 +0000264 BuildMI(MBB, MI, get(SPU::LRr8), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000265 } else if (DestRC == SPU::R16CRegisterClass) {
Scott Michel9bd7a372009-01-02 20:52:08 +0000266 BuildMI(MBB, MI, get(SPU::LRr16), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000267 } else if (DestRC == SPU::R32CRegisterClass) {
Scott Michel9bd7a372009-01-02 20:52:08 +0000268 BuildMI(MBB, MI, get(SPU::LRr32), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000269 } else if (DestRC == SPU::R32FPRegisterClass) {
Scott Michel9bd7a372009-01-02 20:52:08 +0000270 BuildMI(MBB, MI, get(SPU::LRf32), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000271 } else if (DestRC == SPU::R64CRegisterClass) {
Scott Michel9bd7a372009-01-02 20:52:08 +0000272 BuildMI(MBB, MI, get(SPU::LRr64), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000273 } else if (DestRC == SPU::R64FPRegisterClass) {
Scott Michel9bd7a372009-01-02 20:52:08 +0000274 BuildMI(MBB, MI, get(SPU::LRf64), DestReg).addReg(SrcReg);
275 } else if (DestRC == SPU::GPRCRegisterClass) {
276 BuildMI(MBB, MI, get(SPU::LRr128), DestReg).addReg(SrcReg);
277 } else if (DestRC == SPU::VECREGRegisterClass) {
278 BuildMI(MBB, MI, get(SPU::LRv16i8), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000279 } else {
Owen Anderson940f83e2008-08-26 18:03:31 +0000280 // Attempt to copy unknown/unsupported register class!
281 return false;
Owen Andersond10fd972007-12-31 06:32:00 +0000282 }
Scott Michel02d711b2008-12-30 23:28:25 +0000283
Owen Anderson940f83e2008-08-26 18:03:31 +0000284 return true;
Owen Andersond10fd972007-12-31 06:32:00 +0000285}
Owen Andersonf6372aa2008-01-01 21:11:32 +0000286
287void
288SPUInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
289 MachineBasicBlock::iterator MI,
290 unsigned SrcReg, bool isKill, int FrameIdx,
291 const TargetRegisterClass *RC) const
292{
Chris Lattnercc8cd0c2008-01-07 02:48:55 +0000293 unsigned opc;
Scott Michelaedc6372008-12-10 00:15:19 +0000294 bool isValidFrameIdx = (FrameIdx < SPUFrameInfo::maxFrameOffset());
Owen Andersonf6372aa2008-01-01 21:11:32 +0000295 if (RC == SPU::GPRCRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000296 opc = (isValidFrameIdx ? SPU::STQDr128 : SPU::STQXr128);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000297 } else if (RC == SPU::R64CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000298 opc = (isValidFrameIdx ? SPU::STQDr64 : SPU::STQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000299 } else if (RC == SPU::R64FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000300 opc = (isValidFrameIdx ? SPU::STQDr64 : SPU::STQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000301 } else if (RC == SPU::R32CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000302 opc = (isValidFrameIdx ? SPU::STQDr32 : SPU::STQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000303 } else if (RC == SPU::R32FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000304 opc = (isValidFrameIdx ? SPU::STQDr32 : SPU::STQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000305 } else if (RC == SPU::R16CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000306 opc = (isValidFrameIdx ? SPU::STQDr16 : SPU::STQXr16);
307 } else if (RC == SPU::R8CRegisterClass) {
308 opc = (isValidFrameIdx ? SPU::STQDr8 : SPU::STQXr8);
Scott Michelf0569be2008-12-27 04:51:36 +0000309 } else if (RC == SPU::VECREGRegisterClass) {
310 opc = (isValidFrameIdx) ? SPU::STQDv16i8 : SPU::STQXv16i8;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000311 } else {
312 assert(0 && "Unknown regclass!");
313 abort();
314 }
315
316 addFrameReference(BuildMI(MBB, MI, get(opc))
317 .addReg(SrcReg, false, false, isKill), FrameIdx);
318}
319
320void SPUInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
321 bool isKill,
322 SmallVectorImpl<MachineOperand> &Addr,
323 const TargetRegisterClass *RC,
324 SmallVectorImpl<MachineInstr*> &NewMIs) const {
325 cerr << "storeRegToAddr() invoked!\n";
326 abort();
327
Dan Gohmand735b802008-10-03 15:45:36 +0000328 if (Addr[0].isFI()) {
Owen Andersonf6372aa2008-01-01 21:11:32 +0000329 /* do what storeRegToStackSlot does here */
330 } else {
331 unsigned Opc = 0;
332 if (RC == SPU::GPRCRegisterClass) {
333 /* Opc = PPC::STW; */
334 } else if (RC == SPU::R16CRegisterClass) {
335 /* Opc = PPC::STD; */
336 } else if (RC == SPU::R32CRegisterClass) {
337 /* Opc = PPC::STFD; */
338 } else if (RC == SPU::R32FPRegisterClass) {
339 /* Opc = PPC::STFD; */
340 } else if (RC == SPU::R64FPRegisterClass) {
341 /* Opc = PPC::STFS; */
342 } else if (RC == SPU::VECREGRegisterClass) {
343 /* Opc = PPC::STVX; */
344 } else {
345 assert(0 && "Unknown regclass!");
346 abort();
347 }
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000348 MachineInstrBuilder MIB = BuildMI(MF, get(Opc))
Owen Andersonf6372aa2008-01-01 21:11:32 +0000349 .addReg(SrcReg, false, false, isKill);
350 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
351 MachineOperand &MO = Addr[i];
Dan Gohmand735b802008-10-03 15:45:36 +0000352 if (MO.isReg())
Owen Andersonf6372aa2008-01-01 21:11:32 +0000353 MIB.addReg(MO.getReg());
Dan Gohmand735b802008-10-03 15:45:36 +0000354 else if (MO.isImm())
Owen Andersonf6372aa2008-01-01 21:11:32 +0000355 MIB.addImm(MO.getImm());
356 else
357 MIB.addFrameIndex(MO.getIndex());
358 }
359 NewMIs.push_back(MIB);
360 }
361}
362
363void
364SPUInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
365 MachineBasicBlock::iterator MI,
366 unsigned DestReg, int FrameIdx,
367 const TargetRegisterClass *RC) const
368{
Chris Lattnercc8cd0c2008-01-07 02:48:55 +0000369 unsigned opc;
Scott Michelaedc6372008-12-10 00:15:19 +0000370 bool isValidFrameIdx = (FrameIdx < SPUFrameInfo::maxFrameOffset());
Owen Andersonf6372aa2008-01-01 21:11:32 +0000371 if (RC == SPU::GPRCRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000372 opc = (isValidFrameIdx ? SPU::LQDr128 : SPU::LQXr128);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000373 } else if (RC == SPU::R64CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000374 opc = (isValidFrameIdx ? SPU::LQDr64 : SPU::LQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000375 } else if (RC == SPU::R64FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000376 opc = (isValidFrameIdx ? SPU::LQDr64 : SPU::LQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000377 } else if (RC == SPU::R32CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000378 opc = (isValidFrameIdx ? SPU::LQDr32 : SPU::LQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000379 } else if (RC == SPU::R32FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000380 opc = (isValidFrameIdx ? SPU::LQDr32 : SPU::LQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000381 } else if (RC == SPU::R16CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000382 opc = (isValidFrameIdx ? SPU::LQDr16 : SPU::LQXr16);
383 } else if (RC == SPU::R8CRegisterClass) {
384 opc = (isValidFrameIdx ? SPU::LQDr8 : SPU::LQXr8);
Scott Michelf0569be2008-12-27 04:51:36 +0000385 } else if (RC == SPU::VECREGRegisterClass) {
386 opc = (isValidFrameIdx) ? SPU::LQDv16i8 : SPU::LQXv16i8;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000387 } else {
388 assert(0 && "Unknown regclass in loadRegFromStackSlot!");
389 abort();
390 }
391
392 addFrameReference(BuildMI(MBB, MI, get(opc)).addReg(DestReg), FrameIdx);
393}
394
395/*!
396 \note We are really pessimistic here about what kind of a load we're doing.
397 */
398void SPUInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
Scott Michelaedc6372008-12-10 00:15:19 +0000399 SmallVectorImpl<MachineOperand> &Addr,
400 const TargetRegisterClass *RC,
401 SmallVectorImpl<MachineInstr*> &NewMIs)
Owen Andersonf6372aa2008-01-01 21:11:32 +0000402 const {
403 cerr << "loadRegToAddr() invoked!\n";
404 abort();
405
Dan Gohmand735b802008-10-03 15:45:36 +0000406 if (Addr[0].isFI()) {
Owen Andersonf6372aa2008-01-01 21:11:32 +0000407 /* do what loadRegFromStackSlot does here... */
408 } else {
409 unsigned Opc = 0;
410 if (RC == SPU::R8CRegisterClass) {
411 /* do brilliance here */
412 } else if (RC == SPU::R16CRegisterClass) {
413 /* Opc = PPC::LWZ; */
414 } else if (RC == SPU::R32CRegisterClass) {
415 /* Opc = PPC::LD; */
416 } else if (RC == SPU::R32FPRegisterClass) {
417 /* Opc = PPC::LFD; */
418 } else if (RC == SPU::R64FPRegisterClass) {
419 /* Opc = PPC::LFS; */
420 } else if (RC == SPU::VECREGRegisterClass) {
421 /* Opc = PPC::LVX; */
422 } else if (RC == SPU::GPRCRegisterClass) {
423 /* Opc = something else! */
424 } else {
425 assert(0 && "Unknown regclass!");
426 abort();
427 }
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000428 MachineInstrBuilder MIB = BuildMI(MF, get(Opc), DestReg);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000429 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
430 MachineOperand &MO = Addr[i];
Dan Gohmand735b802008-10-03 15:45:36 +0000431 if (MO.isReg())
Owen Andersonf6372aa2008-01-01 21:11:32 +0000432 MIB.addReg(MO.getReg());
Dan Gohmand735b802008-10-03 15:45:36 +0000433 else if (MO.isImm())
Owen Andersonf6372aa2008-01-01 21:11:32 +0000434 MIB.addImm(MO.getImm());
435 else
436 MIB.addFrameIndex(MO.getIndex());
437 }
438 NewMIs.push_back(MIB);
439 }
440}
441
Scott Michel52d00012009-01-03 00:27:53 +0000442//! Return true if the specified load or store can be folded
443bool
444SPUInstrInfo::canFoldMemoryOperand(const MachineInstr *MI,
445 const SmallVectorImpl<unsigned> &Ops) const {
446 if (Ops.size() != 1) return false;
447
448 // Make sure this is a reg-reg copy.
449 unsigned Opc = MI->getOpcode();
450
451 switch (Opc) {
452 case SPU::ORv16i8:
453 case SPU::ORv8i16:
454 case SPU::ORv4i32:
455 case SPU::ORv2i64:
456 case SPU::ORr8:
457 case SPU::ORr16:
458 case SPU::ORr32:
459 case SPU::ORr64:
460 case SPU::ORf32:
461 case SPU::ORf64:
462 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg())
463 return true;
464 break;
465 }
466
467 return false;
468}
469
Owen Anderson43dbe052008-01-07 01:35:02 +0000470/// foldMemoryOperand - SPU, like PPC, can only fold spills into
471/// copy instructions, turning them into load/store instructions.
472MachineInstr *
Dan Gohmanc54baa22008-12-03 18:43:12 +0000473SPUInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
474 MachineInstr *MI,
475 const SmallVectorImpl<unsigned> &Ops,
476 int FrameIndex) const
Owen Anderson43dbe052008-01-07 01:35:02 +0000477{
Scott Michel52d00012009-01-03 00:27:53 +0000478 if (Ops.size() != 1) return 0;
Owen Anderson43dbe052008-01-07 01:35:02 +0000479
480 unsigned OpNum = Ops[0];
481 unsigned Opc = MI->getOpcode();
482 MachineInstr *NewMI = 0;
Scott Michel02d711b2008-12-30 23:28:25 +0000483
Scott Michel52d00012009-01-03 00:27:53 +0000484 switch (Opc) {
485 case SPU::ORv16i8:
486 case SPU::ORv8i16:
487 case SPU::ORv4i32:
488 case SPU::ORv2i64:
489 case SPU::ORr8:
490 case SPU::ORr16:
491 case SPU::ORr32:
492 case SPU::ORr64:
493 case SPU::ORf32:
494 case SPU::ORf64:
Owen Anderson43dbe052008-01-07 01:35:02 +0000495 if (OpNum == 0) { // move -> store
496 unsigned InReg = MI->getOperand(1).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000497 bool isKill = MI->getOperand(1).isKill();
Owen Anderson43dbe052008-01-07 01:35:02 +0000498 if (FrameIndex < SPUFrameInfo::maxFrameOffset()) {
Scott Michel52d00012009-01-03 00:27:53 +0000499 MachineInstrBuilder MIB = BuildMI(MF, get(SPU::STQDr32));
500
501 MIB.addReg(InReg, false, false, isKill);
502 NewMI = addFrameReference(MIB, FrameIndex);
Owen Anderson43dbe052008-01-07 01:35:02 +0000503 }
504 } else { // move -> load
505 unsigned OutReg = MI->getOperand(0).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000506 bool isDead = MI->getOperand(0).isDead();
Scott Michel52d00012009-01-03 00:27:53 +0000507 MachineInstrBuilder MIB = BuildMI(MF, get(Opc));
508
509 MIB.addReg(OutReg, true, false, false, isDead);
Evan Cheng9f1c8312008-07-03 09:09:37 +0000510 Opc = (FrameIndex < SPUFrameInfo::maxFrameOffset())
511 ? SPU::STQDr32 : SPU::STQXr32;
Scott Michel52d00012009-01-03 00:27:53 +0000512 NewMI = addFrameReference(MIB, FrameIndex);
513 break;
514 }
Owen Anderson43dbe052008-01-07 01:35:02 +0000515 }
516
Owen Anderson43dbe052008-01-07 01:35:02 +0000517 return NewMI;
Owen Anderson43dbe052008-01-07 01:35:02 +0000518}
519
Scott Michelaedc6372008-12-10 00:15:19 +0000520//! Branch analysis
Scott Michel9bd7a372009-01-02 20:52:08 +0000521/*!
Scott Michelaedc6372008-12-10 00:15:19 +0000522 \note This code was kiped from PPC. There may be more branch analysis for
523 CellSPU than what's currently done here.
524 */
525bool
526SPUInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
527 MachineBasicBlock *&FBB,
528 SmallVectorImpl<MachineOperand> &Cond) const {
529 // If the block has no terminators, it just falls into the block after it.
530 MachineBasicBlock::iterator I = MBB.end();
531 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
532 return false;
533
534 // Get the last instruction in the block.
535 MachineInstr *LastInst = I;
Scott Michel02d711b2008-12-30 23:28:25 +0000536
Scott Michelaedc6372008-12-10 00:15:19 +0000537 // If there is only one terminator instruction, process it.
538 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
539 if (isUncondBranch(LastInst)) {
540 TBB = LastInst->getOperand(0).getMBB();
541 return false;
542 } else if (isCondBranch(LastInst)) {
543 // Block ends with fall-through condbranch.
544 TBB = LastInst->getOperand(1).getMBB();
Scott Michel9bd7a372009-01-02 20:52:08 +0000545 DEBUG(cerr << "Pushing LastInst: ");
546 DEBUG(LastInst->dump());
547 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Scott Michelaedc6372008-12-10 00:15:19 +0000548 Cond.push_back(LastInst->getOperand(0));
Scott Michelaedc6372008-12-10 00:15:19 +0000549 return false;
550 }
551 // Otherwise, don't know what this is.
552 return true;
553 }
Scott Michel02d711b2008-12-30 23:28:25 +0000554
Scott Michelaedc6372008-12-10 00:15:19 +0000555 // Get the instruction before it if it's a terminator.
556 MachineInstr *SecondLastInst = I;
557
558 // If there are three terminators, we don't know what sort of block this is.
559 if (SecondLastInst && I != MBB.begin() &&
560 isUnpredicatedTerminator(--I))
561 return true;
Scott Michel02d711b2008-12-30 23:28:25 +0000562
Scott Michelaedc6372008-12-10 00:15:19 +0000563 // If the block ends with a conditional and unconditional branch, handle it.
564 if (isCondBranch(SecondLastInst) && isUncondBranch(LastInst)) {
565 TBB = SecondLastInst->getOperand(1).getMBB();
Scott Michel9bd7a372009-01-02 20:52:08 +0000566 DEBUG(cerr << "Pushing SecondLastInst: ");
567 DEBUG(SecondLastInst->dump());
568 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Scott Michelaedc6372008-12-10 00:15:19 +0000569 Cond.push_back(SecondLastInst->getOperand(0));
Scott Michelaedc6372008-12-10 00:15:19 +0000570 FBB = LastInst->getOperand(0).getMBB();
571 return false;
572 }
Scott Michel02d711b2008-12-30 23:28:25 +0000573
Scott Michelaedc6372008-12-10 00:15:19 +0000574 // If the block ends with two unconditional branches, handle it. The second
575 // one is not executed, so remove it.
576 if (isUncondBranch(SecondLastInst) && isUncondBranch(LastInst)) {
577 TBB = SecondLastInst->getOperand(0).getMBB();
578 I = LastInst;
579 I->eraseFromParent();
580 return false;
581 }
582
583 // Otherwise, can't handle this.
584 return true;
585}
Scott Michel02d711b2008-12-30 23:28:25 +0000586
Scott Michelaedc6372008-12-10 00:15:19 +0000587unsigned
588SPUInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
589 MachineBasicBlock::iterator I = MBB.end();
590 if (I == MBB.begin())
591 return 0;
592 --I;
593 if (!isCondBranch(I) && !isUncondBranch(I))
594 return 0;
595
596 // Remove the first branch.
Scott Michel9bd7a372009-01-02 20:52:08 +0000597 DEBUG(cerr << "Removing branch: ");
598 DEBUG(I->dump());
Scott Michelaedc6372008-12-10 00:15:19 +0000599 I->eraseFromParent();
600 I = MBB.end();
601 if (I == MBB.begin())
602 return 1;
603
604 --I;
Scott Michel9bd7a372009-01-02 20:52:08 +0000605 if (!(isCondBranch(I) || isUncondBranch(I)))
Scott Michelaedc6372008-12-10 00:15:19 +0000606 return 1;
607
608 // Remove the second branch.
Scott Michel9bd7a372009-01-02 20:52:08 +0000609 DEBUG(cerr << "Removing second branch: ");
610 DEBUG(I->dump());
Scott Michelaedc6372008-12-10 00:15:19 +0000611 I->eraseFromParent();
612 return 2;
613}
Scott Michel02d711b2008-12-30 23:28:25 +0000614
Scott Michelaedc6372008-12-10 00:15:19 +0000615unsigned
616SPUInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
617 MachineBasicBlock *FBB,
618 const SmallVectorImpl<MachineOperand> &Cond) const {
619 // Shouldn't be a fall through.
620 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Scott Michel02d711b2008-12-30 23:28:25 +0000621 assert((Cond.size() == 2 || Cond.size() == 0) &&
Scott Michelaedc6372008-12-10 00:15:19 +0000622 "SPU branch conditions have two components!");
Scott Michel02d711b2008-12-30 23:28:25 +0000623
Scott Michelaedc6372008-12-10 00:15:19 +0000624 // One-way branch.
625 if (FBB == 0) {
Scott Michel9bd7a372009-01-02 20:52:08 +0000626 if (Cond.empty()) {
627 // Unconditional branch
628 MachineInstrBuilder MIB = BuildMI(&MBB, get(SPU::BR));
629 MIB.addMBB(TBB);
630
631 DEBUG(cerr << "Inserted one-way uncond branch: ");
632 DEBUG((*MIB).dump());
633 } else {
634 // Conditional branch
635 MachineInstrBuilder MIB = BuildMI(&MBB, get(Cond[0].getImm()));
636 MIB.addReg(Cond[1].getReg()).addMBB(TBB);
637
638 DEBUG(cerr << "Inserted one-way cond branch: ");
639 DEBUG((*MIB).dump());
Scott Michelaedc6372008-12-10 00:15:19 +0000640 }
641 return 1;
Scott Michel9bd7a372009-01-02 20:52:08 +0000642 } else {
643 MachineInstrBuilder MIB = BuildMI(&MBB, get(Cond[0].getImm()));
644 MachineInstrBuilder MIB2 = BuildMI(&MBB, get(SPU::BR));
645
646 // Two-way Conditional Branch.
647 MIB.addReg(Cond[1].getReg()).addMBB(TBB);
648 MIB2.addMBB(FBB);
649
650 DEBUG(cerr << "Inserted conditional branch: ");
651 DEBUG((*MIB).dump());
652 DEBUG(cerr << "part 2: ");
653 DEBUG((*MIB2).dump());
654 return 2;
Scott Michelaedc6372008-12-10 00:15:19 +0000655 }
Scott Michelaedc6372008-12-10 00:15:19 +0000656}
657
Scott Michel52d00012009-01-03 00:27:53 +0000658bool
659SPUInstrInfo::BlockHasNoFallThrough(const MachineBasicBlock &MBB) const {
660 return (!MBB.empty() && isUncondBranch(&MBB.back()));
661}
662//! Reverses a branch's condition, returning false on success.
663bool
664SPUInstrInfo::ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond)
665 const {
666 // Pretty brainless way of inverting the condition, but it works, considering
667 // there are only two conditions...
668 static struct {
669 unsigned Opc; //! The incoming opcode
670 unsigned RevCondOpc; //! The reversed condition opcode
671 } revconds[] = {
672 { SPU::BRNZr32, SPU::BRZr32 },
673 { SPU::BRNZv4i32, SPU::BRZv4i32 },
674 { SPU::BRZr32, SPU::BRNZr32 },
675 { SPU::BRZv4i32, SPU::BRNZv4i32 },
676 { SPU::BRHNZr16, SPU::BRHZr16 },
677 { SPU::BRHNZv8i16, SPU::BRHZv8i16 },
678 { SPU::BRHZr16, SPU::BRHNZr16 },
679 { SPU::BRHZv8i16, SPU::BRHNZv8i16 }
680 };
Scott Michelaedc6372008-12-10 00:15:19 +0000681
Scott Michel52d00012009-01-03 00:27:53 +0000682 unsigned Opc = unsigned(Cond[0].getImm());
683 // Pretty dull mapping between the two conditions that SPU can generate:
Misha Brukman93c65c82009-01-07 23:07:29 +0000684 for (int i = sizeof(revconds)/sizeof(revconds[0]) - 1; i >= 0; --i) {
Scott Michel52d00012009-01-03 00:27:53 +0000685 if (revconds[i].Opc == Opc) {
686 Cond[0].setImm(revconds[i].RevCondOpc);
687 return false;
688 }
689 }
690
691 return true;
692}