blob: 5c7ee298f31abd1e420f4370d2127f0d890b8e70 [file] [log] [blame]
Eric Christopher50880d02010-09-18 18:52:28 +00001//===-- PTXISelDAGToDAG.cpp - A dag to dag inst selector for PTX ----------===//
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 defines an instruction selector for the PTX target.
11//
12//===----------------------------------------------------------------------===//
13
14#include "PTX.h"
Justin Holewinskif47dfba2011-09-27 18:12:55 +000015#include "PTXMachineFunctionInfo.h"
Eric Christopher50880d02010-09-18 18:52:28 +000016#include "PTXTargetMachine.h"
Justin Holewinskif47dfba2011-09-27 18:12:55 +000017#include "llvm/ADT/StringExtras.h"
Eric Christopher50880d02010-09-18 18:52:28 +000018#include "llvm/CodeGen/SelectionDAGISel.h"
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +000019#include "llvm/DerivedTypes.h"
Justin Holewinski67a91842011-06-23 18:10:03 +000020#include "llvm/Support/Debug.h"
Che-Liang Chioufd8978b2011-03-02 03:20:28 +000021#include "llvm/Support/raw_ostream.h"
Eric Christopher50880d02010-09-18 18:52:28 +000022
23using namespace llvm;
24
25namespace {
26// PTXDAGToDAGISel - PTX specific code to select PTX machine
27// instructions for SelectionDAG operations.
28class PTXDAGToDAGISel : public SelectionDAGISel {
29 public:
30 PTXDAGToDAGISel(PTXTargetMachine &TM, CodeGenOpt::Level OptLevel);
31
32 virtual const char *getPassName() const {
33 return "PTX DAG->DAG Pattern Instruction Selection";
34 }
35
36 SDNode *Select(SDNode *Node);
37
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +000038 // Complex Pattern Selectors.
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000039 bool SelectADDRrr(SDValue &Addr, SDValue &R1, SDValue &R2);
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +000040 bool SelectADDRri(SDValue &Addr, SDValue &Base, SDValue &Offset);
41 bool SelectADDRii(SDValue &Addr, SDValue &Base, SDValue &Offset);
Justin Holewinski6b8990d2011-09-26 16:20:25 +000042 bool SelectADDRlocal(SDValue &Addr, SDValue &Base, SDValue &Offset);
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +000043
Eric Christopher50880d02010-09-18 18:52:28 +000044 // Include the pieces auto'gened from the target description
45#include "PTXGenDAGISel.inc"
46
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +000047 private:
Che-Liang Chiou88d33672011-03-18 11:08:52 +000048 // We need this only because we can't match intruction BRAdp
49 // pattern (PTXbrcond bb:$d, ...) in PTXInstrInfo.td
50 SDNode *SelectBRCOND(SDNode *Node);
51
Justin Holewinski5422a0f2011-09-22 16:45:46 +000052 SDNode *SelectREADPARAM(SDNode *Node);
53 SDNode *SelectWRITEPARAM(SDNode *Node);
Justin Holewinski6b8990d2011-09-26 16:20:25 +000054 SDNode *SelectFrameIndex(SDNode *Node);
Justin Holewinski5422a0f2011-09-22 16:45:46 +000055
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000056 bool isImm(const SDValue &operand);
57 bool SelectImm(const SDValue &operand, SDValue &imm);
Che-Liang Chiouf48817c2011-03-02 07:36:48 +000058
59 const PTXSubtarget& getSubtarget() const;
Eric Christopher50880d02010-09-18 18:52:28 +000060}; // class PTXDAGToDAGISel
61} // namespace
62
63// createPTXISelDag - This pass converts a legalized DAG into a
64// PTX-specific DAG, ready for instruction scheduling
65FunctionPass *llvm::createPTXISelDag(PTXTargetMachine &TM,
66 CodeGenOpt::Level OptLevel) {
67 return new PTXDAGToDAGISel(TM, OptLevel);
68}
69
70PTXDAGToDAGISel::PTXDAGToDAGISel(PTXTargetMachine &TM,
71 CodeGenOpt::Level OptLevel)
72 : SelectionDAGISel(TM, OptLevel) {}
73
74SDNode *PTXDAGToDAGISel::Select(SDNode *Node) {
Che-Liang Chiou88d33672011-03-18 11:08:52 +000075 switch (Node->getOpcode()) {
Che-Liang Chiou88d33672011-03-18 11:08:52 +000076 case ISD::BRCOND:
77 return SelectBRCOND(Node);
Justin Holewinski5422a0f2011-09-22 16:45:46 +000078 case PTXISD::READ_PARAM:
79 return SelectREADPARAM(Node);
80 case PTXISD::WRITE_PARAM:
81 return SelectWRITEPARAM(Node);
Justin Holewinski6b8990d2011-09-26 16:20:25 +000082 case ISD::FrameIndex:
83 return SelectFrameIndex(Node);
Che-Liang Chiou88d33672011-03-18 11:08:52 +000084 default:
85 return SelectCode(Node);
86 }
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +000087}
88
Che-Liang Chiou88d33672011-03-18 11:08:52 +000089SDNode *PTXDAGToDAGISel::SelectBRCOND(SDNode *Node) {
90 assert(Node->getNumOperands() >= 3);
91
92 SDValue Chain = Node->getOperand(0);
93 SDValue Pred = Node->getOperand(1);
94 SDValue Target = Node->getOperand(2); // branch target
Justin Holewinskif51b7e52011-09-30 14:36:36 +000095 SDValue PredOp = CurDAG->getTargetConstant(PTXPredicate::Normal, MVT::i32);
Che-Liang Chiou88d33672011-03-18 11:08:52 +000096 DebugLoc dl = Node->getDebugLoc();
97
98 assert(Target.getOpcode() == ISD::BasicBlock);
99 assert(Pred.getValueType() == MVT::i1);
100
101 // Emit BRAdp
102 SDValue Ops[] = { Target, Pred, PredOp, Chain };
103 return CurDAG->getMachineNode(PTX::BRAdp, dl, MVT::Other, Ops, 4);
104}
105
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000106SDNode *PTXDAGToDAGISel::SelectREADPARAM(SDNode *Node) {
107 SDValue Chain = Node->getOperand(0);
108 SDValue Index = Node->getOperand(1);
109
110 int OpCode;
111
112 // Get the type of parameter we are reading
113 EVT VT = Node->getValueType(0);
114 assert(VT.isSimple() && "READ_PARAM only implemented for MVT types");
115
116 MVT Type = VT.getSimpleVT();
117
118 if (Type == MVT::i1)
119 OpCode = PTX::READPARAMPRED;
120 else if (Type == MVT::i16)
121 OpCode = PTX::READPARAMI16;
122 else if (Type == MVT::i32)
123 OpCode = PTX::READPARAMI32;
124 else if (Type == MVT::i64)
125 OpCode = PTX::READPARAMI64;
126 else if (Type == MVT::f32)
127 OpCode = PTX::READPARAMF32;
Duncan Sands62c607b2011-10-05 15:13:13 +0000128 else {
129 assert(Type == MVT::f64 && "Unexpected type!");
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000130 OpCode = PTX::READPARAMF64;
Duncan Sands62c607b2011-10-05 15:13:13 +0000131 }
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000132
133 SDValue Pred = CurDAG->getRegister(PTX::NoRegister, MVT::i1);
Justin Holewinskif51b7e52011-09-30 14:36:36 +0000134 SDValue PredOp = CurDAG->getTargetConstant(PTXPredicate::None, MVT::i32);
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000135 DebugLoc dl = Node->getDebugLoc();
136
137 SDValue Ops[] = { Index, Pred, PredOp, Chain };
138 return CurDAG->getMachineNode(OpCode, dl, VT, Ops, 4);
139}
140
141SDNode *PTXDAGToDAGISel::SelectWRITEPARAM(SDNode *Node) {
142
143 SDValue Chain = Node->getOperand(0);
144 SDValue Value = Node->getOperand(1);
145
146 int OpCode;
147
148 //Node->dumpr(CurDAG);
149
150 // Get the type of parameter we are writing
151 EVT VT = Value->getValueType(0);
152 assert(VT.isSimple() && "WRITE_PARAM only implemented for MVT types");
153
154 MVT Type = VT.getSimpleVT();
155
156 if (Type == MVT::i1)
157 OpCode = PTX::WRITEPARAMPRED;
158 else if (Type == MVT::i16)
159 OpCode = PTX::WRITEPARAMI16;
160 else if (Type == MVT::i32)
161 OpCode = PTX::WRITEPARAMI32;
162 else if (Type == MVT::i64)
163 OpCode = PTX::WRITEPARAMI64;
164 else if (Type == MVT::f32)
165 OpCode = PTX::WRITEPARAMF32;
166 else if (Type == MVT::f64)
167 OpCode = PTX::WRITEPARAMF64;
168 else
169 llvm_unreachable("Invalid type in SelectWRITEPARAM");
170
171 SDValue Pred = CurDAG->getRegister(PTX::NoRegister, MVT::i1);
Justin Holewinskif51b7e52011-09-30 14:36:36 +0000172 SDValue PredOp = CurDAG->getTargetConstant(PTXPredicate::None, MVT::i32);
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000173 DebugLoc dl = Node->getDebugLoc();
174
175 SDValue Ops[] = { Value, Pred, PredOp, Chain };
176 SDNode* Ret = CurDAG->getMachineNode(OpCode, dl, MVT::Other, Ops, 4);
177
178 //dbgs() << "SelectWRITEPARAM produced:\n\t";
179 //Ret->dumpr(CurDAG);
180
181 return Ret;
182}
183
Justin Holewinski6b8990d2011-09-26 16:20:25 +0000184SDNode *PTXDAGToDAGISel::SelectFrameIndex(SDNode *Node) {
185 int FI = cast<FrameIndexSDNode>(Node)->getIndex();
186 //dbgs() << "Selecting FrameIndex at index " << FI << "\n";
Justin Holewinskif47dfba2011-09-27 18:12:55 +0000187 //SDValue TFI = CurDAG->getTargetFrameIndex(FI, Node->getValueType(0));
Justin Holewinski6b8990d2011-09-26 16:20:25 +0000188
Justin Holewinskif47dfba2011-09-27 18:12:55 +0000189 PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
Justin Holewinski6b8990d2011-09-26 16:20:25 +0000190
Justin Holewinskif47dfba2011-09-27 18:12:55 +0000191 SDValue FrameSymbol = CurDAG->getTargetExternalSymbol(MFI->getFrameSymbol(FI),
192 Node->getValueType(0));
Justin Holewinski6b8990d2011-09-26 16:20:25 +0000193
Justin Holewinskif47dfba2011-09-27 18:12:55 +0000194 return FrameSymbol.getNode();
Justin Holewinski6b8990d2011-09-26 16:20:25 +0000195}
196
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000197// Match memory operand of the form [reg+reg]
198bool PTXDAGToDAGISel::SelectADDRrr(SDValue &Addr, SDValue &R1, SDValue &R2) {
199 if (Addr.getOpcode() != ISD::ADD || Addr.getNumOperands() < 2 ||
200 isImm(Addr.getOperand(0)) || isImm(Addr.getOperand(1)))
201 return false;
202
Justin Holewinskid6625762011-03-23 16:58:51 +0000203 assert(Addr.getValueType().isSimple() && "Type must be simple");
204
Che-Liang Chiouc88e91b2011-01-01 11:58:58 +0000205 R1 = Addr;
Justin Holewinskid6625762011-03-23 16:58:51 +0000206 R2 = CurDAG->getTargetConstant(0, Addr.getValueType().getSimpleVT());
207
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000208 return true;
209}
210
211// Match memory operand of the form [reg], [imm+reg], and [reg+imm]
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000212bool PTXDAGToDAGISel::SelectADDRri(SDValue &Addr, SDValue &Base,
213 SDValue &Offset) {
Justin Holewinski63602ed2011-09-26 18:57:22 +0000214 // FrameIndex addresses are handled separately
215 //errs() << "SelectADDRri: ";
216 //Addr.getNode()->dumpr();
217 if (isa<FrameIndexSDNode>(Addr)) {
218 //errs() << "Failure\n";
219 return false;
220 }
221
222 if (CurDAG->isBaseWithConstantOffset(Addr)) {
223 Base = Addr.getOperand(0);
224 if (isa<FrameIndexSDNode>(Base)) {
225 //errs() << "Failure\n";
226 return false;
227 }
228 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1));
229 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), MVT::i32);
230 //errs() << "Success\n";
231 return true;
232 }
233
234 /*if (Addr.getNumOperands() == 1) {
235 Base = Addr;
236 Offset = CurDAG->getTargetConstant(0, Addr.getValueType().getSimpleVT());
237 errs() << "Success\n";
238 return true;
239 }*/
240
241 //errs() << "SelectADDRri fails on: ";
242 //Addr.getNode()->dumpr();
243
244 if (isImm(Addr)) {
245 //errs() << "Failure\n";
246 return false;
247 }
248
249 Base = Addr;
250 Offset = CurDAG->getTargetConstant(0, Addr.getValueType().getSimpleVT());
251
252 //errs() << "Success\n";
253 return true;
254
255 /*if (Addr.getOpcode() != ISD::ADD) {
Che-Liang Chiouc88e91b2011-01-01 11:58:58 +0000256 // let SelectADDRii handle the [imm] case
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000257 if (isImm(Addr))
258 return false;
Che-Liang Chiouc88e91b2011-01-01 11:58:58 +0000259 // it is [reg]
Justin Holewinskid6625762011-03-23 16:58:51 +0000260
261 assert(Addr.getValueType().isSimple() && "Type must be simple");
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000262 Base = Addr;
Justin Holewinskid6625762011-03-23 16:58:51 +0000263 Offset = CurDAG->getTargetConstant(0, Addr.getValueType().getSimpleVT());
264
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000265 return true;
266 }
267
Che-Liang Chiouc88e91b2011-01-01 11:58:58 +0000268 if (Addr.getNumOperands() < 2)
269 return false;
270
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000271 // let SelectADDRii handle the [imm+imm] case
Che-Liang Chiouc88e91b2011-01-01 11:58:58 +0000272 if (isImm(Addr.getOperand(0)) && isImm(Addr.getOperand(1)))
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000273 return false;
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000274
275 // try [reg+imm] and [imm+reg]
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000276 for (int i = 0; i < 2; i ++)
277 if (SelectImm(Addr.getOperand(1-i), Offset)) {
278 Base = Addr.getOperand(i);
279 return true;
280 }
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000281
Che-Liang Chiouc88e91b2011-01-01 11:58:58 +0000282 // neither [reg+imm] nor [imm+reg]
Justin Holewinski63602ed2011-09-26 18:57:22 +0000283 return false;*/
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000284}
285
286// Match memory operand of the form [imm+imm] and [imm]
287bool PTXDAGToDAGISel::SelectADDRii(SDValue &Addr, SDValue &Base,
288 SDValue &Offset) {
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000289 // is [imm+imm]?
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000290 if (Addr.getOpcode() == ISD::ADD) {
291 return SelectImm(Addr.getOperand(0), Base) &&
292 SelectImm(Addr.getOperand(1), Offset);
293 }
294
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000295 // is [imm]?
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000296 if (SelectImm(Addr, Base)) {
Justin Holewinskid6625762011-03-23 16:58:51 +0000297 assert(Addr.getValueType().isSimple() && "Type must be simple");
298
299 Offset = CurDAG->getTargetConstant(0, Addr.getValueType().getSimpleVT());
300
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000301 return true;
302 }
303
304 return false;
305}
306
Justin Holewinski6b8990d2011-09-26 16:20:25 +0000307// Match memory operand of the form [reg], [imm+reg], and [reg+imm]
308bool PTXDAGToDAGISel::SelectADDRlocal(SDValue &Addr, SDValue &Base,
309 SDValue &Offset) {
Justin Holewinski63602ed2011-09-26 18:57:22 +0000310 //errs() << "SelectADDRlocal: ";
311 //Addr.getNode()->dumpr();
312 if (isa<FrameIndexSDNode>(Addr)) {
Justin Holewinski6b8990d2011-09-26 16:20:25 +0000313 Base = Addr;
314 Offset = CurDAG->getTargetConstant(0, Addr.getValueType().getSimpleVT());
Justin Holewinski63602ed2011-09-26 18:57:22 +0000315 //errs() << "Success\n";
Justin Holewinski6b8990d2011-09-26 16:20:25 +0000316 return true;
317 }
318
Justin Holewinski63602ed2011-09-26 18:57:22 +0000319 if (CurDAG->isBaseWithConstantOffset(Addr)) {
320 Base = Addr.getOperand(0);
321 if (!isa<FrameIndexSDNode>(Base)) {
322 //errs() << "Failure\n";
323 return false;
Justin Holewinski6b8990d2011-09-26 16:20:25 +0000324 }
Justin Holewinski63602ed2011-09-26 18:57:22 +0000325 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1));
326 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), MVT::i32);
327 //errs() << "Offset: ";
328 //Offset.getNode()->dumpr();
329 //errs() << "Success\n";
330 return true;
331 }
Justin Holewinski6b8990d2011-09-26 16:20:25 +0000332
Justin Holewinski63602ed2011-09-26 18:57:22 +0000333 //errs() << "Failure\n";
Justin Holewinski6b8990d2011-09-26 16:20:25 +0000334 return false;
335}
336
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000337bool PTXDAGToDAGISel::isImm(const SDValue &operand) {
338 return ConstantSDNode::classof(operand.getNode());
339}
340
341bool PTXDAGToDAGISel::SelectImm(const SDValue &operand, SDValue &imm) {
342 SDNode *node = operand.getNode();
343 if (!ConstantSDNode::classof(node))
344 return false;
345
346 ConstantSDNode *CN = cast<ConstantSDNode>(node);
Justin Holewinskid6625762011-03-23 16:58:51 +0000347 imm = CurDAG->getTargetConstant(*CN->getConstantIntValue(),
348 operand.getValueType());
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000349 return true;
350}
Che-Liang Chiouf48817c2011-03-02 07:36:48 +0000351
352const PTXSubtarget& PTXDAGToDAGISel::getSubtarget() const
353{
354 return TM.getSubtarget<PTXSubtarget>();
355}
356