blob: 080434d66789503a8083c8178a54eafaf66d7fa2 [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"
Andrew Trick2da8bc82010-12-24 05:03:26 +000019#include "SPUHazardRecognizers.h"
Scott Michel66377522007-12-04 22:35:58 +000020#include "llvm/CodeGen/MachineInstrBuilder.h"
Scott Michel9bd7a372009-01-02 20:52:08 +000021#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000022#include "llvm/Support/ErrorHandling.h"
Benjamin Kramer072a56e2009-08-23 11:52:17 +000023#include "llvm/Support/raw_ostream.h"
Kalle Raiskila2d25d242011-02-28 14:08:24 +000024#include "llvm/MC/MCContext.h"
Scott Michel66377522007-12-04 22:35:58 +000025
26using namespace llvm;
27
Scott Michelaedc6372008-12-10 00:15:19 +000028namespace {
29 //! Predicate for an unconditional branch instruction
30 inline bool isUncondBranch(const MachineInstr *I) {
31 unsigned opc = I->getOpcode();
32
33 return (opc == SPU::BR
Scott Michel19c10e62009-01-26 03:37:41 +000034 || opc == SPU::BRA
35 || opc == SPU::BI);
Scott Michelaedc6372008-12-10 00:15:19 +000036 }
37
Scott Michel52d00012009-01-03 00:27:53 +000038 //! Predicate for a conditional branch instruction
Scott Michelaedc6372008-12-10 00:15:19 +000039 inline bool isCondBranch(const MachineInstr *I) {
40 unsigned opc = I->getOpcode();
41
Scott Michelf0569be2008-12-27 04:51:36 +000042 return (opc == SPU::BRNZr32
43 || opc == SPU::BRNZv4i32
Scott Michel19c10e62009-01-26 03:37:41 +000044 || opc == SPU::BRZr32
45 || opc == SPU::BRZv4i32
46 || opc == SPU::BRHNZr16
47 || opc == SPU::BRHNZv8i16
48 || opc == SPU::BRHZr16
49 || opc == SPU::BRHZv8i16);
Scott Michelaedc6372008-12-10 00:15:19 +000050 }
51}
52
Scott Michel66377522007-12-04 22:35:58 +000053SPUInstrInfo::SPUInstrInfo(SPUTargetMachine &tm)
Chris Lattner64105522008-01-01 01:03:04 +000054 : TargetInstrInfoImpl(SPUInsts, sizeof(SPUInsts)/sizeof(SPUInsts[0])),
Scott Michel66377522007-12-04 22:35:58 +000055 TM(tm),
56 RI(*TM.getSubtargetImpl(), *this)
Scott Michel52d00012009-01-03 00:27:53 +000057{ /* NOP */ }
Scott Michel66377522007-12-04 22:35:58 +000058
Andrew Trick2da8bc82010-12-24 05:03:26 +000059/// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
60/// this target when scheduling the DAG.
61ScheduleHazardRecognizer *SPUInstrInfo::CreateTargetHazardRecognizer(
62 const TargetMachine *TM,
63 const ScheduleDAG *DAG) const {
64 const TargetInstrInfo *TII = TM->getInstrInfo();
65 assert(TII && "No InstrInfo?");
66 return new SPUHazardRecognizer(*TII);
67}
68
Scott Michel66377522007-12-04 22:35:58 +000069unsigned
Dan Gohmancbad42c2008-11-18 19:49:32 +000070SPUInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
71 int &FrameIndex) const {
Scott Michel66377522007-12-04 22:35:58 +000072 switch (MI->getOpcode()) {
73 default: break;
74 case SPU::LQDv16i8:
75 case SPU::LQDv8i16:
76 case SPU::LQDv4i32:
77 case SPU::LQDv4f32:
78 case SPU::LQDv2f64:
79 case SPU::LQDr128:
80 case SPU::LQDr64:
81 case SPU::LQDr32:
Scott Michelaedc6372008-12-10 00:15:19 +000082 case SPU::LQDr16: {
83 const MachineOperand MOp1 = MI->getOperand(1);
84 const MachineOperand MOp2 = MI->getOperand(2);
Scott Michel52d00012009-01-03 00:27:53 +000085 if (MOp1.isImm() && MOp2.isFI()) {
86 FrameIndex = MOp2.getIndex();
Scott Michelaedc6372008-12-10 00:15:19 +000087 return MI->getOperand(0).getReg();
88 }
89 break;
90 }
Scott Michel66377522007-12-04 22:35:58 +000091 }
92 return 0;
93}
94
95unsigned
Dan Gohmancbad42c2008-11-18 19:49:32 +000096SPUInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
97 int &FrameIndex) const {
Scott Michel66377522007-12-04 22:35:58 +000098 switch (MI->getOpcode()) {
99 default: break;
100 case SPU::STQDv16i8:
101 case SPU::STQDv8i16:
102 case SPU::STQDv4i32:
103 case SPU::STQDv4f32:
104 case SPU::STQDv2f64:
105 case SPU::STQDr128:
106 case SPU::STQDr64:
107 case SPU::STQDr32:
108 case SPU::STQDr16:
Scott Michelaedc6372008-12-10 00:15:19 +0000109 case SPU::STQDr8: {
110 const MachineOperand MOp1 = MI->getOperand(1);
111 const MachineOperand MOp2 = MI->getOperand(2);
Scott Michelf0569be2008-12-27 04:51:36 +0000112 if (MOp1.isImm() && MOp2.isFI()) {
113 FrameIndex = MOp2.getIndex();
Scott Michelaedc6372008-12-10 00:15:19 +0000114 return MI->getOperand(0).getReg();
115 }
116 break;
117 }
Scott Michel66377522007-12-04 22:35:58 +0000118 }
119 return 0;
120}
Owen Andersond10fd972007-12-31 06:32:00 +0000121
Jakob Stoklund Olesen377b7b72010-07-11 07:31:03 +0000122void SPUInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
123 MachineBasicBlock::iterator I, DebugLoc DL,
124 unsigned DestReg, unsigned SrcReg,
125 bool KillSrc) const
Owen Andersond10fd972007-12-31 06:32:00 +0000126{
Chris Lattner5e09da22008-03-09 20:31:11 +0000127 // We support cross register class moves for our aliases, such as R3 in any
128 // reg class to any other reg class containing R3. This is required because
129 // we instruction select bitconvert i64 -> f64 as a noop for example, so our
130 // types have no specific meaning.
Scott Michel02d711b2008-12-30 23:28:25 +0000131
Jakob Stoklund Olesen377b7b72010-07-11 07:31:03 +0000132 BuildMI(MBB, I, DL, get(SPU::LRr128), DestReg)
133 .addReg(SrcReg, getKillRegState(KillSrc));
Owen Andersond10fd972007-12-31 06:32:00 +0000134}
Owen Andersonf6372aa2008-01-01 21:11:32 +0000135
136void
137SPUInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Evan Cheng746ad692010-05-06 19:06:44 +0000138 MachineBasicBlock::iterator MI,
139 unsigned SrcReg, bool isKill, int FrameIdx,
140 const TargetRegisterClass *RC,
141 const TargetRegisterInfo *TRI) const
Owen Andersonf6372aa2008-01-01 21:11:32 +0000142{
Chris Lattnercc8cd0c2008-01-07 02:48:55 +0000143 unsigned opc;
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000144 bool isValidFrameIdx = (FrameIdx < SPUFrameLowering::maxFrameOffset());
Owen Andersonf6372aa2008-01-01 21:11:32 +0000145 if (RC == SPU::GPRCRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000146 opc = (isValidFrameIdx ? SPU::STQDr128 : SPU::STQXr128);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000147 } else if (RC == SPU::R64CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000148 opc = (isValidFrameIdx ? SPU::STQDr64 : SPU::STQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000149 } else if (RC == SPU::R64FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000150 opc = (isValidFrameIdx ? SPU::STQDr64 : SPU::STQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000151 } else if (RC == SPU::R32CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000152 opc = (isValidFrameIdx ? SPU::STQDr32 : SPU::STQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000153 } else if (RC == SPU::R32FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000154 opc = (isValidFrameIdx ? SPU::STQDr32 : SPU::STQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000155 } else if (RC == SPU::R16CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000156 opc = (isValidFrameIdx ? SPU::STQDr16 : SPU::STQXr16);
157 } else if (RC == SPU::R8CRegisterClass) {
158 opc = (isValidFrameIdx ? SPU::STQDr8 : SPU::STQXr8);
Scott Michelf0569be2008-12-27 04:51:36 +0000159 } else if (RC == SPU::VECREGRegisterClass) {
160 opc = (isValidFrameIdx) ? SPU::STQDv16i8 : SPU::STQXv16i8;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000161 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000162 llvm_unreachable("Unknown regclass!");
Owen Andersonf6372aa2008-01-01 21:11:32 +0000163 }
164
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,
176 const TargetRegisterInfo *TRI) const
Owen Andersonf6372aa2008-01-01 21:11:32 +0000177{
Chris Lattnercc8cd0c2008-01-07 02:48:55 +0000178 unsigned opc;
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000179 bool isValidFrameIdx = (FrameIdx < SPUFrameLowering::maxFrameOffset());
Owen Andersonf6372aa2008-01-01 21:11:32 +0000180 if (RC == SPU::GPRCRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000181 opc = (isValidFrameIdx ? SPU::LQDr128 : SPU::LQXr128);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000182 } else if (RC == SPU::R64CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000183 opc = (isValidFrameIdx ? SPU::LQDr64 : SPU::LQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000184 } else if (RC == SPU::R64FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000185 opc = (isValidFrameIdx ? SPU::LQDr64 : SPU::LQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000186 } else if (RC == SPU::R32CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000187 opc = (isValidFrameIdx ? SPU::LQDr32 : SPU::LQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000188 } else if (RC == SPU::R32FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000189 opc = (isValidFrameIdx ? SPU::LQDr32 : SPU::LQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000190 } else if (RC == SPU::R16CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000191 opc = (isValidFrameIdx ? SPU::LQDr16 : SPU::LQXr16);
192 } else if (RC == SPU::R8CRegisterClass) {
193 opc = (isValidFrameIdx ? SPU::LQDr8 : SPU::LQXr8);
Scott Michelf0569be2008-12-27 04:51:36 +0000194 } else if (RC == SPU::VECREGRegisterClass) {
195 opc = (isValidFrameIdx) ? SPU::LQDv16i8 : SPU::LQXv16i8;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000196 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000197 llvm_unreachable("Unknown regclass in loadRegFromStackSlot!");
Owen Andersonf6372aa2008-01-01 21:11:32 +0000198 }
199
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000200 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000201 if (MI != MBB.end()) DL = MI->getDebugLoc();
Jakob Stoklund Olesenf2c3f6a2009-05-16 07:25:44 +0000202 addFrameReference(BuildMI(MBB, MI, DL, get(opc), DestReg), FrameIdx);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000203}
204
Scott Michelaedc6372008-12-10 00:15:19 +0000205//! Branch analysis
Scott Michel9bd7a372009-01-02 20:52:08 +0000206/*!
Scott Michelaedc6372008-12-10 00:15:19 +0000207 \note This code was kiped from PPC. There may be more branch analysis for
208 CellSPU than what's currently done here.
209 */
210bool
211SPUInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
Scott Michel19c10e62009-01-26 03:37:41 +0000212 MachineBasicBlock *&FBB,
Evan Chengdc54d312009-02-09 07:14:22 +0000213 SmallVectorImpl<MachineOperand> &Cond,
214 bool AllowModify) const {
Scott Michelaedc6372008-12-10 00:15:19 +0000215 // If the block has no terminators, it just falls into the block after it.
216 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000217 if (I == MBB.begin())
218 return false;
219 --I;
220 while (I->isDebugValue()) {
221 if (I == MBB.begin())
222 return false;
223 --I;
224 }
225 if (!isUnpredicatedTerminator(I))
Scott Michelaedc6372008-12-10 00:15:19 +0000226 return false;
227
228 // Get the last instruction in the block.
229 MachineInstr *LastInst = I;
Scott Michel02d711b2008-12-30 23:28:25 +0000230
Scott Michelaedc6372008-12-10 00:15:19 +0000231 // If there is only one terminator instruction, process it.
232 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
233 if (isUncondBranch(LastInst)) {
Kalle Raiskila2320a442010-05-11 11:00:02 +0000234 // Check for jump tables
235 if (!LastInst->getOperand(0).isMBB())
236 return true;
Scott Michelaedc6372008-12-10 00:15:19 +0000237 TBB = LastInst->getOperand(0).getMBB();
238 return false;
239 } else if (isCondBranch(LastInst)) {
240 // Block ends with fall-through condbranch.
241 TBB = LastInst->getOperand(1).getMBB();
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000242 DEBUG(errs() << "Pushing LastInst: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000243 DEBUG(LastInst->dump());
244 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Scott Michelaedc6372008-12-10 00:15:19 +0000245 Cond.push_back(LastInst->getOperand(0));
Scott Michelaedc6372008-12-10 00:15:19 +0000246 return false;
247 }
248 // Otherwise, don't know what this is.
249 return true;
250 }
Scott Michel02d711b2008-12-30 23:28:25 +0000251
Scott Michelaedc6372008-12-10 00:15:19 +0000252 // Get the instruction before it if it's a terminator.
253 MachineInstr *SecondLastInst = I;
254
255 // If there are three terminators, we don't know what sort of block this is.
256 if (SecondLastInst && I != MBB.begin() &&
257 isUnpredicatedTerminator(--I))
258 return true;
Scott Michel02d711b2008-12-30 23:28:25 +0000259
Scott Michelaedc6372008-12-10 00:15:19 +0000260 // If the block ends with a conditional and unconditional branch, handle it.
261 if (isCondBranch(SecondLastInst) && isUncondBranch(LastInst)) {
262 TBB = SecondLastInst->getOperand(1).getMBB();
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000263 DEBUG(errs() << "Pushing SecondLastInst: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000264 DEBUG(SecondLastInst->dump());
265 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Scott Michelaedc6372008-12-10 00:15:19 +0000266 Cond.push_back(SecondLastInst->getOperand(0));
Scott Michelaedc6372008-12-10 00:15:19 +0000267 FBB = LastInst->getOperand(0).getMBB();
268 return false;
269 }
Scott Michel02d711b2008-12-30 23:28:25 +0000270
Scott Michelaedc6372008-12-10 00:15:19 +0000271 // If the block ends with two unconditional branches, handle it. The second
272 // one is not executed, so remove it.
273 if (isUncondBranch(SecondLastInst) && isUncondBranch(LastInst)) {
274 TBB = SecondLastInst->getOperand(0).getMBB();
275 I = LastInst;
Evan Chengdc54d312009-02-09 07:14:22 +0000276 if (AllowModify)
277 I->eraseFromParent();
Scott Michelaedc6372008-12-10 00:15:19 +0000278 return false;
279 }
280
281 // Otherwise, can't handle this.
282 return true;
283}
Scott Michel02d711b2008-12-30 23:28:25 +0000284
Kalle Raiskila2d25d242011-02-28 14:08:24 +0000285// search MBB for branch hint labels and branch hit ops
286static void removeHBR( MachineBasicBlock &MBB) {
287 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I){
288 if (I->getOpcode() == SPU::HBRA ||
289 I->getOpcode() == SPU::HBR_LABEL){
290 I=MBB.erase(I);
291 }
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}