blob: ded83247610aff6fe0a9964b1714f90dd6145447 [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
Scott Michel19c10e62009-01-26 03:37:41 +000031 || opc == SPU::BRA
32 || opc == SPU::BI);
Scott Michelaedc6372008-12-10 00:15:19 +000033 }
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
Scott Michel19c10e62009-01-26 03:37:41 +000041 || 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
Scott Michel66377522007-12-04 22:35:58 +000056bool
57SPUInstrInfo::isMoveInstr(const MachineInstr& MI,
58 unsigned& sourceReg,
Evan Cheng04ee5a12009-01-20 19:12:24 +000059 unsigned& destReg,
60 unsigned& SrcSR, unsigned& DstSR) const {
61 SrcSR = DstSR = 0; // No sub-registers.
62
Scott Michel66377522007-12-04 22:35:58 +000063 // Primarily, ORI and OR are generated by copyRegToReg. But, there are other
64 // cases where we can safely say that what's being done is really a move
65 // (see how PowerPC does this -- it's the model for this code too.)
66 switch (MI.getOpcode()) {
67 default:
68 break;
69 case SPU::ORIv4i32:
70 case SPU::ORIr32:
Scott Michel66377522007-12-04 22:35:58 +000071 case SPU::ORHIv8i16:
72 case SPU::ORHIr16:
Scott Michela59d4692008-02-23 18:41:37 +000073 case SPU::ORHIi8i16:
Scott Michel66377522007-12-04 22:35:58 +000074 case SPU::ORBIv16i8:
Scott Michel504c3692007-12-17 22:32:34 +000075 case SPU::ORBIr8:
Scott Michela59d4692008-02-23 18:41:37 +000076 case SPU::ORIi16i32:
77 case SPU::ORIi8i32:
Scott Michel66377522007-12-04 22:35:58 +000078 case SPU::AHIvec:
79 case SPU::AHIr16:
Scott Michel02d711b2008-12-30 23:28:25 +000080 case SPU::AIv4i32:
Scott Michel66377522007-12-04 22:35:58 +000081 assert(MI.getNumOperands() == 3 &&
Dan Gohmand735b802008-10-03 15:45:36 +000082 MI.getOperand(0).isReg() &&
83 MI.getOperand(1).isReg() &&
84 MI.getOperand(2).isImm() &&
Scott Michel66377522007-12-04 22:35:58 +000085 "invalid SPU ORI/ORHI/ORBI/AHI/AI/SFI/SFHI instruction!");
Chris Lattner9a1ceae2007-12-30 20:49:49 +000086 if (MI.getOperand(2).getImm() == 0) {
Scott Michel66377522007-12-04 22:35:58 +000087 sourceReg = MI.getOperand(1).getReg();
88 destReg = MI.getOperand(0).getReg();
89 return true;
90 }
91 break;
Scott Michel9999e682007-12-19 07:35:06 +000092 case SPU::AIr32:
93 assert(MI.getNumOperands() == 3 &&
94 "wrong number of operands to AIr32");
Dan Gohmand735b802008-10-03 15:45:36 +000095 if (MI.getOperand(0).isReg() &&
Scott Michel02d711b2008-12-30 23:28:25 +000096 MI.getOperand(1).isReg() &&
Dan Gohmand735b802008-10-03 15:45:36 +000097 (MI.getOperand(2).isImm() &&
Chris Lattner9a1ceae2007-12-30 20:49:49 +000098 MI.getOperand(2).getImm() == 0)) {
Scott Michel9999e682007-12-19 07:35:06 +000099 sourceReg = MI.getOperand(1).getReg();
100 destReg = MI.getOperand(0).getReg();
101 return true;
102 }
103 break;
Scott Michelf0569be2008-12-27 04:51:36 +0000104 case SPU::LRr8:
105 case SPU::LRr16:
106 case SPU::LRr32:
107 case SPU::LRf32:
108 case SPU::LRr64:
109 case SPU::LRf64:
110 case SPU::LRr128:
111 case SPU::LRv16i8:
112 case SPU::LRv8i16:
113 case SPU::LRv4i32:
114 case SPU::LRv4f32:
115 case SPU::LRv2i64:
116 case SPU::LRv2f64:
Scott Michel170783a2007-12-19 20:15:47 +0000117 case SPU::ORv16i8_i8:
Scott Michel66377522007-12-04 22:35:58 +0000118 case SPU::ORv8i16_i16:
119 case SPU::ORv4i32_i32:
120 case SPU::ORv2i64_i64:
121 case SPU::ORv4f32_f32:
122 case SPU::ORv2f64_f64:
Scott Michel170783a2007-12-19 20:15:47 +0000123 case SPU::ORi8_v16i8:
Scott Michel66377522007-12-04 22:35:58 +0000124 case SPU::ORi16_v8i16:
125 case SPU::ORi32_v4i32:
126 case SPU::ORi64_v2i64:
127 case SPU::ORf32_v4f32:
Scott Micheldd950092009-01-06 03:36:14 +0000128 case SPU::ORf64_v2f64:
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000129/*
Scott Micheldd950092009-01-06 03:36:14 +0000130 case SPU::ORi128_r64:
131 case SPU::ORi128_f64:
132 case SPU::ORi128_r32:
133 case SPU::ORi128_f32:
134 case SPU::ORi128_r16:
135 case SPU::ORi128_r8:
136 case SPU::ORi128_vec:
137 case SPU::ORr64_i128:
138 case SPU::ORf64_i128:
139 case SPU::ORr32_i128:
140 case SPU::ORf32_i128:
141 case SPU::ORr16_i128:
142 case SPU::ORr8_i128:
143 case SPU::ORvec_i128:
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000144*/
145/*
Scott Micheldd950092009-01-06 03:36:14 +0000146 case SPU::ORr16_r32:
147 case SPU::ORr8_r32:
148 case SPU::ORr32_r16:
149 case SPU::ORr32_r8:
Scott Micheldd950092009-01-06 03:36:14 +0000150 case SPU::ORr16_r64:
151 case SPU::ORr8_r64:
Scott Micheldd950092009-01-06 03:36:14 +0000152 case SPU::ORr64_r16:
153 case SPU::ORr64_r8:
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000154*/
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000155 case SPU::ORr64_r32:
156 case SPU::ORr32_r64:
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000157 case SPU::ORf32_r32:
158 case SPU::ORr32_f32:
159 case SPU::ORf64_r64:
160 case SPU::ORr64_f64: {
Scott Michelf0569be2008-12-27 04:51:36 +0000161 assert(MI.getNumOperands() == 2 &&
162 MI.getOperand(0).isReg() &&
163 MI.getOperand(1).isReg() &&
Scott Michel52d00012009-01-03 00:27:53 +0000164 "invalid SPU OR<type>_<vec> or LR instruction!");
Scott Michelf0569be2008-12-27 04:51:36 +0000165 if (MI.getOperand(0).getReg() == MI.getOperand(1).getReg()) {
166 sourceReg = MI.getOperand(0).getReg();
167 destReg = MI.getOperand(0).getReg();
168 return true;
169 }
170 break;
171 }
Scott Michel66377522007-12-04 22:35:58 +0000172 case SPU::ORv16i8:
173 case SPU::ORv8i16:
174 case SPU::ORv4i32:
Scott Michel52d00012009-01-03 00:27:53 +0000175 case SPU::ORv2i64:
176 case SPU::ORr8:
177 case SPU::ORr16:
Scott Michel66377522007-12-04 22:35:58 +0000178 case SPU::ORr32:
179 case SPU::ORr64:
Scott Michel86c041f2007-12-20 00:44:13 +0000180 case SPU::ORf32:
181 case SPU::ORf64:
Scott Michel66377522007-12-04 22:35:58 +0000182 assert(MI.getNumOperands() == 3 &&
Dan Gohmand735b802008-10-03 15:45:36 +0000183 MI.getOperand(0).isReg() &&
184 MI.getOperand(1).isReg() &&
185 MI.getOperand(2).isReg() &&
Scott Michel66377522007-12-04 22:35:58 +0000186 "invalid SPU OR(vec|r32|r64|gprc) instruction!");
187 if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
188 sourceReg = MI.getOperand(1).getReg();
189 destReg = MI.getOperand(0).getReg();
190 return true;
191 }
192 break;
193 }
194
195 return false;
196}
197
198unsigned
Dan Gohmancbad42c2008-11-18 19:49:32 +0000199SPUInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
200 int &FrameIndex) const {
Scott Michel66377522007-12-04 22:35:58 +0000201 switch (MI->getOpcode()) {
202 default: break;
203 case SPU::LQDv16i8:
204 case SPU::LQDv8i16:
205 case SPU::LQDv4i32:
206 case SPU::LQDv4f32:
207 case SPU::LQDv2f64:
208 case SPU::LQDr128:
209 case SPU::LQDr64:
210 case SPU::LQDr32:
Scott Michelaedc6372008-12-10 00:15:19 +0000211 case SPU::LQDr16: {
212 const MachineOperand MOp1 = MI->getOperand(1);
213 const MachineOperand MOp2 = MI->getOperand(2);
Scott Michel52d00012009-01-03 00:27:53 +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}
223
224unsigned
Dan Gohmancbad42c2008-11-18 19:49:32 +0000225SPUInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
226 int &FrameIndex) const {
Scott Michel66377522007-12-04 22:35:58 +0000227 switch (MI->getOpcode()) {
228 default: break;
229 case SPU::STQDv16i8:
230 case SPU::STQDv8i16:
231 case SPU::STQDv4i32:
232 case SPU::STQDv4f32:
233 case SPU::STQDv2f64:
234 case SPU::STQDr128:
235 case SPU::STQDr64:
236 case SPU::STQDr32:
237 case SPU::STQDr16:
Scott Michelaedc6372008-12-10 00:15:19 +0000238 case SPU::STQDr8: {
239 const MachineOperand MOp1 = MI->getOperand(1);
240 const MachineOperand MOp2 = MI->getOperand(2);
Scott Michelf0569be2008-12-27 04:51:36 +0000241 if (MOp1.isImm() && MOp2.isFI()) {
242 FrameIndex = MOp2.getIndex();
Scott Michelaedc6372008-12-10 00:15:19 +0000243 return MI->getOperand(0).getReg();
244 }
245 break;
246 }
Scott Michel66377522007-12-04 22:35:58 +0000247 }
248 return 0;
249}
Owen Andersond10fd972007-12-31 06:32:00 +0000250
Owen Anderson940f83e2008-08-26 18:03:31 +0000251bool SPUInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
Owen Andersond10fd972007-12-31 06:32:00 +0000252 MachineBasicBlock::iterator MI,
253 unsigned DestReg, unsigned SrcReg,
254 const TargetRegisterClass *DestRC,
255 const TargetRegisterClass *SrcRC) const
256{
Chris Lattner5e09da22008-03-09 20:31:11 +0000257 // We support cross register class moves for our aliases, such as R3 in any
258 // reg class to any other reg class containing R3. This is required because
259 // we instruction select bitconvert i64 -> f64 as a noop for example, so our
260 // types have no specific meaning.
Scott Michel02d711b2008-12-30 23:28:25 +0000261
Owen Andersond10fd972007-12-31 06:32:00 +0000262 if (DestRC == SPU::R8CRegisterClass) {
Scott Michel9bd7a372009-01-02 20:52:08 +0000263 BuildMI(MBB, MI, get(SPU::LRr8), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000264 } else if (DestRC == SPU::R16CRegisterClass) {
Scott Michel9bd7a372009-01-02 20:52:08 +0000265 BuildMI(MBB, MI, get(SPU::LRr16), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000266 } else if (DestRC == SPU::R32CRegisterClass) {
Scott Michel9bd7a372009-01-02 20:52:08 +0000267 BuildMI(MBB, MI, get(SPU::LRr32), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000268 } else if (DestRC == SPU::R32FPRegisterClass) {
Scott Michel9bd7a372009-01-02 20:52:08 +0000269 BuildMI(MBB, MI, get(SPU::LRf32), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000270 } else if (DestRC == SPU::R64CRegisterClass) {
Scott Michel9bd7a372009-01-02 20:52:08 +0000271 BuildMI(MBB, MI, get(SPU::LRr64), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000272 } else if (DestRC == SPU::R64FPRegisterClass) {
Scott Michel9bd7a372009-01-02 20:52:08 +0000273 BuildMI(MBB, MI, get(SPU::LRf64), DestReg).addReg(SrcReg);
274 } else if (DestRC == SPU::GPRCRegisterClass) {
275 BuildMI(MBB, MI, get(SPU::LRr128), DestReg).addReg(SrcReg);
276 } else if (DestRC == SPU::VECREGRegisterClass) {
277 BuildMI(MBB, MI, get(SPU::LRv16i8), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000278 } else {
Owen Anderson940f83e2008-08-26 18:03:31 +0000279 // Attempt to copy unknown/unsupported register class!
280 return false;
Owen Andersond10fd972007-12-31 06:32:00 +0000281 }
Scott Michel02d711b2008-12-30 23:28:25 +0000282
Owen Anderson940f83e2008-08-26 18:03:31 +0000283 return true;
Owen Andersond10fd972007-12-31 06:32:00 +0000284}
Owen Andersonf6372aa2008-01-01 21:11:32 +0000285
286void
287SPUInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
288 MachineBasicBlock::iterator MI,
289 unsigned SrcReg, bool isKill, int FrameIdx,
290 const TargetRegisterClass *RC) const
291{
Chris Lattnercc8cd0c2008-01-07 02:48:55 +0000292 unsigned opc;
Scott Michelaedc6372008-12-10 00:15:19 +0000293 bool isValidFrameIdx = (FrameIdx < SPUFrameInfo::maxFrameOffset());
Owen Andersonf6372aa2008-01-01 21:11:32 +0000294 if (RC == SPU::GPRCRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000295 opc = (isValidFrameIdx ? SPU::STQDr128 : SPU::STQXr128);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000296 } else if (RC == SPU::R64CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000297 opc = (isValidFrameIdx ? SPU::STQDr64 : SPU::STQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000298 } else if (RC == SPU::R64FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000299 opc = (isValidFrameIdx ? SPU::STQDr64 : SPU::STQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000300 } else if (RC == SPU::R32CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000301 opc = (isValidFrameIdx ? SPU::STQDr32 : SPU::STQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000302 } else if (RC == SPU::R32FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000303 opc = (isValidFrameIdx ? SPU::STQDr32 : SPU::STQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000304 } else if (RC == SPU::R16CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000305 opc = (isValidFrameIdx ? SPU::STQDr16 : SPU::STQXr16);
306 } else if (RC == SPU::R8CRegisterClass) {
307 opc = (isValidFrameIdx ? SPU::STQDr8 : SPU::STQXr8);
Scott Michelf0569be2008-12-27 04:51:36 +0000308 } else if (RC == SPU::VECREGRegisterClass) {
309 opc = (isValidFrameIdx) ? SPU::STQDv16i8 : SPU::STQXv16i8;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000310 } else {
311 assert(0 && "Unknown regclass!");
312 abort();
313 }
314
315 addFrameReference(BuildMI(MBB, MI, get(opc))
316 .addReg(SrcReg, false, false, isKill), FrameIdx);
317}
318
319void SPUInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
320 bool isKill,
321 SmallVectorImpl<MachineOperand> &Addr,
322 const TargetRegisterClass *RC,
323 SmallVectorImpl<MachineInstr*> &NewMIs) const {
324 cerr << "storeRegToAddr() invoked!\n";
325 abort();
326
Dan Gohmand735b802008-10-03 15:45:36 +0000327 if (Addr[0].isFI()) {
Owen Andersonf6372aa2008-01-01 21:11:32 +0000328 /* do what storeRegToStackSlot does here */
329 } else {
330 unsigned Opc = 0;
331 if (RC == SPU::GPRCRegisterClass) {
332 /* Opc = PPC::STW; */
333 } else if (RC == SPU::R16CRegisterClass) {
334 /* Opc = PPC::STD; */
335 } else if (RC == SPU::R32CRegisterClass) {
336 /* Opc = PPC::STFD; */
337 } else if (RC == SPU::R32FPRegisterClass) {
338 /* Opc = PPC::STFD; */
339 } else if (RC == SPU::R64FPRegisterClass) {
340 /* Opc = PPC::STFS; */
341 } else if (RC == SPU::VECREGRegisterClass) {
342 /* Opc = PPC::STVX; */
343 } else {
344 assert(0 && "Unknown regclass!");
345 abort();
346 }
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000347 MachineInstrBuilder MIB = BuildMI(MF, get(Opc))
Owen Andersonf6372aa2008-01-01 21:11:32 +0000348 .addReg(SrcReg, false, false, isKill);
349 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
350 MachineOperand &MO = Addr[i];
Dan Gohmand735b802008-10-03 15:45:36 +0000351 if (MO.isReg())
Owen Andersonf6372aa2008-01-01 21:11:32 +0000352 MIB.addReg(MO.getReg());
Dan Gohmand735b802008-10-03 15:45:36 +0000353 else if (MO.isImm())
Owen Andersonf6372aa2008-01-01 21:11:32 +0000354 MIB.addImm(MO.getImm());
355 else
356 MIB.addFrameIndex(MO.getIndex());
357 }
358 NewMIs.push_back(MIB);
359 }
360}
361
362void
363SPUInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
364 MachineBasicBlock::iterator MI,
365 unsigned DestReg, int FrameIdx,
366 const TargetRegisterClass *RC) const
367{
Chris Lattnercc8cd0c2008-01-07 02:48:55 +0000368 unsigned opc;
Scott Michelaedc6372008-12-10 00:15:19 +0000369 bool isValidFrameIdx = (FrameIdx < SPUFrameInfo::maxFrameOffset());
Owen Andersonf6372aa2008-01-01 21:11:32 +0000370 if (RC == SPU::GPRCRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000371 opc = (isValidFrameIdx ? SPU::LQDr128 : SPU::LQXr128);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000372 } else if (RC == SPU::R64CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000373 opc = (isValidFrameIdx ? SPU::LQDr64 : SPU::LQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000374 } else if (RC == SPU::R64FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000375 opc = (isValidFrameIdx ? SPU::LQDr64 : SPU::LQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000376 } else if (RC == SPU::R32CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000377 opc = (isValidFrameIdx ? SPU::LQDr32 : SPU::LQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000378 } else if (RC == SPU::R32FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000379 opc = (isValidFrameIdx ? SPU::LQDr32 : SPU::LQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000380 } else if (RC == SPU::R16CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000381 opc = (isValidFrameIdx ? SPU::LQDr16 : SPU::LQXr16);
382 } else if (RC == SPU::R8CRegisterClass) {
383 opc = (isValidFrameIdx ? SPU::LQDr8 : SPU::LQXr8);
Scott Michelf0569be2008-12-27 04:51:36 +0000384 } else if (RC == SPU::VECREGRegisterClass) {
385 opc = (isValidFrameIdx) ? SPU::LQDv16i8 : SPU::LQXv16i8;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000386 } else {
387 assert(0 && "Unknown regclass in loadRegFromStackSlot!");
388 abort();
389 }
390
391 addFrameReference(BuildMI(MBB, MI, get(opc)).addReg(DestReg), FrameIdx);
392}
393
394/*!
395 \note We are really pessimistic here about what kind of a load we're doing.
396 */
397void SPUInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
Scott Michelaedc6372008-12-10 00:15:19 +0000398 SmallVectorImpl<MachineOperand> &Addr,
399 const TargetRegisterClass *RC,
400 SmallVectorImpl<MachineInstr*> &NewMIs)
Owen Andersonf6372aa2008-01-01 21:11:32 +0000401 const {
402 cerr << "loadRegToAddr() invoked!\n";
403 abort();
404
Dan Gohmand735b802008-10-03 15:45:36 +0000405 if (Addr[0].isFI()) {
Owen Andersonf6372aa2008-01-01 21:11:32 +0000406 /* do what loadRegFromStackSlot does here... */
407 } else {
408 unsigned Opc = 0;
409 if (RC == SPU::R8CRegisterClass) {
410 /* do brilliance here */
411 } else if (RC == SPU::R16CRegisterClass) {
412 /* Opc = PPC::LWZ; */
413 } else if (RC == SPU::R32CRegisterClass) {
414 /* Opc = PPC::LD; */
415 } else if (RC == SPU::R32FPRegisterClass) {
416 /* Opc = PPC::LFD; */
417 } else if (RC == SPU::R64FPRegisterClass) {
418 /* Opc = PPC::LFS; */
419 } else if (RC == SPU::VECREGRegisterClass) {
420 /* Opc = PPC::LVX; */
421 } else if (RC == SPU::GPRCRegisterClass) {
422 /* Opc = something else! */
423 } else {
424 assert(0 && "Unknown regclass!");
425 abort();
426 }
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000427 MachineInstrBuilder MIB = BuildMI(MF, get(Opc), DestReg);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000428 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
429 MachineOperand &MO = Addr[i];
Dan Gohmand735b802008-10-03 15:45:36 +0000430 if (MO.isReg())
Owen Andersonf6372aa2008-01-01 21:11:32 +0000431 MIB.addReg(MO.getReg());
Dan Gohmand735b802008-10-03 15:45:36 +0000432 else if (MO.isImm())
Owen Andersonf6372aa2008-01-01 21:11:32 +0000433 MIB.addImm(MO.getImm());
434 else
435 MIB.addFrameIndex(MO.getIndex());
436 }
437 NewMIs.push_back(MIB);
438 }
439}
440
Scott Michel52d00012009-01-03 00:27:53 +0000441//! Return true if the specified load or store can be folded
442bool
443SPUInstrInfo::canFoldMemoryOperand(const MachineInstr *MI,
444 const SmallVectorImpl<unsigned> &Ops) const {
445 if (Ops.size() != 1) return false;
446
447 // Make sure this is a reg-reg copy.
448 unsigned Opc = MI->getOpcode();
449
450 switch (Opc) {
451 case SPU::ORv16i8:
452 case SPU::ORv8i16:
453 case SPU::ORv4i32:
454 case SPU::ORv2i64:
455 case SPU::ORr8:
456 case SPU::ORr16:
457 case SPU::ORr32:
458 case SPU::ORr64:
459 case SPU::ORf32:
460 case SPU::ORf64:
461 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg())
462 return true;
463 break;
464 }
465
466 return false;
467}
468
Owen Anderson43dbe052008-01-07 01:35:02 +0000469/// foldMemoryOperand - SPU, like PPC, can only fold spills into
470/// copy instructions, turning them into load/store instructions.
471MachineInstr *
Dan Gohmanc54baa22008-12-03 18:43:12 +0000472SPUInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
473 MachineInstr *MI,
474 const SmallVectorImpl<unsigned> &Ops,
475 int FrameIndex) const
Owen Anderson43dbe052008-01-07 01:35:02 +0000476{
Scott Michel52d00012009-01-03 00:27:53 +0000477 if (Ops.size() != 1) return 0;
Owen Anderson43dbe052008-01-07 01:35:02 +0000478
479 unsigned OpNum = Ops[0];
480 unsigned Opc = MI->getOpcode();
481 MachineInstr *NewMI = 0;
Scott Michel02d711b2008-12-30 23:28:25 +0000482
Scott Michel52d00012009-01-03 00:27:53 +0000483 switch (Opc) {
484 case SPU::ORv16i8:
485 case SPU::ORv8i16:
486 case SPU::ORv4i32:
487 case SPU::ORv2i64:
488 case SPU::ORr8:
489 case SPU::ORr16:
490 case SPU::ORr32:
491 case SPU::ORr64:
492 case SPU::ORf32:
493 case SPU::ORf64:
Owen Anderson43dbe052008-01-07 01:35:02 +0000494 if (OpNum == 0) { // move -> store
495 unsigned InReg = MI->getOperand(1).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000496 bool isKill = MI->getOperand(1).isKill();
Owen Anderson43dbe052008-01-07 01:35:02 +0000497 if (FrameIndex < SPUFrameInfo::maxFrameOffset()) {
Scott Michel52d00012009-01-03 00:27:53 +0000498 MachineInstrBuilder MIB = BuildMI(MF, get(SPU::STQDr32));
499
500 MIB.addReg(InReg, false, false, isKill);
501 NewMI = addFrameReference(MIB, FrameIndex);
Owen Anderson43dbe052008-01-07 01:35:02 +0000502 }
503 } else { // move -> load
504 unsigned OutReg = MI->getOperand(0).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000505 bool isDead = MI->getOperand(0).isDead();
Scott Michel52d00012009-01-03 00:27:53 +0000506 MachineInstrBuilder MIB = BuildMI(MF, get(Opc));
507
508 MIB.addReg(OutReg, true, false, false, isDead);
Evan Cheng9f1c8312008-07-03 09:09:37 +0000509 Opc = (FrameIndex < SPUFrameInfo::maxFrameOffset())
510 ? SPU::STQDr32 : SPU::STQXr32;
Scott Michel52d00012009-01-03 00:27:53 +0000511 NewMI = addFrameReference(MIB, FrameIndex);
512 break;
513 }
Owen Anderson43dbe052008-01-07 01:35:02 +0000514 }
515
Owen Anderson43dbe052008-01-07 01:35:02 +0000516 return NewMI;
Owen Anderson43dbe052008-01-07 01:35:02 +0000517}
518
Scott Michelaedc6372008-12-10 00:15:19 +0000519//! Branch analysis
Scott Michel9bd7a372009-01-02 20:52:08 +0000520/*!
Scott Michelaedc6372008-12-10 00:15:19 +0000521 \note This code was kiped from PPC. There may be more branch analysis for
522 CellSPU than what's currently done here.
523 */
524bool
525SPUInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
Scott Michel19c10e62009-01-26 03:37:41 +0000526 MachineBasicBlock *&FBB,
Evan Chengdc54d312009-02-09 07:14:22 +0000527 SmallVectorImpl<MachineOperand> &Cond,
528 bool AllowModify) const {
Scott Michelaedc6372008-12-10 00:15:19 +0000529 // 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;
Evan Chengdc54d312009-02-09 07:14:22 +0000579 if (AllowModify)
580 I->eraseFromParent();
Scott Michelaedc6372008-12-10 00:15:19 +0000581 return false;
582 }
583
584 // Otherwise, can't handle this.
585 return true;
586}
Scott Michel02d711b2008-12-30 23:28:25 +0000587
Scott Michelaedc6372008-12-10 00:15:19 +0000588unsigned
589SPUInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
590 MachineBasicBlock::iterator I = MBB.end();
591 if (I == MBB.begin())
592 return 0;
593 --I;
594 if (!isCondBranch(I) && !isUncondBranch(I))
595 return 0;
596
597 // Remove the first branch.
Scott Michel9bd7a372009-01-02 20:52:08 +0000598 DEBUG(cerr << "Removing branch: ");
599 DEBUG(I->dump());
Scott Michelaedc6372008-12-10 00:15:19 +0000600 I->eraseFromParent();
601 I = MBB.end();
602 if (I == MBB.begin())
603 return 1;
604
605 --I;
Scott Michel9bd7a372009-01-02 20:52:08 +0000606 if (!(isCondBranch(I) || isUncondBranch(I)))
Scott Michelaedc6372008-12-10 00:15:19 +0000607 return 1;
608
609 // Remove the second branch.
Scott Michel9bd7a372009-01-02 20:52:08 +0000610 DEBUG(cerr << "Removing second branch: ");
611 DEBUG(I->dump());
Scott Michelaedc6372008-12-10 00:15:19 +0000612 I->eraseFromParent();
613 return 2;
614}
Scott Michel02d711b2008-12-30 23:28:25 +0000615
Scott Michelaedc6372008-12-10 00:15:19 +0000616unsigned
617SPUInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Scott Michel19c10e62009-01-26 03:37:41 +0000618 MachineBasicBlock *FBB,
619 const SmallVectorImpl<MachineOperand> &Cond) const {
Scott Michelaedc6372008-12-10 00:15:19 +0000620 // Shouldn't be a fall through.
621 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Scott Michel02d711b2008-12-30 23:28:25 +0000622 assert((Cond.size() == 2 || Cond.size() == 0) &&
Scott Michelaedc6372008-12-10 00:15:19 +0000623 "SPU branch conditions have two components!");
Scott Michel02d711b2008-12-30 23:28:25 +0000624
Scott Michelaedc6372008-12-10 00:15:19 +0000625 // One-way branch.
626 if (FBB == 0) {
Scott Michel9bd7a372009-01-02 20:52:08 +0000627 if (Cond.empty()) {
628 // Unconditional branch
629 MachineInstrBuilder MIB = BuildMI(&MBB, get(SPU::BR));
630 MIB.addMBB(TBB);
631
632 DEBUG(cerr << "Inserted one-way uncond branch: ");
633 DEBUG((*MIB).dump());
634 } else {
635 // Conditional branch
636 MachineInstrBuilder MIB = BuildMI(&MBB, get(Cond[0].getImm()));
637 MIB.addReg(Cond[1].getReg()).addMBB(TBB);
638
639 DEBUG(cerr << "Inserted one-way cond branch: ");
640 DEBUG((*MIB).dump());
Scott Michelaedc6372008-12-10 00:15:19 +0000641 }
642 return 1;
Scott Michel9bd7a372009-01-02 20:52:08 +0000643 } else {
644 MachineInstrBuilder MIB = BuildMI(&MBB, get(Cond[0].getImm()));
645 MachineInstrBuilder MIB2 = BuildMI(&MBB, get(SPU::BR));
646
647 // Two-way Conditional Branch.
648 MIB.addReg(Cond[1].getReg()).addMBB(TBB);
649 MIB2.addMBB(FBB);
650
651 DEBUG(cerr << "Inserted conditional branch: ");
652 DEBUG((*MIB).dump());
653 DEBUG(cerr << "part 2: ");
654 DEBUG((*MIB2).dump());
655 return 2;
Scott Michelaedc6372008-12-10 00:15:19 +0000656 }
Scott Michelaedc6372008-12-10 00:15:19 +0000657}
658
Scott Michel52d00012009-01-03 00:27:53 +0000659bool
660SPUInstrInfo::BlockHasNoFallThrough(const MachineBasicBlock &MBB) const {
661 return (!MBB.empty() && isUncondBranch(&MBB.back()));
662}
663//! Reverses a branch's condition, returning false on success.
664bool
665SPUInstrInfo::ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond)
666 const {
667 // Pretty brainless way of inverting the condition, but it works, considering
668 // there are only two conditions...
669 static struct {
670 unsigned Opc; //! The incoming opcode
671 unsigned RevCondOpc; //! The reversed condition opcode
672 } revconds[] = {
673 { SPU::BRNZr32, SPU::BRZr32 },
674 { SPU::BRNZv4i32, SPU::BRZv4i32 },
675 { SPU::BRZr32, SPU::BRNZr32 },
676 { SPU::BRZv4i32, SPU::BRNZv4i32 },
677 { SPU::BRHNZr16, SPU::BRHZr16 },
678 { SPU::BRHNZv8i16, SPU::BRHZv8i16 },
679 { SPU::BRHZr16, SPU::BRHNZr16 },
680 { SPU::BRHZv8i16, SPU::BRHNZv8i16 }
681 };
Scott Michelaedc6372008-12-10 00:15:19 +0000682
Scott Michel52d00012009-01-03 00:27:53 +0000683 unsigned Opc = unsigned(Cond[0].getImm());
684 // Pretty dull mapping between the two conditions that SPU can generate:
Misha Brukman93c65c82009-01-07 23:07:29 +0000685 for (int i = sizeof(revconds)/sizeof(revconds[0]) - 1; i >= 0; --i) {
Scott Michel52d00012009-01-03 00:27:53 +0000686 if (revconds[i].Opc == Opc) {
687 Cond[0].setImm(revconds[i].RevCondOpc);
688 return false;
689 }
690 }
691
692 return true;
693}