blob: 12fae9df8787dfd93cf943426f5e97984e64ab29 [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"
Evan Cheng59ee62d2011-07-11 03:57:24 +000020#include "llvm/MC/MCContext.h"
21#include "llvm/Target/TargetRegistry.h"
Scott Michel9bd7a372009-01-02 20:52:08 +000022#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000023#include "llvm/Support/ErrorHandling.h"
Benjamin Kramer072a56e2009-08-23 11:52:17 +000024#include "llvm/Support/raw_ostream.h"
Scott Michel66377522007-12-04 22:35:58 +000025
Evan Cheng4db3cff2011-07-01 17:57:27 +000026#define GET_INSTRINFO_CTOR
Evan Cheng22fee2d2011-06-28 20:07:07 +000027#define GET_INSTRINFO_MC_DESC
28#include "SPUGenInstrInfo.inc"
29
Scott Michel66377522007-12-04 22:35:58 +000030using namespace llvm;
31
Scott Michelaedc6372008-12-10 00:15:19 +000032namespace {
33 //! Predicate for an unconditional branch instruction
34 inline bool isUncondBranch(const MachineInstr *I) {
35 unsigned opc = I->getOpcode();
36
37 return (opc == SPU::BR
Scott Michel19c10e62009-01-26 03:37:41 +000038 || opc == SPU::BRA
39 || opc == SPU::BI);
Scott Michelaedc6372008-12-10 00:15:19 +000040 }
41
Scott Michel52d00012009-01-03 00:27:53 +000042 //! Predicate for a conditional branch instruction
Scott Michelaedc6372008-12-10 00:15:19 +000043 inline bool isCondBranch(const MachineInstr *I) {
44 unsigned opc = I->getOpcode();
45
Scott Michelf0569be2008-12-27 04:51:36 +000046 return (opc == SPU::BRNZr32
47 || opc == SPU::BRNZv4i32
Scott Michel19c10e62009-01-26 03:37:41 +000048 || opc == SPU::BRZr32
49 || opc == SPU::BRZv4i32
50 || opc == SPU::BRHNZr16
51 || opc == SPU::BRHNZv8i16
52 || opc == SPU::BRHZr16
53 || opc == SPU::BRHZv8i16);
Scott Michelaedc6372008-12-10 00:15:19 +000054 }
55}
56
Scott Michel66377522007-12-04 22:35:58 +000057SPUInstrInfo::SPUInstrInfo(SPUTargetMachine &tm)
Evan Cheng4db3cff2011-07-01 17:57:27 +000058 : SPUGenInstrInfo(SPU::ADJCALLSTACKDOWN, SPU::ADJCALLSTACKUP),
Scott Michel66377522007-12-04 22:35:58 +000059 TM(tm),
60 RI(*TM.getSubtargetImpl(), *this)
Scott Michel52d00012009-01-03 00:27:53 +000061{ /* NOP */ }
Scott Michel66377522007-12-04 22:35:58 +000062
Andrew Trick2da8bc82010-12-24 05:03:26 +000063/// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
64/// this target when scheduling the DAG.
65ScheduleHazardRecognizer *SPUInstrInfo::CreateTargetHazardRecognizer(
66 const TargetMachine *TM,
67 const ScheduleDAG *DAG) const {
68 const TargetInstrInfo *TII = TM->getInstrInfo();
69 assert(TII && "No InstrInfo?");
70 return new SPUHazardRecognizer(*TII);
71}
72
Scott Michel66377522007-12-04 22:35:58 +000073unsigned
Dan Gohmancbad42c2008-11-18 19:49:32 +000074SPUInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
75 int &FrameIndex) const {
Scott Michel66377522007-12-04 22:35:58 +000076 switch (MI->getOpcode()) {
77 default: break;
78 case SPU::LQDv16i8:
79 case SPU::LQDv8i16:
80 case SPU::LQDv4i32:
81 case SPU::LQDv4f32:
82 case SPU::LQDv2f64:
83 case SPU::LQDr128:
84 case SPU::LQDr64:
85 case SPU::LQDr32:
Scott Michelaedc6372008-12-10 00:15:19 +000086 case SPU::LQDr16: {
87 const MachineOperand MOp1 = MI->getOperand(1);
88 const MachineOperand MOp2 = MI->getOperand(2);
Scott Michel52d00012009-01-03 00:27:53 +000089 if (MOp1.isImm() && MOp2.isFI()) {
90 FrameIndex = MOp2.getIndex();
Scott Michelaedc6372008-12-10 00:15:19 +000091 return MI->getOperand(0).getReg();
92 }
93 break;
94 }
Scott Michel66377522007-12-04 22:35:58 +000095 }
96 return 0;
97}
98
99unsigned
Dan Gohmancbad42c2008-11-18 19:49:32 +0000100SPUInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
101 int &FrameIndex) const {
Scott Michel66377522007-12-04 22:35:58 +0000102 switch (MI->getOpcode()) {
103 default: break;
104 case SPU::STQDv16i8:
105 case SPU::STQDv8i16:
106 case SPU::STQDv4i32:
107 case SPU::STQDv4f32:
108 case SPU::STQDv2f64:
109 case SPU::STQDr128:
110 case SPU::STQDr64:
111 case SPU::STQDr32:
112 case SPU::STQDr16:
Scott Michelaedc6372008-12-10 00:15:19 +0000113 case SPU::STQDr8: {
114 const MachineOperand MOp1 = MI->getOperand(1);
115 const MachineOperand MOp2 = MI->getOperand(2);
Scott Michelf0569be2008-12-27 04:51:36 +0000116 if (MOp1.isImm() && MOp2.isFI()) {
117 FrameIndex = MOp2.getIndex();
Scott Michelaedc6372008-12-10 00:15:19 +0000118 return MI->getOperand(0).getReg();
119 }
120 break;
121 }
Scott Michel66377522007-12-04 22:35:58 +0000122 }
123 return 0;
124}
Owen Andersond10fd972007-12-31 06:32:00 +0000125
Jakob Stoklund Olesen377b7b72010-07-11 07:31:03 +0000126void SPUInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
127 MachineBasicBlock::iterator I, DebugLoc DL,
128 unsigned DestReg, unsigned SrcReg,
129 bool KillSrc) const
Owen Andersond10fd972007-12-31 06:32:00 +0000130{
Chris Lattner5e09da22008-03-09 20:31:11 +0000131 // We support cross register class moves for our aliases, such as R3 in any
132 // reg class to any other reg class containing R3. This is required because
133 // we instruction select bitconvert i64 -> f64 as a noop for example, so our
134 // types have no specific meaning.
Scott Michel02d711b2008-12-30 23:28:25 +0000135
Jakob Stoklund Olesen377b7b72010-07-11 07:31:03 +0000136 BuildMI(MBB, I, DL, get(SPU::LRr128), DestReg)
137 .addReg(SrcReg, getKillRegState(KillSrc));
Owen Andersond10fd972007-12-31 06:32:00 +0000138}
Owen Andersonf6372aa2008-01-01 21:11:32 +0000139
140void
141SPUInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Evan Cheng746ad692010-05-06 19:06:44 +0000142 MachineBasicBlock::iterator MI,
143 unsigned SrcReg, bool isKill, int FrameIdx,
144 const TargetRegisterClass *RC,
145 const TargetRegisterInfo *TRI) const
Owen Andersonf6372aa2008-01-01 21:11:32 +0000146{
Chris Lattnercc8cd0c2008-01-07 02:48:55 +0000147 unsigned opc;
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000148 bool isValidFrameIdx = (FrameIdx < SPUFrameLowering::maxFrameOffset());
Owen Andersonf6372aa2008-01-01 21:11:32 +0000149 if (RC == SPU::GPRCRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000150 opc = (isValidFrameIdx ? SPU::STQDr128 : SPU::STQXr128);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000151 } else if (RC == SPU::R64CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000152 opc = (isValidFrameIdx ? SPU::STQDr64 : SPU::STQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000153 } else if (RC == SPU::R64FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000154 opc = (isValidFrameIdx ? SPU::STQDr64 : SPU::STQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000155 } else if (RC == SPU::R32CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000156 opc = (isValidFrameIdx ? SPU::STQDr32 : SPU::STQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000157 } else if (RC == SPU::R32FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000158 opc = (isValidFrameIdx ? SPU::STQDr32 : SPU::STQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000159 } else if (RC == SPU::R16CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000160 opc = (isValidFrameIdx ? SPU::STQDr16 : SPU::STQXr16);
161 } else if (RC == SPU::R8CRegisterClass) {
162 opc = (isValidFrameIdx ? SPU::STQDr8 : SPU::STQXr8);
Scott Michelf0569be2008-12-27 04:51:36 +0000163 } else if (RC == SPU::VECREGRegisterClass) {
164 opc = (isValidFrameIdx) ? SPU::STQDv16i8 : SPU::STQXv16i8;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000165 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000166 llvm_unreachable("Unknown regclass!");
Owen Andersonf6372aa2008-01-01 21:11:32 +0000167 }
168
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000169 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000170 if (MI != MBB.end()) DL = MI->getDebugLoc();
171 addFrameReference(BuildMI(MBB, MI, DL, get(opc))
Bill Wendling587daed2009-05-13 21:33:08 +0000172 .addReg(SrcReg, getKillRegState(isKill)), FrameIdx);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000173}
174
Owen Andersonf6372aa2008-01-01 21:11:32 +0000175void
176SPUInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Evan Cheng746ad692010-05-06 19:06:44 +0000177 MachineBasicBlock::iterator MI,
178 unsigned DestReg, int FrameIdx,
179 const TargetRegisterClass *RC,
180 const TargetRegisterInfo *TRI) const
Owen Andersonf6372aa2008-01-01 21:11:32 +0000181{
Chris Lattnercc8cd0c2008-01-07 02:48:55 +0000182 unsigned opc;
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000183 bool isValidFrameIdx = (FrameIdx < SPUFrameLowering::maxFrameOffset());
Owen Andersonf6372aa2008-01-01 21:11:32 +0000184 if (RC == SPU::GPRCRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000185 opc = (isValidFrameIdx ? SPU::LQDr128 : SPU::LQXr128);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000186 } else if (RC == SPU::R64CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000187 opc = (isValidFrameIdx ? SPU::LQDr64 : SPU::LQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000188 } else if (RC == SPU::R64FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000189 opc = (isValidFrameIdx ? SPU::LQDr64 : SPU::LQXr64);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000190 } else if (RC == SPU::R32CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000191 opc = (isValidFrameIdx ? SPU::LQDr32 : SPU::LQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000192 } else if (RC == SPU::R32FPRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000193 opc = (isValidFrameIdx ? SPU::LQDr32 : SPU::LQXr32);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000194 } else if (RC == SPU::R16CRegisterClass) {
Scott Michelaedc6372008-12-10 00:15:19 +0000195 opc = (isValidFrameIdx ? SPU::LQDr16 : SPU::LQXr16);
196 } else if (RC == SPU::R8CRegisterClass) {
197 opc = (isValidFrameIdx ? SPU::LQDr8 : SPU::LQXr8);
Scott Michelf0569be2008-12-27 04:51:36 +0000198 } else if (RC == SPU::VECREGRegisterClass) {
199 opc = (isValidFrameIdx) ? SPU::LQDv16i8 : SPU::LQXv16i8;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000200 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000201 llvm_unreachable("Unknown regclass in loadRegFromStackSlot!");
Owen Andersonf6372aa2008-01-01 21:11:32 +0000202 }
203
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000204 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000205 if (MI != MBB.end()) DL = MI->getDebugLoc();
Jakob Stoklund Olesenf2c3f6a2009-05-16 07:25:44 +0000206 addFrameReference(BuildMI(MBB, MI, DL, get(opc), DestReg), FrameIdx);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000207}
208
Scott Michelaedc6372008-12-10 00:15:19 +0000209//! Branch analysis
Scott Michel9bd7a372009-01-02 20:52:08 +0000210/*!
Scott Michelaedc6372008-12-10 00:15:19 +0000211 \note This code was kiped from PPC. There may be more branch analysis for
212 CellSPU than what's currently done here.
213 */
214bool
215SPUInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
Scott Michel19c10e62009-01-26 03:37:41 +0000216 MachineBasicBlock *&FBB,
Evan Chengdc54d312009-02-09 07:14:22 +0000217 SmallVectorImpl<MachineOperand> &Cond,
218 bool AllowModify) const {
Scott Michelaedc6372008-12-10 00:15:19 +0000219 // If the block has no terminators, it just falls into the block after it.
220 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000221 if (I == MBB.begin())
222 return false;
223 --I;
224 while (I->isDebugValue()) {
225 if (I == MBB.begin())
226 return false;
227 --I;
228 }
229 if (!isUnpredicatedTerminator(I))
Scott Michelaedc6372008-12-10 00:15:19 +0000230 return false;
231
232 // Get the last instruction in the block.
233 MachineInstr *LastInst = I;
Scott Michel02d711b2008-12-30 23:28:25 +0000234
Scott Michelaedc6372008-12-10 00:15:19 +0000235 // If there is only one terminator instruction, process it.
236 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
237 if (isUncondBranch(LastInst)) {
Kalle Raiskila2320a442010-05-11 11:00:02 +0000238 // Check for jump tables
239 if (!LastInst->getOperand(0).isMBB())
240 return true;
Scott Michelaedc6372008-12-10 00:15:19 +0000241 TBB = LastInst->getOperand(0).getMBB();
242 return false;
243 } else if (isCondBranch(LastInst)) {
244 // Block ends with fall-through condbranch.
245 TBB = LastInst->getOperand(1).getMBB();
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000246 DEBUG(errs() << "Pushing LastInst: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000247 DEBUG(LastInst->dump());
248 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Scott Michelaedc6372008-12-10 00:15:19 +0000249 Cond.push_back(LastInst->getOperand(0));
Scott Michelaedc6372008-12-10 00:15:19 +0000250 return false;
251 }
252 // Otherwise, don't know what this is.
253 return true;
254 }
Scott Michel02d711b2008-12-30 23:28:25 +0000255
Scott Michelaedc6372008-12-10 00:15:19 +0000256 // Get the instruction before it if it's a terminator.
257 MachineInstr *SecondLastInst = I;
258
259 // If there are three terminators, we don't know what sort of block this is.
260 if (SecondLastInst && I != MBB.begin() &&
261 isUnpredicatedTerminator(--I))
262 return true;
Scott Michel02d711b2008-12-30 23:28:25 +0000263
Scott Michelaedc6372008-12-10 00:15:19 +0000264 // If the block ends with a conditional and unconditional branch, handle it.
265 if (isCondBranch(SecondLastInst) && isUncondBranch(LastInst)) {
266 TBB = SecondLastInst->getOperand(1).getMBB();
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000267 DEBUG(errs() << "Pushing SecondLastInst: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000268 DEBUG(SecondLastInst->dump());
269 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Scott Michelaedc6372008-12-10 00:15:19 +0000270 Cond.push_back(SecondLastInst->getOperand(0));
Scott Michelaedc6372008-12-10 00:15:19 +0000271 FBB = LastInst->getOperand(0).getMBB();
272 return false;
273 }
Scott Michel02d711b2008-12-30 23:28:25 +0000274
Scott Michelaedc6372008-12-10 00:15:19 +0000275 // If the block ends with two unconditional branches, handle it. The second
276 // one is not executed, so remove it.
277 if (isUncondBranch(SecondLastInst) && isUncondBranch(LastInst)) {
278 TBB = SecondLastInst->getOperand(0).getMBB();
279 I = LastInst;
Evan Chengdc54d312009-02-09 07:14:22 +0000280 if (AllowModify)
281 I->eraseFromParent();
Scott Michelaedc6372008-12-10 00:15:19 +0000282 return false;
283 }
284
285 // Otherwise, can't handle this.
286 return true;
287}
Scott Michel02d711b2008-12-30 23:28:25 +0000288
Kalle Raiskila2d25d242011-02-28 14:08:24 +0000289// search MBB for branch hint labels and branch hit ops
290static void removeHBR( MachineBasicBlock &MBB) {
291 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I){
292 if (I->getOpcode() == SPU::HBRA ||
293 I->getOpcode() == SPU::HBR_LABEL){
294 I=MBB.erase(I);
295 }
296 }
297}
298
Scott Michelaedc6372008-12-10 00:15:19 +0000299unsigned
300SPUInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
301 MachineBasicBlock::iterator I = MBB.end();
Kalle Raiskila2d25d242011-02-28 14:08:24 +0000302 removeHBR(MBB);
Scott Michelaedc6372008-12-10 00:15:19 +0000303 if (I == MBB.begin())
304 return 0;
305 --I;
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000306 while (I->isDebugValue()) {
307 if (I == MBB.begin())
308 return 0;
309 --I;
310 }
Scott Michelaedc6372008-12-10 00:15:19 +0000311 if (!isCondBranch(I) && !isUncondBranch(I))
312 return 0;
313
314 // Remove the first branch.
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000315 DEBUG(errs() << "Removing branch: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000316 DEBUG(I->dump());
Scott Michelaedc6372008-12-10 00:15:19 +0000317 I->eraseFromParent();
318 I = MBB.end();
319 if (I == MBB.begin())
320 return 1;
321
322 --I;
Scott Michel9bd7a372009-01-02 20:52:08 +0000323 if (!(isCondBranch(I) || isUncondBranch(I)))
Scott Michelaedc6372008-12-10 00:15:19 +0000324 return 1;
325
326 // Remove the second branch.
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000327 DEBUG(errs() << "Removing second branch: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000328 DEBUG(I->dump());
Scott Michelaedc6372008-12-10 00:15:19 +0000329 I->eraseFromParent();
330 return 2;
331}
Scott Michel02d711b2008-12-30 23:28:25 +0000332
Kalle Raiskila2d25d242011-02-28 14:08:24 +0000333/** Find the optimal position for a hint branch instruction in a basic block.
334 * This should take into account:
335 * -the branch hint delays
336 * -congestion of the memory bus
337 * -dual-issue scheduling (i.e. avoid insertion of nops)
338 * Current implementation is rather simplistic.
339 */
340static MachineBasicBlock::iterator findHBRPosition(MachineBasicBlock &MBB)
341{
342 MachineBasicBlock::iterator J = MBB.end();
343 for( int i=0; i<8; i++) {
344 if( J == MBB.begin() ) return J;
345 J--;
346 }
347 return J;
348}
349
Scott Michelaedc6372008-12-10 00:15:19 +0000350unsigned
351SPUInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Scott Michel19c10e62009-01-26 03:37:41 +0000352 MachineBasicBlock *FBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000353 const SmallVectorImpl<MachineOperand> &Cond,
354 DebugLoc DL) const {
Scott Michelaedc6372008-12-10 00:15:19 +0000355 // Shouldn't be a fall through.
356 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Scott Michel02d711b2008-12-30 23:28:25 +0000357 assert((Cond.size() == 2 || Cond.size() == 0) &&
Scott Michelaedc6372008-12-10 00:15:19 +0000358 "SPU branch conditions have two components!");
Scott Michel02d711b2008-12-30 23:28:25 +0000359
Kalle Raiskila2d25d242011-02-28 14:08:24 +0000360 MachineInstrBuilder MIB;
361 //TODO: make a more accurate algorithm.
362 bool haveHBR = MBB.size()>8;
363
364 removeHBR(MBB);
365 MCSymbol *branchLabel = MBB.getParent()->getContext().CreateTempSymbol();
366 // Add a label just before the branch
367 if (haveHBR)
368 MIB = BuildMI(&MBB, DL, get(SPU::HBR_LABEL)).addSym(branchLabel);
369
Scott Michelaedc6372008-12-10 00:15:19 +0000370 // One-way branch.
371 if (FBB == 0) {
Scott Michel9bd7a372009-01-02 20:52:08 +0000372 if (Cond.empty()) {
373 // Unconditional branch
Kalle Raiskila2d25d242011-02-28 14:08:24 +0000374 MIB = BuildMI(&MBB, DL, get(SPU::BR));
Scott Michel9bd7a372009-01-02 20:52:08 +0000375 MIB.addMBB(TBB);
376
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000377 DEBUG(errs() << "Inserted one-way uncond branch: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000378 DEBUG((*MIB).dump());
Kalle Raiskila2d25d242011-02-28 14:08:24 +0000379
380 // basic blocks have just one branch so it is safe to add the hint a its
381 if (haveHBR) {
382 MIB = BuildMI( MBB, findHBRPosition(MBB), DL, get(SPU::HBRA));
383 MIB.addSym(branchLabel);
384 MIB.addMBB(TBB);
385 }
Scott Michel9bd7a372009-01-02 20:52:08 +0000386 } else {
387 // Conditional branch
Kalle Raiskila2d25d242011-02-28 14:08:24 +0000388 MIB = BuildMI(&MBB, DL, get(Cond[0].getImm()));
Scott Michel9bd7a372009-01-02 20:52:08 +0000389 MIB.addReg(Cond[1].getReg()).addMBB(TBB);
390
Kalle Raiskila2d25d242011-02-28 14:08:24 +0000391 if (haveHBR) {
392 MIB = BuildMI(MBB, findHBRPosition(MBB), DL, get(SPU::HBRA));
393 MIB.addSym(branchLabel);
394 MIB.addMBB(TBB);
395 }
396
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000397 DEBUG(errs() << "Inserted one-way cond branch: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000398 DEBUG((*MIB).dump());
Scott Michelaedc6372008-12-10 00:15:19 +0000399 }
400 return 1;
Scott Michel9bd7a372009-01-02 20:52:08 +0000401 } else {
Kalle Raiskila2d25d242011-02-28 14:08:24 +0000402 MIB = BuildMI(&MBB, DL, get(Cond[0].getImm()));
Stuart Hastings3bf91252010-06-17 22:43:56 +0000403 MachineInstrBuilder MIB2 = BuildMI(&MBB, DL, get(SPU::BR));
Scott Michel9bd7a372009-01-02 20:52:08 +0000404
405 // Two-way Conditional Branch.
406 MIB.addReg(Cond[1].getReg()).addMBB(TBB);
407 MIB2.addMBB(FBB);
408
Kalle Raiskila2d25d242011-02-28 14:08:24 +0000409 if (haveHBR) {
410 MIB = BuildMI( MBB, findHBRPosition(MBB), DL, get(SPU::HBRA));
411 MIB.addSym(branchLabel);
412 MIB.addMBB(FBB);
413 }
414
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000415 DEBUG(errs() << "Inserted conditional branch: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000416 DEBUG((*MIB).dump());
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000417 DEBUG(errs() << "part 2: ");
Scott Michel9bd7a372009-01-02 20:52:08 +0000418 DEBUG((*MIB2).dump());
419 return 2;
Scott Michelaedc6372008-12-10 00:15:19 +0000420 }
Scott Michelaedc6372008-12-10 00:15:19 +0000421}
422
Scott Michel52d00012009-01-03 00:27:53 +0000423//! Reverses a branch's condition, returning false on success.
424bool
425SPUInstrInfo::ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond)
426 const {
427 // Pretty brainless way of inverting the condition, but it works, considering
428 // there are only two conditions...
429 static struct {
430 unsigned Opc; //! The incoming opcode
431 unsigned RevCondOpc; //! The reversed condition opcode
432 } revconds[] = {
433 { SPU::BRNZr32, SPU::BRZr32 },
434 { SPU::BRNZv4i32, SPU::BRZv4i32 },
435 { SPU::BRZr32, SPU::BRNZr32 },
436 { SPU::BRZv4i32, SPU::BRNZv4i32 },
437 { SPU::BRHNZr16, SPU::BRHZr16 },
438 { SPU::BRHNZv8i16, SPU::BRHZv8i16 },
439 { SPU::BRHZr16, SPU::BRHNZr16 },
440 { SPU::BRHZv8i16, SPU::BRHNZv8i16 }
441 };
Scott Michelaedc6372008-12-10 00:15:19 +0000442
Scott Michel52d00012009-01-03 00:27:53 +0000443 unsigned Opc = unsigned(Cond[0].getImm());
444 // Pretty dull mapping between the two conditions that SPU can generate:
Misha Brukman93c65c82009-01-07 23:07:29 +0000445 for (int i = sizeof(revconds)/sizeof(revconds[0]) - 1; i >= 0; --i) {
Scott Michel52d00012009-01-03 00:27:53 +0000446 if (revconds[i].Opc == Opc) {
447 Cond[0].setImm(revconds[i].RevCondOpc);
448 return false;
449 }
450 }
451
452 return true;
453}
Evan Cheng59ee62d2011-07-11 03:57:24 +0000454
455MCInstrInfo *createSPUMCInstrInfo() {
456 MCInstrInfo *X = new MCInstrInfo();
457 InitSPUMCInstrInfo(X);
458 return X;
459}
460
461extern "C" void LLVMInitializeCellSPUMCInstrInfo() {
462 TargetRegistry::RegisterMCInstrInfo(TheCellSPUTarget, createSPUMCInstrInfo);
463}