blob: 423da3b4dbebc274b358971620b4fd97467b45b7 [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"
Scott Michel9bd7a372009-01-02 20:52:08 +000020#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000021#include "llvm/Support/ErrorHandling.h"
Benjamin Kramer072a56e2009-08-23 11:52:17 +000022#include "llvm/Support/raw_ostream.h"
Scott Michel66377522007-12-04 22:35:58 +000023
24using namespace llvm;
25
Scott Michelaedc6372008-12-10 00:15:19 +000026namespace {
27 //! Predicate for an unconditional branch instruction
28 inline bool isUncondBranch(const MachineInstr *I) {
29 unsigned opc = I->getOpcode();
30
31 return (opc == SPU::BR
Scott Michel19c10e62009-01-26 03:37:41 +000032 || opc == SPU::BRA
33 || opc == SPU::BI);
Scott Michelaedc6372008-12-10 00:15:19 +000034 }
35
Scott Michel52d00012009-01-03 00:27:53 +000036 //! Predicate for a conditional branch instruction
Scott Michelaedc6372008-12-10 00:15:19 +000037 inline bool isCondBranch(const MachineInstr *I) {
38 unsigned opc = I->getOpcode();
39
Scott Michelf0569be2008-12-27 04:51:36 +000040 return (opc == SPU::BRNZr32
41 || opc == SPU::BRNZv4i32
Scott Michel19c10e62009-01-26 03:37:41 +000042 || opc == SPU::BRZr32
43 || opc == SPU::BRZv4i32
44 || opc == SPU::BRHNZr16
45 || opc == SPU::BRHNZv8i16
46 || opc == SPU::BRHZr16
47 || opc == SPU::BRHZv8i16);
Scott Michelaedc6372008-12-10 00:15:19 +000048 }
49}
50
Scott Michel66377522007-12-04 22:35:58 +000051SPUInstrInfo::SPUInstrInfo(SPUTargetMachine &tm)
Chris Lattner64105522008-01-01 01:03:04 +000052 : TargetInstrInfoImpl(SPUInsts, sizeof(SPUInsts)/sizeof(SPUInsts[0])),
Scott Michel66377522007-12-04 22:35:58 +000053 TM(tm),
54 RI(*TM.getSubtargetImpl(), *this)
Scott Michel52d00012009-01-03 00:27:53 +000055{ /* NOP */ }
Scott Michel66377522007-12-04 22:35:58 +000056
Scott Michel66377522007-12-04 22:35:58 +000057bool
58SPUInstrInfo::isMoveInstr(const MachineInstr& MI,
59 unsigned& sourceReg,
Evan Cheng04ee5a12009-01-20 19:12:24 +000060 unsigned& destReg,
61 unsigned& SrcSR, unsigned& DstSR) const {
62 SrcSR = DstSR = 0; // No sub-registers.
63
Scott Michel66377522007-12-04 22:35:58 +000064 switch (MI.getOpcode()) {
65 default:
66 break;
67 case SPU::ORIv4i32:
68 case SPU::ORIr32:
Scott Michel66377522007-12-04 22:35:58 +000069 case SPU::ORHIv8i16:
70 case SPU::ORHIr16:
Scott Michela59d4692008-02-23 18:41:37 +000071 case SPU::ORHIi8i16:
Scott Michel66377522007-12-04 22:35:58 +000072 case SPU::ORBIv16i8:
Scott Michel504c3692007-12-17 22:32:34 +000073 case SPU::ORBIr8:
Scott Michela59d4692008-02-23 18:41:37 +000074 case SPU::ORIi16i32:
75 case SPU::ORIi8i32:
Scott Michel66377522007-12-04 22:35:58 +000076 case SPU::AHIvec:
77 case SPU::AHIr16:
Scott Michel02d711b2008-12-30 23:28:25 +000078 case SPU::AIv4i32:
Scott Michel66377522007-12-04 22:35:58 +000079 assert(MI.getNumOperands() == 3 &&
Dan Gohmand735b802008-10-03 15:45:36 +000080 MI.getOperand(0).isReg() &&
81 MI.getOperand(1).isReg() &&
82 MI.getOperand(2).isImm() &&
Scott Michel66377522007-12-04 22:35:58 +000083 "invalid SPU ORI/ORHI/ORBI/AHI/AI/SFI/SFHI instruction!");
Chris Lattner9a1ceae2007-12-30 20:49:49 +000084 if (MI.getOperand(2).getImm() == 0) {
Scott Michel66377522007-12-04 22:35:58 +000085 sourceReg = MI.getOperand(1).getReg();
86 destReg = MI.getOperand(0).getReg();
87 return true;
88 }
89 break;
Scott Michel9999e682007-12-19 07:35:06 +000090 case SPU::AIr32:
91 assert(MI.getNumOperands() == 3 &&
92 "wrong number of operands to AIr32");
Dan Gohmand735b802008-10-03 15:45:36 +000093 if (MI.getOperand(0).isReg() &&
Scott Michel02d711b2008-12-30 23:28:25 +000094 MI.getOperand(1).isReg() &&
Dan Gohmand735b802008-10-03 15:45:36 +000095 (MI.getOperand(2).isImm() &&
Chris Lattner9a1ceae2007-12-30 20:49:49 +000096 MI.getOperand(2).getImm() == 0)) {
Scott Michel9999e682007-12-19 07:35:06 +000097 sourceReg = MI.getOperand(1).getReg();
98 destReg = MI.getOperand(0).getReg();
99 return true;
100 }
101 break;
Scott Michelf0569be2008-12-27 04:51:36 +0000102 case SPU::LRr8:
103 case SPU::LRr16:
104 case SPU::LRr32:
105 case SPU::LRf32:
106 case SPU::LRr64:
107 case SPU::LRf64:
108 case SPU::LRr128:
109 case SPU::LRv16i8:
110 case SPU::LRv8i16:
111 case SPU::LRv4i32:
112 case SPU::LRv4f32:
113 case SPU::LRv2i64:
114 case SPU::LRv2f64:
Scott Michel170783a2007-12-19 20:15:47 +0000115 case SPU::ORv16i8_i8:
Scott Michel66377522007-12-04 22:35:58 +0000116 case SPU::ORv8i16_i16:
117 case SPU::ORv4i32_i32:
118 case SPU::ORv2i64_i64:
119 case SPU::ORv4f32_f32:
120 case SPU::ORv2f64_f64:
Scott Michel170783a2007-12-19 20:15:47 +0000121 case SPU::ORi8_v16i8:
Scott Michel66377522007-12-04 22:35:58 +0000122 case SPU::ORi16_v8i16:
123 case SPU::ORi32_v4i32:
124 case SPU::ORi64_v2i64:
125 case SPU::ORf32_v4f32:
Scott Micheldd950092009-01-06 03:36:14 +0000126 case SPU::ORf64_v2f64:
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000127/*
Scott Micheldd950092009-01-06 03:36:14 +0000128 case SPU::ORi128_r64:
129 case SPU::ORi128_f64:
130 case SPU::ORi128_r32:
131 case SPU::ORi128_f32:
132 case SPU::ORi128_r16:
133 case SPU::ORi128_r8:
Scott Michel6e1d1472009-03-16 18:47:25 +0000134*/
Scott Micheldd950092009-01-06 03:36:14 +0000135 case SPU::ORi128_vec:
Scott Michel6e1d1472009-03-16 18:47:25 +0000136/*
Scott Micheldd950092009-01-06 03:36:14 +0000137 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:
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000143*/
Scott Michel6e1d1472009-03-16 18:47:25 +0000144 case SPU::ORvec_i128:
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000145/*
Scott Micheldd950092009-01-06 03:36:14 +0000146 case SPU::ORr16_r32:
147 case SPU::ORr8_r32:
Scott Michel6e1d1472009-03-16 18:47:25 +0000148 case SPU::ORf32_r32:
149 case SPU::ORr32_f32:
Scott Micheldd950092009-01-06 03:36:14 +0000150 case SPU::ORr32_r16:
151 case SPU::ORr32_r8:
Scott Micheldd950092009-01-06 03:36:14 +0000152 case SPU::ORr16_r64:
153 case SPU::ORr8_r64:
Scott Micheldd950092009-01-06 03:36:14 +0000154 case SPU::ORr64_r16:
155 case SPU::ORr64_r8:
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000156*/
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000157 case SPU::ORr64_r32:
158 case SPU::ORr32_r64:
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000159 case SPU::ORf32_r32:
160 case SPU::ORr32_f32:
161 case SPU::ORf64_r64:
162 case SPU::ORr64_f64: {
Scott Michelf0569be2008-12-27 04:51:36 +0000163 assert(MI.getNumOperands() == 2 &&
164 MI.getOperand(0).isReg() &&
165 MI.getOperand(1).isReg() &&
Scott Michel52d00012009-01-03 00:27:53 +0000166 "invalid SPU OR<type>_<vec> or LR instruction!");
Scott Michelf0569be2008-12-27 04:51:36 +0000167 if (MI.getOperand(0).getReg() == MI.getOperand(1).getReg()) {
Scott Michel7ea02ff2009-03-17 01:15:45 +0000168 sourceReg = MI.getOperand(1).getReg();
Scott Michelf0569be2008-12-27 04:51:36 +0000169 destReg = MI.getOperand(0).getReg();
170 return true;
171 }
172 break;
173 }
Scott Michel66377522007-12-04 22:35:58 +0000174 case SPU::ORv16i8:
175 case SPU::ORv8i16:
176 case SPU::ORv4i32:
Scott Michel52d00012009-01-03 00:27:53 +0000177 case SPU::ORv2i64:
178 case SPU::ORr8:
179 case SPU::ORr16:
Scott Michel66377522007-12-04 22:35:58 +0000180 case SPU::ORr32:
181 case SPU::ORr64:
Scott Michel6e1d1472009-03-16 18:47:25 +0000182 case SPU::ORr128:
Scott Michel86c041f2007-12-20 00:44:13 +0000183 case SPU::ORf32:
184 case SPU::ORf64:
Scott Michel66377522007-12-04 22:35:58 +0000185 assert(MI.getNumOperands() == 3 &&
Dan Gohmand735b802008-10-03 15:45:36 +0000186 MI.getOperand(0).isReg() &&
187 MI.getOperand(1).isReg() &&
188 MI.getOperand(2).isReg() &&
Scott Michel66377522007-12-04 22:35:58 +0000189 "invalid SPU OR(vec|r32|r64|gprc) instruction!");
190 if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
191 sourceReg = MI.getOperand(1).getReg();
192 destReg = MI.getOperand(0).getReg();
193 return true;
194 }
195 break;
196 }
197
198 return false;
199}
200
201unsigned
Dan Gohmancbad42c2008-11-18 19:49:32 +0000202SPUInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
203 int &FrameIndex) const {
Scott Michel66377522007-12-04 22:35:58 +0000204 switch (MI->getOpcode()) {
205 default: break;
206 case SPU::LQDv16i8:
207 case SPU::LQDv8i16:
208 case SPU::LQDv4i32:
209 case SPU::LQDv4f32:
210 case SPU::LQDv2f64:
211 case SPU::LQDr128:
212 case SPU::LQDr64:
213 case SPU::LQDr32:
Scott Michelaedc6372008-12-10 00:15:19 +0000214 case SPU::LQDr16: {
215 const MachineOperand MOp1 = MI->getOperand(1);
216 const MachineOperand MOp2 = MI->getOperand(2);
Scott Michel52d00012009-01-03 00:27:53 +0000217 if (MOp1.isImm() && MOp2.isFI()) {
218 FrameIndex = MOp2.getIndex();
Scott Michelaedc6372008-12-10 00:15:19 +0000219 return MI->getOperand(0).getReg();
220 }
221 break;
222 }
Scott Michel66377522007-12-04 22:35:58 +0000223 }
224 return 0;
225}
226
227unsigned
Dan Gohmancbad42c2008-11-18 19:49:32 +0000228SPUInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
229 int &FrameIndex) const {
Scott Michel66377522007-12-04 22:35:58 +0000230 switch (MI->getOpcode()) {
231 default: break;
232 case SPU::STQDv16i8:
233 case SPU::STQDv8i16:
234 case SPU::STQDv4i32:
235 case SPU::STQDv4f32:
236 case SPU::STQDv2f64:
237 case SPU::STQDr128:
238 case SPU::STQDr64:
239 case SPU::STQDr32:
240 case SPU::STQDr16:
Scott Michelaedc6372008-12-10 00:15:19 +0000241 case SPU::STQDr8: {
242 const MachineOperand MOp1 = MI->getOperand(1);
243 const MachineOperand MOp2 = MI->getOperand(2);
Scott Michelf0569be2008-12-27 04:51:36 +0000244 if (MOp1.isImm() && MOp2.isFI()) {
245 FrameIndex = MOp2.getIndex();
Scott Michelaedc6372008-12-10 00:15:19 +0000246 return MI->getOperand(0).getReg();
247 }
248 break;
249 }
Scott Michel66377522007-12-04 22:35:58 +0000250 }
251 return 0;
252}
Owen Andersond10fd972007-12-31 06:32:00 +0000253
Owen Anderson940f83e2008-08-26 18:03:31 +0000254bool SPUInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
Owen Andersond10fd972007-12-31 06:32:00 +0000255 MachineBasicBlock::iterator MI,
256 unsigned DestReg, unsigned SrcReg,
257 const TargetRegisterClass *DestRC,
258 const TargetRegisterClass *SrcRC) const
259{
Chris Lattner5e09da22008-03-09 20:31:11 +0000260 // We support cross register class moves for our aliases, such as R3 in any
261 // reg class to any other reg class containing R3. This is required because
262 // we instruction select bitconvert i64 -> f64 as a noop for example, so our
263 // types have no specific meaning.
Scott Michel02d711b2008-12-30 23:28:25 +0000264
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000265 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000266 if (MI != MBB.end()) DL = MI->getDebugLoc();
267
Owen Andersond10fd972007-12-31 06:32:00 +0000268 if (DestRC == SPU::R8CRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000269 BuildMI(MBB, MI, DL, get(SPU::LRr8), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000270 } else if (DestRC == SPU::R16CRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000271 BuildMI(MBB, MI, DL, get(SPU::LRr16), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000272 } else if (DestRC == SPU::R32CRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000273 BuildMI(MBB, MI, DL, get(SPU::LRr32), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000274 } else if (DestRC == SPU::R32FPRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000275 BuildMI(MBB, MI, DL, get(SPU::LRf32), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000276 } else if (DestRC == SPU::R64CRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000277 BuildMI(MBB, MI, DL, get(SPU::LRr64), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000278 } else if (DestRC == SPU::R64FPRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000279 BuildMI(MBB, MI, DL, get(SPU::LRf64), DestReg).addReg(SrcReg);
Scott Michel9bd7a372009-01-02 20:52:08 +0000280 } else if (DestRC == SPU::GPRCRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000281 BuildMI(MBB, MI, DL, get(SPU::LRr128), DestReg).addReg(SrcReg);
Scott Michel9bd7a372009-01-02 20:52:08 +0000282 } else if (DestRC == SPU::VECREGRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000283 BuildMI(MBB, MI, DL, get(SPU::LRv16i8), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000284 } else {
Owen Anderson940f83e2008-08-26 18:03:31 +0000285 // Attempt to copy unknown/unsupported register class!
286 return false;
Owen Andersond10fd972007-12-31 06:32:00 +0000287 }
Scott Michel02d711b2008-12-30 23:28:25 +0000288
Owen Anderson940f83e2008-08-26 18:03:31 +0000289 return true;
Owen Andersond10fd972007-12-31 06:32:00 +0000290}
Owen Andersonf6372aa2008-01-01 21:11:32 +0000291
292void
293SPUInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
294 MachineBasicBlock::iterator MI,
295 unsigned SrcReg, bool isKill, int FrameIdx,
296 const TargetRegisterClass *RC) const
297{
Chris Lattnercc8cd0c2008-01-07 02:48:55 +0000298 unsigned opc;
Scott Michelaedc6372008-12-10 00:15:19 +0000299 bool isValidFrameIdx = (FrameIdx < SPUFrameInfo::maxFrameOffset());
Owen Andersonf6372aa2008-01-01 21:11:32 +0000300 if (RC == SPU::GPRCRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000301 opc = (isValidFrameIdx ? SPU::STQDr128 : SPU::STQXr128);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000302 } else if (RC == SPU::R64CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000303 opc = (isValidFrameIdx ? SPU::STQDr64 : SPU::STQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000304 } else if (RC == SPU::R64FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000305 opc = (isValidFrameIdx ? SPU::STQDr64 : SPU::STQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000306 } else if (RC == SPU::R32CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000307 opc = (isValidFrameIdx ? SPU::STQDr32 : SPU::STQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000308 } else if (RC == SPU::R32FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000309 opc = (isValidFrameIdx ? SPU::STQDr32 : SPU::STQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000310 } else if (RC == SPU::R16CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000311 opc = (isValidFrameIdx ? SPU::STQDr16 : SPU::STQXr16);
312 } else if (RC == SPU::R8CRegisterClass) {
313 opc = (isValidFrameIdx ? SPU::STQDr8 : SPU::STQXr8);
Scott Michelf0569be2008-12-27 04:51:36 +0000314 } else if (RC == SPU::VECREGRegisterClass) {
315 opc = (isValidFrameIdx) ? SPU::STQDv16i8 : SPU::STQXv16i8;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000316 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000317 llvm_unreachable("Unknown regclass!");
Owen Andersonf6372aa2008-01-01 21:11:32 +0000318 }
319
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000320 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000321 if (MI != MBB.end()) DL = MI->getDebugLoc();
322 addFrameReference(BuildMI(MBB, MI, DL, get(opc))
Bill Wendling587daed2009-05-13 21:33:08 +0000323 .addReg(SrcReg, getKillRegState(isKill)), FrameIdx);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000324}
325
Owen Andersonf6372aa2008-01-01 21:11:32 +0000326void
327SPUInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
328 MachineBasicBlock::iterator MI,
329 unsigned DestReg, int FrameIdx,
330 const TargetRegisterClass *RC) const
331{
Chris Lattnercc8cd0c2008-01-07 02:48:55 +0000332 unsigned opc;
Scott Michelaedc6372008-12-10 00:15:19 +0000333 bool isValidFrameIdx = (FrameIdx < SPUFrameInfo::maxFrameOffset());
Owen Andersonf6372aa2008-01-01 21:11:32 +0000334 if (RC == SPU::GPRCRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000335 opc = (isValidFrameIdx ? SPU::LQDr128 : SPU::LQXr128);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000336 } else if (RC == SPU::R64CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000337 opc = (isValidFrameIdx ? SPU::LQDr64 : SPU::LQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000338 } else if (RC == SPU::R64FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000339 opc = (isValidFrameIdx ? SPU::LQDr64 : SPU::LQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000340 } else if (RC == SPU::R32CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000341 opc = (isValidFrameIdx ? SPU::LQDr32 : SPU::LQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000342 } else if (RC == SPU::R32FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000343 opc = (isValidFrameIdx ? SPU::LQDr32 : SPU::LQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000344 } else if (RC == SPU::R16CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000345 opc = (isValidFrameIdx ? SPU::LQDr16 : SPU::LQXr16);
346 } else if (RC == SPU::R8CRegisterClass) {
347 opc = (isValidFrameIdx ? SPU::LQDr8 : SPU::LQXr8);
Scott Michelf0569be2008-12-27 04:51:36 +0000348 } else if (RC == SPU::VECREGRegisterClass) {
349 opc = (isValidFrameIdx) ? SPU::LQDv16i8 : SPU::LQXv16i8;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000350 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000351 llvm_unreachable("Unknown regclass in loadRegFromStackSlot!");
Owen Andersonf6372aa2008-01-01 21:11:32 +0000352 }
353
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000354 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000355 if (MI != MBB.end()) DL = MI->getDebugLoc();
Jakob Stoklund Olesenf2c3f6a2009-05-16 07:25:44 +0000356 addFrameReference(BuildMI(MBB, MI, DL, get(opc), DestReg), FrameIdx);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000357}
358
Scott Michel52d00012009-01-03 00:27:53 +0000359//! Return true if the specified load or store can be folded
360bool
361SPUInstrInfo::canFoldMemoryOperand(const MachineInstr *MI,
362 const SmallVectorImpl<unsigned> &Ops) const {
363 if (Ops.size() != 1) return false;
364
365 // Make sure this is a reg-reg copy.
366 unsigned Opc = MI->getOpcode();
367
368 switch (Opc) {
369 case SPU::ORv16i8:
370 case SPU::ORv8i16:
371 case SPU::ORv4i32:
372 case SPU::ORv2i64:
373 case SPU::ORr8:
374 case SPU::ORr16:
375 case SPU::ORr32:
376 case SPU::ORr64:
377 case SPU::ORf32:
378 case SPU::ORf64:
379 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg())
380 return true;
381 break;
382 }
383
384 return false;
385}
386
Owen Anderson43dbe052008-01-07 01:35:02 +0000387/// foldMemoryOperand - SPU, like PPC, can only fold spills into
388/// copy instructions, turning them into load/store instructions.
389MachineInstr *
Dan Gohmanc54baa22008-12-03 18:43:12 +0000390SPUInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
391 MachineInstr *MI,
392 const SmallVectorImpl<unsigned> &Ops,
393 int FrameIndex) const
Owen Anderson43dbe052008-01-07 01:35:02 +0000394{
Scott Michel52d00012009-01-03 00:27:53 +0000395 if (Ops.size() != 1) return 0;
Owen Anderson43dbe052008-01-07 01:35:02 +0000396
397 unsigned OpNum = Ops[0];
398 unsigned Opc = MI->getOpcode();
399 MachineInstr *NewMI = 0;
Scott Michel02d711b2008-12-30 23:28:25 +0000400
Scott Michel52d00012009-01-03 00:27:53 +0000401 switch (Opc) {
402 case SPU::ORv16i8:
403 case SPU::ORv8i16:
404 case SPU::ORv4i32:
405 case SPU::ORv2i64:
406 case SPU::ORr8:
407 case SPU::ORr16:
408 case SPU::ORr32:
409 case SPU::ORr64:
410 case SPU::ORf32:
411 case SPU::ORf64:
Owen Anderson43dbe052008-01-07 01:35:02 +0000412 if (OpNum == 0) { // move -> store
413 unsigned InReg = MI->getOperand(1).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000414 bool isKill = MI->getOperand(1).isKill();
Evan Cheng2578ba22009-07-01 01:59:31 +0000415 bool isUndef = MI->getOperand(1).isUndef();
Owen Anderson43dbe052008-01-07 01:35:02 +0000416 if (FrameIndex < SPUFrameInfo::maxFrameOffset()) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000417 MachineInstrBuilder MIB = BuildMI(MF, MI->getDebugLoc(),
418 get(SPU::STQDr32));
Scott Michel52d00012009-01-03 00:27:53 +0000419
Evan Cheng2578ba22009-07-01 01:59:31 +0000420 MIB.addReg(InReg, getKillRegState(isKill) | getUndefRegState(isUndef));
Scott Michel52d00012009-01-03 00:27:53 +0000421 NewMI = addFrameReference(MIB, FrameIndex);
Owen Anderson43dbe052008-01-07 01:35:02 +0000422 }
423 } else { // move -> load
424 unsigned OutReg = MI->getOperand(0).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000425 bool isDead = MI->getOperand(0).isDead();
Evan Cheng2578ba22009-07-01 01:59:31 +0000426 bool isUndef = MI->getOperand(0).isUndef();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000427 MachineInstrBuilder MIB = BuildMI(MF, MI->getDebugLoc(), get(Opc));
Scott Michel52d00012009-01-03 00:27:53 +0000428
Evan Cheng2578ba22009-07-01 01:59:31 +0000429 MIB.addReg(OutReg, RegState::Define | getDeadRegState(isDead) |
430 getUndefRegState(isUndef));
Evan Cheng9f1c8312008-07-03 09:09:37 +0000431 Opc = (FrameIndex < SPUFrameInfo::maxFrameOffset())
432 ? SPU::STQDr32 : SPU::STQXr32;
Scott Michel52d00012009-01-03 00:27:53 +0000433 NewMI = addFrameReference(MIB, FrameIndex);
434 break;
435 }
Owen Anderson43dbe052008-01-07 01:35:02 +0000436 }
437
Owen Anderson43dbe052008-01-07 01:35:02 +0000438 return NewMI;
Owen Anderson43dbe052008-01-07 01:35:02 +0000439}
440
Scott Michelaedc6372008-12-10 00:15:19 +0000441//! Branch analysis
Scott Michel9bd7a372009-01-02 20:52:08 +0000442/*!
Scott Michelaedc6372008-12-10 00:15:19 +0000443 \note This code was kiped from PPC. There may be more branch analysis for
444 CellSPU than what's currently done here.
445 */
446bool
447SPUInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
Scott Michel19c10e62009-01-26 03:37:41 +0000448 MachineBasicBlock *&FBB,
Evan Chengdc54d312009-02-09 07:14:22 +0000449 SmallVectorImpl<MachineOperand> &Cond,
450 bool AllowModify) const {
Scott Michelaedc6372008-12-10 00:15:19 +0000451 // If the block has no terminators, it just falls into the block after it.
452 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000453 if (I == MBB.begin())
454 return false;
455 --I;
456 while (I->isDebugValue()) {
457 if (I == MBB.begin())
458 return false;
459 --I;
460 }
461 if (!isUnpredicatedTerminator(I))
Scott Michelaedc6372008-12-10 00:15:19 +0000462 return false;
463
464 // Get the last instruction in the block.
465 MachineInstr *LastInst = I;
Scott Michel02d711b2008-12-30 23:28:25 +0000466
Scott Michelaedc6372008-12-10 00:15:19 +0000467 // If there is only one terminator instruction, process it.
468 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
469 if (isUncondBranch(LastInst)) {
470 TBB = LastInst->getOperand(0).getMBB();
471 return false;
472 } else if (isCondBranch(LastInst)) {
473 // Block ends with fall-through condbranch.
474 TBB = LastInst->getOperand(1).getMBB();
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000475 DEBUG(errs() << "Pushing LastInst: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000476 DEBUG(LastInst->dump());
477 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Scott Michelaedc6372008-12-10 00:15:19 +0000478 Cond.push_back(LastInst->getOperand(0));
Scott Michelaedc6372008-12-10 00:15:19 +0000479 return false;
480 }
481 // Otherwise, don't know what this is.
482 return true;
483 }
Scott Michel02d711b2008-12-30 23:28:25 +0000484
Scott Michelaedc6372008-12-10 00:15:19 +0000485 // Get the instruction before it if it's a terminator.
486 MachineInstr *SecondLastInst = I;
487
488 // If there are three terminators, we don't know what sort of block this is.
489 if (SecondLastInst && I != MBB.begin() &&
490 isUnpredicatedTerminator(--I))
491 return true;
Scott Michel02d711b2008-12-30 23:28:25 +0000492
Scott Michelaedc6372008-12-10 00:15:19 +0000493 // If the block ends with a conditional and unconditional branch, handle it.
494 if (isCondBranch(SecondLastInst) && isUncondBranch(LastInst)) {
495 TBB = SecondLastInst->getOperand(1).getMBB();
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000496 DEBUG(errs() << "Pushing SecondLastInst: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000497 DEBUG(SecondLastInst->dump());
498 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Scott Michelaedc6372008-12-10 00:15:19 +0000499 Cond.push_back(SecondLastInst->getOperand(0));
Scott Michelaedc6372008-12-10 00:15:19 +0000500 FBB = LastInst->getOperand(0).getMBB();
501 return false;
502 }
Scott Michel02d711b2008-12-30 23:28:25 +0000503
Scott Michelaedc6372008-12-10 00:15:19 +0000504 // If the block ends with two unconditional branches, handle it. The second
505 // one is not executed, so remove it.
506 if (isUncondBranch(SecondLastInst) && isUncondBranch(LastInst)) {
507 TBB = SecondLastInst->getOperand(0).getMBB();
508 I = LastInst;
Evan Chengdc54d312009-02-09 07:14:22 +0000509 if (AllowModify)
510 I->eraseFromParent();
Scott Michelaedc6372008-12-10 00:15:19 +0000511 return false;
512 }
513
514 // Otherwise, can't handle this.
515 return true;
516}
Scott Michel02d711b2008-12-30 23:28:25 +0000517
Scott Michelaedc6372008-12-10 00:15:19 +0000518unsigned
519SPUInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
520 MachineBasicBlock::iterator I = MBB.end();
521 if (I == MBB.begin())
522 return 0;
523 --I;
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000524 while (I->isDebugValue()) {
525 if (I == MBB.begin())
526 return 0;
527 --I;
528 }
Scott Michelaedc6372008-12-10 00:15:19 +0000529 if (!isCondBranch(I) && !isUncondBranch(I))
530 return 0;
531
532 // Remove the first branch.
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000533 DEBUG(errs() << "Removing branch: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000534 DEBUG(I->dump());
Scott Michelaedc6372008-12-10 00:15:19 +0000535 I->eraseFromParent();
536 I = MBB.end();
537 if (I == MBB.begin())
538 return 1;
539
540 --I;
Scott Michel9bd7a372009-01-02 20:52:08 +0000541 if (!(isCondBranch(I) || isUncondBranch(I)))
Scott Michelaedc6372008-12-10 00:15:19 +0000542 return 1;
543
544 // Remove the second branch.
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000545 DEBUG(errs() << "Removing second branch: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000546 DEBUG(I->dump());
Scott Michelaedc6372008-12-10 00:15:19 +0000547 I->eraseFromParent();
548 return 2;
549}
Scott Michel02d711b2008-12-30 23:28:25 +0000550
Scott Michelaedc6372008-12-10 00:15:19 +0000551unsigned
552SPUInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Scott Michel19c10e62009-01-26 03:37:41 +0000553 MachineBasicBlock *FBB,
554 const SmallVectorImpl<MachineOperand> &Cond) const {
Dale Johannesen01b36e62009-02-13 02:30:42 +0000555 // FIXME this should probably have a DebugLoc argument
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000556 DebugLoc dl;
Scott Michelaedc6372008-12-10 00:15:19 +0000557 // Shouldn't be a fall through.
558 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Scott Michel02d711b2008-12-30 23:28:25 +0000559 assert((Cond.size() == 2 || Cond.size() == 0) &&
Scott Michelaedc6372008-12-10 00:15:19 +0000560 "SPU branch conditions have two components!");
Scott Michel02d711b2008-12-30 23:28:25 +0000561
Scott Michelaedc6372008-12-10 00:15:19 +0000562 // One-way branch.
563 if (FBB == 0) {
Scott Michel9bd7a372009-01-02 20:52:08 +0000564 if (Cond.empty()) {
565 // Unconditional branch
Dale Johannesen01b36e62009-02-13 02:30:42 +0000566 MachineInstrBuilder MIB = BuildMI(&MBB, dl, get(SPU::BR));
Scott Michel9bd7a372009-01-02 20:52:08 +0000567 MIB.addMBB(TBB);
568
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000569 DEBUG(errs() << "Inserted one-way uncond branch: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000570 DEBUG((*MIB).dump());
571 } else {
572 // Conditional branch
Dale Johannesen01b36e62009-02-13 02:30:42 +0000573 MachineInstrBuilder MIB = BuildMI(&MBB, dl, get(Cond[0].getImm()));
Scott Michel9bd7a372009-01-02 20:52:08 +0000574 MIB.addReg(Cond[1].getReg()).addMBB(TBB);
575
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000576 DEBUG(errs() << "Inserted one-way cond branch: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000577 DEBUG((*MIB).dump());
Scott Michelaedc6372008-12-10 00:15:19 +0000578 }
579 return 1;
Scott Michel9bd7a372009-01-02 20:52:08 +0000580 } else {
Dale Johannesen01b36e62009-02-13 02:30:42 +0000581 MachineInstrBuilder MIB = BuildMI(&MBB, dl, get(Cond[0].getImm()));
582 MachineInstrBuilder MIB2 = BuildMI(&MBB, dl, get(SPU::BR));
Scott Michel9bd7a372009-01-02 20:52:08 +0000583
584 // Two-way Conditional Branch.
585 MIB.addReg(Cond[1].getReg()).addMBB(TBB);
586 MIB2.addMBB(FBB);
587
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000588 DEBUG(errs() << "Inserted conditional branch: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000589 DEBUG((*MIB).dump());
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000590 DEBUG(errs() << "part 2: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000591 DEBUG((*MIB2).dump());
592 return 2;
Scott Michelaedc6372008-12-10 00:15:19 +0000593 }
Scott Michelaedc6372008-12-10 00:15:19 +0000594}
595
Scott Michel52d00012009-01-03 00:27:53 +0000596//! Reverses a branch's condition, returning false on success.
597bool
598SPUInstrInfo::ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond)
599 const {
600 // Pretty brainless way of inverting the condition, but it works, considering
601 // there are only two conditions...
602 static struct {
603 unsigned Opc; //! The incoming opcode
604 unsigned RevCondOpc; //! The reversed condition opcode
605 } revconds[] = {
606 { SPU::BRNZr32, SPU::BRZr32 },
607 { SPU::BRNZv4i32, SPU::BRZv4i32 },
608 { SPU::BRZr32, SPU::BRNZr32 },
609 { SPU::BRZv4i32, SPU::BRNZv4i32 },
610 { SPU::BRHNZr16, SPU::BRHZr16 },
611 { SPU::BRHNZv8i16, SPU::BRHZv8i16 },
612 { SPU::BRHZr16, SPU::BRHNZr16 },
613 { SPU::BRHZv8i16, SPU::BRHNZv8i16 }
614 };
Scott Michelaedc6372008-12-10 00:15:19 +0000615
Scott Michel52d00012009-01-03 00:27:53 +0000616 unsigned Opc = unsigned(Cond[0].getImm());
617 // Pretty dull mapping between the two conditions that SPU can generate:
Misha Brukman93c65c82009-01-07 23:07:29 +0000618 for (int i = sizeof(revconds)/sizeof(revconds[0]) - 1; i >= 0; --i) {
Scott Michel52d00012009-01-03 00:27:53 +0000619 if (revconds[i].Opc == Opc) {
620 Cond[0].setImm(revconds[i].RevCondOpc);
621 return false;
622 }
623 }
624
625 return true;
626}