blob: e8675f27e4161c7e54ab058ab7d675b4d3924eea [file] [log] [blame]
Eric Christopher50880d02010-09-18 18:52:28 +00001//===- PTXInstrInfo.cpp - PTX Instruction Information ---------------------===//
2//
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
Che-Liang Chioub48f2c22010-10-19 13:14:40 +000016#include "PTX.h"
Eric Christopher50880d02010-09-18 18:52:28 +000017#include "PTXInstrInfo.h"
Che-Liang Chioub48f2c22010-10-19 13:14:40 +000018#include "llvm/CodeGen/MachineInstrBuilder.h"
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +000019#include "llvm/CodeGen/SelectionDAG.h"
20#include "llvm/CodeGen/SelectionDAGNodes.h"
Che-Liang Chiou5e0872e2011-03-22 14:12:00 +000021#include "llvm/Support/Debug.h"
22#include "llvm/Support/raw_ostream.h"
Eric Christopher50880d02010-09-18 18:52:28 +000023
24using namespace llvm;
25
26#include "PTXGenInstrInfo.inc"
27
28PTXInstrInfo::PTXInstrInfo(PTXTargetMachine &_TM)
29 : TargetInstrInfoImpl(PTXInsts, array_lengthof(PTXInsts)),
30 RI(_TM, *this), TM(_TM) {}
Che-Liang Chioub48f2c22010-10-19 13:14:40 +000031
32static const struct map_entry {
33 const TargetRegisterClass *cls;
34 const int opcode;
35} map[] = {
Che-Liang Chioufd8978b2011-03-02 03:20:28 +000036 { &PTX::RRegu16RegClass, PTX::MOVU16rr },
37 { &PTX::RRegu32RegClass, PTX::MOVU32rr },
38 { &PTX::RRegu64RegClass, PTX::MOVU64rr },
39 { &PTX::RRegf32RegClass, PTX::MOVF32rr },
40 { &PTX::RRegf64RegClass, PTX::MOVF64rr },
41 { &PTX::PredsRegClass, PTX::MOVPREDrr }
Che-Liang Chioub48f2c22010-10-19 13:14:40 +000042};
43
44void PTXInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
45 MachineBasicBlock::iterator I, DebugLoc DL,
46 unsigned DstReg, unsigned SrcReg,
47 bool KillSrc) const {
Che-Liang Chiouf7172022011-02-28 06:34:09 +000048 for (int i = 0, e = sizeof(map)/sizeof(map[0]); i != e; ++ i) {
49 if (map[i].cls->contains(DstReg, SrcReg)) {
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +000050 const TargetInstrDesc &TID = get(map[i].opcode);
51 MachineInstr *MI = BuildMI(MBB, I, DL, TID, DstReg).
52 addReg(SrcReg, getKillRegState(KillSrc));
53 AddDefaultPredicate(MI);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +000054 return;
55 }
Che-Liang Chiouf7172022011-02-28 06:34:09 +000056 }
Che-Liang Chioub48f2c22010-10-19 13:14:40 +000057
58 llvm_unreachable("Impossible reg-to-reg copy");
59}
60
61bool PTXInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
62 MachineBasicBlock::iterator I,
63 unsigned DstReg, unsigned SrcReg,
64 const TargetRegisterClass *DstRC,
65 const TargetRegisterClass *SrcRC,
66 DebugLoc DL) const {
67 if (DstRC != SrcRC)
68 return false;
69
70 for (int i = 0, e = sizeof(map)/sizeof(map[0]); i != e; ++ i)
71 if (DstRC == map[i].cls) {
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +000072 const TargetInstrDesc &TID = get(map[i].opcode);
73 MachineInstr *MI = BuildMI(MBB, I, DL, TID, DstReg).addReg(SrcReg);
74 AddDefaultPredicate(MI);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +000075 return true;
76 }
77
78 return false;
79}
80
81bool PTXInstrInfo::isMoveInstr(const MachineInstr& MI,
82 unsigned &SrcReg, unsigned &DstReg,
83 unsigned &SrcSubIdx, unsigned &DstSubIdx) const {
84 switch (MI.getOpcode()) {
85 default:
86 return false;
Che-Liang Chioufd8978b2011-03-02 03:20:28 +000087 case PTX::MOVU16rr:
88 case PTX::MOVU32rr:
89 case PTX::MOVU64rr:
90 case PTX::MOVF32rr:
91 case PTX::MOVF64rr:
92 case PTX::MOVPREDrr:
Che-Liang Chioub48f2c22010-10-19 13:14:40 +000093 assert(MI.getNumOperands() >= 2 &&
94 MI.getOperand(0).isReg() && MI.getOperand(1).isReg() &&
95 "Invalid register-register move instruction");
96 SrcSubIdx = DstSubIdx = 0; // No sub-registers
97 DstReg = MI.getOperand(0).getReg();
98 SrcReg = MI.getOperand(1).getReg();
99 return true;
100 }
101}
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000102
103// predicate support
104
105bool PTXInstrInfo::isPredicated(const MachineInstr *MI) const {
106 int i = MI->findFirstPredOperandIdx();
Che-Liang Chiouf78847e2011-03-14 11:26:01 +0000107 return i != -1 && MI->getOperand(i).getReg() != PTX::NoRegister;
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000108}
109
110bool PTXInstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
111 return !isPredicated(MI) && get(MI->getOpcode()).isTerminator();
112}
113
114bool PTXInstrInfo::
115PredicateInstruction(MachineInstr *MI,
116 const SmallVectorImpl<MachineOperand> &Pred) const {
117 if (Pred.size() < 2)
118 llvm_unreachable("lesser than 2 predicate operands are provided");
119
120 int i = MI->findFirstPredOperandIdx();
121 if (i == -1)
122 llvm_unreachable("missing predicate operand");
123
124 MI->getOperand(i).setReg(Pred[0].getReg());
125 MI->getOperand(i+1).setImm(Pred[1].getImm());
126
127 return true;
128}
129
130bool PTXInstrInfo::
131SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
132 const SmallVectorImpl<MachineOperand> &Pred2) const {
133 // TODO Implement SubsumesPredicate
134 // Returns true if the first specified predicate subsumes the second,
135 // e.g. GE subsumes GT.
136 return false;
137}
138
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000139bool PTXInstrInfo::
140DefinesPredicate(MachineInstr *MI,
141 std::vector<MachineOperand> &Pred) const {
142 // TODO Implement DefinesPredicate
143 // If the specified instruction defines any predicate or condition code
144 // register(s) used for predication, returns true as well as the definition
145 // predicate(s) by reference.
Che-Liang Chiouf78847e2011-03-14 11:26:01 +0000146
147 switch (MI->getOpcode()) {
148 default:
149 return false;
150 case PTX::SETPEQu32rr:
151 case PTX::SETPEQu32ri:
152 case PTX::SETPNEu32rr:
153 case PTX::SETPNEu32ri:
154 case PTX::SETPLTu32rr:
155 case PTX::SETPLTu32ri:
156 case PTX::SETPLEu32rr:
157 case PTX::SETPLEu32ri:
158 case PTX::SETPGTu32rr:
159 case PTX::SETPGTu32ri:
160 case PTX::SETPGEu32rr:
161 case PTX::SETPGEu32ri: {
162 const MachineOperand &MO = MI->getOperand(0);
163 assert(MO.isReg() && RI.getRegClass(MO.getReg()) == &PTX::PredsRegClass);
164 Pred.push_back(MO);
165 Pred.push_back(MachineOperand::CreateImm(PTX::PRED_NORMAL));
166 return true;
167 }
168 }
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000169}
170
Che-Liang Chiou5e0872e2011-03-22 14:12:00 +0000171// branch support
172
173bool PTXInstrInfo::
174AnalyzeBranch(MachineBasicBlock &MBB,
175 MachineBasicBlock *&TBB,
176 MachineBasicBlock *&FBB,
177 SmallVectorImpl<MachineOperand> &Cond,
178 bool AllowModify) const {
179 // TODO implement cases when AllowModify is true
180
181 if (MBB.empty())
182 return true;
183
184 MachineBasicBlock::const_iterator iter = MBB.end();
185 const MachineInstr& instLast1 = *--iter;
186 const TargetInstrDesc &desc1 = instLast1.getDesc();
187 // for special case that MBB has only 1 instruction
188 const bool IsSizeOne = MBB.size() == 1;
189 // if IsSizeOne is true, *--iter and instLast2 are invalid
190 // we put a dummy value in instLast2 and desc2 since they are used
191 const MachineInstr& instLast2 = IsSizeOne ? instLast1 : *--iter;
192 const TargetInstrDesc &desc2 = IsSizeOne ? desc1 : instLast2.getDesc();
193
194 DEBUG(dbgs() << "\n");
195 DEBUG(dbgs() << "AnalyzeBranch: opcode: " << instLast1.getOpcode() << "\n");
196 DEBUG(dbgs() << "AnalyzeBranch: MBB: " << MBB.getName().str() << "\n");
197 DEBUG(dbgs() << "AnalyzeBranch: TBB: " << TBB << "\n");
198 DEBUG(dbgs() << "AnalyzeBranch: FBB: " << FBB << "\n");
199
200 // this block ends with no branches
201 if (!IsAnyKindOfBranch(instLast1)) {
202 DEBUG(dbgs() << "AnalyzeBranch: ends with no branch\n");
203 return false;
204 }
205
206 // this block ends with only an unconditional branch
207 if (desc1.isUnconditionalBranch() &&
208 // when IsSizeOne is true, it "absorbs" the evaluation of instLast2
209 (IsSizeOne || !IsAnyKindOfBranch(instLast2))) {
210 DEBUG(dbgs() << "AnalyzeBranch: ends with only uncond branch\n");
211 TBB = GetBranchTarget(instLast1);
212 return false;
213 }
214
215 // this block ends with a conditional branch and
216 // it falls through to a successor block
217 if (desc1.isConditionalBranch() &&
218 IsAnySuccessorAlsoLayoutSuccessor(MBB)) {
219 DEBUG(dbgs() << "AnalyzeBranch: ends with cond branch and fall through\n");
220 TBB = GetBranchTarget(instLast1);
221 int i = instLast1.findFirstPredOperandIdx();
222 Cond.push_back(instLast1.getOperand(i));
223 Cond.push_back(instLast1.getOperand(i+1));
224 return false;
225 }
226
227 // when IsSizeOne is true, we are done
228 if (IsSizeOne)
229 return true;
230
231 // this block ends with a conditional branch
232 // followed by an unconditional branch
233 if (desc2.isConditionalBranch() &&
234 desc1.isUnconditionalBranch()) {
235 DEBUG(dbgs() << "AnalyzeBranch: ends with cond and uncond branch\n");
236 TBB = GetBranchTarget(instLast2);
237 FBB = GetBranchTarget(instLast1);
238 int i = instLast2.findFirstPredOperandIdx();
239 Cond.push_back(instLast2.getOperand(i));
240 Cond.push_back(instLast2.getOperand(i+1));
241 return false;
242 }
243
244 // branch cannot be understood
245 DEBUG(dbgs() << "AnalyzeBranch: cannot be understood\n");
246 return true;
247}
248
249unsigned PTXInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Che-Liang Chioufb4a8342011-03-28 10:23:13 +0000250 unsigned count = 0;
251 while (!MBB.empty())
252 if (IsAnyKindOfBranch(MBB.back())) {
253 MBB.pop_back();
254 ++count;
255 } else
256 break;
Che-Liang Chiou5e0872e2011-03-22 14:12:00 +0000257 DEBUG(dbgs() << "RemoveBranch: MBB: " << MBB.getName().str() << "\n");
Che-Liang Chioufb4a8342011-03-28 10:23:13 +0000258 DEBUG(dbgs() << "RemoveBranch: remove " << count << " branch inst\n");
Che-Liang Chiou5e0872e2011-03-22 14:12:00 +0000259 return count;
260}
261
262unsigned PTXInstrInfo::
263InsertBranch(MachineBasicBlock &MBB,
264 MachineBasicBlock *TBB,
265 MachineBasicBlock *FBB,
266 const SmallVectorImpl<MachineOperand> &Cond,
267 DebugLoc DL) const {
268 DEBUG(dbgs() << "InsertBranch: MBB: " << MBB.getName().str() << "\n");
Che-Liang Chioufb4a8342011-03-28 10:23:13 +0000269 DEBUG(if (TBB) dbgs() << "InsertBranch: TBB: " << TBB->getName().str()
270 << "\n";
271 else dbgs() << "InsertBranch: TBB: (NULL)\n");
272 DEBUG(if (FBB) dbgs() << "InsertBranch: FBB: " << FBB->getName().str()
273 << "\n";
274 else dbgs() << "InsertBranch: FBB: (NULL)\n");
Che-Liang Chiou5e0872e2011-03-22 14:12:00 +0000275 DEBUG(dbgs() << "InsertBranch: Cond size: " << Cond.size() << "\n");
276
277 assert(TBB && "TBB is NULL");
278
279 if (FBB) {
280 BuildMI(&MBB, DL, get(PTX::BRAdp))
281 .addMBB(TBB).addReg(Cond[0].getReg()).addImm(Cond[1].getImm());
282 BuildMI(&MBB, DL, get(PTX::BRAd))
283 .addMBB(FBB).addReg(PTX::NoRegister).addImm(PTX::PRED_NORMAL);
284 return 2;
285 } else if (Cond.size()) {
286 BuildMI(&MBB, DL, get(PTX::BRAdp))
287 .addMBB(TBB).addReg(Cond[0].getReg()).addImm(Cond[1].getImm());
288 return 1;
289 } else {
290 BuildMI(&MBB, DL, get(PTX::BRAd))
291 .addMBB(TBB).addReg(PTX::NoRegister).addImm(PTX::PRED_NORMAL);
292 return 1;
293 }
294}
295
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000296// static helper routines
297
298MachineSDNode *PTXInstrInfo::
299GetPTXMachineNode(SelectionDAG *DAG, unsigned Opcode,
300 DebugLoc dl, EVT VT, SDValue Op1) {
Che-Liang Chiouf78847e2011-03-14 11:26:01 +0000301 SDValue predReg = DAG->getRegister(PTX::NoRegister, MVT::i1);
302 SDValue predOp = DAG->getTargetConstant(PTX::PRED_NORMAL, MVT::i32);
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000303 SDValue ops[] = { Op1, predReg, predOp };
304 return DAG->getMachineNode(Opcode, dl, VT, ops, array_lengthof(ops));
305}
306
307MachineSDNode *PTXInstrInfo::
308GetPTXMachineNode(SelectionDAG *DAG, unsigned Opcode,
309 DebugLoc dl, EVT VT, SDValue Op1, SDValue Op2) {
Che-Liang Chiouf78847e2011-03-14 11:26:01 +0000310 SDValue predReg = DAG->getRegister(PTX::NoRegister, MVT::i1);
311 SDValue predOp = DAG->getTargetConstant(PTX::PRED_NORMAL, MVT::i32);
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000312 SDValue ops[] = { Op1, Op2, predReg, predOp };
313 return DAG->getMachineNode(Opcode, dl, VT, ops, array_lengthof(ops));
314}
315
316void PTXInstrInfo::AddDefaultPredicate(MachineInstr *MI) {
317 if (MI->findFirstPredOperandIdx() == -1) {
Che-Liang Chiou5e0872e2011-03-22 14:12:00 +0000318 MI->addOperand(MachineOperand::CreateReg(PTX::NoRegister, /*IsDef=*/false));
Che-Liang Chiouf78847e2011-03-14 11:26:01 +0000319 MI->addOperand(MachineOperand::CreateImm(PTX::PRED_NORMAL));
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000320 }
321}
Che-Liang Chiou5e0872e2011-03-22 14:12:00 +0000322
323bool PTXInstrInfo::IsAnyKindOfBranch(const MachineInstr& inst) {
324 const TargetInstrDesc &desc = inst.getDesc();
325 return desc.isTerminator() || desc.isBranch() || desc.isIndirectBranch();
326}
327
328bool PTXInstrInfo::
329IsAnySuccessorAlsoLayoutSuccessor(const MachineBasicBlock& MBB) {
330 for (MachineBasicBlock::const_succ_iterator
331 i = MBB.succ_begin(), e = MBB.succ_end(); i != e; ++i)
332 if (MBB.isLayoutSuccessor((const MachineBasicBlock*) &*i))
333 return true;
334 return false;
335}
336
337MachineBasicBlock *PTXInstrInfo::GetBranchTarget(const MachineInstr& inst) {
338 // FIXME So far all branch instructions put destination in 1st operand
339 const MachineOperand& target = inst.getOperand(0);
340 assert(target.isMBB() && "FIXME: detect branch target operand");
341 return target.getMBB();
342}