1 //===- SPUInstrInfo.cpp - Cell SPU Instruction Information ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the Cell SPU implementation of the TargetInstrInfo class.
12 //===----------------------------------------------------------------------===//
14 #include "SPURegisterNames.h"
15 #include "SPUInstrInfo.h"
16 #include "SPUInstrBuilder.h"
17 #include "SPUTargetMachine.h"
18 #include "SPUGenInstrInfo.inc"
19 #include "SPUHazardRecognizers.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include "llvm/MC/MCContext.h"
29 //! Predicate for an unconditional branch instruction
30 inline bool isUncondBranch(const MachineInstr
*I
) {
31 unsigned opc
= I
->getOpcode();
33 return (opc
== SPU::BR
38 //! Predicate for a conditional branch instruction
39 inline bool isCondBranch(const MachineInstr
*I
) {
40 unsigned opc
= I
->getOpcode();
42 return (opc
== SPU::BRNZr32
43 || opc
== SPU::BRNZv4i32
45 || opc
== SPU::BRZv4i32
46 || opc
== SPU::BRHNZr16
47 || opc
== SPU::BRHNZv8i16
48 || opc
== SPU::BRHZr16
49 || opc
== SPU::BRHZv8i16
);
53 SPUInstrInfo::SPUInstrInfo(SPUTargetMachine
&tm
)
54 : TargetInstrInfoImpl(SPUInsts
, sizeof(SPUInsts
)/sizeof(SPUInsts
[0])),
56 RI(*TM
.getSubtargetImpl(), *this)
59 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
60 /// this target when scheduling the DAG.
61 ScheduleHazardRecognizer
*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
);
70 SPUInstrInfo::isLoadFromStackSlot(const MachineInstr
*MI
,
71 int &FrameIndex
) const {
72 switch (MI
->getOpcode()) {
83 const MachineOperand MOp1
= MI
->getOperand(1);
84 const MachineOperand MOp2
= MI
->getOperand(2);
85 if (MOp1
.isImm() && MOp2
.isFI()) {
86 FrameIndex
= MOp2
.getIndex();
87 return MI
->getOperand(0).getReg();
96 SPUInstrInfo::isStoreToStackSlot(const MachineInstr
*MI
,
97 int &FrameIndex
) const {
98 switch (MI
->getOpcode()) {
110 const MachineOperand MOp1
= MI
->getOperand(1);
111 const MachineOperand MOp2
= MI
->getOperand(2);
112 if (MOp1
.isImm() && MOp2
.isFI()) {
113 FrameIndex
= MOp2
.getIndex();
114 return MI
->getOperand(0).getReg();
122 void SPUInstrInfo::copyPhysReg(MachineBasicBlock
&MBB
,
123 MachineBasicBlock::iterator I
, DebugLoc DL
,
124 unsigned DestReg
, unsigned SrcReg
,
127 // 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.
132 BuildMI(MBB
, I
, DL
, get(SPU::LRr128
), DestReg
)
133 .addReg(SrcReg
, getKillRegState(KillSrc
));
137 SPUInstrInfo::storeRegToStackSlot(MachineBasicBlock
&MBB
,
138 MachineBasicBlock::iterator MI
,
139 unsigned SrcReg
, bool isKill
, int FrameIdx
,
140 const TargetRegisterClass
*RC
,
141 const TargetRegisterInfo
*TRI
) const
144 bool isValidFrameIdx
= (FrameIdx
< SPUFrameLowering::maxFrameOffset());
145 if (RC
== SPU::GPRCRegisterClass
) {
146 opc
= (isValidFrameIdx
? SPU::STQDr128
: SPU::STQXr128
);
147 } else if (RC
== SPU::R64CRegisterClass
) {
148 opc
= (isValidFrameIdx
? SPU::STQDr64
: SPU::STQXr64
);
149 } else if (RC
== SPU::R64FPRegisterClass
) {
150 opc
= (isValidFrameIdx
? SPU::STQDr64
: SPU::STQXr64
);
151 } else if (RC
== SPU::R32CRegisterClass
) {
152 opc
= (isValidFrameIdx
? SPU::STQDr32
: SPU::STQXr32
);
153 } else if (RC
== SPU::R32FPRegisterClass
) {
154 opc
= (isValidFrameIdx
? SPU::STQDr32
: SPU::STQXr32
);
155 } else if (RC
== SPU::R16CRegisterClass
) {
156 opc
= (isValidFrameIdx
? SPU::STQDr16
: SPU::STQXr16
);
157 } else if (RC
== SPU::R8CRegisterClass
) {
158 opc
= (isValidFrameIdx
? SPU::STQDr8
: SPU::STQXr8
);
159 } else if (RC
== SPU::VECREGRegisterClass
) {
160 opc
= (isValidFrameIdx
) ? SPU::STQDv16i8
: SPU::STQXv16i8
;
162 llvm_unreachable("Unknown regclass!");
166 if (MI
!= MBB
.end()) DL
= MI
->getDebugLoc();
167 addFrameReference(BuildMI(MBB
, MI
, DL
, get(opc
))
168 .addReg(SrcReg
, getKillRegState(isKill
)), FrameIdx
);
172 SPUInstrInfo::loadRegFromStackSlot(MachineBasicBlock
&MBB
,
173 MachineBasicBlock::iterator MI
,
174 unsigned DestReg
, int FrameIdx
,
175 const TargetRegisterClass
*RC
,
176 const TargetRegisterInfo
*TRI
) const
179 bool isValidFrameIdx
= (FrameIdx
< SPUFrameLowering::maxFrameOffset());
180 if (RC
== SPU::GPRCRegisterClass
) {
181 opc
= (isValidFrameIdx
? SPU::LQDr128
: SPU::LQXr128
);
182 } else if (RC
== SPU::R64CRegisterClass
) {
183 opc
= (isValidFrameIdx
? SPU::LQDr64
: SPU::LQXr64
);
184 } else if (RC
== SPU::R64FPRegisterClass
) {
185 opc
= (isValidFrameIdx
? SPU::LQDr64
: SPU::LQXr64
);
186 } else if (RC
== SPU::R32CRegisterClass
) {
187 opc
= (isValidFrameIdx
? SPU::LQDr32
: SPU::LQXr32
);
188 } else if (RC
== SPU::R32FPRegisterClass
) {
189 opc
= (isValidFrameIdx
? SPU::LQDr32
: SPU::LQXr32
);
190 } else if (RC
== SPU::R16CRegisterClass
) {
191 opc
= (isValidFrameIdx
? SPU::LQDr16
: SPU::LQXr16
);
192 } else if (RC
== SPU::R8CRegisterClass
) {
193 opc
= (isValidFrameIdx
? SPU::LQDr8
: SPU::LQXr8
);
194 } else if (RC
== SPU::VECREGRegisterClass
) {
195 opc
= (isValidFrameIdx
) ? SPU::LQDv16i8
: SPU::LQXv16i8
;
197 llvm_unreachable("Unknown regclass in loadRegFromStackSlot!");
201 if (MI
!= MBB
.end()) DL
= MI
->getDebugLoc();
202 addFrameReference(BuildMI(MBB
, MI
, DL
, get(opc
), DestReg
), FrameIdx
);
207 \note This code was kiped from PPC. There may be more branch analysis for
208 CellSPU than what's currently done here.
211 SPUInstrInfo::AnalyzeBranch(MachineBasicBlock
&MBB
, MachineBasicBlock
*&TBB
,
212 MachineBasicBlock
*&FBB
,
213 SmallVectorImpl
<MachineOperand
> &Cond
,
214 bool AllowModify
) const {
215 // If the block has no terminators, it just falls into the block after it.
216 MachineBasicBlock::iterator I
= MBB
.end();
217 if (I
== MBB
.begin())
220 while (I
->isDebugValue()) {
221 if (I
== MBB
.begin())
225 if (!isUnpredicatedTerminator(I
))
228 // Get the last instruction in the block.
229 MachineInstr
*LastInst
= I
;
231 // If there is only one terminator instruction, process it.
232 if (I
== MBB
.begin() || !isUnpredicatedTerminator(--I
)) {
233 if (isUncondBranch(LastInst
)) {
234 // Check for jump tables
235 if (!LastInst
->getOperand(0).isMBB())
237 TBB
= LastInst
->getOperand(0).getMBB();
239 } else if (isCondBranch(LastInst
)) {
240 // Block ends with fall-through condbranch.
241 TBB
= LastInst
->getOperand(1).getMBB();
242 DEBUG(errs() << "Pushing LastInst: ");
243 DEBUG(LastInst
->dump());
244 Cond
.push_back(MachineOperand::CreateImm(LastInst
->getOpcode()));
245 Cond
.push_back(LastInst
->getOperand(0));
248 // Otherwise, don't know what this is.
252 // Get the instruction before it if it's a terminator.
253 MachineInstr
*SecondLastInst
= I
;
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
))
260 // If the block ends with a conditional and unconditional branch, handle it.
261 if (isCondBranch(SecondLastInst
) && isUncondBranch(LastInst
)) {
262 TBB
= SecondLastInst
->getOperand(1).getMBB();
263 DEBUG(errs() << "Pushing SecondLastInst: ");
264 DEBUG(SecondLastInst
->dump());
265 Cond
.push_back(MachineOperand::CreateImm(SecondLastInst
->getOpcode()));
266 Cond
.push_back(SecondLastInst
->getOperand(0));
267 FBB
= LastInst
->getOperand(0).getMBB();
271 // 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();
277 I
->eraseFromParent();
281 // Otherwise, can't handle this.
285 // search MBB for branch hint labels and branch hit ops
286 static 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
){
296 SPUInstrInfo::RemoveBranch(MachineBasicBlock
&MBB
) const {
297 MachineBasicBlock::iterator I
= MBB
.end();
299 if (I
== MBB
.begin())
302 while (I
->isDebugValue()) {
303 if (I
== MBB
.begin())
307 if (!isCondBranch(I
) && !isUncondBranch(I
))
310 // Remove the first branch.
311 DEBUG(errs() << "Removing branch: ");
313 I
->eraseFromParent();
315 if (I
== MBB
.begin())
319 if (!(isCondBranch(I
) || isUncondBranch(I
)))
322 // Remove the second branch.
323 DEBUG(errs() << "Removing second branch: ");
325 I
->eraseFromParent();
329 /** 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.
336 static MachineBasicBlock::iterator
findHBRPosition(MachineBasicBlock
&MBB
)
338 MachineBasicBlock::iterator J
= MBB
.end();
339 for( int i
=0; i
<8; i
++) {
340 if( J
== MBB
.begin() ) return J
;
347 SPUInstrInfo::InsertBranch(MachineBasicBlock
&MBB
, MachineBasicBlock
*TBB
,
348 MachineBasicBlock
*FBB
,
349 const SmallVectorImpl
<MachineOperand
> &Cond
,
351 // Shouldn't be a fall through.
352 assert(TBB
&& "InsertBranch must not be told to insert a fallthrough");
353 assert((Cond
.size() == 2 || Cond
.size() == 0) &&
354 "SPU branch conditions have two components!");
356 MachineInstrBuilder MIB
;
357 //TODO: make a more accurate algorithm.
358 bool haveHBR
= MBB
.size()>8;
361 MCSymbol
*branchLabel
= MBB
.getParent()->getContext().CreateTempSymbol();
362 // Add a label just before the branch
364 MIB
= BuildMI(&MBB
, DL
, get(SPU::HBR_LABEL
)).addSym(branchLabel
);
369 // Unconditional branch
370 MIB
= BuildMI(&MBB
, DL
, get(SPU::BR
));
373 DEBUG(errs() << "Inserted one-way uncond branch: ");
374 DEBUG((*MIB
).dump());
376 // basic blocks have just one branch so it is safe to add the hint a its
378 MIB
= BuildMI( MBB
, findHBRPosition(MBB
), DL
, get(SPU::HBRA
));
379 MIB
.addSym(branchLabel
);
383 // Conditional branch
384 MIB
= BuildMI(&MBB
, DL
, get(Cond
[0].getImm()));
385 MIB
.addReg(Cond
[1].getReg()).addMBB(TBB
);
388 MIB
= BuildMI(MBB
, findHBRPosition(MBB
), DL
, get(SPU::HBRA
));
389 MIB
.addSym(branchLabel
);
393 DEBUG(errs() << "Inserted one-way cond branch: ");
394 DEBUG((*MIB
).dump());
398 MIB
= BuildMI(&MBB
, DL
, get(Cond
[0].getImm()));
399 MachineInstrBuilder MIB2
= BuildMI(&MBB
, DL
, get(SPU::BR
));
401 // Two-way Conditional Branch.
402 MIB
.addReg(Cond
[1].getReg()).addMBB(TBB
);
406 MIB
= BuildMI( MBB
, findHBRPosition(MBB
), DL
, get(SPU::HBRA
));
407 MIB
.addSym(branchLabel
);
411 DEBUG(errs() << "Inserted conditional branch: ");
412 DEBUG((*MIB
).dump());
413 DEBUG(errs() << "part 2: ");
414 DEBUG((*MIB2
).dump());
419 //! Reverses a branch's condition, returning false on success.
421 SPUInstrInfo::ReverseBranchCondition(SmallVectorImpl
<MachineOperand
> &Cond
)
423 // Pretty brainless way of inverting the condition, but it works, considering
424 // there are only two conditions...
426 unsigned Opc
; //! The incoming opcode
427 unsigned RevCondOpc
; //! The reversed condition opcode
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
}
439 unsigned Opc
= unsigned(Cond
[0].getImm());
440 // Pretty dull mapping between the two conditions that SPU can generate:
441 for (int i
= sizeof(revconds
)/sizeof(revconds
[0]) - 1; i
>= 0; --i
) {
442 if (revconds
[i
].Opc
== Opc
) {
443 Cond
[0].setImm(revconds
[i
].RevCondOpc
);