blob: a30de4688eef6bc9c7ca182111583c0fd59e0593 [file] [log] [blame]
Jia Liu31d157a2012-02-18 12:03:15 +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
Scott Michel66377522007-12-04 22:35:58 +000014#include "SPUInstrInfo.h"
Owen Andersonf6372aa2008-01-01 21:11:32 +000015#include "SPUInstrBuilder.h"
Scott Michel66377522007-12-04 22:35:58 +000016#include "SPUTargetMachine.h"
Andrew Trick2da8bc82010-12-24 05:03:26 +000017#include "SPUHazardRecognizers.h"
Scott Michel66377522007-12-04 22:35:58 +000018#include "llvm/CodeGen/MachineInstrBuilder.h"
Evan Cheng59ee62d2011-07-11 03:57:24 +000019#include "llvm/MC/MCContext.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"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000022#include "llvm/Support/TargetRegistry.h"
Benjamin Kramer072a56e2009-08-23 11:52:17 +000023#include "llvm/Support/raw_ostream.h"
Scott Michel66377522007-12-04 22:35:58 +000024
Evan Cheng4db3cff2011-07-01 17:57:27 +000025#define GET_INSTRINFO_CTOR
Evan Cheng22fee2d2011-06-28 20:07:07 +000026#include "SPUGenInstrInfo.inc"
27
Scott Michel66377522007-12-04 22:35:58 +000028using namespace llvm;
29
Scott Michelaedc6372008-12-10 00:15:19 +000030namespace {
31 //! Predicate for an unconditional branch instruction
32 inline bool isUncondBranch(const MachineInstr *I) {
33 unsigned opc = I->getOpcode();
34
35 return (opc == SPU::BR
Scott Michel19c10e62009-01-26 03:37:41 +000036 || opc == SPU::BRA
37 || opc == SPU::BI);
Scott Michelaedc6372008-12-10 00:15:19 +000038 }
39
Scott Michel52d00012009-01-03 00:27:53 +000040 //! Predicate for a conditional branch instruction
Scott Michelaedc6372008-12-10 00:15:19 +000041 inline bool isCondBranch(const MachineInstr *I) {
42 unsigned opc = I->getOpcode();
43
Scott Michelf0569be2008-12-27 04:51:36 +000044 return (opc == SPU::BRNZr32
45 || opc == SPU::BRNZv4i32
Scott Michel19c10e62009-01-26 03:37:41 +000046 || opc == SPU::BRZr32
47 || opc == SPU::BRZv4i32
48 || opc == SPU::BRHNZr16
49 || opc == SPU::BRHNZv8i16
50 || opc == SPU::BRHZr16
51 || opc == SPU::BRHZv8i16);
Scott Michelaedc6372008-12-10 00:15:19 +000052 }
53}
54
Scott Michel66377522007-12-04 22:35:58 +000055SPUInstrInfo::SPUInstrInfo(SPUTargetMachine &tm)
Evan Cheng4db3cff2011-07-01 17:57:27 +000056 : SPUGenInstrInfo(SPU::ADJCALLSTACKDOWN, SPU::ADJCALLSTACKUP),
Scott Michel66377522007-12-04 22:35:58 +000057 TM(tm),
58 RI(*TM.getSubtargetImpl(), *this)
Scott Michel52d00012009-01-03 00:27:53 +000059{ /* NOP */ }
Scott Michel66377522007-12-04 22:35:58 +000060
Andrew Trick2da8bc82010-12-24 05:03:26 +000061/// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
62/// this target when scheduling the DAG.
63ScheduleHazardRecognizer *SPUInstrInfo::CreateTargetHazardRecognizer(
64 const TargetMachine *TM,
65 const ScheduleDAG *DAG) const {
66 const TargetInstrInfo *TII = TM->getInstrInfo();
67 assert(TII && "No InstrInfo?");
68 return new SPUHazardRecognizer(*TII);
69}
70
Scott Michel66377522007-12-04 22:35:58 +000071unsigned
Dan Gohmancbad42c2008-11-18 19:49:32 +000072SPUInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
73 int &FrameIndex) const {
Scott Michel66377522007-12-04 22:35:58 +000074 switch (MI->getOpcode()) {
75 default: break;
76 case SPU::LQDv16i8:
77 case SPU::LQDv8i16:
78 case SPU::LQDv4i32:
79 case SPU::LQDv4f32:
80 case SPU::LQDv2f64:
81 case SPU::LQDr128:
82 case SPU::LQDr64:
83 case SPU::LQDr32:
Scott Michelaedc6372008-12-10 00:15:19 +000084 case SPU::LQDr16: {
85 const MachineOperand MOp1 = MI->getOperand(1);
86 const MachineOperand MOp2 = MI->getOperand(2);
Scott Michel52d00012009-01-03 00:27:53 +000087 if (MOp1.isImm() && MOp2.isFI()) {
88 FrameIndex = MOp2.getIndex();
Scott Michelaedc6372008-12-10 00:15:19 +000089 return MI->getOperand(0).getReg();
90 }
91 break;
92 }
Scott Michel66377522007-12-04 22:35:58 +000093 }
94 return 0;
95}
96
97unsigned
Dan Gohmancbad42c2008-11-18 19:49:32 +000098SPUInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
99 int &FrameIndex) const {
Scott Michel66377522007-12-04 22:35:58 +0000100 switch (MI->getOpcode()) {
101 default: break;
102 case SPU::STQDv16i8:
103 case SPU::STQDv8i16:
104 case SPU::STQDv4i32:
105 case SPU::STQDv4f32:
106 case SPU::STQDv2f64:
107 case SPU::STQDr128:
108 case SPU::STQDr64:
109 case SPU::STQDr32:
110 case SPU::STQDr16:
Scott Michelaedc6372008-12-10 00:15:19 +0000111 case SPU::STQDr8: {
112 const MachineOperand MOp1 = MI->getOperand(1);
113 const MachineOperand MOp2 = MI->getOperand(2);
Scott Michelf0569be2008-12-27 04:51:36 +0000114 if (MOp1.isImm() && MOp2.isFI()) {
115 FrameIndex = MOp2.getIndex();
Scott Michelaedc6372008-12-10 00:15:19 +0000116 return MI->getOperand(0).getReg();
117 }
118 break;
119 }
Scott Michel66377522007-12-04 22:35:58 +0000120 }
121 return 0;
122}
Owen Andersond10fd972007-12-31 06:32:00 +0000123
Jakob Stoklund Olesen377b7b72010-07-11 07:31:03 +0000124void SPUInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
125 MachineBasicBlock::iterator I, DebugLoc DL,
126 unsigned DestReg, unsigned SrcReg,
127 bool KillSrc) const
Owen Andersond10fd972007-12-31 06:32:00 +0000128{
Chris Lattner5e09da22008-03-09 20:31:11 +0000129 // We support cross register class moves for our aliases, such as R3 in any
130 // reg class to any other reg class containing R3. This is required because
131 // we instruction select bitconvert i64 -> f64 as a noop for example, so our
132 // types have no specific meaning.
Scott Michel02d711b2008-12-30 23:28:25 +0000133
Jakob Stoklund Olesen377b7b72010-07-11 07:31:03 +0000134 BuildMI(MBB, I, DL, get(SPU::LRr128), DestReg)
135 .addReg(SrcReg, getKillRegState(KillSrc));
Owen Andersond10fd972007-12-31 06:32:00 +0000136}
Owen Andersonf6372aa2008-01-01 21:11:32 +0000137
138void
139SPUInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Evan Cheng746ad692010-05-06 19:06:44 +0000140 MachineBasicBlock::iterator MI,
141 unsigned SrcReg, bool isKill, int FrameIdx,
142 const TargetRegisterClass *RC,
Craig Topperc9099502012-04-20 06:31:50 +0000143 const TargetRegisterInfo *TRI) const {
Chris Lattnercc8cd0c2008-01-07 02:48:55 +0000144 unsigned opc;
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000145 bool isValidFrameIdx = (FrameIdx < SPUFrameLowering::maxFrameOffset());
Craig Topperc9099502012-04-20 06:31:50 +0000146 if (RC == SPU::GPRCRegisterClass)
147 opc = isValidFrameIdx ? SPU::STQDr128 : SPU::STQXr128;
148 else if (RC == SPU::R64CRegisterClass)
149 opc = isValidFrameIdx ? SPU::STQDr64 : SPU::STQXr64;
150 else if (RC == SPU::R64FPRegisterClass)
151 opc = isValidFrameIdx ? SPU::STQDr64 : SPU::STQXr64;
152 else if (RC == SPU::R32CRegisterClass)
153 opc = isValidFrameIdx ? SPU::STQDr32 : SPU::STQXr32;
154 else if (RC == SPU::R32FPRegisterClass)
155 opc = isValidFrameIdx ? SPU::STQDr32 : SPU::STQXr32;
156 else if (RC == SPU::R16CRegisterClass)
157 opc = isValidFrameIdx ? SPU::STQDr16 : SPU::STQXr16;
158 else if (RC == SPU::R8CRegisterClass)
159 opc = isValidFrameIdx ? SPU::STQDr8 : SPU::STQXr8;
160 else if (RC == SPU::VECREGRegisterClass)
161 opc = isValidFrameIdx ? SPU::STQDv16i8 : SPU::STQXv16i8;
162 else
Torok Edwinc23197a2009-07-14 16:55:14 +0000163 llvm_unreachable("Unknown regclass!");
Owen Andersonf6372aa2008-01-01 21:11:32 +0000164
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000165 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000166 if (MI != MBB.end()) DL = MI->getDebugLoc();
167 addFrameReference(BuildMI(MBB, MI, DL, get(opc))
Bill Wendling587daed2009-05-13 21:33:08 +0000168 .addReg(SrcReg, getKillRegState(isKill)), FrameIdx);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000169}
170
Owen Andersonf6372aa2008-01-01 21:11:32 +0000171void
172SPUInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Evan Cheng746ad692010-05-06 19:06:44 +0000173 MachineBasicBlock::iterator MI,
174 unsigned DestReg, int FrameIdx,
175 const TargetRegisterClass *RC,
Craig Topperc9099502012-04-20 06:31:50 +0000176 const TargetRegisterInfo *TRI) const {
Chris Lattnercc8cd0c2008-01-07 02:48:55 +0000177 unsigned opc;
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000178 bool isValidFrameIdx = (FrameIdx < SPUFrameLowering::maxFrameOffset());
Craig Topperc9099502012-04-20 06:31:50 +0000179 if (RC == SPU::GPRCRegisterClass)
180 opc = isValidFrameIdx ? SPU::LQDr128 : SPU::LQXr128;
181 else if (RC == SPU::R64CRegisterClass)
182 opc = isValidFrameIdx ? SPU::LQDr64 : SPU::LQXr64;
183 else if (RC == SPU::R64FPRegisterClass)
184 opc = isValidFrameIdx ? SPU::LQDr64 : SPU::LQXr64;
185 else if (RC == SPU::R32CRegisterClass)
186 opc = isValidFrameIdx ? SPU::LQDr32 : SPU::LQXr32;
187 else if (RC == SPU::R32FPRegisterClass)
188 opc = isValidFrameIdx ? SPU::LQDr32 : SPU::LQXr32;
189 else if (RC == SPU::R16CRegisterClass)
190 opc = isValidFrameIdx ? SPU::LQDr16 : SPU::LQXr16;
191 else if (RC == SPU::R8CRegisterClass)
192 opc = isValidFrameIdx ? SPU::LQDr8 : SPU::LQXr8;
193 else if (RC == SPU::VECREGRegisterClass)
194 opc = isValidFrameIdx ? SPU::LQDv16i8 : SPU::LQXv16i8;
195 else
Torok Edwinc23197a2009-07-14 16:55:14 +0000196 llvm_unreachable("Unknown regclass in loadRegFromStackSlot!");
Owen Andersonf6372aa2008-01-01 21:11:32 +0000197
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000198 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000199 if (MI != MBB.end()) DL = MI->getDebugLoc();
Jakob Stoklund Olesenf2c3f6a2009-05-16 07:25:44 +0000200 addFrameReference(BuildMI(MBB, MI, DL, get(opc), DestReg), FrameIdx);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000201}
202
Scott Michelaedc6372008-12-10 00:15:19 +0000203//! Branch analysis
Scott Michel9bd7a372009-01-02 20:52:08 +0000204/*!
Scott Michelaedc6372008-12-10 00:15:19 +0000205 \note This code was kiped from PPC. There may be more branch analysis for
206 CellSPU than what's currently done here.
207 */
208bool
209SPUInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
Scott Michel19c10e62009-01-26 03:37:41 +0000210 MachineBasicBlock *&FBB,
Evan Chengdc54d312009-02-09 07:14:22 +0000211 SmallVectorImpl<MachineOperand> &Cond,
212 bool AllowModify) const {
Scott Michelaedc6372008-12-10 00:15:19 +0000213 // If the block has no terminators, it just falls into the block after it.
214 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000215 if (I == MBB.begin())
216 return false;
217 --I;
218 while (I->isDebugValue()) {
219 if (I == MBB.begin())
220 return false;
221 --I;
222 }
223 if (!isUnpredicatedTerminator(I))
Scott Michelaedc6372008-12-10 00:15:19 +0000224 return false;
225
226 // Get the last instruction in the block.
227 MachineInstr *LastInst = I;
Scott Michel02d711b2008-12-30 23:28:25 +0000228
Scott Michelaedc6372008-12-10 00:15:19 +0000229 // If there is only one terminator instruction, process it.
230 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
231 if (isUncondBranch(LastInst)) {
Kalle Raiskila2320a442010-05-11 11:00:02 +0000232 // Check for jump tables
233 if (!LastInst->getOperand(0).isMBB())
234 return true;
Scott Michelaedc6372008-12-10 00:15:19 +0000235 TBB = LastInst->getOperand(0).getMBB();
236 return false;
237 } else if (isCondBranch(LastInst)) {
238 // Block ends with fall-through condbranch.
239 TBB = LastInst->getOperand(1).getMBB();
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000240 DEBUG(errs() << "Pushing LastInst: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000241 DEBUG(LastInst->dump());
242 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Scott Michelaedc6372008-12-10 00:15:19 +0000243 Cond.push_back(LastInst->getOperand(0));
Scott Michelaedc6372008-12-10 00:15:19 +0000244 return false;
245 }
246 // Otherwise, don't know what this is.
247 return true;
248 }
Scott Michel02d711b2008-12-30 23:28:25 +0000249
Scott Michelaedc6372008-12-10 00:15:19 +0000250 // Get the instruction before it if it's a terminator.
251 MachineInstr *SecondLastInst = I;
252
253 // If there are three terminators, we don't know what sort of block this is.
254 if (SecondLastInst && I != MBB.begin() &&
255 isUnpredicatedTerminator(--I))
256 return true;
Scott Michel02d711b2008-12-30 23:28:25 +0000257
Scott Michelaedc6372008-12-10 00:15:19 +0000258 // If the block ends with a conditional and unconditional branch, handle it.
259 if (isCondBranch(SecondLastInst) && isUncondBranch(LastInst)) {
260 TBB = SecondLastInst->getOperand(1).getMBB();
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000261 DEBUG(errs() << "Pushing SecondLastInst: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000262 DEBUG(SecondLastInst->dump());
263 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Scott Michelaedc6372008-12-10 00:15:19 +0000264 Cond.push_back(SecondLastInst->getOperand(0));
Scott Michelaedc6372008-12-10 00:15:19 +0000265 FBB = LastInst->getOperand(0).getMBB();
266 return false;
267 }
Scott Michel02d711b2008-12-30 23:28:25 +0000268
Scott Michelaedc6372008-12-10 00:15:19 +0000269 // If the block ends with two unconditional branches, handle it. The second
270 // one is not executed, so remove it.
271 if (isUncondBranch(SecondLastInst) && isUncondBranch(LastInst)) {
272 TBB = SecondLastInst->getOperand(0).getMBB();
273 I = LastInst;
Evan Chengdc54d312009-02-09 07:14:22 +0000274 if (AllowModify)
275 I->eraseFromParent();
Scott Michelaedc6372008-12-10 00:15:19 +0000276 return false;
277 }
278
279 // Otherwise, can't handle this.
280 return true;
281}
Scott Michel02d711b2008-12-30 23:28:25 +0000282
Kalle Raiskila2d25d242011-02-28 14:08:24 +0000283// search MBB for branch hint labels and branch hit ops
284static void removeHBR( MachineBasicBlock &MBB) {
285 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I){
286 if (I->getOpcode() == SPU::HBRA ||
287 I->getOpcode() == SPU::HBR_LABEL){
288 I=MBB.erase(I);
Kalle Raiskila56354d42011-10-11 12:55:18 +0000289 if (I == MBB.end())
290 break;
Kalle Raiskila2d25d242011-02-28 14:08:24 +0000291 }
292 }
293}
294
Scott Michelaedc6372008-12-10 00:15:19 +0000295unsigned
296SPUInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
297 MachineBasicBlock::iterator I = MBB.end();
Kalle Raiskila2d25d242011-02-28 14:08:24 +0000298 removeHBR(MBB);
Scott Michelaedc6372008-12-10 00:15:19 +0000299 if (I == MBB.begin())
300 return 0;
301 --I;
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000302 while (I->isDebugValue()) {
303 if (I == MBB.begin())
304 return 0;
305 --I;
306 }
Scott Michelaedc6372008-12-10 00:15:19 +0000307 if (!isCondBranch(I) && !isUncondBranch(I))
308 return 0;
309
310 // Remove the first branch.
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000311 DEBUG(errs() << "Removing branch: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000312 DEBUG(I->dump());
Scott Michelaedc6372008-12-10 00:15:19 +0000313 I->eraseFromParent();
314 I = MBB.end();
315 if (I == MBB.begin())
316 return 1;
317
318 --I;
Scott Michel9bd7a372009-01-02 20:52:08 +0000319 if (!(isCondBranch(I) || isUncondBranch(I)))
Scott Michelaedc6372008-12-10 00:15:19 +0000320 return 1;
321
322 // Remove the second branch.
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000323 DEBUG(errs() << "Removing second branch: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000324 DEBUG(I->dump());
Scott Michelaedc6372008-12-10 00:15:19 +0000325 I->eraseFromParent();
326 return 2;
327}
Scott Michel02d711b2008-12-30 23:28:25 +0000328
Kalle Raiskila2d25d242011-02-28 14:08:24 +0000329/** Find the optimal position for a hint branch instruction in a basic block.
330 * This should take into account:
331 * -the branch hint delays
332 * -congestion of the memory bus
333 * -dual-issue scheduling (i.e. avoid insertion of nops)
334 * Current implementation is rather simplistic.
335 */
336static MachineBasicBlock::iterator findHBRPosition(MachineBasicBlock &MBB)
337{
338 MachineBasicBlock::iterator J = MBB.end();
339 for( int i=0; i<8; i++) {
340 if( J == MBB.begin() ) return J;
341 J--;
342 }
343 return J;
344}
345
Scott Michelaedc6372008-12-10 00:15:19 +0000346unsigned
347SPUInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Scott Michel19c10e62009-01-26 03:37:41 +0000348 MachineBasicBlock *FBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000349 const SmallVectorImpl<MachineOperand> &Cond,
350 DebugLoc DL) const {
Scott Michelaedc6372008-12-10 00:15:19 +0000351 // Shouldn't be a fall through.
352 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Scott Michel02d711b2008-12-30 23:28:25 +0000353 assert((Cond.size() == 2 || Cond.size() == 0) &&
Scott Michelaedc6372008-12-10 00:15:19 +0000354 "SPU branch conditions have two components!");
Scott Michel02d711b2008-12-30 23:28:25 +0000355
Kalle Raiskila2d25d242011-02-28 14:08:24 +0000356 MachineInstrBuilder MIB;
357 //TODO: make a more accurate algorithm.
358 bool haveHBR = MBB.size()>8;
359
360 removeHBR(MBB);
361 MCSymbol *branchLabel = MBB.getParent()->getContext().CreateTempSymbol();
362 // Add a label just before the branch
363 if (haveHBR)
364 MIB = BuildMI(&MBB, DL, get(SPU::HBR_LABEL)).addSym(branchLabel);
365
Scott Michelaedc6372008-12-10 00:15:19 +0000366 // One-way branch.
367 if (FBB == 0) {
Scott Michel9bd7a372009-01-02 20:52:08 +0000368 if (Cond.empty()) {
369 // Unconditional branch
Kalle Raiskila2d25d242011-02-28 14:08:24 +0000370 MIB = BuildMI(&MBB, DL, get(SPU::BR));
Scott Michel9bd7a372009-01-02 20:52:08 +0000371 MIB.addMBB(TBB);
372
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000373 DEBUG(errs() << "Inserted one-way uncond branch: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000374 DEBUG((*MIB).dump());
Kalle Raiskila2d25d242011-02-28 14:08:24 +0000375
376 // basic blocks have just one branch so it is safe to add the hint a its
377 if (haveHBR) {
378 MIB = BuildMI( MBB, findHBRPosition(MBB), DL, get(SPU::HBRA));
379 MIB.addSym(branchLabel);
380 MIB.addMBB(TBB);
381 }
Scott Michel9bd7a372009-01-02 20:52:08 +0000382 } else {
383 // Conditional branch
Kalle Raiskila2d25d242011-02-28 14:08:24 +0000384 MIB = BuildMI(&MBB, DL, get(Cond[0].getImm()));
Scott Michel9bd7a372009-01-02 20:52:08 +0000385 MIB.addReg(Cond[1].getReg()).addMBB(TBB);
386
Kalle Raiskila2d25d242011-02-28 14:08:24 +0000387 if (haveHBR) {
388 MIB = BuildMI(MBB, findHBRPosition(MBB), DL, get(SPU::HBRA));
389 MIB.addSym(branchLabel);
390 MIB.addMBB(TBB);
391 }
392
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000393 DEBUG(errs() << "Inserted one-way cond branch: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000394 DEBUG((*MIB).dump());
Scott Michelaedc6372008-12-10 00:15:19 +0000395 }
396 return 1;
Scott Michel9bd7a372009-01-02 20:52:08 +0000397 } else {
Kalle Raiskila2d25d242011-02-28 14:08:24 +0000398 MIB = BuildMI(&MBB, DL, get(Cond[0].getImm()));
Stuart Hastings3bf91252010-06-17 22:43:56 +0000399 MachineInstrBuilder MIB2 = BuildMI(&MBB, DL, get(SPU::BR));
Scott Michel9bd7a372009-01-02 20:52:08 +0000400
401 // Two-way Conditional Branch.
402 MIB.addReg(Cond[1].getReg()).addMBB(TBB);
403 MIB2.addMBB(FBB);
404
Kalle Raiskila2d25d242011-02-28 14:08:24 +0000405 if (haveHBR) {
406 MIB = BuildMI( MBB, findHBRPosition(MBB), DL, get(SPU::HBRA));
407 MIB.addSym(branchLabel);
408 MIB.addMBB(FBB);
409 }
410
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000411 DEBUG(errs() << "Inserted conditional branch: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000412 DEBUG((*MIB).dump());
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000413 DEBUG(errs() << "part 2: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000414 DEBUG((*MIB2).dump());
415 return 2;
Scott Michelaedc6372008-12-10 00:15:19 +0000416 }
Scott Michelaedc6372008-12-10 00:15:19 +0000417}
418
Scott Michel52d00012009-01-03 00:27:53 +0000419//! Reverses a branch's condition, returning false on success.
420bool
421SPUInstrInfo::ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond)
422 const {
423 // Pretty brainless way of inverting the condition, but it works, considering
424 // there are only two conditions...
425 static struct {
426 unsigned Opc; //! The incoming opcode
427 unsigned RevCondOpc; //! The reversed condition opcode
428 } revconds[] = {
429 { SPU::BRNZr32, SPU::BRZr32 },
430 { SPU::BRNZv4i32, SPU::BRZv4i32 },
431 { SPU::BRZr32, SPU::BRNZr32 },
432 { SPU::BRZv4i32, SPU::BRNZv4i32 },
433 { SPU::BRHNZr16, SPU::BRHZr16 },
434 { SPU::BRHNZv8i16, SPU::BRHZv8i16 },
435 { SPU::BRHZr16, SPU::BRHNZr16 },
436 { SPU::BRHZv8i16, SPU::BRHNZv8i16 }
437 };
Scott Michelaedc6372008-12-10 00:15:19 +0000438
Scott Michel52d00012009-01-03 00:27:53 +0000439 unsigned Opc = unsigned(Cond[0].getImm());
440 // Pretty dull mapping between the two conditions that SPU can generate:
Misha Brukman93c65c82009-01-07 23:07:29 +0000441 for (int i = sizeof(revconds)/sizeof(revconds[0]) - 1; i >= 0; --i) {
Scott Michel52d00012009-01-03 00:27:53 +0000442 if (revconds[i].Opc == Opc) {
443 Cond[0].setImm(revconds[i].RevCondOpc);
444 return false;
445 }
446 }
447
448 return true;
449}