blob: 225e217638ad42858510a80c52f9f3dc651b68b9 [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
35 inline bool isCondBranch(const MachineInstr *I) {
36 unsigned opc = I->getOpcode();
37
Scott Michelf0569be2008-12-27 04:51:36 +000038 return (opc == SPU::BRNZr32
39 || opc == SPU::BRNZv4i32
40 || opc == SPU::BRZr32
41 || opc == SPU::BRZv4i32
42 || opc == SPU::BRHNZr16
43 || opc == SPU::BRHNZv8i16
44 || opc == SPU::BRHZr16
45 || opc == SPU::BRHZv8i16);
Scott Michelaedc6372008-12-10 00:15:19 +000046 }
47}
48
Scott Michel66377522007-12-04 22:35:58 +000049SPUInstrInfo::SPUInstrInfo(SPUTargetMachine &tm)
Chris Lattner64105522008-01-01 01:03:04 +000050 : TargetInstrInfoImpl(SPUInsts, sizeof(SPUInsts)/sizeof(SPUInsts[0])),
Scott Michel66377522007-12-04 22:35:58 +000051 TM(tm),
52 RI(*TM.getSubtargetImpl(), *this)
53{
54 /* NOP */
55}
56
57/// getPointerRegClass - Return the register class to use to hold pointers.
58/// This is used for addressing modes.
59const TargetRegisterClass *
60SPUInstrInfo::getPointerRegClass() const
61{
62 return &SPU::R32CRegClass;
63}
64
65bool
66SPUInstrInfo::isMoveInstr(const MachineInstr& MI,
67 unsigned& sourceReg,
68 unsigned& destReg) const {
69 // Primarily, ORI and OR are generated by copyRegToReg. But, there are other
70 // cases where we can safely say that what's being done is really a move
71 // (see how PowerPC does this -- it's the model for this code too.)
72 switch (MI.getOpcode()) {
73 default:
74 break;
75 case SPU::ORIv4i32:
76 case SPU::ORIr32:
Scott Michel66377522007-12-04 22:35:58 +000077 case SPU::ORHIv8i16:
78 case SPU::ORHIr16:
Scott Michela59d4692008-02-23 18:41:37 +000079 case SPU::ORHIi8i16:
Scott Michel66377522007-12-04 22:35:58 +000080 case SPU::ORBIv16i8:
Scott Michel504c3692007-12-17 22:32:34 +000081 case SPU::ORBIr8:
Scott Michela59d4692008-02-23 18:41:37 +000082 case SPU::ORIi16i32:
83 case SPU::ORIi8i32:
Scott Michel66377522007-12-04 22:35:58 +000084 case SPU::AHIvec:
85 case SPU::AHIr16:
Scott Michel02d711b2008-12-30 23:28:25 +000086 case SPU::AIv4i32:
Scott Michel66377522007-12-04 22:35:58 +000087 assert(MI.getNumOperands() == 3 &&
Dan Gohmand735b802008-10-03 15:45:36 +000088 MI.getOperand(0).isReg() &&
89 MI.getOperand(1).isReg() &&
90 MI.getOperand(2).isImm() &&
Scott Michel66377522007-12-04 22:35:58 +000091 "invalid SPU ORI/ORHI/ORBI/AHI/AI/SFI/SFHI instruction!");
Chris Lattner9a1ceae2007-12-30 20:49:49 +000092 if (MI.getOperand(2).getImm() == 0) {
Scott Michel66377522007-12-04 22:35:58 +000093 sourceReg = MI.getOperand(1).getReg();
94 destReg = MI.getOperand(0).getReg();
95 return true;
96 }
97 break;
Scott Michel9999e682007-12-19 07:35:06 +000098 case SPU::AIr32:
99 assert(MI.getNumOperands() == 3 &&
100 "wrong number of operands to AIr32");
Dan Gohmand735b802008-10-03 15:45:36 +0000101 if (MI.getOperand(0).isReg() &&
Scott Michel02d711b2008-12-30 23:28:25 +0000102 MI.getOperand(1).isReg() &&
Dan Gohmand735b802008-10-03 15:45:36 +0000103 (MI.getOperand(2).isImm() &&
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000104 MI.getOperand(2).getImm() == 0)) {
Scott Michel9999e682007-12-19 07:35:06 +0000105 sourceReg = MI.getOperand(1).getReg();
106 destReg = MI.getOperand(0).getReg();
107 return true;
108 }
109 break;
Scott Michelf0569be2008-12-27 04:51:36 +0000110 case SPU::LRr8:
111 case SPU::LRr16:
112 case SPU::LRr32:
113 case SPU::LRf32:
114 case SPU::LRr64:
115 case SPU::LRf64:
116 case SPU::LRr128:
117 case SPU::LRv16i8:
118 case SPU::LRv8i16:
119 case SPU::LRv4i32:
120 case SPU::LRv4f32:
121 case SPU::LRv2i64:
122 case SPU::LRv2f64:
Scott Michel170783a2007-12-19 20:15:47 +0000123 case SPU::ORv16i8_i8:
Scott Michel66377522007-12-04 22:35:58 +0000124 case SPU::ORv8i16_i16:
125 case SPU::ORv4i32_i32:
126 case SPU::ORv2i64_i64:
127 case SPU::ORv4f32_f32:
128 case SPU::ORv2f64_f64:
Scott Michel170783a2007-12-19 20:15:47 +0000129 case SPU::ORi8_v16i8:
Scott Michel66377522007-12-04 22:35:58 +0000130 case SPU::ORi16_v8i16:
131 case SPU::ORi32_v4i32:
132 case SPU::ORi64_v2i64:
133 case SPU::ORf32_v4f32:
Scott Michelf0569be2008-12-27 04:51:36 +0000134 case SPU::ORf64_v2f64: {
135 assert(MI.getNumOperands() == 2 &&
136 MI.getOperand(0).isReg() &&
137 MI.getOperand(1).isReg() &&
138 "invalid SPU OR<type>_<vec> instruction!");
139 if (MI.getOperand(0).getReg() == MI.getOperand(1).getReg()) {
140 sourceReg = MI.getOperand(0).getReg();
141 destReg = MI.getOperand(0).getReg();
142 return true;
143 }
144 break;
145 }
Scott Michel66377522007-12-04 22:35:58 +0000146 case SPU::ORv16i8:
147 case SPU::ORv8i16:
148 case SPU::ORv4i32:
149 case SPU::ORr32:
150 case SPU::ORr64:
Scott Michel86c041f2007-12-20 00:44:13 +0000151 case SPU::ORf32:
152 case SPU::ORf64:
Scott Michel66377522007-12-04 22:35:58 +0000153 assert(MI.getNumOperands() == 3 &&
Dan Gohmand735b802008-10-03 15:45:36 +0000154 MI.getOperand(0).isReg() &&
155 MI.getOperand(1).isReg() &&
156 MI.getOperand(2).isReg() &&
Scott Michel66377522007-12-04 22:35:58 +0000157 "invalid SPU OR(vec|r32|r64|gprc) instruction!");
158 if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
159 sourceReg = MI.getOperand(1).getReg();
160 destReg = MI.getOperand(0).getReg();
161 return true;
162 }
163 break;
164 }
165
166 return false;
167}
168
169unsigned
Dan Gohmancbad42c2008-11-18 19:49:32 +0000170SPUInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
171 int &FrameIndex) const {
Scott Michel66377522007-12-04 22:35:58 +0000172 switch (MI->getOpcode()) {
173 default: break;
174 case SPU::LQDv16i8:
175 case SPU::LQDv8i16:
176 case SPU::LQDv4i32:
177 case SPU::LQDv4f32:
178 case SPU::LQDv2f64:
179 case SPU::LQDr128:
180 case SPU::LQDr64:
181 case SPU::LQDr32:
Scott Michelaedc6372008-12-10 00:15:19 +0000182 case SPU::LQDr16: {
183 const MachineOperand MOp1 = MI->getOperand(1);
184 const MachineOperand MOp2 = MI->getOperand(2);
185 if (MOp1.isImm()
186 && (MOp2.isFI()
187 || (MOp2.isReg() && MOp2.getReg() == SPU::R1))) {
188 if (MOp2.isFI())
189 FrameIndex = MOp2.getIndex();
190 else
191 FrameIndex = MOp1.getImm() / SPUFrameInfo::stackSlotSize();
192 return MI->getOperand(0).getReg();
193 }
194 break;
195 }
Scott Michel66377522007-12-04 22:35:58 +0000196 case SPU::LQXv4i32:
197 case SPU::LQXr128:
198 case SPU::LQXr64:
199 case SPU::LQXr32:
200 case SPU::LQXr16:
Scott Michelaedc6372008-12-10 00:15:19 +0000201 if (MI->getOperand(1).isReg() && MI->getOperand(2).isReg()
202 && (MI->getOperand(2).getReg() == SPU::R1
203 || MI->getOperand(1).getReg() == SPU::R1)) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000204 FrameIndex = MI->getOperand(2).getIndex();
Scott Michel66377522007-12-04 22:35:58 +0000205 return MI->getOperand(0).getReg();
206 }
207 break;
208 }
209 return 0;
210}
211
212unsigned
Dan Gohmancbad42c2008-11-18 19:49:32 +0000213SPUInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
214 int &FrameIndex) const {
Scott Michel66377522007-12-04 22:35:58 +0000215 switch (MI->getOpcode()) {
216 default: break;
217 case SPU::STQDv16i8:
218 case SPU::STQDv8i16:
219 case SPU::STQDv4i32:
220 case SPU::STQDv4f32:
221 case SPU::STQDv2f64:
222 case SPU::STQDr128:
223 case SPU::STQDr64:
224 case SPU::STQDr32:
225 case SPU::STQDr16:
Scott Michelaedc6372008-12-10 00:15:19 +0000226 case SPU::STQDr8: {
227 const MachineOperand MOp1 = MI->getOperand(1);
228 const MachineOperand MOp2 = MI->getOperand(2);
Scott Michelf0569be2008-12-27 04:51:36 +0000229 if (MOp1.isImm() && MOp2.isFI()) {
230 FrameIndex = MOp2.getIndex();
Scott Michelaedc6372008-12-10 00:15:19 +0000231 return MI->getOperand(0).getReg();
232 }
233 break;
234 }
Scott Michelf0569be2008-12-27 04:51:36 +0000235#if 0
236 case SPU::STQXv16i8:
Scott Michel66377522007-12-04 22:35:58 +0000237 case SPU::STQXv8i16:
238 case SPU::STQXv4i32:
239 case SPU::STQXv4f32:
240 case SPU::STQXv2f64:
241 case SPU::STQXr128:
242 case SPU::STQXr64:
243 case SPU::STQXr32:
244 case SPU::STQXr16:
Scott Michel9c0c6b22008-11-21 02:56:16 +0000245 case SPU::STQXr8:
Scott Michelaedc6372008-12-10 00:15:19 +0000246 if (MI->getOperand(1).isReg() && MI->getOperand(2).isReg()
247 && (MI->getOperand(2).getReg() == SPU::R1
248 || MI->getOperand(1).getReg() == SPU::R1)) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000249 FrameIndex = MI->getOperand(2).getIndex();
Scott Michel66377522007-12-04 22:35:58 +0000250 return MI->getOperand(0).getReg();
251 }
252 break;
Scott Michelf0569be2008-12-27 04:51:36 +0000253#endif
Scott Michel66377522007-12-04 22:35:58 +0000254 }
255 return 0;
256}
Owen Andersond10fd972007-12-31 06:32:00 +0000257
Owen Anderson940f83e2008-08-26 18:03:31 +0000258bool SPUInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
Owen Andersond10fd972007-12-31 06:32:00 +0000259 MachineBasicBlock::iterator MI,
260 unsigned DestReg, unsigned SrcReg,
261 const TargetRegisterClass *DestRC,
262 const TargetRegisterClass *SrcRC) const
263{
Chris Lattner5e09da22008-03-09 20:31:11 +0000264 // We support cross register class moves for our aliases, such as R3 in any
265 // reg class to any other reg class containing R3. This is required because
266 // we instruction select bitconvert i64 -> f64 as a noop for example, so our
267 // types have no specific meaning.
Scott Michel02d711b2008-12-30 23:28:25 +0000268
Owen Andersond10fd972007-12-31 06:32:00 +0000269 if (DestRC == SPU::R8CRegisterClass) {
Scott Michel9bd7a372009-01-02 20:52:08 +0000270 BuildMI(MBB, MI, get(SPU::LRr8), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000271 } else if (DestRC == SPU::R16CRegisterClass) {
Scott Michel9bd7a372009-01-02 20:52:08 +0000272 BuildMI(MBB, MI, get(SPU::LRr16), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000273 } else if (DestRC == SPU::R32CRegisterClass) {
Scott Michel9bd7a372009-01-02 20:52:08 +0000274 BuildMI(MBB, MI, get(SPU::LRr32), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000275 } else if (DestRC == SPU::R32FPRegisterClass) {
Scott Michel9bd7a372009-01-02 20:52:08 +0000276 BuildMI(MBB, MI, get(SPU::LRf32), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000277 } else if (DestRC == SPU::R64CRegisterClass) {
Scott Michel9bd7a372009-01-02 20:52:08 +0000278 BuildMI(MBB, MI, get(SPU::LRr64), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000279 } else if (DestRC == SPU::R64FPRegisterClass) {
Scott Michel9bd7a372009-01-02 20:52:08 +0000280 BuildMI(MBB, MI, get(SPU::LRf64), DestReg).addReg(SrcReg);
281 } else if (DestRC == SPU::GPRCRegisterClass) {
282 BuildMI(MBB, MI, get(SPU::LRr128), DestReg).addReg(SrcReg);
283 } else if (DestRC == SPU::VECREGRegisterClass) {
284 BuildMI(MBB, MI, get(SPU::LRv16i8), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000285 } else {
Owen Anderson940f83e2008-08-26 18:03:31 +0000286 // Attempt to copy unknown/unsupported register class!
287 return false;
Owen Andersond10fd972007-12-31 06:32:00 +0000288 }
Scott Michel02d711b2008-12-30 23:28:25 +0000289
Owen Anderson940f83e2008-08-26 18:03:31 +0000290 return true;
Owen Andersond10fd972007-12-31 06:32:00 +0000291}
Owen Andersonf6372aa2008-01-01 21:11:32 +0000292
293void
294SPUInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
295 MachineBasicBlock::iterator MI,
296 unsigned SrcReg, bool isKill, int FrameIdx,
297 const TargetRegisterClass *RC) const
298{
Chris Lattnercc8cd0c2008-01-07 02:48:55 +0000299 unsigned opc;
Scott Michelaedc6372008-12-10 00:15:19 +0000300 bool isValidFrameIdx = (FrameIdx < SPUFrameInfo::maxFrameOffset());
Owen Andersonf6372aa2008-01-01 21:11:32 +0000301 if (RC == SPU::GPRCRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000302 opc = (isValidFrameIdx ? SPU::STQDr128 : SPU::STQXr128);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000303 } else if (RC == SPU::R64CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000304 opc = (isValidFrameIdx ? SPU::STQDr64 : SPU::STQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000305 } else if (RC == SPU::R64FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000306 opc = (isValidFrameIdx ? SPU::STQDr64 : SPU::STQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000307 } else if (RC == SPU::R32CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000308 opc = (isValidFrameIdx ? SPU::STQDr32 : SPU::STQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000309 } else if (RC == SPU::R32FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000310 opc = (isValidFrameIdx ? SPU::STQDr32 : SPU::STQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000311 } else if (RC == SPU::R16CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000312 opc = (isValidFrameIdx ? SPU::STQDr16 : SPU::STQXr16);
313 } else if (RC == SPU::R8CRegisterClass) {
314 opc = (isValidFrameIdx ? SPU::STQDr8 : SPU::STQXr8);
Scott Michelf0569be2008-12-27 04:51:36 +0000315 } else if (RC == SPU::VECREGRegisterClass) {
316 opc = (isValidFrameIdx) ? SPU::STQDv16i8 : SPU::STQXv16i8;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000317 } else {
318 assert(0 && "Unknown regclass!");
319 abort();
320 }
321
322 addFrameReference(BuildMI(MBB, MI, get(opc))
323 .addReg(SrcReg, false, false, isKill), FrameIdx);
324}
325
326void SPUInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
327 bool isKill,
328 SmallVectorImpl<MachineOperand> &Addr,
329 const TargetRegisterClass *RC,
330 SmallVectorImpl<MachineInstr*> &NewMIs) const {
331 cerr << "storeRegToAddr() invoked!\n";
332 abort();
333
Dan Gohmand735b802008-10-03 15:45:36 +0000334 if (Addr[0].isFI()) {
Owen Andersonf6372aa2008-01-01 21:11:32 +0000335 /* do what storeRegToStackSlot does here */
336 } else {
337 unsigned Opc = 0;
338 if (RC == SPU::GPRCRegisterClass) {
339 /* Opc = PPC::STW; */
340 } else if (RC == SPU::R16CRegisterClass) {
341 /* Opc = PPC::STD; */
342 } else if (RC == SPU::R32CRegisterClass) {
343 /* Opc = PPC::STFD; */
344 } else if (RC == SPU::R32FPRegisterClass) {
345 /* Opc = PPC::STFD; */
346 } else if (RC == SPU::R64FPRegisterClass) {
347 /* Opc = PPC::STFS; */
348 } else if (RC == SPU::VECREGRegisterClass) {
349 /* Opc = PPC::STVX; */
350 } else {
351 assert(0 && "Unknown regclass!");
352 abort();
353 }
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000354 MachineInstrBuilder MIB = BuildMI(MF, get(Opc))
Owen Andersonf6372aa2008-01-01 21:11:32 +0000355 .addReg(SrcReg, false, false, isKill);
356 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
357 MachineOperand &MO = Addr[i];
Dan Gohmand735b802008-10-03 15:45:36 +0000358 if (MO.isReg())
Owen Andersonf6372aa2008-01-01 21:11:32 +0000359 MIB.addReg(MO.getReg());
Dan Gohmand735b802008-10-03 15:45:36 +0000360 else if (MO.isImm())
Owen Andersonf6372aa2008-01-01 21:11:32 +0000361 MIB.addImm(MO.getImm());
362 else
363 MIB.addFrameIndex(MO.getIndex());
364 }
365 NewMIs.push_back(MIB);
366 }
367}
368
369void
370SPUInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
371 MachineBasicBlock::iterator MI,
372 unsigned DestReg, int FrameIdx,
373 const TargetRegisterClass *RC) const
374{
Chris Lattnercc8cd0c2008-01-07 02:48:55 +0000375 unsigned opc;
Scott Michelaedc6372008-12-10 00:15:19 +0000376 bool isValidFrameIdx = (FrameIdx < SPUFrameInfo::maxFrameOffset());
Owen Andersonf6372aa2008-01-01 21:11:32 +0000377 if (RC == SPU::GPRCRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000378 opc = (isValidFrameIdx ? SPU::LQDr128 : SPU::LQXr128);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000379 } else if (RC == SPU::R64CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000380 opc = (isValidFrameIdx ? SPU::LQDr64 : SPU::LQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000381 } else if (RC == SPU::R64FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000382 opc = (isValidFrameIdx ? SPU::LQDr64 : SPU::LQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000383 } else if (RC == SPU::R32CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000384 opc = (isValidFrameIdx ? SPU::LQDr32 : SPU::LQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000385 } else if (RC == SPU::R32FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000386 opc = (isValidFrameIdx ? SPU::LQDr32 : SPU::LQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000387 } else if (RC == SPU::R16CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000388 opc = (isValidFrameIdx ? SPU::LQDr16 : SPU::LQXr16);
389 } else if (RC == SPU::R8CRegisterClass) {
390 opc = (isValidFrameIdx ? SPU::LQDr8 : SPU::LQXr8);
Scott Michelf0569be2008-12-27 04:51:36 +0000391 } else if (RC == SPU::VECREGRegisterClass) {
392 opc = (isValidFrameIdx) ? SPU::LQDv16i8 : SPU::LQXv16i8;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000393 } else {
394 assert(0 && "Unknown regclass in loadRegFromStackSlot!");
395 abort();
396 }
397
398 addFrameReference(BuildMI(MBB, MI, get(opc)).addReg(DestReg), FrameIdx);
399}
400
401/*!
402 \note We are really pessimistic here about what kind of a load we're doing.
403 */
404void SPUInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
Scott Michelaedc6372008-12-10 00:15:19 +0000405 SmallVectorImpl<MachineOperand> &Addr,
406 const TargetRegisterClass *RC,
407 SmallVectorImpl<MachineInstr*> &NewMIs)
Owen Andersonf6372aa2008-01-01 21:11:32 +0000408 const {
409 cerr << "loadRegToAddr() invoked!\n";
410 abort();
411
Dan Gohmand735b802008-10-03 15:45:36 +0000412 if (Addr[0].isFI()) {
Owen Andersonf6372aa2008-01-01 21:11:32 +0000413 /* do what loadRegFromStackSlot does here... */
414 } else {
415 unsigned Opc = 0;
416 if (RC == SPU::R8CRegisterClass) {
417 /* do brilliance here */
418 } else if (RC == SPU::R16CRegisterClass) {
419 /* Opc = PPC::LWZ; */
420 } else if (RC == SPU::R32CRegisterClass) {
421 /* Opc = PPC::LD; */
422 } else if (RC == SPU::R32FPRegisterClass) {
423 /* Opc = PPC::LFD; */
424 } else if (RC == SPU::R64FPRegisterClass) {
425 /* Opc = PPC::LFS; */
426 } else if (RC == SPU::VECREGRegisterClass) {
427 /* Opc = PPC::LVX; */
428 } else if (RC == SPU::GPRCRegisterClass) {
429 /* Opc = something else! */
430 } else {
431 assert(0 && "Unknown regclass!");
432 abort();
433 }
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000434 MachineInstrBuilder MIB = BuildMI(MF, get(Opc), DestReg);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000435 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
436 MachineOperand &MO = Addr[i];
Dan Gohmand735b802008-10-03 15:45:36 +0000437 if (MO.isReg())
Owen Andersonf6372aa2008-01-01 21:11:32 +0000438 MIB.addReg(MO.getReg());
Dan Gohmand735b802008-10-03 15:45:36 +0000439 else if (MO.isImm())
Owen Andersonf6372aa2008-01-01 21:11:32 +0000440 MIB.addImm(MO.getImm());
441 else
442 MIB.addFrameIndex(MO.getIndex());
443 }
444 NewMIs.push_back(MIB);
445 }
446}
447
Owen Anderson43dbe052008-01-07 01:35:02 +0000448/// foldMemoryOperand - SPU, like PPC, can only fold spills into
449/// copy instructions, turning them into load/store instructions.
450MachineInstr *
Dan Gohmanc54baa22008-12-03 18:43:12 +0000451SPUInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
452 MachineInstr *MI,
453 const SmallVectorImpl<unsigned> &Ops,
454 int FrameIndex) const
Owen Anderson43dbe052008-01-07 01:35:02 +0000455{
456#if SOMEDAY_SCOTT_LOOKS_AT_ME_AGAIN
457 if (Ops.size() != 1) return NULL;
458
459 unsigned OpNum = Ops[0];
460 unsigned Opc = MI->getOpcode();
461 MachineInstr *NewMI = 0;
Scott Michel02d711b2008-12-30 23:28:25 +0000462
Owen Anderson43dbe052008-01-07 01:35:02 +0000463 if ((Opc == SPU::ORr32
464 || Opc == SPU::ORv4i32)
465 && MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
466 if (OpNum == 0) { // move -> store
467 unsigned InReg = MI->getOperand(1).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000468 bool isKill = MI->getOperand(1).isKill();
Owen Anderson43dbe052008-01-07 01:35:02 +0000469 if (FrameIndex < SPUFrameInfo::maxFrameOffset()) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000470 NewMI = addFrameReference(BuildMI(MF, TII.get(SPU::STQDr32))
Evan Cheng9f1c8312008-07-03 09:09:37 +0000471 .addReg(InReg, false, false, isKill),
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000472 FrameIndex);
Owen Anderson43dbe052008-01-07 01:35:02 +0000473 }
474 } else { // move -> load
475 unsigned OutReg = MI->getOperand(0).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000476 bool isDead = MI->getOperand(0).isDead();
477 Opc = (FrameIndex < SPUFrameInfo::maxFrameOffset())
478 ? SPU::STQDr32 : SPU::STQXr32;
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000479 NewMI = addFrameReference(BuildMI(MF, TII.get(Opc))
Evan Cheng9f1c8312008-07-03 09:09:37 +0000480 .addReg(OutReg, true, false, false, isDead), FrameIndex);
Owen Anderson43dbe052008-01-07 01:35:02 +0000481 }
482 }
483
Owen Anderson43dbe052008-01-07 01:35:02 +0000484 return NewMI;
485#else
486 return 0;
487#endif
488}
489
Scott Michelaedc6372008-12-10 00:15:19 +0000490//! Branch analysis
Scott Michel9bd7a372009-01-02 20:52:08 +0000491/*!
Scott Michelaedc6372008-12-10 00:15:19 +0000492 \note This code was kiped from PPC. There may be more branch analysis for
493 CellSPU than what's currently done here.
494 */
495bool
496SPUInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
497 MachineBasicBlock *&FBB,
498 SmallVectorImpl<MachineOperand> &Cond) const {
499 // If the block has no terminators, it just falls into the block after it.
500 MachineBasicBlock::iterator I = MBB.end();
501 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
502 return false;
503
504 // Get the last instruction in the block.
505 MachineInstr *LastInst = I;
Scott Michel02d711b2008-12-30 23:28:25 +0000506
Scott Michelaedc6372008-12-10 00:15:19 +0000507 // If there is only one terminator instruction, process it.
508 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
509 if (isUncondBranch(LastInst)) {
510 TBB = LastInst->getOperand(0).getMBB();
511 return false;
512 } else if (isCondBranch(LastInst)) {
513 // Block ends with fall-through condbranch.
514 TBB = LastInst->getOperand(1).getMBB();
Scott Michel9bd7a372009-01-02 20:52:08 +0000515 DEBUG(cerr << "Pushing LastInst: ");
516 DEBUG(LastInst->dump());
517 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Scott Michelaedc6372008-12-10 00:15:19 +0000518 Cond.push_back(LastInst->getOperand(0));
Scott Michelaedc6372008-12-10 00:15:19 +0000519 return false;
520 }
521 // Otherwise, don't know what this is.
522 return true;
523 }
Scott Michel02d711b2008-12-30 23:28:25 +0000524
Scott Michelaedc6372008-12-10 00:15:19 +0000525 // Get the instruction before it if it's a terminator.
526 MachineInstr *SecondLastInst = I;
527
528 // If there are three terminators, we don't know what sort of block this is.
529 if (SecondLastInst && I != MBB.begin() &&
530 isUnpredicatedTerminator(--I))
531 return true;
Scott Michel02d711b2008-12-30 23:28:25 +0000532
Scott Michelaedc6372008-12-10 00:15:19 +0000533 // If the block ends with a conditional and unconditional branch, handle it.
534 if (isCondBranch(SecondLastInst) && isUncondBranch(LastInst)) {
535 TBB = SecondLastInst->getOperand(1).getMBB();
Scott Michel9bd7a372009-01-02 20:52:08 +0000536 DEBUG(cerr << "Pushing SecondLastInst: ");
537 DEBUG(SecondLastInst->dump());
538 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Scott Michelaedc6372008-12-10 00:15:19 +0000539 Cond.push_back(SecondLastInst->getOperand(0));
Scott Michelaedc6372008-12-10 00:15:19 +0000540 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.
Scott Michel9bd7a372009-01-02 20:52:08 +0000567 DEBUG(cerr << "Removing branch: ");
568 DEBUG(I->dump());
Scott Michelaedc6372008-12-10 00:15:19 +0000569 I->eraseFromParent();
570 I = MBB.end();
571 if (I == MBB.begin())
572 return 1;
573
574 --I;
Scott Michel9bd7a372009-01-02 20:52:08 +0000575 if (!(isCondBranch(I) || isUncondBranch(I)))
Scott Michelaedc6372008-12-10 00:15:19 +0000576 return 1;
577
578 // Remove the second branch.
Scott Michel9bd7a372009-01-02 20:52:08 +0000579 DEBUG(cerr << "Removing second branch: ");
580 DEBUG(I->dump());
Scott Michelaedc6372008-12-10 00:15:19 +0000581 I->eraseFromParent();
582 return 2;
583}
Scott Michel02d711b2008-12-30 23:28:25 +0000584
Scott Michelaedc6372008-12-10 00:15:19 +0000585unsigned
586SPUInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
587 MachineBasicBlock *FBB,
588 const SmallVectorImpl<MachineOperand> &Cond) const {
589 // Shouldn't be a fall through.
590 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Scott Michel02d711b2008-12-30 23:28:25 +0000591 assert((Cond.size() == 2 || Cond.size() == 0) &&
Scott Michelaedc6372008-12-10 00:15:19 +0000592 "SPU branch conditions have two components!");
Scott Michel02d711b2008-12-30 23:28:25 +0000593
Scott Michelaedc6372008-12-10 00:15:19 +0000594 // One-way branch.
595 if (FBB == 0) {
Scott Michel9bd7a372009-01-02 20:52:08 +0000596 if (Cond.empty()) {
597 // Unconditional branch
598 MachineInstrBuilder MIB = BuildMI(&MBB, get(SPU::BR));
599 MIB.addMBB(TBB);
600
601 DEBUG(cerr << "Inserted one-way uncond branch: ");
602 DEBUG((*MIB).dump());
603 } else {
604 // Conditional branch
605 MachineInstrBuilder MIB = BuildMI(&MBB, get(Cond[0].getImm()));
606 MIB.addReg(Cond[1].getReg()).addMBB(TBB);
607
608 DEBUG(cerr << "Inserted one-way cond branch: ");
609 DEBUG((*MIB).dump());
Scott Michelaedc6372008-12-10 00:15:19 +0000610 }
611 return 1;
Scott Michel9bd7a372009-01-02 20:52:08 +0000612 } else {
613 MachineInstrBuilder MIB = BuildMI(&MBB, get(Cond[0].getImm()));
614 MachineInstrBuilder MIB2 = BuildMI(&MBB, get(SPU::BR));
615
616 // Two-way Conditional Branch.
617 MIB.addReg(Cond[1].getReg()).addMBB(TBB);
618 MIB2.addMBB(FBB);
619
620 DEBUG(cerr << "Inserted conditional branch: ");
621 DEBUG((*MIB).dump());
622 DEBUG(cerr << "part 2: ");
623 DEBUG((*MIB2).dump());
624 return 2;
Scott Michelaedc6372008-12-10 00:15:19 +0000625 }
Scott Michelaedc6372008-12-10 00:15:19 +0000626}
627
628