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 "llvm/CodeGen/MachineInstrBuilder.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/raw_ostream.h"
27 //! Predicate for an unconditional branch instruction
28 inline bool isUncondBranch(const MachineInstr
*I
) {
29 unsigned opc
= I
->getOpcode();
31 return (opc
== SPU::BR
36 //! Predicate for a conditional branch instruction
37 inline bool isCondBranch(const MachineInstr
*I
) {
38 unsigned opc
= I
->getOpcode();
40 return (opc
== SPU::BRNZr32
41 || opc
== SPU::BRNZv4i32
43 || opc
== SPU::BRZv4i32
44 || opc
== SPU::BRHNZr16
45 || opc
== SPU::BRHNZv8i16
46 || opc
== SPU::BRHZr16
47 || opc
== SPU::BRHZv8i16
);
51 SPUInstrInfo::SPUInstrInfo(SPUTargetMachine
&tm
)
52 : TargetInstrInfoImpl(SPUInsts
, sizeof(SPUInsts
)/sizeof(SPUInsts
[0])),
54 RI(*TM
.getSubtargetImpl(), *this)
58 SPUInstrInfo::isLoadFromStackSlot(const MachineInstr
*MI
,
59 int &FrameIndex
) const {
60 switch (MI
->getOpcode()) {
71 const MachineOperand MOp1
= MI
->getOperand(1);
72 const MachineOperand MOp2
= MI
->getOperand(2);
73 if (MOp1
.isImm() && MOp2
.isFI()) {
74 FrameIndex
= MOp2
.getIndex();
75 return MI
->getOperand(0).getReg();
84 SPUInstrInfo::isStoreToStackSlot(const MachineInstr
*MI
,
85 int &FrameIndex
) const {
86 switch (MI
->getOpcode()) {
98 const MachineOperand MOp1
= MI
->getOperand(1);
99 const MachineOperand MOp2
= MI
->getOperand(2);
100 if (MOp1
.isImm() && MOp2
.isFI()) {
101 FrameIndex
= MOp2
.getIndex();
102 return MI
->getOperand(0).getReg();
110 void SPUInstrInfo::copyPhysReg(MachineBasicBlock
&MBB
,
111 MachineBasicBlock::iterator I
, DebugLoc DL
,
112 unsigned DestReg
, unsigned SrcReg
,
115 // We support cross register class moves for our aliases, such as R3 in any
116 // reg class to any other reg class containing R3. This is required because
117 // we instruction select bitconvert i64 -> f64 as a noop for example, so our
118 // types have no specific meaning.
120 BuildMI(MBB
, I
, DL
, get(SPU::LRr128
), DestReg
)
121 .addReg(SrcReg
, getKillRegState(KillSrc
));
125 SPUInstrInfo::storeRegToStackSlot(MachineBasicBlock
&MBB
,
126 MachineBasicBlock::iterator MI
,
127 unsigned SrcReg
, bool isKill
, int FrameIdx
,
128 const TargetRegisterClass
*RC
,
129 const TargetRegisterInfo
*TRI
) const
132 bool isValidFrameIdx
= (FrameIdx
< SPUFrameInfo::maxFrameOffset());
133 if (RC
== SPU::GPRCRegisterClass
) {
134 opc
= (isValidFrameIdx
? SPU::STQDr128
: SPU::STQXr128
);
135 } else if (RC
== SPU::R64CRegisterClass
) {
136 opc
= (isValidFrameIdx
? SPU::STQDr64
: SPU::STQXr64
);
137 } else if (RC
== SPU::R64FPRegisterClass
) {
138 opc
= (isValidFrameIdx
? SPU::STQDr64
: SPU::STQXr64
);
139 } else if (RC
== SPU::R32CRegisterClass
) {
140 opc
= (isValidFrameIdx
? SPU::STQDr32
: SPU::STQXr32
);
141 } else if (RC
== SPU::R32FPRegisterClass
) {
142 opc
= (isValidFrameIdx
? SPU::STQDr32
: SPU::STQXr32
);
143 } else if (RC
== SPU::R16CRegisterClass
) {
144 opc
= (isValidFrameIdx
? SPU::STQDr16
: SPU::STQXr16
);
145 } else if (RC
== SPU::R8CRegisterClass
) {
146 opc
= (isValidFrameIdx
? SPU::STQDr8
: SPU::STQXr8
);
147 } else if (RC
== SPU::VECREGRegisterClass
) {
148 opc
= (isValidFrameIdx
) ? SPU::STQDv16i8
: SPU::STQXv16i8
;
150 llvm_unreachable("Unknown regclass!");
154 if (MI
!= MBB
.end()) DL
= MI
->getDebugLoc();
155 addFrameReference(BuildMI(MBB
, MI
, DL
, get(opc
))
156 .addReg(SrcReg
, getKillRegState(isKill
)), FrameIdx
);
160 SPUInstrInfo::loadRegFromStackSlot(MachineBasicBlock
&MBB
,
161 MachineBasicBlock::iterator MI
,
162 unsigned DestReg
, int FrameIdx
,
163 const TargetRegisterClass
*RC
,
164 const TargetRegisterInfo
*TRI
) const
167 bool isValidFrameIdx
= (FrameIdx
< SPUFrameInfo::maxFrameOffset());
168 if (RC
== SPU::GPRCRegisterClass
) {
169 opc
= (isValidFrameIdx
? SPU::LQDr128
: SPU::LQXr128
);
170 } else if (RC
== SPU::R64CRegisterClass
) {
171 opc
= (isValidFrameIdx
? SPU::LQDr64
: SPU::LQXr64
);
172 } else if (RC
== SPU::R64FPRegisterClass
) {
173 opc
= (isValidFrameIdx
? SPU::LQDr64
: SPU::LQXr64
);
174 } else if (RC
== SPU::R32CRegisterClass
) {
175 opc
= (isValidFrameIdx
? SPU::LQDr32
: SPU::LQXr32
);
176 } else if (RC
== SPU::R32FPRegisterClass
) {
177 opc
= (isValidFrameIdx
? SPU::LQDr32
: SPU::LQXr32
);
178 } else if (RC
== SPU::R16CRegisterClass
) {
179 opc
= (isValidFrameIdx
? SPU::LQDr16
: SPU::LQXr16
);
180 } else if (RC
== SPU::R8CRegisterClass
) {
181 opc
= (isValidFrameIdx
? SPU::LQDr8
: SPU::LQXr8
);
182 } else if (RC
== SPU::VECREGRegisterClass
) {
183 opc
= (isValidFrameIdx
) ? SPU::LQDv16i8
: SPU::LQXv16i8
;
185 llvm_unreachable("Unknown regclass in loadRegFromStackSlot!");
189 if (MI
!= MBB
.end()) DL
= MI
->getDebugLoc();
190 addFrameReference(BuildMI(MBB
, MI
, DL
, get(opc
), DestReg
), FrameIdx
);
195 \note This code was kiped from PPC. There may be more branch analysis for
196 CellSPU than what's currently done here.
199 SPUInstrInfo::AnalyzeBranch(MachineBasicBlock
&MBB
, MachineBasicBlock
*&TBB
,
200 MachineBasicBlock
*&FBB
,
201 SmallVectorImpl
<MachineOperand
> &Cond
,
202 bool AllowModify
) const {
203 // If the block has no terminators, it just falls into the block after it.
204 MachineBasicBlock::iterator I
= MBB
.end();
205 if (I
== MBB
.begin())
208 while (I
->isDebugValue()) {
209 if (I
== MBB
.begin())
213 if (!isUnpredicatedTerminator(I
))
216 // Get the last instruction in the block.
217 MachineInstr
*LastInst
= I
;
219 // If there is only one terminator instruction, process it.
220 if (I
== MBB
.begin() || !isUnpredicatedTerminator(--I
)) {
221 if (isUncondBranch(LastInst
)) {
222 // Check for jump tables
223 if (!LastInst
->getOperand(0).isMBB())
225 TBB
= LastInst
->getOperand(0).getMBB();
227 } else if (isCondBranch(LastInst
)) {
228 // Block ends with fall-through condbranch.
229 TBB
= LastInst
->getOperand(1).getMBB();
230 DEBUG(errs() << "Pushing LastInst: ");
231 DEBUG(LastInst
->dump());
232 Cond
.push_back(MachineOperand::CreateImm(LastInst
->getOpcode()));
233 Cond
.push_back(LastInst
->getOperand(0));
236 // Otherwise, don't know what this is.
240 // Get the instruction before it if it's a terminator.
241 MachineInstr
*SecondLastInst
= I
;
243 // If there are three terminators, we don't know what sort of block this is.
244 if (SecondLastInst
&& I
!= MBB
.begin() &&
245 isUnpredicatedTerminator(--I
))
248 // If the block ends with a conditional and unconditional branch, handle it.
249 if (isCondBranch(SecondLastInst
) && isUncondBranch(LastInst
)) {
250 TBB
= SecondLastInst
->getOperand(1).getMBB();
251 DEBUG(errs() << "Pushing SecondLastInst: ");
252 DEBUG(SecondLastInst
->dump());
253 Cond
.push_back(MachineOperand::CreateImm(SecondLastInst
->getOpcode()));
254 Cond
.push_back(SecondLastInst
->getOperand(0));
255 FBB
= LastInst
->getOperand(0).getMBB();
259 // If the block ends with two unconditional branches, handle it. The second
260 // one is not executed, so remove it.
261 if (isUncondBranch(SecondLastInst
) && isUncondBranch(LastInst
)) {
262 TBB
= SecondLastInst
->getOperand(0).getMBB();
265 I
->eraseFromParent();
269 // Otherwise, can't handle this.
274 SPUInstrInfo::RemoveBranch(MachineBasicBlock
&MBB
) const {
275 MachineBasicBlock::iterator I
= MBB
.end();
276 if (I
== MBB
.begin())
279 while (I
->isDebugValue()) {
280 if (I
== MBB
.begin())
284 if (!isCondBranch(I
) && !isUncondBranch(I
))
287 // Remove the first branch.
288 DEBUG(errs() << "Removing branch: ");
290 I
->eraseFromParent();
292 if (I
== MBB
.begin())
296 if (!(isCondBranch(I
) || isUncondBranch(I
)))
299 // Remove the second branch.
300 DEBUG(errs() << "Removing second branch: ");
302 I
->eraseFromParent();
307 SPUInstrInfo::InsertBranch(MachineBasicBlock
&MBB
, MachineBasicBlock
*TBB
,
308 MachineBasicBlock
*FBB
,
309 const SmallVectorImpl
<MachineOperand
> &Cond
,
311 // Shouldn't be a fall through.
312 assert(TBB
&& "InsertBranch must not be told to insert a fallthrough");
313 assert((Cond
.size() == 2 || Cond
.size() == 0) &&
314 "SPU branch conditions have two components!");
319 // Unconditional branch
320 MachineInstrBuilder MIB
= BuildMI(&MBB
, DL
, get(SPU::BR
));
323 DEBUG(errs() << "Inserted one-way uncond branch: ");
324 DEBUG((*MIB
).dump());
326 // Conditional branch
327 MachineInstrBuilder MIB
= BuildMI(&MBB
, DL
, get(Cond
[0].getImm()));
328 MIB
.addReg(Cond
[1].getReg()).addMBB(TBB
);
330 DEBUG(errs() << "Inserted one-way cond branch: ");
331 DEBUG((*MIB
).dump());
335 MachineInstrBuilder MIB
= BuildMI(&MBB
, DL
, get(Cond
[0].getImm()));
336 MachineInstrBuilder MIB2
= BuildMI(&MBB
, DL
, get(SPU::BR
));
338 // Two-way Conditional Branch.
339 MIB
.addReg(Cond
[1].getReg()).addMBB(TBB
);
342 DEBUG(errs() << "Inserted conditional branch: ");
343 DEBUG((*MIB
).dump());
344 DEBUG(errs() << "part 2: ");
345 DEBUG((*MIB2
).dump());
350 //! Reverses a branch's condition, returning false on success.
352 SPUInstrInfo::ReverseBranchCondition(SmallVectorImpl
<MachineOperand
> &Cond
)
354 // Pretty brainless way of inverting the condition, but it works, considering
355 // there are only two conditions...
357 unsigned Opc
; //! The incoming opcode
358 unsigned RevCondOpc
; //! The reversed condition opcode
360 { SPU::BRNZr32
, SPU::BRZr32
},
361 { SPU::BRNZv4i32
, SPU::BRZv4i32
},
362 { SPU::BRZr32
, SPU::BRNZr32
},
363 { SPU::BRZv4i32
, SPU::BRNZv4i32
},
364 { SPU::BRHNZr16
, SPU::BRHZr16
},
365 { SPU::BRHNZv8i16
, SPU::BRHZv8i16
},
366 { SPU::BRHZr16
, SPU::BRHNZr16
},
367 { SPU::BRHZv8i16
, SPU::BRHNZv8i16
}
370 unsigned Opc
= unsigned(Cond
[0].getImm());
371 // Pretty dull mapping between the two conditions that SPU can generate:
372 for (int i
= sizeof(revconds
)/sizeof(revconds
[0]) - 1; i
>= 0; --i
) {
373 if (revconds
[i
].Opc
== Opc
) {
374 Cond
[0].setImm(revconds
[i
].RevCondOpc
);