blob: d2ec7b1a1e42c8588d4c2b8d7fee75bf3085a1d5 [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
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000262 DebugLoc DL = DebugLoc::getUnknownLoc();
263 if (MI != MBB.end()) DL = MI->getDebugLoc();
264
Owen Andersond10fd972007-12-31 06:32:00 +0000265 if (DestRC == SPU::R8CRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000266 BuildMI(MBB, MI, DL, get(SPU::LRr8), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000267 } else if (DestRC == SPU::R16CRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000268 BuildMI(MBB, MI, DL, get(SPU::LRr16), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000269 } else if (DestRC == SPU::R32CRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000270 BuildMI(MBB, MI, DL, get(SPU::LRr32), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000271 } else if (DestRC == SPU::R32FPRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000272 BuildMI(MBB, MI, DL, get(SPU::LRf32), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000273 } else if (DestRC == SPU::R64CRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000274 BuildMI(MBB, MI, DL, get(SPU::LRr64), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000275 } else if (DestRC == SPU::R64FPRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000276 BuildMI(MBB, MI, DL, get(SPU::LRf64), DestReg).addReg(SrcReg);
Scott Michel9bd7a372009-01-02 20:52:08 +0000277 } else if (DestRC == SPU::GPRCRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000278 BuildMI(MBB, MI, DL, get(SPU::LRr128), DestReg).addReg(SrcReg);
Scott Michel9bd7a372009-01-02 20:52:08 +0000279 } else if (DestRC == SPU::VECREGRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000280 BuildMI(MBB, MI, DL, get(SPU::LRv16i8), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000281 } else {
Owen Anderson940f83e2008-08-26 18:03:31 +0000282 // Attempt to copy unknown/unsupported register class!
283 return false;
Owen Andersond10fd972007-12-31 06:32:00 +0000284 }
Scott Michel02d711b2008-12-30 23:28:25 +0000285
Owen Anderson940f83e2008-08-26 18:03:31 +0000286 return true;
Owen Andersond10fd972007-12-31 06:32:00 +0000287}
Owen Andersonf6372aa2008-01-01 21:11:32 +0000288
289void
290SPUInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
291 MachineBasicBlock::iterator MI,
292 unsigned SrcReg, bool isKill, int FrameIdx,
293 const TargetRegisterClass *RC) const
294{
Chris Lattnercc8cd0c2008-01-07 02:48:55 +0000295 unsigned opc;
Scott Michelaedc6372008-12-10 00:15:19 +0000296 bool isValidFrameIdx = (FrameIdx < SPUFrameInfo::maxFrameOffset());
Owen Andersonf6372aa2008-01-01 21:11:32 +0000297 if (RC == SPU::GPRCRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000298 opc = (isValidFrameIdx ? SPU::STQDr128 : SPU::STQXr128);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000299 } else if (RC == SPU::R64CRegisterClass) {
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::R64FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000302 opc = (isValidFrameIdx ? SPU::STQDr64 : SPU::STQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000303 } else if (RC == SPU::R32CRegisterClass) {
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::R32FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000306 opc = (isValidFrameIdx ? SPU::STQDr32 : SPU::STQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000307 } else if (RC == SPU::R16CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000308 opc = (isValidFrameIdx ? SPU::STQDr16 : SPU::STQXr16);
309 } else if (RC == SPU::R8CRegisterClass) {
310 opc = (isValidFrameIdx ? SPU::STQDr8 : SPU::STQXr8);
Scott Michelf0569be2008-12-27 04:51:36 +0000311 } else if (RC == SPU::VECREGRegisterClass) {
312 opc = (isValidFrameIdx) ? SPU::STQDv16i8 : SPU::STQXv16i8;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000313 } else {
314 assert(0 && "Unknown regclass!");
315 abort();
316 }
317
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000318 DebugLoc DL = DebugLoc::getUnknownLoc();
319 if (MI != MBB.end()) DL = MI->getDebugLoc();
320 addFrameReference(BuildMI(MBB, MI, DL, get(opc))
Owen Andersonf6372aa2008-01-01 21:11:32 +0000321 .addReg(SrcReg, false, false, isKill), FrameIdx);
322}
323
324void SPUInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000325 bool isKill,
326 SmallVectorImpl<MachineOperand> &Addr,
327 const TargetRegisterClass *RC,
328 SmallVectorImpl<MachineInstr*> &NewMIs) const {
Owen Andersonf6372aa2008-01-01 21:11:32 +0000329 cerr << "storeRegToAddr() invoked!\n";
330 abort();
331
Dan Gohmand735b802008-10-03 15:45:36 +0000332 if (Addr[0].isFI()) {
Owen Andersonf6372aa2008-01-01 21:11:32 +0000333 /* do what storeRegToStackSlot does here */
334 } else {
335 unsigned Opc = 0;
336 if (RC == SPU::GPRCRegisterClass) {
337 /* Opc = PPC::STW; */
338 } else if (RC == SPU::R16CRegisterClass) {
339 /* Opc = PPC::STD; */
340 } else if (RC == SPU::R32CRegisterClass) {
341 /* Opc = PPC::STFD; */
342 } else if (RC == SPU::R32FPRegisterClass) {
343 /* Opc = PPC::STFD; */
344 } else if (RC == SPU::R64FPRegisterClass) {
345 /* Opc = PPC::STFS; */
346 } else if (RC == SPU::VECREGRegisterClass) {
347 /* Opc = PPC::STVX; */
348 } else {
349 assert(0 && "Unknown regclass!");
350 abort();
351 }
Dale Johannesen21b55412009-02-12 23:08:38 +0000352 DebugLoc DL = DebugLoc::getUnknownLoc();
353 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Opc))
Owen Andersonf6372aa2008-01-01 21:11:32 +0000354 .addReg(SrcReg, false, false, isKill);
355 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
356 MachineOperand &MO = Addr[i];
Dan Gohmand735b802008-10-03 15:45:36 +0000357 if (MO.isReg())
Owen Andersonf6372aa2008-01-01 21:11:32 +0000358 MIB.addReg(MO.getReg());
Dan Gohmand735b802008-10-03 15:45:36 +0000359 else if (MO.isImm())
Owen Andersonf6372aa2008-01-01 21:11:32 +0000360 MIB.addImm(MO.getImm());
361 else
362 MIB.addFrameIndex(MO.getIndex());
363 }
364 NewMIs.push_back(MIB);
365 }
366}
367
368void
369SPUInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
370 MachineBasicBlock::iterator MI,
371 unsigned DestReg, int FrameIdx,
372 const TargetRegisterClass *RC) const
373{
Chris Lattnercc8cd0c2008-01-07 02:48:55 +0000374 unsigned opc;
Scott Michelaedc6372008-12-10 00:15:19 +0000375 bool isValidFrameIdx = (FrameIdx < SPUFrameInfo::maxFrameOffset());
Owen Andersonf6372aa2008-01-01 21:11:32 +0000376 if (RC == SPU::GPRCRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000377 opc = (isValidFrameIdx ? SPU::LQDr128 : SPU::LQXr128);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000378 } else if (RC == SPU::R64CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000379 opc = (isValidFrameIdx ? SPU::LQDr64 : SPU::LQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000380 } else if (RC == SPU::R64FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000381 opc = (isValidFrameIdx ? SPU::LQDr64 : SPU::LQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000382 } else if (RC == SPU::R32CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000383 opc = (isValidFrameIdx ? SPU::LQDr32 : SPU::LQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000384 } else if (RC == SPU::R32FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000385 opc = (isValidFrameIdx ? SPU::LQDr32 : SPU::LQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000386 } else if (RC == SPU::R16CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000387 opc = (isValidFrameIdx ? SPU::LQDr16 : SPU::LQXr16);
388 } else if (RC == SPU::R8CRegisterClass) {
389 opc = (isValidFrameIdx ? SPU::LQDr8 : SPU::LQXr8);
Scott Michelf0569be2008-12-27 04:51:36 +0000390 } else if (RC == SPU::VECREGRegisterClass) {
391 opc = (isValidFrameIdx) ? SPU::LQDv16i8 : SPU::LQXv16i8;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000392 } else {
393 assert(0 && "Unknown regclass in loadRegFromStackSlot!");
394 abort();
395 }
396
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000397 DebugLoc DL = DebugLoc::getUnknownLoc();
398 if (MI != MBB.end()) DL = MI->getDebugLoc();
399 addFrameReference(BuildMI(MBB, MI, DL, get(opc)).addReg(DestReg), FrameIdx);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000400}
401
402/*!
403 \note We are really pessimistic here about what kind of a load we're doing.
404 */
405void SPUInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
Scott Michelaedc6372008-12-10 00:15:19 +0000406 SmallVectorImpl<MachineOperand> &Addr,
407 const TargetRegisterClass *RC,
408 SmallVectorImpl<MachineInstr*> &NewMIs)
Owen Andersonf6372aa2008-01-01 21:11:32 +0000409 const {
410 cerr << "loadRegToAddr() invoked!\n";
411 abort();
412
Dan Gohmand735b802008-10-03 15:45:36 +0000413 if (Addr[0].isFI()) {
Owen Andersonf6372aa2008-01-01 21:11:32 +0000414 /* do what loadRegFromStackSlot does here... */
415 } else {
416 unsigned Opc = 0;
417 if (RC == SPU::R8CRegisterClass) {
418 /* do brilliance here */
419 } else if (RC == SPU::R16CRegisterClass) {
420 /* Opc = PPC::LWZ; */
421 } else if (RC == SPU::R32CRegisterClass) {
422 /* Opc = PPC::LD; */
423 } else if (RC == SPU::R32FPRegisterClass) {
424 /* Opc = PPC::LFD; */
425 } else if (RC == SPU::R64FPRegisterClass) {
426 /* Opc = PPC::LFS; */
427 } else if (RC == SPU::VECREGRegisterClass) {
428 /* Opc = PPC::LVX; */
429 } else if (RC == SPU::GPRCRegisterClass) {
430 /* Opc = something else! */
431 } else {
432 assert(0 && "Unknown regclass!");
433 abort();
434 }
Dale Johannesen21b55412009-02-12 23:08:38 +0000435 DebugLoc DL = DebugLoc::getUnknownLoc();
436 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Opc), DestReg);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000437 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
438 MachineOperand &MO = Addr[i];
Dan Gohmand735b802008-10-03 15:45:36 +0000439 if (MO.isReg())
Owen Andersonf6372aa2008-01-01 21:11:32 +0000440 MIB.addReg(MO.getReg());
Dan Gohmand735b802008-10-03 15:45:36 +0000441 else if (MO.isImm())
Owen Andersonf6372aa2008-01-01 21:11:32 +0000442 MIB.addImm(MO.getImm());
443 else
444 MIB.addFrameIndex(MO.getIndex());
445 }
446 NewMIs.push_back(MIB);
447 }
448}
449
Scott Michel52d00012009-01-03 00:27:53 +0000450//! Return true if the specified load or store can be folded
451bool
452SPUInstrInfo::canFoldMemoryOperand(const MachineInstr *MI,
453 const SmallVectorImpl<unsigned> &Ops) const {
454 if (Ops.size() != 1) return false;
455
456 // Make sure this is a reg-reg copy.
457 unsigned Opc = MI->getOpcode();
458
459 switch (Opc) {
460 case SPU::ORv16i8:
461 case SPU::ORv8i16:
462 case SPU::ORv4i32:
463 case SPU::ORv2i64:
464 case SPU::ORr8:
465 case SPU::ORr16:
466 case SPU::ORr32:
467 case SPU::ORr64:
468 case SPU::ORf32:
469 case SPU::ORf64:
470 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg())
471 return true;
472 break;
473 }
474
475 return false;
476}
477
Owen Anderson43dbe052008-01-07 01:35:02 +0000478/// foldMemoryOperand - SPU, like PPC, can only fold spills into
479/// copy instructions, turning them into load/store instructions.
480MachineInstr *
Dan Gohmanc54baa22008-12-03 18:43:12 +0000481SPUInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
482 MachineInstr *MI,
483 const SmallVectorImpl<unsigned> &Ops,
484 int FrameIndex) const
Owen Anderson43dbe052008-01-07 01:35:02 +0000485{
Scott Michel52d00012009-01-03 00:27:53 +0000486 if (Ops.size() != 1) return 0;
Owen Anderson43dbe052008-01-07 01:35:02 +0000487
488 unsigned OpNum = Ops[0];
489 unsigned Opc = MI->getOpcode();
490 MachineInstr *NewMI = 0;
Scott Michel02d711b2008-12-30 23:28:25 +0000491
Scott Michel52d00012009-01-03 00:27:53 +0000492 switch (Opc) {
493 case SPU::ORv16i8:
494 case SPU::ORv8i16:
495 case SPU::ORv4i32:
496 case SPU::ORv2i64:
497 case SPU::ORr8:
498 case SPU::ORr16:
499 case SPU::ORr32:
500 case SPU::ORr64:
501 case SPU::ORf32:
502 case SPU::ORf64:
Owen Anderson43dbe052008-01-07 01:35:02 +0000503 if (OpNum == 0) { // move -> store
504 unsigned InReg = MI->getOperand(1).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000505 bool isKill = MI->getOperand(1).isKill();
Owen Anderson43dbe052008-01-07 01:35:02 +0000506 if (FrameIndex < SPUFrameInfo::maxFrameOffset()) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000507 MachineInstrBuilder MIB = BuildMI(MF, MI->getDebugLoc(),
508 get(SPU::STQDr32));
Scott Michel52d00012009-01-03 00:27:53 +0000509
510 MIB.addReg(InReg, false, false, isKill);
511 NewMI = addFrameReference(MIB, FrameIndex);
Owen Anderson43dbe052008-01-07 01:35:02 +0000512 }
513 } else { // move -> load
514 unsigned OutReg = MI->getOperand(0).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000515 bool isDead = MI->getOperand(0).isDead();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000516 MachineInstrBuilder MIB = BuildMI(MF, MI->getDebugLoc(), get(Opc));
Scott Michel52d00012009-01-03 00:27:53 +0000517
518 MIB.addReg(OutReg, true, false, false, isDead);
Evan Cheng9f1c8312008-07-03 09:09:37 +0000519 Opc = (FrameIndex < SPUFrameInfo::maxFrameOffset())
520 ? SPU::STQDr32 : SPU::STQXr32;
Scott Michel52d00012009-01-03 00:27:53 +0000521 NewMI = addFrameReference(MIB, FrameIndex);
522 break;
523 }
Owen Anderson43dbe052008-01-07 01:35:02 +0000524 }
525
Owen Anderson43dbe052008-01-07 01:35:02 +0000526 return NewMI;
Owen Anderson43dbe052008-01-07 01:35:02 +0000527}
528
Scott Michelaedc6372008-12-10 00:15:19 +0000529//! Branch analysis
Scott Michel9bd7a372009-01-02 20:52:08 +0000530/*!
Scott Michelaedc6372008-12-10 00:15:19 +0000531 \note This code was kiped from PPC. There may be more branch analysis for
532 CellSPU than what's currently done here.
533 */
534bool
535SPUInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
Scott Michel19c10e62009-01-26 03:37:41 +0000536 MachineBasicBlock *&FBB,
Evan Chengdc54d312009-02-09 07:14:22 +0000537 SmallVectorImpl<MachineOperand> &Cond,
538 bool AllowModify) const {
Scott Michelaedc6372008-12-10 00:15:19 +0000539 // If the block has no terminators, it just falls into the block after it.
540 MachineBasicBlock::iterator I = MBB.end();
541 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
542 return false;
543
544 // Get the last instruction in the block.
545 MachineInstr *LastInst = I;
Scott Michel02d711b2008-12-30 23:28:25 +0000546
Scott Michelaedc6372008-12-10 00:15:19 +0000547 // If there is only one terminator instruction, process it.
548 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
549 if (isUncondBranch(LastInst)) {
550 TBB = LastInst->getOperand(0).getMBB();
551 return false;
552 } else if (isCondBranch(LastInst)) {
553 // Block ends with fall-through condbranch.
554 TBB = LastInst->getOperand(1).getMBB();
Scott Michel9bd7a372009-01-02 20:52:08 +0000555 DEBUG(cerr << "Pushing LastInst: ");
556 DEBUG(LastInst->dump());
557 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Scott Michelaedc6372008-12-10 00:15:19 +0000558 Cond.push_back(LastInst->getOperand(0));
Scott Michelaedc6372008-12-10 00:15:19 +0000559 return false;
560 }
561 // Otherwise, don't know what this is.
562 return true;
563 }
Scott Michel02d711b2008-12-30 23:28:25 +0000564
Scott Michelaedc6372008-12-10 00:15:19 +0000565 // Get the instruction before it if it's a terminator.
566 MachineInstr *SecondLastInst = I;
567
568 // If there are three terminators, we don't know what sort of block this is.
569 if (SecondLastInst && I != MBB.begin() &&
570 isUnpredicatedTerminator(--I))
571 return true;
Scott Michel02d711b2008-12-30 23:28:25 +0000572
Scott Michelaedc6372008-12-10 00:15:19 +0000573 // If the block ends with a conditional and unconditional branch, handle it.
574 if (isCondBranch(SecondLastInst) && isUncondBranch(LastInst)) {
575 TBB = SecondLastInst->getOperand(1).getMBB();
Scott Michel9bd7a372009-01-02 20:52:08 +0000576 DEBUG(cerr << "Pushing SecondLastInst: ");
577 DEBUG(SecondLastInst->dump());
578 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Scott Michelaedc6372008-12-10 00:15:19 +0000579 Cond.push_back(SecondLastInst->getOperand(0));
Scott Michelaedc6372008-12-10 00:15:19 +0000580 FBB = LastInst->getOperand(0).getMBB();
581 return false;
582 }
Scott Michel02d711b2008-12-30 23:28:25 +0000583
Scott Michelaedc6372008-12-10 00:15:19 +0000584 // If the block ends with two unconditional branches, handle it. The second
585 // one is not executed, so remove it.
586 if (isUncondBranch(SecondLastInst) && isUncondBranch(LastInst)) {
587 TBB = SecondLastInst->getOperand(0).getMBB();
588 I = LastInst;
Evan Chengdc54d312009-02-09 07:14:22 +0000589 if (AllowModify)
590 I->eraseFromParent();
Scott Michelaedc6372008-12-10 00:15:19 +0000591 return false;
592 }
593
594 // Otherwise, can't handle this.
595 return true;
596}
Scott Michel02d711b2008-12-30 23:28:25 +0000597
Scott Michelaedc6372008-12-10 00:15:19 +0000598unsigned
599SPUInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
600 MachineBasicBlock::iterator I = MBB.end();
601 if (I == MBB.begin())
602 return 0;
603 --I;
604 if (!isCondBranch(I) && !isUncondBranch(I))
605 return 0;
606
607 // Remove the first branch.
Scott Michel9bd7a372009-01-02 20:52:08 +0000608 DEBUG(cerr << "Removing branch: ");
609 DEBUG(I->dump());
Scott Michelaedc6372008-12-10 00:15:19 +0000610 I->eraseFromParent();
611 I = MBB.end();
612 if (I == MBB.begin())
613 return 1;
614
615 --I;
Scott Michel9bd7a372009-01-02 20:52:08 +0000616 if (!(isCondBranch(I) || isUncondBranch(I)))
Scott Michelaedc6372008-12-10 00:15:19 +0000617 return 1;
618
619 // Remove the second branch.
Scott Michel9bd7a372009-01-02 20:52:08 +0000620 DEBUG(cerr << "Removing second branch: ");
621 DEBUG(I->dump());
Scott Michelaedc6372008-12-10 00:15:19 +0000622 I->eraseFromParent();
623 return 2;
624}
Scott Michel02d711b2008-12-30 23:28:25 +0000625
Scott Michelaedc6372008-12-10 00:15:19 +0000626unsigned
627SPUInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Scott Michel19c10e62009-01-26 03:37:41 +0000628 MachineBasicBlock *FBB,
629 const SmallVectorImpl<MachineOperand> &Cond) const {
Scott Michelaedc6372008-12-10 00:15:19 +0000630 // Shouldn't be a fall through.
631 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Scott Michel02d711b2008-12-30 23:28:25 +0000632 assert((Cond.size() == 2 || Cond.size() == 0) &&
Scott Michelaedc6372008-12-10 00:15:19 +0000633 "SPU branch conditions have two components!");
Scott Michel02d711b2008-12-30 23:28:25 +0000634
Scott Michelaedc6372008-12-10 00:15:19 +0000635 // One-way branch.
636 if (FBB == 0) {
Scott Michel9bd7a372009-01-02 20:52:08 +0000637 if (Cond.empty()) {
638 // Unconditional branch
639 MachineInstrBuilder MIB = BuildMI(&MBB, get(SPU::BR));
640 MIB.addMBB(TBB);
641
642 DEBUG(cerr << "Inserted one-way uncond branch: ");
643 DEBUG((*MIB).dump());
644 } else {
645 // Conditional branch
646 MachineInstrBuilder MIB = BuildMI(&MBB, get(Cond[0].getImm()));
647 MIB.addReg(Cond[1].getReg()).addMBB(TBB);
648
649 DEBUG(cerr << "Inserted one-way cond branch: ");
650 DEBUG((*MIB).dump());
Scott Michelaedc6372008-12-10 00:15:19 +0000651 }
652 return 1;
Scott Michel9bd7a372009-01-02 20:52:08 +0000653 } else {
654 MachineInstrBuilder MIB = BuildMI(&MBB, get(Cond[0].getImm()));
655 MachineInstrBuilder MIB2 = BuildMI(&MBB, get(SPU::BR));
656
657 // Two-way Conditional Branch.
658 MIB.addReg(Cond[1].getReg()).addMBB(TBB);
659 MIB2.addMBB(FBB);
660
661 DEBUG(cerr << "Inserted conditional branch: ");
662 DEBUG((*MIB).dump());
663 DEBUG(cerr << "part 2: ");
664 DEBUG((*MIB2).dump());
665 return 2;
Scott Michelaedc6372008-12-10 00:15:19 +0000666 }
Scott Michelaedc6372008-12-10 00:15:19 +0000667}
668
Scott Michel52d00012009-01-03 00:27:53 +0000669bool
670SPUInstrInfo::BlockHasNoFallThrough(const MachineBasicBlock &MBB) const {
671 return (!MBB.empty() && isUncondBranch(&MBB.back()));
672}
673//! Reverses a branch's condition, returning false on success.
674bool
675SPUInstrInfo::ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond)
676 const {
677 // Pretty brainless way of inverting the condition, but it works, considering
678 // there are only two conditions...
679 static struct {
680 unsigned Opc; //! The incoming opcode
681 unsigned RevCondOpc; //! The reversed condition opcode
682 } revconds[] = {
683 { SPU::BRNZr32, SPU::BRZr32 },
684 { SPU::BRNZv4i32, SPU::BRZv4i32 },
685 { SPU::BRZr32, SPU::BRNZr32 },
686 { SPU::BRZv4i32, SPU::BRNZv4i32 },
687 { SPU::BRHNZr16, SPU::BRHZr16 },
688 { SPU::BRHNZv8i16, SPU::BRHZv8i16 },
689 { SPU::BRHZr16, SPU::BRHNZr16 },
690 { SPU::BRHZv8i16, SPU::BRHNZv8i16 }
691 };
Scott Michelaedc6372008-12-10 00:15:19 +0000692
Scott Michel52d00012009-01-03 00:27:53 +0000693 unsigned Opc = unsigned(Cond[0].getImm());
694 // Pretty dull mapping between the two conditions that SPU can generate:
Misha Brukman93c65c82009-01-07 23:07:29 +0000695 for (int i = sizeof(revconds)/sizeof(revconds[0]) - 1; i >= 0; --i) {
Scott Michel52d00012009-01-03 00:27:53 +0000696 if (revconds[i].Opc == Opc) {
697 Cond[0].setImm(revconds[i].RevCondOpc);
698 return false;
699 }
700 }
701
702 return true;
703}