blob: 177f1bc85784ed7319d3adc045d314bfa7a84b35 [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 Michel7ea02ff2009-03-17 01:15:45 +0000167 sourceReg = MI.getOperand(1).getReg();
Scott Michelf0569be2008-12-27 04:51:36 +0000168 destReg = MI.getOperand(0).getReg();
169 return true;
Scott Michelf0569be2008-12-27 04:51:36 +0000170 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 Michel6e1d1472009-03-16 18:47:25 +0000180 case SPU::ORr128:
Scott Michel86c041f2007-12-20 00:44:13 +0000181 case SPU::ORf32:
182 case SPU::ORf64:
Scott Michel66377522007-12-04 22:35:58 +0000183 assert(MI.getNumOperands() == 3 &&
Dan Gohmand735b802008-10-03 15:45:36 +0000184 MI.getOperand(0).isReg() &&
185 MI.getOperand(1).isReg() &&
186 MI.getOperand(2).isReg() &&
Scott Michel66377522007-12-04 22:35:58 +0000187 "invalid SPU OR(vec|r32|r64|gprc) instruction!");
188 if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
189 sourceReg = MI.getOperand(1).getReg();
190 destReg = MI.getOperand(0).getReg();
191 return true;
192 }
193 break;
194 }
195
196 return false;
197}
198
199unsigned
Dan Gohmancbad42c2008-11-18 19:49:32 +0000200SPUInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
201 int &FrameIndex) const {
Scott Michel66377522007-12-04 22:35:58 +0000202 switch (MI->getOpcode()) {
203 default: break;
204 case SPU::LQDv16i8:
205 case SPU::LQDv8i16:
206 case SPU::LQDv4i32:
207 case SPU::LQDv4f32:
208 case SPU::LQDv2f64:
209 case SPU::LQDr128:
210 case SPU::LQDr64:
211 case SPU::LQDr32:
Scott Michelaedc6372008-12-10 00:15:19 +0000212 case SPU::LQDr16: {
213 const MachineOperand MOp1 = MI->getOperand(1);
214 const MachineOperand MOp2 = MI->getOperand(2);
Scott Michel52d00012009-01-03 00:27:53 +0000215 if (MOp1.isImm() && MOp2.isFI()) {
216 FrameIndex = MOp2.getIndex();
Scott Michelaedc6372008-12-10 00:15:19 +0000217 return MI->getOperand(0).getReg();
218 }
219 break;
220 }
Scott Michel66377522007-12-04 22:35:58 +0000221 }
222 return 0;
223}
224
225unsigned
Dan Gohmancbad42c2008-11-18 19:49:32 +0000226SPUInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
227 int &FrameIndex) const {
Scott Michel66377522007-12-04 22:35:58 +0000228 switch (MI->getOpcode()) {
229 default: break;
230 case SPU::STQDv16i8:
231 case SPU::STQDv8i16:
232 case SPU::STQDv4i32:
233 case SPU::STQDv4f32:
234 case SPU::STQDv2f64:
235 case SPU::STQDr128:
236 case SPU::STQDr64:
237 case SPU::STQDr32:
238 case SPU::STQDr16:
Scott Michelaedc6372008-12-10 00:15:19 +0000239 case SPU::STQDr8: {
240 const MachineOperand MOp1 = MI->getOperand(1);
241 const MachineOperand MOp2 = MI->getOperand(2);
Scott Michelf0569be2008-12-27 04:51:36 +0000242 if (MOp1.isImm() && MOp2.isFI()) {
243 FrameIndex = MOp2.getIndex();
Scott Michelaedc6372008-12-10 00:15:19 +0000244 return MI->getOperand(0).getReg();
245 }
246 break;
247 }
Scott Michel66377522007-12-04 22:35:58 +0000248 }
249 return 0;
250}
Owen Andersond10fd972007-12-31 06:32:00 +0000251
Jakob Stoklund Olesen377b7b72010-07-11 07:31:03 +0000252void SPUInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
253 MachineBasicBlock::iterator I, DebugLoc DL,
254 unsigned DestReg, unsigned SrcReg,
255 bool KillSrc) const
Owen Andersond10fd972007-12-31 06:32:00 +0000256{
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
Jakob Stoklund Olesen377b7b72010-07-11 07:31:03 +0000262 BuildMI(MBB, I, DL, get(SPU::LRr128), DestReg)
263 .addReg(SrcReg, getKillRegState(KillSrc));
Owen Andersond10fd972007-12-31 06:32:00 +0000264}
Owen Andersonf6372aa2008-01-01 21:11:32 +0000265
266void
267SPUInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Evan Cheng746ad692010-05-06 19:06:44 +0000268 MachineBasicBlock::iterator MI,
269 unsigned SrcReg, bool isKill, int FrameIdx,
270 const TargetRegisterClass *RC,
271 const TargetRegisterInfo *TRI) const
Owen Andersonf6372aa2008-01-01 21:11:32 +0000272{
Chris Lattnercc8cd0c2008-01-07 02:48:55 +0000273 unsigned opc;
Scott Michelaedc6372008-12-10 00:15:19 +0000274 bool isValidFrameIdx = (FrameIdx < SPUFrameInfo::maxFrameOffset());
Owen Andersonf6372aa2008-01-01 21:11:32 +0000275 if (RC == SPU::GPRCRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000276 opc = (isValidFrameIdx ? SPU::STQDr128 : SPU::STQXr128);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000277 } else if (RC == SPU::R64CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000278 opc = (isValidFrameIdx ? SPU::STQDr64 : SPU::STQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000279 } else if (RC == SPU::R64FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000280 opc = (isValidFrameIdx ? SPU::STQDr64 : SPU::STQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000281 } else if (RC == SPU::R32CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000282 opc = (isValidFrameIdx ? SPU::STQDr32 : SPU::STQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000283 } else if (RC == SPU::R32FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000284 opc = (isValidFrameIdx ? SPU::STQDr32 : SPU::STQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000285 } else if (RC == SPU::R16CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000286 opc = (isValidFrameIdx ? SPU::STQDr16 : SPU::STQXr16);
287 } else if (RC == SPU::R8CRegisterClass) {
288 opc = (isValidFrameIdx ? SPU::STQDr8 : SPU::STQXr8);
Scott Michelf0569be2008-12-27 04:51:36 +0000289 } else if (RC == SPU::VECREGRegisterClass) {
290 opc = (isValidFrameIdx) ? SPU::STQDv16i8 : SPU::STQXv16i8;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000291 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000292 llvm_unreachable("Unknown regclass!");
Owen Andersonf6372aa2008-01-01 21:11:32 +0000293 }
294
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000295 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000296 if (MI != MBB.end()) DL = MI->getDebugLoc();
297 addFrameReference(BuildMI(MBB, MI, DL, get(opc))
Bill Wendling587daed2009-05-13 21:33:08 +0000298 .addReg(SrcReg, getKillRegState(isKill)), FrameIdx);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000299}
300
Owen Andersonf6372aa2008-01-01 21:11:32 +0000301void
302SPUInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Evan Cheng746ad692010-05-06 19:06:44 +0000303 MachineBasicBlock::iterator MI,
304 unsigned DestReg, int FrameIdx,
305 const TargetRegisterClass *RC,
306 const TargetRegisterInfo *TRI) const
Owen Andersonf6372aa2008-01-01 21:11:32 +0000307{
Chris Lattnercc8cd0c2008-01-07 02:48:55 +0000308 unsigned opc;
Scott Michelaedc6372008-12-10 00:15:19 +0000309 bool isValidFrameIdx = (FrameIdx < SPUFrameInfo::maxFrameOffset());
Owen Andersonf6372aa2008-01-01 21:11:32 +0000310 if (RC == SPU::GPRCRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000311 opc = (isValidFrameIdx ? SPU::LQDr128 : SPU::LQXr128);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000312 } else if (RC == SPU::R64CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000313 opc = (isValidFrameIdx ? SPU::LQDr64 : SPU::LQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000314 } else if (RC == SPU::R64FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000315 opc = (isValidFrameIdx ? SPU::LQDr64 : SPU::LQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000316 } else if (RC == SPU::R32CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000317 opc = (isValidFrameIdx ? SPU::LQDr32 : SPU::LQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000318 } else if (RC == SPU::R32FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000319 opc = (isValidFrameIdx ? SPU::LQDr32 : SPU::LQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000320 } else if (RC == SPU::R16CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000321 opc = (isValidFrameIdx ? SPU::LQDr16 : SPU::LQXr16);
322 } else if (RC == SPU::R8CRegisterClass) {
323 opc = (isValidFrameIdx ? SPU::LQDr8 : SPU::LQXr8);
Scott Michelf0569be2008-12-27 04:51:36 +0000324 } else if (RC == SPU::VECREGRegisterClass) {
325 opc = (isValidFrameIdx) ? SPU::LQDv16i8 : SPU::LQXv16i8;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000326 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000327 llvm_unreachable("Unknown regclass in loadRegFromStackSlot!");
Owen Andersonf6372aa2008-01-01 21:11:32 +0000328 }
329
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000330 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000331 if (MI != MBB.end()) DL = MI->getDebugLoc();
Jakob Stoklund Olesenf2c3f6a2009-05-16 07:25:44 +0000332 addFrameReference(BuildMI(MBB, MI, DL, get(opc), DestReg), FrameIdx);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000333}
334
Scott Michel52d00012009-01-03 00:27:53 +0000335//! Return true if the specified load or store can be folded
336bool
337SPUInstrInfo::canFoldMemoryOperand(const MachineInstr *MI,
338 const SmallVectorImpl<unsigned> &Ops) const {
339 if (Ops.size() != 1) return false;
340
341 // Make sure this is a reg-reg copy.
342 unsigned Opc = MI->getOpcode();
343
344 switch (Opc) {
345 case SPU::ORv16i8:
346 case SPU::ORv8i16:
347 case SPU::ORv4i32:
348 case SPU::ORv2i64:
349 case SPU::ORr8:
350 case SPU::ORr16:
351 case SPU::ORr32:
352 case SPU::ORr64:
353 case SPU::ORf32:
354 case SPU::ORf64:
355 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg())
356 return true;
357 break;
358 }
359
360 return false;
361}
362
Owen Anderson43dbe052008-01-07 01:35:02 +0000363/// foldMemoryOperand - SPU, like PPC, can only fold spills into
364/// copy instructions, turning them into load/store instructions.
365MachineInstr *
Dan Gohmanc54baa22008-12-03 18:43:12 +0000366SPUInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
367 MachineInstr *MI,
368 const SmallVectorImpl<unsigned> &Ops,
369 int FrameIndex) const
Owen Anderson43dbe052008-01-07 01:35:02 +0000370{
Scott Michel52d00012009-01-03 00:27:53 +0000371 if (Ops.size() != 1) return 0;
Owen Anderson43dbe052008-01-07 01:35:02 +0000372
373 unsigned OpNum = Ops[0];
374 unsigned Opc = MI->getOpcode();
375 MachineInstr *NewMI = 0;
Scott Michel02d711b2008-12-30 23:28:25 +0000376
Scott Michel52d00012009-01-03 00:27:53 +0000377 switch (Opc) {
378 case SPU::ORv16i8:
379 case SPU::ORv8i16:
380 case SPU::ORv4i32:
381 case SPU::ORv2i64:
382 case SPU::ORr8:
383 case SPU::ORr16:
384 case SPU::ORr32:
385 case SPU::ORr64:
386 case SPU::ORf32:
387 case SPU::ORf64:
Owen Anderson43dbe052008-01-07 01:35:02 +0000388 if (OpNum == 0) { // move -> store
389 unsigned InReg = MI->getOperand(1).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000390 bool isKill = MI->getOperand(1).isKill();
Evan Cheng2578ba22009-07-01 01:59:31 +0000391 bool isUndef = MI->getOperand(1).isUndef();
Owen Anderson43dbe052008-01-07 01:35:02 +0000392 if (FrameIndex < SPUFrameInfo::maxFrameOffset()) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000393 MachineInstrBuilder MIB = BuildMI(MF, MI->getDebugLoc(),
394 get(SPU::STQDr32));
Scott Michel52d00012009-01-03 00:27:53 +0000395
Evan Cheng2578ba22009-07-01 01:59:31 +0000396 MIB.addReg(InReg, getKillRegState(isKill) | getUndefRegState(isUndef));
Scott Michel52d00012009-01-03 00:27:53 +0000397 NewMI = addFrameReference(MIB, FrameIndex);
Owen Anderson43dbe052008-01-07 01:35:02 +0000398 }
399 } else { // move -> load
400 unsigned OutReg = MI->getOperand(0).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000401 bool isDead = MI->getOperand(0).isDead();
Evan Cheng2578ba22009-07-01 01:59:31 +0000402 bool isUndef = MI->getOperand(0).isUndef();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000403 MachineInstrBuilder MIB = BuildMI(MF, MI->getDebugLoc(), get(Opc));
Scott Michel52d00012009-01-03 00:27:53 +0000404
Evan Cheng2578ba22009-07-01 01:59:31 +0000405 MIB.addReg(OutReg, RegState::Define | getDeadRegState(isDead) |
406 getUndefRegState(isUndef));
Evan Cheng9f1c8312008-07-03 09:09:37 +0000407 Opc = (FrameIndex < SPUFrameInfo::maxFrameOffset())
408 ? SPU::STQDr32 : SPU::STQXr32;
Scott Michel52d00012009-01-03 00:27:53 +0000409 NewMI = addFrameReference(MIB, FrameIndex);
410 break;
411 }
Owen Anderson43dbe052008-01-07 01:35:02 +0000412 }
413
Owen Anderson43dbe052008-01-07 01:35:02 +0000414 return NewMI;
Owen Anderson43dbe052008-01-07 01:35:02 +0000415}
416
Scott Michelaedc6372008-12-10 00:15:19 +0000417//! Branch analysis
Scott Michel9bd7a372009-01-02 20:52:08 +0000418/*!
Scott Michelaedc6372008-12-10 00:15:19 +0000419 \note This code was kiped from PPC. There may be more branch analysis for
420 CellSPU than what's currently done here.
421 */
422bool
423SPUInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
Scott Michel19c10e62009-01-26 03:37:41 +0000424 MachineBasicBlock *&FBB,
Evan Chengdc54d312009-02-09 07:14:22 +0000425 SmallVectorImpl<MachineOperand> &Cond,
426 bool AllowModify) const {
Scott Michelaedc6372008-12-10 00:15:19 +0000427 // If the block has no terminators, it just falls into the block after it.
428 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000429 if (I == MBB.begin())
430 return false;
431 --I;
432 while (I->isDebugValue()) {
433 if (I == MBB.begin())
434 return false;
435 --I;
436 }
437 if (!isUnpredicatedTerminator(I))
Scott Michelaedc6372008-12-10 00:15:19 +0000438 return false;
439
440 // Get the last instruction in the block.
441 MachineInstr *LastInst = I;
Scott Michel02d711b2008-12-30 23:28:25 +0000442
Scott Michelaedc6372008-12-10 00:15:19 +0000443 // If there is only one terminator instruction, process it.
444 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
445 if (isUncondBranch(LastInst)) {
Kalle Raiskila2320a442010-05-11 11:00:02 +0000446 // Check for jump tables
447 if (!LastInst->getOperand(0).isMBB())
448 return true;
Scott Michelaedc6372008-12-10 00:15:19 +0000449 TBB = LastInst->getOperand(0).getMBB();
450 return false;
451 } else if (isCondBranch(LastInst)) {
452 // Block ends with fall-through condbranch.
453 TBB = LastInst->getOperand(1).getMBB();
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000454 DEBUG(errs() << "Pushing LastInst: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000455 DEBUG(LastInst->dump());
456 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Scott Michelaedc6372008-12-10 00:15:19 +0000457 Cond.push_back(LastInst->getOperand(0));
Scott Michelaedc6372008-12-10 00:15:19 +0000458 return false;
459 }
460 // Otherwise, don't know what this is.
461 return true;
462 }
Scott Michel02d711b2008-12-30 23:28:25 +0000463
Scott Michelaedc6372008-12-10 00:15:19 +0000464 // Get the instruction before it if it's a terminator.
465 MachineInstr *SecondLastInst = I;
466
467 // If there are three terminators, we don't know what sort of block this is.
468 if (SecondLastInst && I != MBB.begin() &&
469 isUnpredicatedTerminator(--I))
470 return true;
Scott Michel02d711b2008-12-30 23:28:25 +0000471
Scott Michelaedc6372008-12-10 00:15:19 +0000472 // If the block ends with a conditional and unconditional branch, handle it.
473 if (isCondBranch(SecondLastInst) && isUncondBranch(LastInst)) {
474 TBB = SecondLastInst->getOperand(1).getMBB();
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000475 DEBUG(errs() << "Pushing SecondLastInst: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000476 DEBUG(SecondLastInst->dump());
477 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Scott Michelaedc6372008-12-10 00:15:19 +0000478 Cond.push_back(SecondLastInst->getOperand(0));
Scott Michelaedc6372008-12-10 00:15:19 +0000479 FBB = LastInst->getOperand(0).getMBB();
480 return false;
481 }
Scott Michel02d711b2008-12-30 23:28:25 +0000482
Scott Michelaedc6372008-12-10 00:15:19 +0000483 // If the block ends with two unconditional branches, handle it. The second
484 // one is not executed, so remove it.
485 if (isUncondBranch(SecondLastInst) && isUncondBranch(LastInst)) {
486 TBB = SecondLastInst->getOperand(0).getMBB();
487 I = LastInst;
Evan Chengdc54d312009-02-09 07:14:22 +0000488 if (AllowModify)
489 I->eraseFromParent();
Scott Michelaedc6372008-12-10 00:15:19 +0000490 return false;
491 }
492
493 // Otherwise, can't handle this.
494 return true;
495}
Scott Michel02d711b2008-12-30 23:28:25 +0000496
Scott Michelaedc6372008-12-10 00:15:19 +0000497unsigned
498SPUInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
499 MachineBasicBlock::iterator I = MBB.end();
500 if (I == MBB.begin())
501 return 0;
502 --I;
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000503 while (I->isDebugValue()) {
504 if (I == MBB.begin())
505 return 0;
506 --I;
507 }
Scott Michelaedc6372008-12-10 00:15:19 +0000508 if (!isCondBranch(I) && !isUncondBranch(I))
509 return 0;
510
511 // Remove the first branch.
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000512 DEBUG(errs() << "Removing branch: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000513 DEBUG(I->dump());
Scott Michelaedc6372008-12-10 00:15:19 +0000514 I->eraseFromParent();
515 I = MBB.end();
516 if (I == MBB.begin())
517 return 1;
518
519 --I;
Scott Michel9bd7a372009-01-02 20:52:08 +0000520 if (!(isCondBranch(I) || isUncondBranch(I)))
Scott Michelaedc6372008-12-10 00:15:19 +0000521 return 1;
522
523 // Remove the second branch.
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000524 DEBUG(errs() << "Removing second branch: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000525 DEBUG(I->dump());
Scott Michelaedc6372008-12-10 00:15:19 +0000526 I->eraseFromParent();
527 return 2;
528}
Scott Michel02d711b2008-12-30 23:28:25 +0000529
Scott Michelaedc6372008-12-10 00:15:19 +0000530unsigned
531SPUInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Scott Michel19c10e62009-01-26 03:37:41 +0000532 MachineBasicBlock *FBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000533 const SmallVectorImpl<MachineOperand> &Cond,
534 DebugLoc DL) const {
Scott Michelaedc6372008-12-10 00:15:19 +0000535 // Shouldn't be a fall through.
536 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Scott Michel02d711b2008-12-30 23:28:25 +0000537 assert((Cond.size() == 2 || Cond.size() == 0) &&
Scott Michelaedc6372008-12-10 00:15:19 +0000538 "SPU branch conditions have two components!");
Scott Michel02d711b2008-12-30 23:28:25 +0000539
Scott Michelaedc6372008-12-10 00:15:19 +0000540 // One-way branch.
541 if (FBB == 0) {
Scott Michel9bd7a372009-01-02 20:52:08 +0000542 if (Cond.empty()) {
543 // Unconditional branch
Stuart Hastings3bf91252010-06-17 22:43:56 +0000544 MachineInstrBuilder MIB = BuildMI(&MBB, DL, get(SPU::BR));
Scott Michel9bd7a372009-01-02 20:52:08 +0000545 MIB.addMBB(TBB);
546
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000547 DEBUG(errs() << "Inserted one-way uncond branch: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000548 DEBUG((*MIB).dump());
549 } else {
550 // Conditional branch
Stuart Hastings3bf91252010-06-17 22:43:56 +0000551 MachineInstrBuilder MIB = BuildMI(&MBB, DL, get(Cond[0].getImm()));
Scott Michel9bd7a372009-01-02 20:52:08 +0000552 MIB.addReg(Cond[1].getReg()).addMBB(TBB);
553
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000554 DEBUG(errs() << "Inserted one-way cond branch: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000555 DEBUG((*MIB).dump());
Scott Michelaedc6372008-12-10 00:15:19 +0000556 }
557 return 1;
Scott Michel9bd7a372009-01-02 20:52:08 +0000558 } else {
Stuart Hastings3bf91252010-06-17 22:43:56 +0000559 MachineInstrBuilder MIB = BuildMI(&MBB, DL, get(Cond[0].getImm()));
560 MachineInstrBuilder MIB2 = BuildMI(&MBB, DL, get(SPU::BR));
Scott Michel9bd7a372009-01-02 20:52:08 +0000561
562 // Two-way Conditional Branch.
563 MIB.addReg(Cond[1].getReg()).addMBB(TBB);
564 MIB2.addMBB(FBB);
565
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000566 DEBUG(errs() << "Inserted conditional branch: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000567 DEBUG((*MIB).dump());
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000568 DEBUG(errs() << "part 2: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000569 DEBUG((*MIB2).dump());
570 return 2;
Scott Michelaedc6372008-12-10 00:15:19 +0000571 }
Scott Michelaedc6372008-12-10 00:15:19 +0000572}
573
Scott Michel52d00012009-01-03 00:27:53 +0000574//! Reverses a branch's condition, returning false on success.
575bool
576SPUInstrInfo::ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond)
577 const {
578 // Pretty brainless way of inverting the condition, but it works, considering
579 // there are only two conditions...
580 static struct {
581 unsigned Opc; //! The incoming opcode
582 unsigned RevCondOpc; //! The reversed condition opcode
583 } revconds[] = {
584 { SPU::BRNZr32, SPU::BRZr32 },
585 { SPU::BRNZv4i32, SPU::BRZv4i32 },
586 { SPU::BRZr32, SPU::BRNZr32 },
587 { SPU::BRZv4i32, SPU::BRNZv4i32 },
588 { SPU::BRHNZr16, SPU::BRHZr16 },
589 { SPU::BRHNZv8i16, SPU::BRHZv8i16 },
590 { SPU::BRHZr16, SPU::BRHNZr16 },
591 { SPU::BRHZv8i16, SPU::BRHNZv8i16 }
592 };
Scott Michelaedc6372008-12-10 00:15:19 +0000593
Scott Michel52d00012009-01-03 00:27:53 +0000594 unsigned Opc = unsigned(Cond[0].getImm());
595 // Pretty dull mapping between the two conditions that SPU can generate:
Misha Brukman93c65c82009-01-07 23:07:29 +0000596 for (int i = sizeof(revconds)/sizeof(revconds[0]) - 1; i >= 0; --i) {
Scott Michel52d00012009-01-03 00:27:53 +0000597 if (revconds[i].Opc == Opc) {
598 Cond[0].setImm(revconds[i].RevCondOpc);
599 return false;
600 }
601 }
602
603 return true;
604}