blob: 93b6d4c55691d692518e9f522266a7b61ef6cddf [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"
Andrew Trick2da8bc82010-12-24 05:03:26 +000018#include "SPUHazardRecognizers.h"
Scott Michel66377522007-12-04 22:35:58 +000019#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"
Kalle Raiskila2d25d242011-02-28 14:08:24 +000023#include "llvm/MC/MCContext.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#define GET_INSTRINFO_MC_DESC
27#include "SPUGenInstrInfo.inc"
28
Scott Michel66377522007-12-04 22:35:58 +000029using namespace llvm;
30
Scott Michelaedc6372008-12-10 00:15:19 +000031namespace {
32 //! Predicate for an unconditional branch instruction
33 inline bool isUncondBranch(const MachineInstr *I) {
34 unsigned opc = I->getOpcode();
35
36 return (opc == SPU::BR
Scott Michel19c10e62009-01-26 03:37:41 +000037 || opc == SPU::BRA
38 || opc == SPU::BI);
Scott Michelaedc6372008-12-10 00:15:19 +000039 }
40
Scott Michel52d00012009-01-03 00:27:53 +000041 //! Predicate for a conditional branch instruction
Scott Michelaedc6372008-12-10 00:15:19 +000042 inline bool isCondBranch(const MachineInstr *I) {
43 unsigned opc = I->getOpcode();
44
Scott Michelf0569be2008-12-27 04:51:36 +000045 return (opc == SPU::BRNZr32
46 || opc == SPU::BRNZv4i32
Scott Michel19c10e62009-01-26 03:37:41 +000047 || opc == SPU::BRZr32
48 || opc == SPU::BRZv4i32
49 || opc == SPU::BRHNZr16
50 || opc == SPU::BRHNZv8i16
51 || opc == SPU::BRHZr16
52 || opc == SPU::BRHZv8i16);
Scott Michelaedc6372008-12-10 00:15:19 +000053 }
54}
55
Scott Michel66377522007-12-04 22:35:58 +000056SPUInstrInfo::SPUInstrInfo(SPUTargetMachine &tm)
Evan Cheng4db3cff2011-07-01 17:57:27 +000057 : SPUGenInstrInfo(SPU::ADJCALLSTACKDOWN, SPU::ADJCALLSTACKUP),
Scott Michel66377522007-12-04 22:35:58 +000058 TM(tm),
59 RI(*TM.getSubtargetImpl(), *this)
Scott Michel52d00012009-01-03 00:27:53 +000060{ /* NOP */ }
Scott Michel66377522007-12-04 22:35:58 +000061
Andrew Trick2da8bc82010-12-24 05:03:26 +000062/// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
63/// this target when scheduling the DAG.
64ScheduleHazardRecognizer *SPUInstrInfo::CreateTargetHazardRecognizer(
65 const TargetMachine *TM,
66 const ScheduleDAG *DAG) const {
67 const TargetInstrInfo *TII = TM->getInstrInfo();
68 assert(TII && "No InstrInfo?");
69 return new SPUHazardRecognizer(*TII);
70}
71
Scott Michel66377522007-12-04 22:35:58 +000072unsigned
Dan Gohmancbad42c2008-11-18 19:49:32 +000073SPUInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
74 int &FrameIndex) const {
Scott Michel66377522007-12-04 22:35:58 +000075 switch (MI->getOpcode()) {
76 default: break;
77 case SPU::LQDv16i8:
78 case SPU::LQDv8i16:
79 case SPU::LQDv4i32:
80 case SPU::LQDv4f32:
81 case SPU::LQDv2f64:
82 case SPU::LQDr128:
83 case SPU::LQDr64:
84 case SPU::LQDr32:
Scott Michelaedc6372008-12-10 00:15:19 +000085 case SPU::LQDr16: {
86 const MachineOperand MOp1 = MI->getOperand(1);
87 const MachineOperand MOp2 = MI->getOperand(2);
Scott Michel52d00012009-01-03 00:27:53 +000088 if (MOp1.isImm() && MOp2.isFI()) {
89 FrameIndex = MOp2.getIndex();
Scott Michelaedc6372008-12-10 00:15:19 +000090 return MI->getOperand(0).getReg();
91 }
92 break;
93 }
Scott Michel66377522007-12-04 22:35:58 +000094 }
95 return 0;
96}
97
98unsigned
Dan Gohmancbad42c2008-11-18 19:49:32 +000099SPUInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
100 int &FrameIndex) const {
Scott Michel66377522007-12-04 22:35:58 +0000101 switch (MI->getOpcode()) {
102 default: break;
103 case SPU::STQDv16i8:
104 case SPU::STQDv8i16:
105 case SPU::STQDv4i32:
106 case SPU::STQDv4f32:
107 case SPU::STQDv2f64:
108 case SPU::STQDr128:
109 case SPU::STQDr64:
110 case SPU::STQDr32:
111 case SPU::STQDr16:
Scott Michelaedc6372008-12-10 00:15:19 +0000112 case SPU::STQDr8: {
113 const MachineOperand MOp1 = MI->getOperand(1);
114 const MachineOperand MOp2 = MI->getOperand(2);
Scott Michelf0569be2008-12-27 04:51:36 +0000115 if (MOp1.isImm() && MOp2.isFI()) {
116 FrameIndex = MOp2.getIndex();
Scott Michelaedc6372008-12-10 00:15:19 +0000117 return MI->getOperand(0).getReg();
118 }
119 break;
120 }
Scott Michel66377522007-12-04 22:35:58 +0000121 }
122 return 0;
123}
Owen Andersond10fd972007-12-31 06:32:00 +0000124
Jakob Stoklund Olesen377b7b72010-07-11 07:31:03 +0000125void SPUInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
126 MachineBasicBlock::iterator I, DebugLoc DL,
127 unsigned DestReg, unsigned SrcReg,
128 bool KillSrc) const
Owen Andersond10fd972007-12-31 06:32:00 +0000129{
Chris Lattner5e09da22008-03-09 20:31:11 +0000130 // We support cross register class moves for our aliases, such as R3 in any
131 // reg class to any other reg class containing R3. This is required because
132 // we instruction select bitconvert i64 -> f64 as a noop for example, so our
133 // types have no specific meaning.
Scott Michel02d711b2008-12-30 23:28:25 +0000134
Jakob Stoklund Olesen377b7b72010-07-11 07:31:03 +0000135 BuildMI(MBB, I, DL, get(SPU::LRr128), DestReg)
136 .addReg(SrcReg, getKillRegState(KillSrc));
Owen Andersond10fd972007-12-31 06:32:00 +0000137}
Owen Andersonf6372aa2008-01-01 21:11:32 +0000138
139void
140SPUInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Evan Cheng746ad692010-05-06 19:06:44 +0000141 MachineBasicBlock::iterator MI,
142 unsigned SrcReg, bool isKill, int FrameIdx,
143 const TargetRegisterClass *RC,
144 const TargetRegisterInfo *TRI) const
Owen Andersonf6372aa2008-01-01 21:11:32 +0000145{
Chris Lattnercc8cd0c2008-01-07 02:48:55 +0000146 unsigned opc;
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000147 bool isValidFrameIdx = (FrameIdx < SPUFrameLowering::maxFrameOffset());
Owen Andersonf6372aa2008-01-01 21:11:32 +0000148 if (RC == SPU::GPRCRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000149 opc = (isValidFrameIdx ? SPU::STQDr128 : SPU::STQXr128);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000150 } else if (RC == SPU::R64CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000151 opc = (isValidFrameIdx ? SPU::STQDr64 : SPU::STQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000152 } else if (RC == SPU::R64FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000153 opc = (isValidFrameIdx ? SPU::STQDr64 : SPU::STQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000154 } else if (RC == SPU::R32CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000155 opc = (isValidFrameIdx ? SPU::STQDr32 : SPU::STQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000156 } else if (RC == SPU::R32FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000157 opc = (isValidFrameIdx ? SPU::STQDr32 : SPU::STQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000158 } else if (RC == SPU::R16CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000159 opc = (isValidFrameIdx ? SPU::STQDr16 : SPU::STQXr16);
160 } else if (RC == SPU::R8CRegisterClass) {
161 opc = (isValidFrameIdx ? SPU::STQDr8 : SPU::STQXr8);
Scott Michelf0569be2008-12-27 04:51:36 +0000162 } else if (RC == SPU::VECREGRegisterClass) {
163 opc = (isValidFrameIdx) ? SPU::STQDv16i8 : SPU::STQXv16i8;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000164 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000165 llvm_unreachable("Unknown regclass!");
Owen Andersonf6372aa2008-01-01 21:11:32 +0000166 }
167
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000168 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000169 if (MI != MBB.end()) DL = MI->getDebugLoc();
170 addFrameReference(BuildMI(MBB, MI, DL, get(opc))
Bill Wendling587daed2009-05-13 21:33:08 +0000171 .addReg(SrcReg, getKillRegState(isKill)), FrameIdx);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000172}
173
Owen Andersonf6372aa2008-01-01 21:11:32 +0000174void
175SPUInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Evan Cheng746ad692010-05-06 19:06:44 +0000176 MachineBasicBlock::iterator MI,
177 unsigned DestReg, int FrameIdx,
178 const TargetRegisterClass *RC,
179 const TargetRegisterInfo *TRI) const
Owen Andersonf6372aa2008-01-01 21:11:32 +0000180{
Chris Lattnercc8cd0c2008-01-07 02:48:55 +0000181 unsigned opc;
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000182 bool isValidFrameIdx = (FrameIdx < SPUFrameLowering::maxFrameOffset());
Owen Andersonf6372aa2008-01-01 21:11:32 +0000183 if (RC == SPU::GPRCRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000184 opc = (isValidFrameIdx ? SPU::LQDr128 : SPU::LQXr128);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000185 } else if (RC == SPU::R64CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000186 opc = (isValidFrameIdx ? SPU::LQDr64 : SPU::LQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000187 } else if (RC == SPU::R64FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000188 opc = (isValidFrameIdx ? SPU::LQDr64 : SPU::LQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000189 } else if (RC == SPU::R32CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000190 opc = (isValidFrameIdx ? SPU::LQDr32 : SPU::LQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000191 } else if (RC == SPU::R32FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000192 opc = (isValidFrameIdx ? SPU::LQDr32 : SPU::LQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000193 } else if (RC == SPU::R16CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000194 opc = (isValidFrameIdx ? SPU::LQDr16 : SPU::LQXr16);
195 } else if (RC == SPU::R8CRegisterClass) {
196 opc = (isValidFrameIdx ? SPU::LQDr8 : SPU::LQXr8);
Scott Michelf0569be2008-12-27 04:51:36 +0000197 } else if (RC == SPU::VECREGRegisterClass) {
198 opc = (isValidFrameIdx) ? SPU::LQDv16i8 : SPU::LQXv16i8;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000199 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000200 llvm_unreachable("Unknown regclass in loadRegFromStackSlot!");
Owen Andersonf6372aa2008-01-01 21:11:32 +0000201 }
202
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000203 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000204 if (MI != MBB.end()) DL = MI->getDebugLoc();
Jakob Stoklund Olesenf2c3f6a2009-05-16 07:25:44 +0000205 addFrameReference(BuildMI(MBB, MI, DL, get(opc), DestReg), FrameIdx);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000206}
207
Scott Michelaedc6372008-12-10 00:15:19 +0000208//! Branch analysis
Scott Michel9bd7a372009-01-02 20:52:08 +0000209/*!
Scott Michelaedc6372008-12-10 00:15:19 +0000210 \note This code was kiped from PPC. There may be more branch analysis for
211 CellSPU than what's currently done here.
212 */
213bool
214SPUInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
Scott Michel19c10e62009-01-26 03:37:41 +0000215 MachineBasicBlock *&FBB,
Evan Chengdc54d312009-02-09 07:14:22 +0000216 SmallVectorImpl<MachineOperand> &Cond,
217 bool AllowModify) const {
Scott Michelaedc6372008-12-10 00:15:19 +0000218 // If the block has no terminators, it just falls into the block after it.
219 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000220 if (I == MBB.begin())
221 return false;
222 --I;
223 while (I->isDebugValue()) {
224 if (I == MBB.begin())
225 return false;
226 --I;
227 }
228 if (!isUnpredicatedTerminator(I))
Scott Michelaedc6372008-12-10 00:15:19 +0000229 return false;
230
231 // Get the last instruction in the block.
232 MachineInstr *LastInst = I;
Scott Michel02d711b2008-12-30 23:28:25 +0000233
Scott Michelaedc6372008-12-10 00:15:19 +0000234 // If there is only one terminator instruction, process it.
235 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
236 if (isUncondBranch(LastInst)) {
Kalle Raiskila2320a442010-05-11 11:00:02 +0000237 // Check for jump tables
238 if (!LastInst->getOperand(0).isMBB())
239 return true;
Scott Michelaedc6372008-12-10 00:15:19 +0000240 TBB = LastInst->getOperand(0).getMBB();
241 return false;
242 } else if (isCondBranch(LastInst)) {
243 // Block ends with fall-through condbranch.
244 TBB = LastInst->getOperand(1).getMBB();
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000245 DEBUG(errs() << "Pushing LastInst: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000246 DEBUG(LastInst->dump());
247 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Scott Michelaedc6372008-12-10 00:15:19 +0000248 Cond.push_back(LastInst->getOperand(0));
Scott Michelaedc6372008-12-10 00:15:19 +0000249 return false;
250 }
251 // Otherwise, don't know what this is.
252 return true;
253 }
Scott Michel02d711b2008-12-30 23:28:25 +0000254
Scott Michelaedc6372008-12-10 00:15:19 +0000255 // Get the instruction before it if it's a terminator.
256 MachineInstr *SecondLastInst = I;
257
258 // If there are three terminators, we don't know what sort of block this is.
259 if (SecondLastInst && I != MBB.begin() &&
260 isUnpredicatedTerminator(--I))
261 return true;
Scott Michel02d711b2008-12-30 23:28:25 +0000262
Scott Michelaedc6372008-12-10 00:15:19 +0000263 // If the block ends with a conditional and unconditional branch, handle it.
264 if (isCondBranch(SecondLastInst) && isUncondBranch(LastInst)) {
265 TBB = SecondLastInst->getOperand(1).getMBB();
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000266 DEBUG(errs() << "Pushing SecondLastInst: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000267 DEBUG(SecondLastInst->dump());
268 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Scott Michelaedc6372008-12-10 00:15:19 +0000269 Cond.push_back(SecondLastInst->getOperand(0));
Scott Michelaedc6372008-12-10 00:15:19 +0000270 FBB = LastInst->getOperand(0).getMBB();
271 return false;
272 }
Scott Michel02d711b2008-12-30 23:28:25 +0000273
Scott Michelaedc6372008-12-10 00:15:19 +0000274 // If the block ends with two unconditional branches, handle it. The second
275 // one is not executed, so remove it.
276 if (isUncondBranch(SecondLastInst) && isUncondBranch(LastInst)) {
277 TBB = SecondLastInst->getOperand(0).getMBB();
278 I = LastInst;
Evan Chengdc54d312009-02-09 07:14:22 +0000279 if (AllowModify)
280 I->eraseFromParent();
Scott Michelaedc6372008-12-10 00:15:19 +0000281 return false;
282 }
283
284 // Otherwise, can't handle this.
285 return true;
286}
Scott Michel02d711b2008-12-30 23:28:25 +0000287
Kalle Raiskila2d25d242011-02-28 14:08:24 +0000288// search MBB for branch hint labels and branch hit ops
289static void removeHBR( MachineBasicBlock &MBB) {
290 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I){
291 if (I->getOpcode() == SPU::HBRA ||
292 I->getOpcode() == SPU::HBR_LABEL){
293 I=MBB.erase(I);
294 }
295 }
296}
297
Scott Michelaedc6372008-12-10 00:15:19 +0000298unsigned
299SPUInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
300 MachineBasicBlock::iterator I = MBB.end();
Kalle Raiskila2d25d242011-02-28 14:08:24 +0000301 removeHBR(MBB);
Scott Michelaedc6372008-12-10 00:15:19 +0000302 if (I == MBB.begin())
303 return 0;
304 --I;
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000305 while (I->isDebugValue()) {
306 if (I == MBB.begin())
307 return 0;
308 --I;
309 }
Scott Michelaedc6372008-12-10 00:15:19 +0000310 if (!isCondBranch(I) && !isUncondBranch(I))
311 return 0;
312
313 // Remove the first branch.
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000314 DEBUG(errs() << "Removing branch: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000315 DEBUG(I->dump());
Scott Michelaedc6372008-12-10 00:15:19 +0000316 I->eraseFromParent();
317 I = MBB.end();
318 if (I == MBB.begin())
319 return 1;
320
321 --I;
Scott Michel9bd7a372009-01-02 20:52:08 +0000322 if (!(isCondBranch(I) || isUncondBranch(I)))
Scott Michelaedc6372008-12-10 00:15:19 +0000323 return 1;
324
325 // Remove the second branch.
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000326 DEBUG(errs() << "Removing second branch: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000327 DEBUG(I->dump());
Scott Michelaedc6372008-12-10 00:15:19 +0000328 I->eraseFromParent();
329 return 2;
330}
Scott Michel02d711b2008-12-30 23:28:25 +0000331
Kalle Raiskila2d25d242011-02-28 14:08:24 +0000332/** Find the optimal position for a hint branch instruction in a basic block.
333 * This should take into account:
334 * -the branch hint delays
335 * -congestion of the memory bus
336 * -dual-issue scheduling (i.e. avoid insertion of nops)
337 * Current implementation is rather simplistic.
338 */
339static MachineBasicBlock::iterator findHBRPosition(MachineBasicBlock &MBB)
340{
341 MachineBasicBlock::iterator J = MBB.end();
342 for( int i=0; i<8; i++) {
343 if( J == MBB.begin() ) return J;
344 J--;
345 }
346 return J;
347}
348
Scott Michelaedc6372008-12-10 00:15:19 +0000349unsigned
350SPUInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Scott Michel19c10e62009-01-26 03:37:41 +0000351 MachineBasicBlock *FBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000352 const SmallVectorImpl<MachineOperand> &Cond,
353 DebugLoc DL) const {
Scott Michelaedc6372008-12-10 00:15:19 +0000354 // Shouldn't be a fall through.
355 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Scott Michel02d711b2008-12-30 23:28:25 +0000356 assert((Cond.size() == 2 || Cond.size() == 0) &&
Scott Michelaedc6372008-12-10 00:15:19 +0000357 "SPU branch conditions have two components!");
Scott Michel02d711b2008-12-30 23:28:25 +0000358
Kalle Raiskila2d25d242011-02-28 14:08:24 +0000359 MachineInstrBuilder MIB;
360 //TODO: make a more accurate algorithm.
361 bool haveHBR = MBB.size()>8;
362
363 removeHBR(MBB);
364 MCSymbol *branchLabel = MBB.getParent()->getContext().CreateTempSymbol();
365 // Add a label just before the branch
366 if (haveHBR)
367 MIB = BuildMI(&MBB, DL, get(SPU::HBR_LABEL)).addSym(branchLabel);
368
Scott Michelaedc6372008-12-10 00:15:19 +0000369 // One-way branch.
370 if (FBB == 0) {
Scott Michel9bd7a372009-01-02 20:52:08 +0000371 if (Cond.empty()) {
372 // Unconditional branch
Kalle Raiskila2d25d242011-02-28 14:08:24 +0000373 MIB = BuildMI(&MBB, DL, get(SPU::BR));
Scott Michel9bd7a372009-01-02 20:52:08 +0000374 MIB.addMBB(TBB);
375
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000376 DEBUG(errs() << "Inserted one-way uncond branch: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000377 DEBUG((*MIB).dump());
Kalle Raiskila2d25d242011-02-28 14:08:24 +0000378
379 // basic blocks have just one branch so it is safe to add the hint a its
380 if (haveHBR) {
381 MIB = BuildMI( MBB, findHBRPosition(MBB), DL, get(SPU::HBRA));
382 MIB.addSym(branchLabel);
383 MIB.addMBB(TBB);
384 }
Scott Michel9bd7a372009-01-02 20:52:08 +0000385 } else {
386 // Conditional branch
Kalle Raiskila2d25d242011-02-28 14:08:24 +0000387 MIB = BuildMI(&MBB, DL, get(Cond[0].getImm()));
Scott Michel9bd7a372009-01-02 20:52:08 +0000388 MIB.addReg(Cond[1].getReg()).addMBB(TBB);
389
Kalle Raiskila2d25d242011-02-28 14:08:24 +0000390 if (haveHBR) {
391 MIB = BuildMI(MBB, findHBRPosition(MBB), DL, get(SPU::HBRA));
392 MIB.addSym(branchLabel);
393 MIB.addMBB(TBB);
394 }
395
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000396 DEBUG(errs() << "Inserted one-way cond branch: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000397 DEBUG((*MIB).dump());
Scott Michelaedc6372008-12-10 00:15:19 +0000398 }
399 return 1;
Scott Michel9bd7a372009-01-02 20:52:08 +0000400 } else {
Kalle Raiskila2d25d242011-02-28 14:08:24 +0000401 MIB = BuildMI(&MBB, DL, get(Cond[0].getImm()));
Stuart Hastings3bf91252010-06-17 22:43:56 +0000402 MachineInstrBuilder MIB2 = BuildMI(&MBB, DL, get(SPU::BR));
Scott Michel9bd7a372009-01-02 20:52:08 +0000403
404 // Two-way Conditional Branch.
405 MIB.addReg(Cond[1].getReg()).addMBB(TBB);
406 MIB2.addMBB(FBB);
407
Kalle Raiskila2d25d242011-02-28 14:08:24 +0000408 if (haveHBR) {
409 MIB = BuildMI( MBB, findHBRPosition(MBB), DL, get(SPU::HBRA));
410 MIB.addSym(branchLabel);
411 MIB.addMBB(FBB);
412 }
413
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000414 DEBUG(errs() << "Inserted conditional branch: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000415 DEBUG((*MIB).dump());
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000416 DEBUG(errs() << "part 2: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000417 DEBUG((*MIB2).dump());
418 return 2;
Scott Michelaedc6372008-12-10 00:15:19 +0000419 }
Scott Michelaedc6372008-12-10 00:15:19 +0000420}
421
Scott Michel52d00012009-01-03 00:27:53 +0000422//! Reverses a branch's condition, returning false on success.
423bool
424SPUInstrInfo::ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond)
425 const {
426 // Pretty brainless way of inverting the condition, but it works, considering
427 // there are only two conditions...
428 static struct {
429 unsigned Opc; //! The incoming opcode
430 unsigned RevCondOpc; //! The reversed condition opcode
431 } revconds[] = {
432 { SPU::BRNZr32, SPU::BRZr32 },
433 { SPU::BRNZv4i32, SPU::BRZv4i32 },
434 { SPU::BRZr32, SPU::BRNZr32 },
435 { SPU::BRZv4i32, SPU::BRNZv4i32 },
436 { SPU::BRHNZr16, SPU::BRHZr16 },
437 { SPU::BRHNZv8i16, SPU::BRHZv8i16 },
438 { SPU::BRHZr16, SPU::BRHNZr16 },
439 { SPU::BRHZv8i16, SPU::BRHNZv8i16 }
440 };
Scott Michelaedc6372008-12-10 00:15:19 +0000441
Scott Michel52d00012009-01-03 00:27:53 +0000442 unsigned Opc = unsigned(Cond[0].getImm());
443 // Pretty dull mapping between the two conditions that SPU can generate:
Misha Brukman93c65c82009-01-07 23:07:29 +0000444 for (int i = sizeof(revconds)/sizeof(revconds[0]) - 1; i >= 0; --i) {
Scott Michel52d00012009-01-03 00:27:53 +0000445 if (revconds[i].Opc == Opc) {
446 Cond[0].setImm(revconds[i].RevCondOpc);
447 return false;
448 }
449 }
450
451 return true;
452}