blob: 26d6b4f25ef12351aebdb8f75e5f19700c29d1a4 [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 +000057unsigned
Dan Gohmancbad42c2008-11-18 19:49:32 +000058SPUInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
59 int &FrameIndex) const {
Scott Michel66377522007-12-04 22:35:58 +000060 switch (MI->getOpcode()) {
61 default: break;
62 case SPU::LQDv16i8:
63 case SPU::LQDv8i16:
64 case SPU::LQDv4i32:
65 case SPU::LQDv4f32:
66 case SPU::LQDv2f64:
67 case SPU::LQDr128:
68 case SPU::LQDr64:
69 case SPU::LQDr32:
Scott Michelaedc6372008-12-10 00:15:19 +000070 case SPU::LQDr16: {
71 const MachineOperand MOp1 = MI->getOperand(1);
72 const MachineOperand MOp2 = MI->getOperand(2);
Scott Michel52d00012009-01-03 00:27:53 +000073 if (MOp1.isImm() && MOp2.isFI()) {
74 FrameIndex = MOp2.getIndex();
Scott Michelaedc6372008-12-10 00:15:19 +000075 return MI->getOperand(0).getReg();
76 }
77 break;
78 }
Scott Michel66377522007-12-04 22:35:58 +000079 }
80 return 0;
81}
82
83unsigned
Dan Gohmancbad42c2008-11-18 19:49:32 +000084SPUInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
85 int &FrameIndex) const {
Scott Michel66377522007-12-04 22:35:58 +000086 switch (MI->getOpcode()) {
87 default: break;
88 case SPU::STQDv16i8:
89 case SPU::STQDv8i16:
90 case SPU::STQDv4i32:
91 case SPU::STQDv4f32:
92 case SPU::STQDv2f64:
93 case SPU::STQDr128:
94 case SPU::STQDr64:
95 case SPU::STQDr32:
96 case SPU::STQDr16:
Scott Michelaedc6372008-12-10 00:15:19 +000097 case SPU::STQDr8: {
98 const MachineOperand MOp1 = MI->getOperand(1);
99 const MachineOperand MOp2 = MI->getOperand(2);
Scott Michelf0569be2008-12-27 04:51:36 +0000100 if (MOp1.isImm() && MOp2.isFI()) {
101 FrameIndex = MOp2.getIndex();
Scott Michelaedc6372008-12-10 00:15:19 +0000102 return MI->getOperand(0).getReg();
103 }
104 break;
105 }
Scott Michel66377522007-12-04 22:35:58 +0000106 }
107 return 0;
108}
Owen Andersond10fd972007-12-31 06:32:00 +0000109
Jakob Stoklund Olesen377b7b72010-07-11 07:31:03 +0000110void SPUInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
111 MachineBasicBlock::iterator I, DebugLoc DL,
112 unsigned DestReg, unsigned SrcReg,
113 bool KillSrc) const
Owen Andersond10fd972007-12-31 06:32:00 +0000114{
Chris Lattner5e09da22008-03-09 20:31:11 +0000115 // We support cross register class moves for our aliases, such as R3 in any
116 // reg class to any other reg class containing R3. This is required because
117 // we instruction select bitconvert i64 -> f64 as a noop for example, so our
118 // types have no specific meaning.
Scott Michel02d711b2008-12-30 23:28:25 +0000119
Jakob Stoklund Olesen377b7b72010-07-11 07:31:03 +0000120 BuildMI(MBB, I, DL, get(SPU::LRr128), DestReg)
121 .addReg(SrcReg, getKillRegState(KillSrc));
Owen Andersond10fd972007-12-31 06:32:00 +0000122}
Owen Andersonf6372aa2008-01-01 21:11:32 +0000123
124void
125SPUInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Evan Cheng746ad692010-05-06 19:06:44 +0000126 MachineBasicBlock::iterator MI,
127 unsigned SrcReg, bool isKill, int FrameIdx,
128 const TargetRegisterClass *RC,
129 const TargetRegisterInfo *TRI) const
Owen Andersonf6372aa2008-01-01 21:11:32 +0000130{
Chris Lattnercc8cd0c2008-01-07 02:48:55 +0000131 unsigned opc;
Scott Michelaedc6372008-12-10 00:15:19 +0000132 bool isValidFrameIdx = (FrameIdx < SPUFrameInfo::maxFrameOffset());
Owen Andersonf6372aa2008-01-01 21:11:32 +0000133 if (RC == SPU::GPRCRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000134 opc = (isValidFrameIdx ? SPU::STQDr128 : SPU::STQXr128);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000135 } else if (RC == SPU::R64CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000136 opc = (isValidFrameIdx ? SPU::STQDr64 : SPU::STQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000137 } else if (RC == SPU::R64FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000138 opc = (isValidFrameIdx ? SPU::STQDr64 : SPU::STQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000139 } else if (RC == SPU::R32CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000140 opc = (isValidFrameIdx ? SPU::STQDr32 : SPU::STQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000141 } else if (RC == SPU::R32FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000142 opc = (isValidFrameIdx ? SPU::STQDr32 : SPU::STQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000143 } else if (RC == SPU::R16CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000144 opc = (isValidFrameIdx ? SPU::STQDr16 : SPU::STQXr16);
145 } else if (RC == SPU::R8CRegisterClass) {
146 opc = (isValidFrameIdx ? SPU::STQDr8 : SPU::STQXr8);
Scott Michelf0569be2008-12-27 04:51:36 +0000147 } else if (RC == SPU::VECREGRegisterClass) {
148 opc = (isValidFrameIdx) ? SPU::STQDv16i8 : SPU::STQXv16i8;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000149 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000150 llvm_unreachable("Unknown regclass!");
Owen Andersonf6372aa2008-01-01 21:11:32 +0000151 }
152
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000153 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000154 if (MI != MBB.end()) DL = MI->getDebugLoc();
155 addFrameReference(BuildMI(MBB, MI, DL, get(opc))
Bill Wendling587daed2009-05-13 21:33:08 +0000156 .addReg(SrcReg, getKillRegState(isKill)), FrameIdx);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000157}
158
Owen Andersonf6372aa2008-01-01 21:11:32 +0000159void
160SPUInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Evan Cheng746ad692010-05-06 19:06:44 +0000161 MachineBasicBlock::iterator MI,
162 unsigned DestReg, int FrameIdx,
163 const TargetRegisterClass *RC,
164 const TargetRegisterInfo *TRI) const
Owen Andersonf6372aa2008-01-01 21:11:32 +0000165{
Chris Lattnercc8cd0c2008-01-07 02:48:55 +0000166 unsigned opc;
Scott Michelaedc6372008-12-10 00:15:19 +0000167 bool isValidFrameIdx = (FrameIdx < SPUFrameInfo::maxFrameOffset());
Owen Andersonf6372aa2008-01-01 21:11:32 +0000168 if (RC == SPU::GPRCRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000169 opc = (isValidFrameIdx ? SPU::LQDr128 : SPU::LQXr128);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000170 } else if (RC == SPU::R64CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000171 opc = (isValidFrameIdx ? SPU::LQDr64 : SPU::LQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000172 } else if (RC == SPU::R64FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000173 opc = (isValidFrameIdx ? SPU::LQDr64 : SPU::LQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000174 } else if (RC == SPU::R32CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000175 opc = (isValidFrameIdx ? SPU::LQDr32 : SPU::LQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000176 } else if (RC == SPU::R32FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000177 opc = (isValidFrameIdx ? SPU::LQDr32 : SPU::LQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000178 } else if (RC == SPU::R16CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000179 opc = (isValidFrameIdx ? SPU::LQDr16 : SPU::LQXr16);
180 } else if (RC == SPU::R8CRegisterClass) {
181 opc = (isValidFrameIdx ? SPU::LQDr8 : SPU::LQXr8);
Scott Michelf0569be2008-12-27 04:51:36 +0000182 } else if (RC == SPU::VECREGRegisterClass) {
183 opc = (isValidFrameIdx) ? SPU::LQDv16i8 : SPU::LQXv16i8;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000184 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000185 llvm_unreachable("Unknown regclass in loadRegFromStackSlot!");
Owen Andersonf6372aa2008-01-01 21:11:32 +0000186 }
187
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000188 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000189 if (MI != MBB.end()) DL = MI->getDebugLoc();
Jakob Stoklund Olesenf2c3f6a2009-05-16 07:25:44 +0000190 addFrameReference(BuildMI(MBB, MI, DL, get(opc), DestReg), FrameIdx);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000191}
192
Scott Michelaedc6372008-12-10 00:15:19 +0000193//! Branch analysis
Scott Michel9bd7a372009-01-02 20:52:08 +0000194/*!
Scott Michelaedc6372008-12-10 00:15:19 +0000195 \note This code was kiped from PPC. There may be more branch analysis for
196 CellSPU than what's currently done here.
197 */
198bool
199SPUInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
Scott Michel19c10e62009-01-26 03:37:41 +0000200 MachineBasicBlock *&FBB,
Evan Chengdc54d312009-02-09 07:14:22 +0000201 SmallVectorImpl<MachineOperand> &Cond,
202 bool AllowModify) const {
Scott Michelaedc6372008-12-10 00:15:19 +0000203 // If the block has no terminators, it just falls into the block after it.
204 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000205 if (I == MBB.begin())
206 return false;
207 --I;
208 while (I->isDebugValue()) {
209 if (I == MBB.begin())
210 return false;
211 --I;
212 }
213 if (!isUnpredicatedTerminator(I))
Scott Michelaedc6372008-12-10 00:15:19 +0000214 return false;
215
216 // Get the last instruction in the block.
217 MachineInstr *LastInst = I;
Scott Michel02d711b2008-12-30 23:28:25 +0000218
Scott Michelaedc6372008-12-10 00:15:19 +0000219 // If there is only one terminator instruction, process it.
220 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
221 if (isUncondBranch(LastInst)) {
Kalle Raiskila2320a442010-05-11 11:00:02 +0000222 // Check for jump tables
223 if (!LastInst->getOperand(0).isMBB())
224 return true;
Scott Michelaedc6372008-12-10 00:15:19 +0000225 TBB = LastInst->getOperand(0).getMBB();
226 return false;
227 } else if (isCondBranch(LastInst)) {
228 // Block ends with fall-through condbranch.
229 TBB = LastInst->getOperand(1).getMBB();
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000230 DEBUG(errs() << "Pushing LastInst: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000231 DEBUG(LastInst->dump());
232 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Scott Michelaedc6372008-12-10 00:15:19 +0000233 Cond.push_back(LastInst->getOperand(0));
Scott Michelaedc6372008-12-10 00:15:19 +0000234 return false;
235 }
236 // Otherwise, don't know what this is.
237 return true;
238 }
Scott Michel02d711b2008-12-30 23:28:25 +0000239
Scott Michelaedc6372008-12-10 00:15:19 +0000240 // Get the instruction before it if it's a terminator.
241 MachineInstr *SecondLastInst = I;
242
243 // If there are three terminators, we don't know what sort of block this is.
244 if (SecondLastInst && I != MBB.begin() &&
245 isUnpredicatedTerminator(--I))
246 return true;
Scott Michel02d711b2008-12-30 23:28:25 +0000247
Scott Michelaedc6372008-12-10 00:15:19 +0000248 // If the block ends with a conditional and unconditional branch, handle it.
249 if (isCondBranch(SecondLastInst) && isUncondBranch(LastInst)) {
250 TBB = SecondLastInst->getOperand(1).getMBB();
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000251 DEBUG(errs() << "Pushing SecondLastInst: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000252 DEBUG(SecondLastInst->dump());
253 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Scott Michelaedc6372008-12-10 00:15:19 +0000254 Cond.push_back(SecondLastInst->getOperand(0));
Scott Michelaedc6372008-12-10 00:15:19 +0000255 FBB = LastInst->getOperand(0).getMBB();
256 return false;
257 }
Scott Michel02d711b2008-12-30 23:28:25 +0000258
Scott Michelaedc6372008-12-10 00:15:19 +0000259 // If the block ends with two unconditional branches, handle it. The second
260 // one is not executed, so remove it.
261 if (isUncondBranch(SecondLastInst) && isUncondBranch(LastInst)) {
262 TBB = SecondLastInst->getOperand(0).getMBB();
263 I = LastInst;
Evan Chengdc54d312009-02-09 07:14:22 +0000264 if (AllowModify)
265 I->eraseFromParent();
Scott Michelaedc6372008-12-10 00:15:19 +0000266 return false;
267 }
268
269 // Otherwise, can't handle this.
270 return true;
271}
Scott Michel02d711b2008-12-30 23:28:25 +0000272
Scott Michelaedc6372008-12-10 00:15:19 +0000273unsigned
274SPUInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
275 MachineBasicBlock::iterator I = MBB.end();
276 if (I == MBB.begin())
277 return 0;
278 --I;
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000279 while (I->isDebugValue()) {
280 if (I == MBB.begin())
281 return 0;
282 --I;
283 }
Scott Michelaedc6372008-12-10 00:15:19 +0000284 if (!isCondBranch(I) && !isUncondBranch(I))
285 return 0;
286
287 // Remove the first branch.
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000288 DEBUG(errs() << "Removing branch: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000289 DEBUG(I->dump());
Scott Michelaedc6372008-12-10 00:15:19 +0000290 I->eraseFromParent();
291 I = MBB.end();
292 if (I == MBB.begin())
293 return 1;
294
295 --I;
Scott Michel9bd7a372009-01-02 20:52:08 +0000296 if (!(isCondBranch(I) || isUncondBranch(I)))
Scott Michelaedc6372008-12-10 00:15:19 +0000297 return 1;
298
299 // Remove the second branch.
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000300 DEBUG(errs() << "Removing second branch: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000301 DEBUG(I->dump());
Scott Michelaedc6372008-12-10 00:15:19 +0000302 I->eraseFromParent();
303 return 2;
304}
Scott Michel02d711b2008-12-30 23:28:25 +0000305
Scott Michelaedc6372008-12-10 00:15:19 +0000306unsigned
307SPUInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Scott Michel19c10e62009-01-26 03:37:41 +0000308 MachineBasicBlock *FBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000309 const SmallVectorImpl<MachineOperand> &Cond,
310 DebugLoc DL) const {
Scott Michelaedc6372008-12-10 00:15:19 +0000311 // Shouldn't be a fall through.
312 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Scott Michel02d711b2008-12-30 23:28:25 +0000313 assert((Cond.size() == 2 || Cond.size() == 0) &&
Scott Michelaedc6372008-12-10 00:15:19 +0000314 "SPU branch conditions have two components!");
Scott Michel02d711b2008-12-30 23:28:25 +0000315
Scott Michelaedc6372008-12-10 00:15:19 +0000316 // One-way branch.
317 if (FBB == 0) {
Scott Michel9bd7a372009-01-02 20:52:08 +0000318 if (Cond.empty()) {
319 // Unconditional branch
Stuart Hastings3bf91252010-06-17 22:43:56 +0000320 MachineInstrBuilder MIB = BuildMI(&MBB, DL, get(SPU::BR));
Scott Michel9bd7a372009-01-02 20:52:08 +0000321 MIB.addMBB(TBB);
322
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000323 DEBUG(errs() << "Inserted one-way uncond branch: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000324 DEBUG((*MIB).dump());
325 } else {
326 // Conditional branch
Stuart Hastings3bf91252010-06-17 22:43:56 +0000327 MachineInstrBuilder MIB = BuildMI(&MBB, DL, get(Cond[0].getImm()));
Scott Michel9bd7a372009-01-02 20:52:08 +0000328 MIB.addReg(Cond[1].getReg()).addMBB(TBB);
329
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000330 DEBUG(errs() << "Inserted one-way cond branch: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000331 DEBUG((*MIB).dump());
Scott Michelaedc6372008-12-10 00:15:19 +0000332 }
333 return 1;
Scott Michel9bd7a372009-01-02 20:52:08 +0000334 } else {
Stuart Hastings3bf91252010-06-17 22:43:56 +0000335 MachineInstrBuilder MIB = BuildMI(&MBB, DL, get(Cond[0].getImm()));
336 MachineInstrBuilder MIB2 = BuildMI(&MBB, DL, get(SPU::BR));
Scott Michel9bd7a372009-01-02 20:52:08 +0000337
338 // Two-way Conditional Branch.
339 MIB.addReg(Cond[1].getReg()).addMBB(TBB);
340 MIB2.addMBB(FBB);
341
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000342 DEBUG(errs() << "Inserted conditional branch: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000343 DEBUG((*MIB).dump());
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000344 DEBUG(errs() << "part 2: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000345 DEBUG((*MIB2).dump());
346 return 2;
Scott Michelaedc6372008-12-10 00:15:19 +0000347 }
Scott Michelaedc6372008-12-10 00:15:19 +0000348}
349
Scott Michel52d00012009-01-03 00:27:53 +0000350//! Reverses a branch's condition, returning false on success.
351bool
352SPUInstrInfo::ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond)
353 const {
354 // Pretty brainless way of inverting the condition, but it works, considering
355 // there are only two conditions...
356 static struct {
357 unsigned Opc; //! The incoming opcode
358 unsigned RevCondOpc; //! The reversed condition opcode
359 } revconds[] = {
360 { SPU::BRNZr32, SPU::BRZr32 },
361 { SPU::BRNZv4i32, SPU::BRZv4i32 },
362 { SPU::BRZr32, SPU::BRNZr32 },
363 { SPU::BRZv4i32, SPU::BRNZv4i32 },
364 { SPU::BRHNZr16, SPU::BRHZr16 },
365 { SPU::BRHNZv8i16, SPU::BRHZv8i16 },
366 { SPU::BRHZr16, SPU::BRHNZr16 },
367 { SPU::BRHZv8i16, SPU::BRHNZv8i16 }
368 };
Scott Michelaedc6372008-12-10 00:15:19 +0000369
Scott Michel52d00012009-01-03 00:27:53 +0000370 unsigned Opc = unsigned(Cond[0].getImm());
371 // Pretty dull mapping between the two conditions that SPU can generate:
Misha Brukman93c65c82009-01-07 23:07:29 +0000372 for (int i = sizeof(revconds)/sizeof(revconds[0]) - 1; i >= 0; --i) {
Scott Michel52d00012009-01-03 00:27:53 +0000373 if (revconds[i].Opc == Opc) {
374 Cond[0].setImm(revconds[i].RevCondOpc);
375 return false;
376 }
377 }
378
379 return true;
380}