blob: 443cd54906c287f250f079670230bcb6e5933319 [file] [log] [blame]
Jia Liu31d157a2012-02-18 12:03:15 +00001//===-- PTXInstrInfo.cpp - PTX Instruction Information --------------------===//
Eric Christopher50880d02010-09-18 18:52:28 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the PTX implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
Che-Liang Chiou5e0872e2011-03-22 14:12:00 +000014#define DEBUG_TYPE "ptx-instrinfo"
15
Eric Christopher50880d02010-09-18 18:52:28 +000016#include "PTXInstrInfo.h"
Craig Topper79aa3412012-03-17 18:46:09 +000017#include "PTX.h"
Che-Liang Chioub48f2c22010-10-19 13:14:40 +000018#include "llvm/CodeGen/MachineInstrBuilder.h"
Justin Holewinski297984d2011-09-22 16:45:40 +000019#include "llvm/CodeGen/MachineRegisterInfo.h"
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +000020#include "llvm/CodeGen/SelectionDAG.h"
21#include "llvm/CodeGen/SelectionDAGNodes.h"
Che-Liang Chiou5e0872e2011-03-22 14:12:00 +000022#include "llvm/Support/Debug.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000023#include "llvm/Support/TargetRegistry.h"
Che-Liang Chiou5e0872e2011-03-22 14:12:00 +000024#include "llvm/Support/raw_ostream.h"
Eric Christopher50880d02010-09-18 18:52:28 +000025
Evan Cheng4db3cff2011-07-01 17:57:27 +000026#define GET_INSTRINFO_CTOR
Eric Christopher50880d02010-09-18 18:52:28 +000027#include "PTXGenInstrInfo.inc"
28
Evan Cheng22fee2d2011-06-28 20:07:07 +000029using namespace llvm;
30
Eric Christopher50880d02010-09-18 18:52:28 +000031PTXInstrInfo::PTXInstrInfo(PTXTargetMachine &_TM)
Evan Cheng4db3cff2011-07-01 17:57:27 +000032 : PTXGenInstrInfo(),
Eric Christopher50880d02010-09-18 18:52:28 +000033 RI(_TM, *this), TM(_TM) {}
Che-Liang Chioub48f2c22010-10-19 13:14:40 +000034
35static const struct map_entry {
36 const TargetRegisterClass *cls;
37 const int opcode;
38} map[] = {
Justin Holewinski1b91bcd2011-06-16 17:49:58 +000039 { &PTX::RegI16RegClass, PTX::MOVU16rr },
40 { &PTX::RegI32RegClass, PTX::MOVU32rr },
41 { &PTX::RegI64RegClass, PTX::MOVU64rr },
42 { &PTX::RegF32RegClass, PTX::MOVF32rr },
43 { &PTX::RegF64RegClass, PTX::MOVF64rr },
44 { &PTX::RegPredRegClass, PTX::MOVPREDrr }
Che-Liang Chioub48f2c22010-10-19 13:14:40 +000045};
46
47void PTXInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
48 MachineBasicBlock::iterator I, DebugLoc DL,
49 unsigned DstReg, unsigned SrcReg,
50 bool KillSrc) const {
Justin Holewinski297984d2011-09-22 16:45:40 +000051
52 const MachineRegisterInfo& MRI = MBB.getParent()->getRegInfo();
Justin Holewinski5422a0f2011-09-22 16:45:46 +000053 //assert(MRI.getRegClass(SrcReg) == MRI.getRegClass(DstReg) &&
54 // "Invalid register copy between two register classes");
Justin Holewinski297984d2011-09-22 16:45:40 +000055
56 for (int i = 0, e = sizeof(map)/sizeof(map[0]); i != e; ++i) {
Justin Holewinski5422a0f2011-09-22 16:45:46 +000057 if (map[i].cls == MRI.getRegClass(DstReg)) {
Evan Chenge837dea2011-06-28 19:10:37 +000058 const MCInstrDesc &MCID = get(map[i].opcode);
59 MachineInstr *MI = BuildMI(MBB, I, DL, MCID, DstReg).
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +000060 addReg(SrcReg, getKillRegState(KillSrc));
61 AddDefaultPredicate(MI);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +000062 return;
63 }
Che-Liang Chiouf7172022011-02-28 06:34:09 +000064 }
Che-Liang Chioub48f2c22010-10-19 13:14:40 +000065
66 llvm_unreachable("Impossible reg-to-reg copy");
67}
68
69bool PTXInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
70 MachineBasicBlock::iterator I,
71 unsigned DstReg, unsigned SrcReg,
72 const TargetRegisterClass *DstRC,
73 const TargetRegisterClass *SrcRC,
74 DebugLoc DL) const {
75 if (DstRC != SrcRC)
76 return false;
77
78 for (int i = 0, e = sizeof(map)/sizeof(map[0]); i != e; ++ i)
79 if (DstRC == map[i].cls) {
Evan Chenge837dea2011-06-28 19:10:37 +000080 const MCInstrDesc &MCID = get(map[i].opcode);
81 MachineInstr *MI = BuildMI(MBB, I, DL, MCID, DstReg).addReg(SrcReg);
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +000082 AddDefaultPredicate(MI);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +000083 return true;
84 }
85
86 return false;
87}
88
89bool PTXInstrInfo::isMoveInstr(const MachineInstr& MI,
90 unsigned &SrcReg, unsigned &DstReg,
91 unsigned &SrcSubIdx, unsigned &DstSubIdx) const {
92 switch (MI.getOpcode()) {
93 default:
94 return false;
Che-Liang Chioufd8978b2011-03-02 03:20:28 +000095 case PTX::MOVU16rr:
96 case PTX::MOVU32rr:
97 case PTX::MOVU64rr:
98 case PTX::MOVF32rr:
99 case PTX::MOVF64rr:
100 case PTX::MOVPREDrr:
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000101 assert(MI.getNumOperands() >= 2 &&
102 MI.getOperand(0).isReg() && MI.getOperand(1).isReg() &&
103 "Invalid register-register move instruction");
104 SrcSubIdx = DstSubIdx = 0; // No sub-registers
105 DstReg = MI.getOperand(0).getReg();
106 SrcReg = MI.getOperand(1).getReg();
107 return true;
108 }
109}
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000110
111// predicate support
112
113bool PTXInstrInfo::isPredicated(const MachineInstr *MI) const {
114 int i = MI->findFirstPredOperandIdx();
Che-Liang Chiouf78847e2011-03-14 11:26:01 +0000115 return i != -1 && MI->getOperand(i).getReg() != PTX::NoRegister;
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000116}
117
118bool PTXInstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000119 return !isPredicated(MI) && MI->isTerminator();
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000120}
121
122bool PTXInstrInfo::
123PredicateInstruction(MachineInstr *MI,
124 const SmallVectorImpl<MachineOperand> &Pred) const {
125 if (Pred.size() < 2)
126 llvm_unreachable("lesser than 2 predicate operands are provided");
127
128 int i = MI->findFirstPredOperandIdx();
129 if (i == -1)
130 llvm_unreachable("missing predicate operand");
131
132 MI->getOperand(i).setReg(Pred[0].getReg());
133 MI->getOperand(i+1).setImm(Pred[1].getImm());
134
135 return true;
136}
137
138bool PTXInstrInfo::
139SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
140 const SmallVectorImpl<MachineOperand> &Pred2) const {
Che-Liang Chiou357be5e2011-04-02 08:51:39 +0000141 const MachineOperand &PredReg1 = Pred1[0];
142 const MachineOperand &PredReg2 = Pred2[0];
143 if (PredReg1.getReg() != PredReg2.getReg())
144 return false;
145
146 const MachineOperand &PredOp1 = Pred1[1];
147 const MachineOperand &PredOp2 = Pred2[1];
148 if (PredOp1.getImm() != PredOp2.getImm())
149 return false;
150
151 return true;
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000152}
153
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000154bool PTXInstrInfo::
155DefinesPredicate(MachineInstr *MI,
156 std::vector<MachineOperand> &Pred) const {
Che-Liang Chiou357be5e2011-04-02 08:51:39 +0000157 // If an instruction sets a predicate register, it defines a predicate.
Che-Liang Chiouf78847e2011-03-14 11:26:01 +0000158
Che-Liang Chiou357be5e2011-04-02 08:51:39 +0000159 // TODO supprot 5-operand format of setp instruction
160
161 if (MI->getNumOperands() < 1)
Che-Liang Chiouf78847e2011-03-14 11:26:01 +0000162 return false;
Che-Liang Chiou357be5e2011-04-02 08:51:39 +0000163
164 const MachineOperand &MO = MI->getOperand(0);
165
Justin Holewinski1b91bcd2011-06-16 17:49:58 +0000166 if (!MO.isReg() || RI.getRegClass(MO.getReg()) != &PTX::RegPredRegClass)
Che-Liang Chiou357be5e2011-04-02 08:51:39 +0000167 return false;
168
169 Pred.push_back(MO);
Justin Holewinskif51b7e52011-09-30 14:36:36 +0000170 Pred.push_back(MachineOperand::CreateImm(PTXPredicate::None));
Che-Liang Chiou357be5e2011-04-02 08:51:39 +0000171 return true;
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000172}
173
Che-Liang Chiou5e0872e2011-03-22 14:12:00 +0000174// branch support
175
176bool PTXInstrInfo::
177AnalyzeBranch(MachineBasicBlock &MBB,
178 MachineBasicBlock *&TBB,
179 MachineBasicBlock *&FBB,
180 SmallVectorImpl<MachineOperand> &Cond,
181 bool AllowModify) const {
182 // TODO implement cases when AllowModify is true
183
184 if (MBB.empty())
185 return true;
186
Evan Cheng7c2a4a32011-12-06 22:12:01 +0000187 MachineBasicBlock::iterator iter = MBB.end();
Che-Liang Chiou5e0872e2011-03-22 14:12:00 +0000188 const MachineInstr& instLast1 = *--iter;
Che-Liang Chiou5e0872e2011-03-22 14:12:00 +0000189 // for special case that MBB has only 1 instruction
190 const bool IsSizeOne = MBB.size() == 1;
191 // if IsSizeOne is true, *--iter and instLast2 are invalid
192 // we put a dummy value in instLast2 and desc2 since they are used
193 const MachineInstr& instLast2 = IsSizeOne ? instLast1 : *--iter;
Che-Liang Chiou5e0872e2011-03-22 14:12:00 +0000194
195 DEBUG(dbgs() << "\n");
196 DEBUG(dbgs() << "AnalyzeBranch: opcode: " << instLast1.getOpcode() << "\n");
197 DEBUG(dbgs() << "AnalyzeBranch: MBB: " << MBB.getName().str() << "\n");
198 DEBUG(dbgs() << "AnalyzeBranch: TBB: " << TBB << "\n");
199 DEBUG(dbgs() << "AnalyzeBranch: FBB: " << FBB << "\n");
200
201 // this block ends with no branches
202 if (!IsAnyKindOfBranch(instLast1)) {
203 DEBUG(dbgs() << "AnalyzeBranch: ends with no branch\n");
204 return false;
205 }
206
207 // this block ends with only an unconditional branch
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000208 if (instLast1.isUnconditionalBranch() &&
Che-Liang Chiou5e0872e2011-03-22 14:12:00 +0000209 // when IsSizeOne is true, it "absorbs" the evaluation of instLast2
210 (IsSizeOne || !IsAnyKindOfBranch(instLast2))) {
211 DEBUG(dbgs() << "AnalyzeBranch: ends with only uncond branch\n");
212 TBB = GetBranchTarget(instLast1);
213 return false;
214 }
215
216 // this block ends with a conditional branch and
217 // it falls through to a successor block
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000218 if (instLast1.isConditionalBranch() &&
Che-Liang Chiou5e0872e2011-03-22 14:12:00 +0000219 IsAnySuccessorAlsoLayoutSuccessor(MBB)) {
220 DEBUG(dbgs() << "AnalyzeBranch: ends with cond branch and fall through\n");
221 TBB = GetBranchTarget(instLast1);
222 int i = instLast1.findFirstPredOperandIdx();
223 Cond.push_back(instLast1.getOperand(i));
224 Cond.push_back(instLast1.getOperand(i+1));
225 return false;
226 }
227
228 // when IsSizeOne is true, we are done
229 if (IsSizeOne)
230 return true;
231
232 // this block ends with a conditional branch
233 // followed by an unconditional branch
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000234 if (instLast2.isConditionalBranch() &&
235 instLast1.isUnconditionalBranch()) {
Che-Liang Chiou5e0872e2011-03-22 14:12:00 +0000236 DEBUG(dbgs() << "AnalyzeBranch: ends with cond and uncond branch\n");
237 TBB = GetBranchTarget(instLast2);
238 FBB = GetBranchTarget(instLast1);
239 int i = instLast2.findFirstPredOperandIdx();
240 Cond.push_back(instLast2.getOperand(i));
241 Cond.push_back(instLast2.getOperand(i+1));
242 return false;
243 }
244
245 // branch cannot be understood
246 DEBUG(dbgs() << "AnalyzeBranch: cannot be understood\n");
247 return true;
248}
249
250unsigned PTXInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Che-Liang Chioufb4a8342011-03-28 10:23:13 +0000251 unsigned count = 0;
252 while (!MBB.empty())
253 if (IsAnyKindOfBranch(MBB.back())) {
254 MBB.pop_back();
255 ++count;
256 } else
257 break;
Che-Liang Chiou5e0872e2011-03-22 14:12:00 +0000258 DEBUG(dbgs() << "RemoveBranch: MBB: " << MBB.getName().str() << "\n");
Che-Liang Chioufb4a8342011-03-28 10:23:13 +0000259 DEBUG(dbgs() << "RemoveBranch: remove " << count << " branch inst\n");
Che-Liang Chiou5e0872e2011-03-22 14:12:00 +0000260 return count;
261}
262
263unsigned PTXInstrInfo::
264InsertBranch(MachineBasicBlock &MBB,
265 MachineBasicBlock *TBB,
266 MachineBasicBlock *FBB,
267 const SmallVectorImpl<MachineOperand> &Cond,
268 DebugLoc DL) const {
269 DEBUG(dbgs() << "InsertBranch: MBB: " << MBB.getName().str() << "\n");
Che-Liang Chioufb4a8342011-03-28 10:23:13 +0000270 DEBUG(if (TBB) dbgs() << "InsertBranch: TBB: " << TBB->getName().str()
271 << "\n";
272 else dbgs() << "InsertBranch: TBB: (NULL)\n");
273 DEBUG(if (FBB) dbgs() << "InsertBranch: FBB: " << FBB->getName().str()
274 << "\n";
275 else dbgs() << "InsertBranch: FBB: (NULL)\n");
Che-Liang Chiou5e0872e2011-03-22 14:12:00 +0000276 DEBUG(dbgs() << "InsertBranch: Cond size: " << Cond.size() << "\n");
277
278 assert(TBB && "TBB is NULL");
279
280 if (FBB) {
281 BuildMI(&MBB, DL, get(PTX::BRAdp))
282 .addMBB(TBB).addReg(Cond[0].getReg()).addImm(Cond[1].getImm());
283 BuildMI(&MBB, DL, get(PTX::BRAd))
Justin Holewinskif51b7e52011-09-30 14:36:36 +0000284 .addMBB(FBB).addReg(PTX::NoRegister).addImm(PTXPredicate::None);
Che-Liang Chiou5e0872e2011-03-22 14:12:00 +0000285 return 2;
286 } else if (Cond.size()) {
287 BuildMI(&MBB, DL, get(PTX::BRAdp))
288 .addMBB(TBB).addReg(Cond[0].getReg()).addImm(Cond[1].getImm());
289 return 1;
290 } else {
291 BuildMI(&MBB, DL, get(PTX::BRAd))
Justin Holewinskif51b7e52011-09-30 14:36:36 +0000292 .addMBB(TBB).addReg(PTX::NoRegister).addImm(PTXPredicate::None);
Che-Liang Chiou5e0872e2011-03-22 14:12:00 +0000293 return 1;
294 }
295}
296
Justin Holewinskidf1c8d82011-06-20 15:56:20 +0000297// Memory operand folding for spills
298void PTXInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
299 MachineBasicBlock::iterator MII,
Justin Holewinski08d03162011-06-22 16:07:03 +0000300 unsigned SrcReg, bool isKill, int FrameIdx,
Justin Holewinskidf1c8d82011-06-20 15:56:20 +0000301 const TargetRegisterClass *RC,
302 const TargetRegisterInfo *TRI) const {
Craig Topperbc219812012-02-07 02:50:20 +0000303 llvm_unreachable("storeRegToStackSlot should not be called for PTX");
Justin Holewinskidf1c8d82011-06-20 15:56:20 +0000304}
305
306void PTXInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
307 MachineBasicBlock::iterator MII,
308 unsigned DestReg, int FrameIdx,
309 const TargetRegisterClass *RC,
310 const TargetRegisterInfo *TRI) const {
Craig Topperbc219812012-02-07 02:50:20 +0000311 llvm_unreachable("loadRegFromStackSlot should not be called for PTX");
Justin Holewinskidf1c8d82011-06-20 15:56:20 +0000312}
313
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000314// static helper routines
315
316MachineSDNode *PTXInstrInfo::
317GetPTXMachineNode(SelectionDAG *DAG, unsigned Opcode,
318 DebugLoc dl, EVT VT, SDValue Op1) {
Che-Liang Chiouf78847e2011-03-14 11:26:01 +0000319 SDValue predReg = DAG->getRegister(PTX::NoRegister, MVT::i1);
Justin Holewinskif51b7e52011-09-30 14:36:36 +0000320 SDValue predOp = DAG->getTargetConstant(PTXPredicate::None, MVT::i32);
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000321 SDValue ops[] = { Op1, predReg, predOp };
322 return DAG->getMachineNode(Opcode, dl, VT, ops, array_lengthof(ops));
323}
324
325MachineSDNode *PTXInstrInfo::
326GetPTXMachineNode(SelectionDAG *DAG, unsigned Opcode,
327 DebugLoc dl, EVT VT, SDValue Op1, SDValue Op2) {
Che-Liang Chiouf78847e2011-03-14 11:26:01 +0000328 SDValue predReg = DAG->getRegister(PTX::NoRegister, MVT::i1);
Justin Holewinskif51b7e52011-09-30 14:36:36 +0000329 SDValue predOp = DAG->getTargetConstant(PTXPredicate::None, MVT::i32);
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000330 SDValue ops[] = { Op1, Op2, predReg, predOp };
331 return DAG->getMachineNode(Opcode, dl, VT, ops, array_lengthof(ops));
332}
333
334void PTXInstrInfo::AddDefaultPredicate(MachineInstr *MI) {
335 if (MI->findFirstPredOperandIdx() == -1) {
Che-Liang Chiou5e0872e2011-03-22 14:12:00 +0000336 MI->addOperand(MachineOperand::CreateReg(PTX::NoRegister, /*IsDef=*/false));
Justin Holewinskif51b7e52011-09-30 14:36:36 +0000337 MI->addOperand(MachineOperand::CreateImm(PTXPredicate::None));
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000338 }
339}
Che-Liang Chiou5e0872e2011-03-22 14:12:00 +0000340
341bool PTXInstrInfo::IsAnyKindOfBranch(const MachineInstr& inst) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000342 return inst.isTerminator() || inst.isBranch() || inst.isIndirectBranch();
Che-Liang Chiou5e0872e2011-03-22 14:12:00 +0000343}
344
345bool PTXInstrInfo::
346IsAnySuccessorAlsoLayoutSuccessor(const MachineBasicBlock& MBB) {
347 for (MachineBasicBlock::const_succ_iterator
348 i = MBB.succ_begin(), e = MBB.succ_end(); i != e; ++i)
349 if (MBB.isLayoutSuccessor((const MachineBasicBlock*) &*i))
350 return true;
351 return false;
352}
353
354MachineBasicBlock *PTXInstrInfo::GetBranchTarget(const MachineInstr& inst) {
355 // FIXME So far all branch instructions put destination in 1st operand
356 const MachineOperand& target = inst.getOperand(0);
357 assert(target.isMBB() && "FIXME: detect branch target operand");
358 return target.getMBB();
359}