Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 1 | //===-- 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" |
| 15 | #include "PTXTargetMachine.h" |
| 16 | #include "llvm/CodeGen/SelectionDAGISel.h" |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 17 | #include "llvm/DerivedTypes.h" |
Justin Holewinski | 67a9184 | 2011-06-23 18:10:03 +0000 | [diff] [blame^] | 18 | #include "llvm/Support/Debug.h" |
Che-Liang Chiou | fd8978b | 2011-03-02 03:20:28 +0000 | [diff] [blame] | 19 | #include "llvm/Support/raw_ostream.h" |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace llvm; |
| 22 | |
| 23 | namespace { |
| 24 | // PTXDAGToDAGISel - PTX specific code to select PTX machine |
| 25 | // instructions for SelectionDAG operations. |
| 26 | class PTXDAGToDAGISel : public SelectionDAGISel { |
| 27 | public: |
| 28 | PTXDAGToDAGISel(PTXTargetMachine &TM, CodeGenOpt::Level OptLevel); |
| 29 | |
| 30 | virtual const char *getPassName() const { |
| 31 | return "PTX DAG->DAG Pattern Instruction Selection"; |
| 32 | } |
| 33 | |
| 34 | SDNode *Select(SDNode *Node); |
| 35 | |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 36 | // Complex Pattern Selectors. |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 37 | bool SelectADDRrr(SDValue &Addr, SDValue &R1, SDValue &R2); |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 38 | bool SelectADDRri(SDValue &Addr, SDValue &Base, SDValue &Offset); |
| 39 | bool SelectADDRii(SDValue &Addr, SDValue &Base, SDValue &Offset); |
| 40 | |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 41 | // Include the pieces auto'gened from the target description |
| 42 | #include "PTXGenDAGISel.inc" |
| 43 | |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 44 | private: |
Che-Liang Chiou | 8e5d01c | 2011-02-10 12:01:24 +0000 | [diff] [blame] | 45 | SDNode *SelectREAD_PARAM(SDNode *Node); |
Justin Holewinski | 67a9184 | 2011-06-23 18:10:03 +0000 | [diff] [blame^] | 46 | //SDNode *SelectSTORE_PARAM(SDNode *Node); |
| 47 | |
Che-Liang Chiou | 88d3367 | 2011-03-18 11:08:52 +0000 | [diff] [blame] | 48 | // 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 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 52 | bool isImm(const SDValue &operand); |
| 53 | bool SelectImm(const SDValue &operand, SDValue &imm); |
Che-Liang Chiou | f48817c | 2011-03-02 07:36:48 +0000 | [diff] [blame] | 54 | |
| 55 | const PTXSubtarget& getSubtarget() const; |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 56 | }; // class PTXDAGToDAGISel |
| 57 | } // namespace |
| 58 | |
| 59 | // createPTXISelDag - This pass converts a legalized DAG into a |
| 60 | // PTX-specific DAG, ready for instruction scheduling |
| 61 | FunctionPass *llvm::createPTXISelDag(PTXTargetMachine &TM, |
| 62 | CodeGenOpt::Level OptLevel) { |
| 63 | return new PTXDAGToDAGISel(TM, OptLevel); |
| 64 | } |
| 65 | |
| 66 | PTXDAGToDAGISel::PTXDAGToDAGISel(PTXTargetMachine &TM, |
| 67 | CodeGenOpt::Level OptLevel) |
| 68 | : SelectionDAGISel(TM, OptLevel) {} |
| 69 | |
| 70 | SDNode *PTXDAGToDAGISel::Select(SDNode *Node) { |
Che-Liang Chiou | 88d3367 | 2011-03-18 11:08:52 +0000 | [diff] [blame] | 71 | switch (Node->getOpcode()) { |
| 72 | case PTXISD::READ_PARAM: |
| 73 | return SelectREAD_PARAM(Node); |
Justin Holewinski | 67a9184 | 2011-06-23 18:10:03 +0000 | [diff] [blame^] | 74 | // case PTXISD::STORE_PARAM: |
| 75 | // return SelectSTORE_PARAM(Node); |
Che-Liang Chiou | 88d3367 | 2011-03-18 11:08:52 +0000 | [diff] [blame] | 76 | case ISD::BRCOND: |
| 77 | return SelectBRCOND(Node); |
| 78 | default: |
| 79 | return SelectCode(Node); |
| 80 | } |
Che-Liang Chiou | 8e5d01c | 2011-02-10 12:01:24 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | SDNode *PTXDAGToDAGISel::SelectREAD_PARAM(SDNode *Node) { |
Che-Liang Chiou | fd8978b | 2011-03-02 03:20:28 +0000 | [diff] [blame] | 84 | SDValue index = Node->getOperand(1); |
| 85 | DebugLoc dl = Node->getDebugLoc(); |
| 86 | unsigned opcode; |
Che-Liang Chiou | 8e5d01c | 2011-02-10 12:01:24 +0000 | [diff] [blame] | 87 | |
| 88 | if (index.getOpcode() != ISD::TargetConstant) |
| 89 | llvm_unreachable("READ_PARAM: index is not ISD::TargetConstant"); |
| 90 | |
Che-Liang Chiou | fd8978b | 2011-03-02 03:20:28 +0000 | [diff] [blame] | 91 | if (Node->getValueType(0) == MVT::i16) { |
| 92 | opcode = PTX::LDpiU16; |
Justin Holewinski | 67a9184 | 2011-06-23 18:10:03 +0000 | [diff] [blame^] | 93 | } else if (Node->getValueType(0) == MVT::i32) { |
Che-Liang Chiou | fd8978b | 2011-03-02 03:20:28 +0000 | [diff] [blame] | 94 | opcode = PTX::LDpiU32; |
Justin Holewinski | 67a9184 | 2011-06-23 18:10:03 +0000 | [diff] [blame^] | 95 | } else if (Node->getValueType(0) == MVT::i64) { |
Che-Liang Chiou | fd8978b | 2011-03-02 03:20:28 +0000 | [diff] [blame] | 96 | opcode = PTX::LDpiU64; |
Justin Holewinski | 67a9184 | 2011-06-23 18:10:03 +0000 | [diff] [blame^] | 97 | } else if (Node->getValueType(0) == MVT::f32) { |
Che-Liang Chiou | fd8978b | 2011-03-02 03:20:28 +0000 | [diff] [blame] | 98 | opcode = PTX::LDpiF32; |
Justin Holewinski | 67a9184 | 2011-06-23 18:10:03 +0000 | [diff] [blame^] | 99 | } else if (Node->getValueType(0) == MVT::f64) { |
Che-Liang Chiou | fd8978b | 2011-03-02 03:20:28 +0000 | [diff] [blame] | 100 | opcode = PTX::LDpiF64; |
Justin Holewinski | 67a9184 | 2011-06-23 18:10:03 +0000 | [diff] [blame^] | 101 | } else { |
Che-Liang Chiou | fd8978b | 2011-03-02 03:20:28 +0000 | [diff] [blame] | 102 | llvm_unreachable("Unknown parameter type for ld.param"); |
| 103 | } |
| 104 | |
Che-Liang Chiou | 8e5d01c | 2011-02-10 12:01:24 +0000 | [diff] [blame] | 105 | return PTXInstrInfo:: |
Che-Liang Chiou | fd8978b | 2011-03-02 03:20:28 +0000 | [diff] [blame] | 106 | GetPTXMachineNode(CurDAG, opcode, dl, Node->getValueType(0), index); |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 107 | } |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 108 | |
Justin Holewinski | 67a9184 | 2011-06-23 18:10:03 +0000 | [diff] [blame^] | 109 | // SDNode *PTXDAGToDAGISel::SelectSTORE_PARAM(SDNode *Node) { |
| 110 | // SDValue Chain = Node->getOperand(0); |
| 111 | // SDValue index = Node->getOperand(1); |
| 112 | // SDValue value = Node->getOperand(2); |
| 113 | // DebugLoc dl = Node->getDebugLoc(); |
| 114 | // unsigned opcode; |
| 115 | |
| 116 | // if (index.getOpcode() != ISD::TargetConstant) |
| 117 | // llvm_unreachable("STORE_PARAM: index is not ISD::TargetConstant"); |
| 118 | |
| 119 | // if (value->getValueType(0) == MVT::i16) { |
| 120 | // opcode = PTX::STpiU16; |
| 121 | // } else if (value->getValueType(0) == MVT::i32) { |
| 122 | // opcode = PTX::STpiU32; |
| 123 | // } else if (value->getValueType(0) == MVT::i64) { |
| 124 | // opcode = PTX::STpiU64; |
| 125 | // } else if (value->getValueType(0) == MVT::f32) { |
| 126 | // opcode = PTX::STpiF32; |
| 127 | // } else if (value->getValueType(0) == MVT::f64) { |
| 128 | // opcode = PTX::STpiF64; |
| 129 | // } else { |
| 130 | // llvm_unreachable("Unknown parameter type for st.param"); |
| 131 | // } |
| 132 | |
| 133 | // SDVTList VTs = CurDAG->getVTList(MVT::Other, MVT::Glue); |
| 134 | // SDValue PredReg = CurDAG->getRegister(PTX::NoRegister, MVT::i1); |
| 135 | // SDValue PredOp = CurDAG->getTargetConstant(PTX::PRED_NORMAL, MVT::i32); |
| 136 | // SDValue Ops[] = { Chain, index, value, PredReg, PredOp }; |
| 137 | // //SDNode *RetNode = PTXInstrInfo:: |
| 138 | // // GetPTXMachineNode(CurDAG, opcode, dl, VTs, index, value); |
| 139 | // SDNode *RetNode = CurDAG->getMachineNode(opcode, dl, VTs, Ops, array_lengthof(Ops)); |
| 140 | // DEBUG(dbgs() << "SelectSTORE_PARAM: Selected: "); |
| 141 | // RetNode->dumpr(CurDAG); |
| 142 | // return RetNode; |
| 143 | // } |
| 144 | |
Che-Liang Chiou | 88d3367 | 2011-03-18 11:08:52 +0000 | [diff] [blame] | 145 | SDNode *PTXDAGToDAGISel::SelectBRCOND(SDNode *Node) { |
| 146 | assert(Node->getNumOperands() >= 3); |
| 147 | |
| 148 | SDValue Chain = Node->getOperand(0); |
| 149 | SDValue Pred = Node->getOperand(1); |
| 150 | SDValue Target = Node->getOperand(2); // branch target |
| 151 | SDValue PredOp = CurDAG->getTargetConstant(PTX::PRED_NORMAL, MVT::i32); |
| 152 | DebugLoc dl = Node->getDebugLoc(); |
| 153 | |
| 154 | assert(Target.getOpcode() == ISD::BasicBlock); |
| 155 | assert(Pred.getValueType() == MVT::i1); |
| 156 | |
| 157 | // Emit BRAdp |
| 158 | SDValue Ops[] = { Target, Pred, PredOp, Chain }; |
| 159 | return CurDAG->getMachineNode(PTX::BRAdp, dl, MVT::Other, Ops, 4); |
| 160 | } |
| 161 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 162 | // Match memory operand of the form [reg+reg] |
| 163 | bool PTXDAGToDAGISel::SelectADDRrr(SDValue &Addr, SDValue &R1, SDValue &R2) { |
| 164 | if (Addr.getOpcode() != ISD::ADD || Addr.getNumOperands() < 2 || |
| 165 | isImm(Addr.getOperand(0)) || isImm(Addr.getOperand(1))) |
| 166 | return false; |
| 167 | |
Justin Holewinski | d662576 | 2011-03-23 16:58:51 +0000 | [diff] [blame] | 168 | assert(Addr.getValueType().isSimple() && "Type must be simple"); |
| 169 | |
Che-Liang Chiou | c88e91b | 2011-01-01 11:58:58 +0000 | [diff] [blame] | 170 | R1 = Addr; |
Justin Holewinski | d662576 | 2011-03-23 16:58:51 +0000 | [diff] [blame] | 171 | R2 = CurDAG->getTargetConstant(0, Addr.getValueType().getSimpleVT()); |
| 172 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 173 | return true; |
| 174 | } |
| 175 | |
| 176 | // Match memory operand of the form [reg], [imm+reg], and [reg+imm] |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 177 | bool PTXDAGToDAGISel::SelectADDRri(SDValue &Addr, SDValue &Base, |
| 178 | SDValue &Offset) { |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 179 | if (Addr.getOpcode() != ISD::ADD) { |
Che-Liang Chiou | c88e91b | 2011-01-01 11:58:58 +0000 | [diff] [blame] | 180 | // let SelectADDRii handle the [imm] case |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 181 | if (isImm(Addr)) |
| 182 | return false; |
Che-Liang Chiou | c88e91b | 2011-01-01 11:58:58 +0000 | [diff] [blame] | 183 | // it is [reg] |
Justin Holewinski | d662576 | 2011-03-23 16:58:51 +0000 | [diff] [blame] | 184 | |
| 185 | assert(Addr.getValueType().isSimple() && "Type must be simple"); |
| 186 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 187 | Base = Addr; |
Justin Holewinski | d662576 | 2011-03-23 16:58:51 +0000 | [diff] [blame] | 188 | Offset = CurDAG->getTargetConstant(0, Addr.getValueType().getSimpleVT()); |
| 189 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 190 | return true; |
| 191 | } |
| 192 | |
Che-Liang Chiou | c88e91b | 2011-01-01 11:58:58 +0000 | [diff] [blame] | 193 | if (Addr.getNumOperands() < 2) |
| 194 | return false; |
| 195 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 196 | // let SelectADDRii handle the [imm+imm] case |
Che-Liang Chiou | c88e91b | 2011-01-01 11:58:58 +0000 | [diff] [blame] | 197 | if (isImm(Addr.getOperand(0)) && isImm(Addr.getOperand(1))) |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 198 | return false; |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 199 | |
| 200 | // try [reg+imm] and [imm+reg] |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 201 | for (int i = 0; i < 2; i ++) |
| 202 | if (SelectImm(Addr.getOperand(1-i), Offset)) { |
| 203 | Base = Addr.getOperand(i); |
| 204 | return true; |
| 205 | } |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 206 | |
Che-Liang Chiou | c88e91b | 2011-01-01 11:58:58 +0000 | [diff] [blame] | 207 | // neither [reg+imm] nor [imm+reg] |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 208 | return false; |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | // Match memory operand of the form [imm+imm] and [imm] |
| 212 | bool PTXDAGToDAGISel::SelectADDRii(SDValue &Addr, SDValue &Base, |
| 213 | SDValue &Offset) { |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 214 | // is [imm+imm]? |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 215 | if (Addr.getOpcode() == ISD::ADD) { |
| 216 | return SelectImm(Addr.getOperand(0), Base) && |
| 217 | SelectImm(Addr.getOperand(1), Offset); |
| 218 | } |
| 219 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 220 | // is [imm]? |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 221 | if (SelectImm(Addr, Base)) { |
Justin Holewinski | d662576 | 2011-03-23 16:58:51 +0000 | [diff] [blame] | 222 | assert(Addr.getValueType().isSimple() && "Type must be simple"); |
| 223 | |
| 224 | Offset = CurDAG->getTargetConstant(0, Addr.getValueType().getSimpleVT()); |
| 225 | |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 226 | return true; |
| 227 | } |
| 228 | |
| 229 | return false; |
| 230 | } |
| 231 | |
| 232 | bool PTXDAGToDAGISel::isImm(const SDValue &operand) { |
| 233 | return ConstantSDNode::classof(operand.getNode()); |
| 234 | } |
| 235 | |
| 236 | bool PTXDAGToDAGISel::SelectImm(const SDValue &operand, SDValue &imm) { |
| 237 | SDNode *node = operand.getNode(); |
| 238 | if (!ConstantSDNode::classof(node)) |
| 239 | return false; |
| 240 | |
| 241 | ConstantSDNode *CN = cast<ConstantSDNode>(node); |
Justin Holewinski | d662576 | 2011-03-23 16:58:51 +0000 | [diff] [blame] | 242 | imm = CurDAG->getTargetConstant(*CN->getConstantIntValue(), |
| 243 | operand.getValueType()); |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 244 | return true; |
| 245 | } |
Che-Liang Chiou | f48817c | 2011-03-02 07:36:48 +0000 | [diff] [blame] | 246 | |
| 247 | const PTXSubtarget& PTXDAGToDAGISel::getSubtarget() const |
| 248 | { |
| 249 | return TM.getSubtarget<PTXSubtarget>(); |
| 250 | } |
| 251 | |